var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.injectPluginGradleSync=exports.parseGradlePropertiesSync=exports.parseSettingsGradleSync=exports.parseAppBuildGradleSync=exports.parseBuildGradleSync=void 0;var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _chalk=_interopRequireDefault(require("chalk"));
var _common=require("../../common");







var _resolve=require("../../resolve");
var _logger=require("../../systemTools/logger");
var _fileutils=require("../../systemTools/fileutils");

var parseBuildGradleSync=function parseBuildGradleSync(c,platform){
var appFolder=(0,_common.getAppFolder)(c,platform);

var dexOptions='';

if(c.pluginConfigAndroid.buildGradleBuildScriptDexOptions){
dexOptions="dexOptions() {\n            "+
c.pluginConfigAndroid.buildGradleBuildScriptDexOptions+"\n        }";

}

(0,_fileutils.writeCleanFile)(
(0,_common.getBuildFilePath)(c,platform,'build.gradle'),
_path.default.join(appFolder,'build.gradle'),
[
{
pattern:'{{COMPILE_SDK_VERSION}}',
override:c.pluginConfigAndroid.compileSdkVersion},

{
pattern:'{{SUPPORT_LIB_VERSION}}',
override:c.pluginConfigAndroid.supportLibVersion},

{
pattern:'{{BUILD_TOOLS_VERSION}}',
override:c.pluginConfigAndroid.buildToolsVersion},

{
pattern:'{{PLUGIN_INJECT_ALLPROJECTS_REPOSITORIES}}',
override:
c.pluginConfigAndroid.buildGradleAllProjectsRepositories},

{
pattern:'{{PLUGIN_INJECT_BUILDSCRIPT_REPOSITORIES}}',
override:
c.pluginConfigAndroid.buildGradleBuildScriptRepositories},

{
pattern:'{{PLUGIN_INJECT_BUILDSCRIPT_DEPENDENCIES}}',
override:
c.pluginConfigAndroid.buildGradleBuildScriptDependencies},

{pattern:'{{PLUGIN_INJECT_DEXOPTIONS}}',override:dexOptions},
{
pattern:'{{PATH_JSC_ANDROID}}',
override:(0,_resolve.doResolve)('jsc-android',true,{forceForwardPaths:true})},

{
pattern:'{{PATH_REACT_NATIVE}}',
override:(0,_resolve.doResolve)('react-native',true,{forceForwardPaths:true})}],

null,c);

};exports.parseBuildGradleSync=parseBuildGradleSync;

