diff --git a/README.md b/README.md index fa4f09d..3a20423 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This plugin adds a gradle file to your android project. Depending on your config ## Install ````bash -cordova plugin add cordova-plugin-abi-filter +cordova plugin add cordova-plugin-abi-filter # --variable ABI_FILTER=""armeabi-v7a","armeabi-v8a","x86","x86_64"" ```` or diff --git a/package.json b/package.json index 5c8a54f..a488bbc 100644 --- a/package.json +++ b/package.json @@ -25,5 +25,7 @@ "bugs": { "url": "https://github.com/Ponsen/cordova-plugin-abi-filter/issues" }, - "homepage": "https://github.com/Ponsen/cordova-plugin-abi-filter#readme" + "homepage": "https://github.com/Ponsen/cordova-plugin-abi-filter#readme", + "dependencies": { + } } diff --git a/plugin.xml b/plugin.xml index b50fba0..7cbcbb0 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,25 +1,21 @@ - - + + + cordova-plugin-abi-filter + Filter desired platform ABIs from your project build. + Apache 2.0 License + Paul Stresow + cordova,android,build,abi,armeabi,armeabi-v7a,arm64-v8a,x86,x86_64 - cordova-plugin-abi-filter - Filter desired platform ABIs from your project build. - Apache 2.0 License - Paul Stresow - cordova,android,build,abi,armeabi,armeabi-v7a,arm64-v8a,x86,x86_64 - - - + + + + + - - - - - - - - - - + + + + + + \ No newline at end of file diff --git a/scripts/apply.js b/scripts/apply.js index ea64188..dd13a5c 100644 --- a/scripts/apply.js +++ b/scripts/apply.js @@ -1 +1,117 @@ -//TODO \ No newline at end of file +module.exports = function (context) { + //only procecss android platform + if (!context.opts.cordova.platforms.includes('android')) { + return + } + + console.log('ABI Filter applying changes...') + + //Requirements + const Q = context.requireCordovaModule('q') + const deferral = new Q.defer() + + const common = context.requireCordovaModule('cordova-common') + const utils = context.requireCordovaModule('cordova-lib/src/cordova/util') + const fs = context.requireCordovaModule('fs') + const path = context.requireCordovaModule('path') + const readline = context.requireCordovaModule('readline') + const os = context.requireCordovaModule('os') + + const projectRoot = context.opts.projectRoot + + const gradleFilePath = path.join(projectRoot, 'platforms/android/' + context.opts.plugin.id) + + + const ABI_PREF_KEY = "ABI_FILTER" + + //helper funcs + function searchFilesByFilterRecursive(startPath, filter, callback) { + + if (!fs.existsSync(startPath)) { + console.log("no dir ", startPath) + deferral.reject(new Error("folder " + startPath + " does not exist")) + } + + var files = fs.readdirSync(startPath) + for (var i = 0; i < files.length; i++) { + var filename = path.join(startPath, files[i]) + var stat = fs.lstatSync(filename) + if (stat.isDirectory()) { + searchFilesByFilterRecursive(filename, filter, callback) //recurse + } + else if (filter.test(filename)) callback(filename) + } + } + + //prefix changes according to project name - just searching for the file. folder is known anyway + searchFilesByFilterRecursive(gradleFilePath, /\-build-extras.gradle$/, (filepath) => { + + //default values + var abi_values = "armeabi-v7a|armeabi-v8a|x86|x86_64" + + //Variables can come from 3 different places + + //plugin.xml + //Prio 3 - default if user did not specify cli args or if plugin is just added + if (context.opts.plugin.pluginInfo.getPreferences().ABI_FILTER) { + abi_values = context.opts.plugin.pluginInfo.getPreferences().ABI_FILTER + } + + //CLI Arg + //Prio 2 - is present when plugin is added with --variable ABI_FILTER="a|b|c" + if (context.opts.cli_variables && context.opts.cli_variables.ABI_FILTER) { + abi_values = context.opts.cli_variables.ABI_FILTER + } + + //config.xml + //Prio 1 - plugin was already added or user added key to config.xml manually + const configFile = new common.ConfigParser(utils.projectConfig(projectRoot)) + if (configFile.getPlugin(context.opts.plugin.id).variables.ABI_FILTER) { + abi_values = configFile.getPlugin(context.opts.plugin.id).variables.ABI_FILTER + } + + //parse them values to a gradle readable string + var abi_values_parsed = ""; + let abi_arr = abi_values.split("|"); + for (var i = 0; i < abi_arr.length; ++i) { + abi_values_parsed += '"' + abi_arr[i] + '"' + (abi_arr.length - 1 === i ? "" : ",") + } + + //find the line "abiFilters" + const line_key = "abiFilters" + var replacement_line = " abiFilters " + abi_values_parsed + var replacement_file = "" + + //read gradle file from project + var reader = readline.createInterface({ + input: fs.createReadStream(filepath) + }) + + reader.on('line', (line) => { + if(line.trim().includes(line_key)){ + replacement_file += replacement_line + os.EOL + } else { + replacement_file += line + os.EOL + } + }) + + reader.on('close', () => { + + //save (override) gradle file from project + fs.writeFile(filepath, replacement_file, {encoding:'utf8',flag:'w'}, function(err) { + if(err) { + console.log("Applied ABI-Filters: " + abi_values) + deferral.reject(err) + } + + deferral.resolve() + }); + + console.log("Applied ABI-Filters: " + abi_values) + + deferral.resolve() + }) + }) + + return deferral.promise +} \ No newline at end of file diff --git a/src/android/build-extras.gradle b/src/android/build-extras.gradle new file mode 100644 index 0000000..ba0b31b --- /dev/null +++ b/src/android/build-extras.gradle @@ -0,0 +1,7 @@ +android { + defaultConfig { + ndk { + abiFilters "armeabi","armeabi-v7a","armeabi-v8a","x86","x86_64" + } + } +} \ No newline at end of file diff --git a/src/android/build-extras.gradle.template b/src/android/build-extras.gradle.template deleted file mode 100644 index 189afdf..0000000 --- a/src/android/build-extras.gradle.template +++ /dev/null @@ -1,7 +0,0 @@ -android { - defaultConfig { - ndk { - abiFilters {{ABI_FILTER}} - } - } -} \ No newline at end of file