var parseAppBuildGradleSync=function parseAppBuildGradleSync(c,platform){var _c$buildConfig,_pluginConfig$platfor,_pluginConfig$platfor2,_pluginConfig$platfor3,_pluginConfig$platfor4,_pluginConfig$platfor5,_pluginConfig$platfor6,_pluginConfig$platfor7,_pluginConfig$platfor8;
(0,_logger.logTask)('parseAppBuildGradleSync');
var appFolder=(0,_common.getAppFolder)(c,platform);


c.pluginConfigAndroid.minSdkVersion=(0,_common.getConfigProp)(
c,
platform,
'minSdkVersion',
21);

c.pluginConfigAndroid.targetSdkVersion=(0,_common.getConfigProp)(
c,
platform,
'targetSdkVersion',
28);

c.pluginConfigAndroid.compileSdkVersion=(0,_common.getConfigProp)(
c,
platform,
'compileSdkVersion',
28);

c.pluginConfigAndroid.supportLibVersion=(0,_common.getConfigProp)(
c,
platform,
'supportLibVersion',
'28.0.0');

c.pluginConfigAndroid.buildToolsVersion=(0,_common.getConfigProp)(
c,
platform,
'buildToolsVersion',
'28.0.0');



var debugSigning="\n    debug {\n        storeFile file('debug.keystore')\n        storePassword \"android\"\n        keyAlias \"androiddebugkey\"\n        keyPassword \"android\"\n    }";







c.pluginConfigAndroid.appBuildGradleSigningConfigs=debugSigning+"\n    release";

c.pluginConfigAndroid.localProperties='';


if(
c.files.workspace.appConfig.configPrivate&&
c.files.workspace.appConfig.configPrivate[platform])
{
var keystorePath=c.files.workspace.appConfig.configPrivate[platform].storeFile;
var keystorePathFull;
if(keystorePath){
if(keystorePath.startsWith('./')){
keystorePathFull=_path.default.join(
c.paths.workspace.appConfig.dir,
keystorePath);

}else{
keystorePathFull=keystorePath;
}
}

if(_fs.default.existsSync(keystorePathFull)){
var genPropsPath=_path.default.join(appFolder,'keystore.properties');
(0,_fileutils.fsWriteFileSync)(
genPropsPath,"# auto generated by ReNative\nstoreFile="+

keystorePathFull+"\nkeyAlias="+
c.files.workspace.appConfig.configPrivate[platform].keyAlias+"\nstorePassword="+
c.files.workspace.appConfig.configPrivate[platform].storePassword+"\nkeyPassword="+
c.files.workspace.appConfig.configPrivate[platform].keyPassword);


c.pluginConfigAndroid.appBuildGradleSigningConfigs=debugSigning+"\n            release {\n                storeFile file(keystoreProps['storeFile'])\n                storePassword keystoreProps['storePassword']\n                keyAlias keystoreProps['keyAlias']\n                keyPassword keystoreProps['keyPassword']\n            }";







c.pluginConfigAndroid.localProperties="\n          def keystorePropsFile = rootProject.file(\"keystore.properties\")\n          def keystoreProps = new Properties()\n          keystoreProps.load(new FileInputStream(keystorePropsFile))";



}else{
(0,_logger.logWarning)("Your "+
_chalk.default.white(
keystorePathFull)+" does not exist. You won't be able to make production releases without it!");


}
}


var pluginConfig=(_c$buildConfig=c.buildConfig)!=null?_c$buildConfig:{};
var debugBuildTypes=(_pluginConfig$platfor=pluginConfig==null?void 0:(_pluginConfig$platfor2=pluginConfig.platforms[platform])==null?void 0:(_pluginConfig$platfor3=_pluginConfig$platfor2.gradle)==null?void 0:(_pluginConfig$platfor4=_pluginConfig$platfor3.buildTypes)==null?void 0:_pluginConfig$platfor4.debug)!=null?_pluginConfig$platfor:[];
var releaseBuildTypes=(_pluginConfig$platfor5=pluginConfig==null?void 0:(_pluginConfig$platfor6=pluginConfig.platforms[platform])==null?void 0:(_pluginConfig$platfor7=_pluginConfig$platfor6.gradle)==null?void 0:(_pluginConfig$platfor8=_pluginConfig$platfor7.buildTypes)==null?void 0:_pluginConfig$platfor8.release)!=null?_pluginConfig$platfor5:[];
c.pluginConfigAndroid.buildTypes="\n    debug {\n        minifyEnabled false\n        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'\n        "+



debugBuildTypes.join('\n        ')+"\n    }\n    release {\n        minifyEnabled false\n        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'\n        signingConfig signingConfigs.release\n        "+





releaseBuildTypes.join('\n        ')+"\n    }";



var versionCodeOffset=(0,_common.getConfigProp)(
c,
platform,
'versionCodeOffset',
0);

var isMultiApk=(0,_common.getConfigProp)(c,platform,'multipleAPKs',false)===true;
c.pluginConfigAndroid.multiAPKs='';
if(isMultiApk){
c.pluginConfigAndroid.multiAPKs="\n      ext.abiCodes = [\"armeabi-v7a\": 1, \"x86\": 2, \"arm64-v8a\": 3, \"x86_64\": 4]\n      import com.android.build.OutputFile\n\n      android.applicationVariants.all { variant ->\n        variant.outputs.each { output ->\n          def bavc = project.ext.abiCodes.get(output.getFilter(OutputFile.ABI))\n          if (bavc != null) {\n            output.versionCodeOverride = Integer.parseInt(Integer.toString(variant.versionCode) + Integer.toString(bavc)) + "+







versionCodeOffset+"\n          }\n        }\n      }";



}


c.pluginConfigAndroid.splits='';
if(isMultiApk){
c.pluginConfigAndroid.splits="\n    splits {\n      abi {\n          reset()\n          enable true\n          include \"armeabi-v7a\", \"x86\", \"arm64-v8a\", \"x86_64\"\n          universalApk false\n      }\n    }\n";









}


c.pluginConfigAndroid.packagingOptions="\n    exclude 'META-INF/DEPENDENCIES.txt'\n    exclude 'META-INF/DEPENDENCIES'\n    exclude 'META-INF/dependencies.txt'\n    exclude 'META-INF/LICENSE.txt'\n    exclude 'META-INF/LICENSE'\n    exclude 'META-INF/license.txt'\n    exclude 'META-INF/LGPL2.1'\n    exclude 'META-INF/NOTICE.txt'\n    exclude 'META-INF/NOTICE'\n    exclude 'META-INF/notice.txt'\n    pickFirst 'lib/armeabi-v7a/libc++_shared.so'\n    pickFirst 'lib/x86_64/libc++_shared.so'\n    pickFirst 'lib/x86/libc++_shared.so'\n    pickFirst 'lib/arm64-v8a/libc++_shared.so'\n    pickFirst 'lib/arm64-v8a/libjsc.so'\n    pickFirst 'lib/x86_64/libjsc.so'";


















c.pluginConfigAndroid.compileOptions="\n    sourceCompatibility 1.8\n    targetCompatibility 1.8";




var enableAndroidX=(0,_common.getConfigProp)(
c,
platform,
'enableAndroidX',
'androidx.appcompat:appcompat:1.1.0');

if(enableAndroidX===true){
enableAndroidX='androidx.appcompat:appcompat:1.1.0';
}

if(enableAndroidX!==false){
c.pluginConfigAndroid.appBuildGradleImplementations+="    implementation \""+enableAndroidX+"\"\n";
}else{
c.pluginConfigAndroid.appBuildGradleImplementations+=
"    implementation 'com.android.support:appcompat-v7:27.0.2'\n";
}

c.pluginConfigAndroid.appBuildGradleImplementations+=
'    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-alpha02"\n';

var enableHermes=(0,_common.getConfigProp)(c,platform,'enableHermes',false);

if(enableHermes){
c.pluginConfigAndroid.appBuildGradleImplementations+="    debugImplementation files(\""+(0,_resolve.doResolve)(
'hermes-engine',true,{forceForwardPaths:true})+"/android/hermes-debug.aar\")\n";

c.pluginConfigAndroid.appBuildGradleImplementations+="    releaseImplementation files(\""+(0,_resolve.doResolve)(
'hermes-engine',true,{forceForwardPaths:true})+"/android/hermes-release.aar\")\n";

}else{
c.pluginConfigAndroid.appBuildGradleImplementations+=
"    implementation 'org.webkit:android-jsc:+'\n";
}
c.pluginConfigAndroid.enableHermes="    enableHermes: "+enableHermes+",";

(0,_fileutils.writeCleanFile)(
(0,_common.getBuildFilePath)(c,platform,'app/build.gradle'),
_path.default.join(appFolder,'app/build.gradle'),
[
{
pattern:'{{PLUGIN_APPLY}}',
override:c.pluginConfigAndroid.applyPlugin},

{pattern:'{{APPLICATION_ID}}',override:(0,_common.getAppId)(c,platform)},
{
pattern:'{{VERSION_CODE}}',
override:(0,_common.getAppVersionCode)(c,platform)},

{
pattern:'{{VERSION_NAME}}',
override:(0,_common.getAppVersion)(c,platform)},

{
pattern:'{{PLUGIN_IMPLEMENTATIONS}}',
override:c.pluginConfigAndroid.appBuildGradleImplementations},

{
pattern:'{{PLUGIN_AFTER_EVALUATE}}',
override:c.pluginConfigAndroid.appBuildGradleAfterEvaluate},

{
pattern:'{{PLUGIN_SIGNING_CONFIGS}}',
override:c.pluginConfigAndroid.appBuildGradleSigningConfigs},

{
pattern:'{{PLUGIN_SPLITS}}',
override:c.pluginConfigAndroid.splits},

{
pattern:'{{PLUGIN_ANDROID_DEFAULT_CONFIG}}',
override:c.pluginConfigAndroid.defaultConfig},

{
pattern:'{{PLUGIN_PACKAGING_OPTIONS}}',
override:c.pluginConfigAndroid.packagingOptions},

{
pattern:'{{PLUGIN_BUILD_TYPES}}',
override:c.pluginConfigAndroid.buildTypes},

{
pattern:'{{PLUGIN_MULTI_APKS}}',
override:c.pluginConfigAndroid.multiAPKs},

{
pattern:'{{MIN_SDK_VERSION}}',
override:c.pluginConfigAndroid.minSdkVersion},

{
pattern:'{{TARGET_SDK_VERSION}}',
override:c.pluginConfigAndroid.targetSdkVersion},

{
pattern:'{{COMPILE_SDK_VERSION}}',
override:c.pluginConfigAndroid.compileSdkVersion},

{
pattern:'{{PLUGIN_COMPILE_OPTIONS}}',
override:c.pluginConfigAndroid.compileOptions},

{
pattern:'{{PLUGIN_LOCAL_PROPERTIES}}',
override:c.pluginConfigAndroid.localProperties},

{
pattern:'{{PLUGIN_ENABLE_HERMES}}',
override:c.pluginConfigAndroid.enableHermes},

{
pattern:'{{PATH_JSC_ANDROID}}',
override:(0,_resolve.doResolve)('jsc-android',true,{forceForwardPaths:true})},

{
pattern:'{{PATH_REACT_NATIVE}}',
override:(0,_resolve.doResolve)('react-native',true,{forceForwardPaths:true})},

{
pattern:'{{PATH_HERMES_ENGINE}}',
override:(0,_resolve.doResolve)('hermes-engine',true,{forceForwardPaths:true})}],

null,c);

};exports.parseAppBuildGradleSync=parseAppBuildGradleSync;

var parseSettingsGradleSync=function parseSettingsGradleSync(c,platform){
var appFolder=(0,_common.getAppFolder)(c,platform);

(0,_fileutils.writeCleanFile)(
(0,_common.getBuildFilePath)(c,platform,'settings.gradle'),
_path.default.join(appFolder,'settings.gradle'),
[
{
pattern:'{{PLUGIN_INCLUDES}}',
override:c.pluginConfigAndroid.pluginIncludes},

{
pattern:'{{PLUGIN_PATHS}}',
override:c.pluginConfigAndroid.pluginPaths}],

null,c);

};exports.parseSettingsGradleSync=parseSettingsGradleSync;

var parseGradlePropertiesSync=function parseGradlePropertiesSync(c,platform){var _c$buildConfig2,_c$buildConfig2$platf;
var appFolder=(0,_common.getAppFolder)(c,platform);

var pluginGradleProperties='';
var pluginConfigAndroid=((_c$buildConfig2=c.buildConfig)==null?void 0:(_c$buildConfig2$platf=_c$buildConfig2.platforms)==null?void 0:_c$buildConfig2$platf[platform])||{};

var gradleProps=pluginConfigAndroid['gradle.properties'];

if(gradleProps){
var enableAndroidX=(0,_common.getConfigProp)(
c,
platform,
'enableAndroidX',
true);

if(enableAndroidX===true){
gradleProps['android.useAndroidX']=true;
gradleProps['android.enableJetifier']=true;
}

Object.keys(gradleProps).forEach(function(key){
pluginGradleProperties+=key+"="+gradleProps[key]+"\n";
});
}

var gradleProperties='gradle.properties';
(0,_fileutils.writeCleanFile)(
(0,_common.getBuildFilePath)(c,platform,gradleProperties),
_path.default.join(appFolder,gradleProperties),
[
{
pattern:'{{PLUGIN_GRADLE_PROPERTIES}}',
override:pluginGradleProperties}],

null,c);

};exports.parseGradlePropertiesSync=parseGradlePropertiesSync;

var injectPluginGradleSync=function injectPluginGradleSync(c,plugin,key,pkg,pluginRoot){var _buildGradle$allproje,_buildGradle$buildscr,_buildGradle$buildscr2;















var className=pkg?pkg.split('.').pop():null;
var packageParams='';
if(plugin.packageParams){
packageParams=plugin.packageParams.join(',');
}
var keyFixed=key.replace(/\//g,'-').replace(/@/g,'');
var pathFixed=plugin.path?""+plugin.path:key+"/android";
var skipPathResolutions=pluginRoot['no-npm'];
var pathAbsolute;

if(!skipPathResolutions){
pathAbsolute=(0,_resolve.doResolvePath)(pathFixed,true,{forceForwardPaths:true});
}


if(plugin.projectName){
if(!plugin.skipLinking&&!skipPathResolutions){
c.pluginConfigAndroid.pluginIncludes+=", ':"+plugin.projectName+"'";

c.pluginConfigAndroid.pluginPaths+="project(':"+plugin.projectName+"').projectDir = new File('"+pathAbsolute+"')\n";
}
if(!plugin.skipImplementation){
if(plugin.implementation){
c.pluginConfigAndroid.appBuildGradleImplementations+=plugin.implementation+"\n";
}else{
c.pluginConfigAndroid.appBuildGradleImplementations+="    implementation project(':"+plugin.projectName+"')\n";
}
}
}else{
if(!plugin.skipLinking&&!skipPathResolutions){
c.pluginConfigAndroid.pluginIncludes+=", ':"+keyFixed+"'";
c.pluginConfigAndroid.pluginPaths+="project(':"+keyFixed+"').projectDir = new File('"+pathAbsolute+"')\n";
}
if(!plugin.skipImplementation){
if(plugin.implementation){
c.pluginConfigAndroid.appBuildGradleImplementations+=plugin.implementation+"\n";
}else{
c.pluginConfigAndroid.appBuildGradleImplementations+="    implementation project(':"+keyFixed+"')\n";
}
}
}

if(plugin.implementations){
plugin.implementations.forEach(function(v){
c.pluginConfigAndroid.appBuildGradleImplementations+="    implementation "+v+"\n";
});
}

var appBuildGradle=plugin['app/build.gradle'];
if(appBuildGradle){
if(appBuildGradle.apply){
appBuildGradle.apply.forEach(function(v){
c.pluginConfigAndroid.applyPlugin+="apply "+v+"\n";
});
}

if(appBuildGradle.defaultConfig){
appBuildGradle.defaultConfig.forEach(function(v){
c.pluginConfigAndroid.defaultConfig+=v+"\n";
});
}
}

if(plugin.afterEvaluate){
plugin.afterEvaluate.forEach(function(v){
c.pluginConfigAndroid.appBuildGradleAfterEvaluate+=" "+v+"\n";
});
}
if(!skipPathResolutions){
_fixAndroidLegacy(c,pathAbsolute);
}


var buildGradle=plugin.BuildGradle;
var allProjRepos=buildGradle==null?void 0:(_buildGradle$allproje=buildGradle.allprojects)==null?void 0:_buildGradle$allproje.repositories;
if(allProjRepos){
for(k in allProjRepos){
if(allProjRepos[k]===true){
c.pluginConfigAndroid.buildGradleAllProjectsRepositories+=k+"\n";
}
}
}

var buildscriptRepos=buildGradle==null?void 0:(_buildGradle$buildscr=buildGradle.buildscript)==null?void 0:_buildGradle$buildscr.repositories;
if(buildscriptRepos){
for(k in buildscriptRepos){
if(buildscriptRepos[k]===true){
c.pluginConfigAndroid.buildGradleBuildScriptRepositories+=k+"\n";
}
}
}

var buildscriptDeps=buildGradle==null?void 0:(_buildGradle$buildscr2=buildGradle.buildscript)==null?void 0:_buildGradle$buildscr2.dependencies;
if(buildscriptDeps){
for(k in buildscriptDeps){
if(buildscriptDeps[k]===true){
c.pluginConfigAndroid.buildGradleBuildScriptDependencies+=k+"\n";
}
}
}

var buildscriptDexOptions=buildGradle==null?void 0:buildGradle.dexOptions;
if(buildscriptDexOptions){
for(k in buildscriptDexOptions){
if(buildscriptDexOptions[k]===true){
c.pluginConfigAndroid.buildGradleBuildScriptDexOptions+=k+"\n";
}
}
}
};exports.injectPluginGradleSync=injectPluginGradleSync;

var _fixAndroidLegacy=function _fixAndroidLegacy(c,modulePath){
var buildGradle=_path.default.join(
c.paths.project.dir,
modulePath,
'build.gradle');


if(_fs.default.existsSync(buildGradle)){
(0,_logger.logDebug)('FIX:',buildGradle);
(0,_fileutils.writeCleanFile)(buildGradle,buildGradle,[
{pattern:" compile '",override:"  implementation '"},
{pattern:' compile "',override:'  implementation "'},
{pattern:' testCompile "',override:'  testImplementation "'},
{pattern:" provided '",override:"  compileOnly '"},
{pattern:' provided "',override:'  compileOnly "'},
{
pattern:' compile fileTree',
override:'  implementation fileTree'}],

null,c);
}
};
//# sourceMappingURL=gradleParser.js.map