From 29240cda7913e1e6fa00267b3dd9ab2bae11beb6 Mon Sep 17 00:00:00 2001 From: Jaime Bernardo Date: Tue, 26 Jun 2018 16:15:06 +0100 Subject: [PATCH] ios: cache native modules override preference Saves the NODEJS_MOBILE_BUILD_NATIVE_MODULES environment variable value to a file if it is defined during the Cordova prepare phase to be read by iOS during build time, similar to the way it is working for Android. It's also possible to create the file in www/ instead of using the environment variable. --- ...ter-prepare-create-macOS-builder-helper.js | 15 --------- ...after-prepare-native-modules-preference.js | 32 +++++++++++++++++++ install/hooks/ios/after-plugin-install.js | 20 ++++++++++-- plugin.xml | 2 ++ 4 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 install/hooks/both/after-prepare-native-modules-preference.js diff --git a/install/hooks/android/after-prepare-create-macOS-builder-helper.js b/install/hooks/android/after-prepare-create-macOS-builder-helper.js index 6c9174f..4413eb0 100644 --- a/install/hooks/android/after-prepare-create-macOS-builder-helper.js +++ b/install/hooks/android/after-prepare-create-macOS-builder-helper.js @@ -24,26 +24,11 @@ function buildMacOSHelperNpmBuildScript(context, platform) ); } -// Adds a file to save the contents of the NODEJS_MOBILE_BUILD_NATIVE_MODULES -// environment variable if it is set during the prepare step. -// This workaround is needed for Android Studio on macOS when it is not started -// from the command line, since environment variables set in the shell won't -// be available. -function saveBuildNativeModulesPreference(context, platform) -{ - var wwwPath = getPlatformWWWPath(context, platform); - var saveBuildNativeModulesPreferencePath = path.join(wwwPath, 'NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt'); - if (process.env.NODEJS_MOBILE_BUILD_NATIVE_MODULES !== undefined) { - fs.writeFileSync(saveBuildNativeModulesPreferencePath, process.env.NODEJS_MOBILE_BUILD_NATIVE_MODULES); - } -} - module.exports = function(context) { if (context.opts.platforms.indexOf('android') >= 0) { if (process.platform === 'darwin') { buildMacOSHelperNpmBuildScript(context, 'android'); } - saveBuildNativeModulesPreference(context, 'android'); } } diff --git a/install/hooks/both/after-prepare-native-modules-preference.js b/install/hooks/both/after-prepare-native-modules-preference.js new file mode 100644 index 0000000..aa6cdc5 --- /dev/null +++ b/install/hooks/both/after-prepare-native-modules-preference.js @@ -0,0 +1,32 @@ +const fs = require('fs'); +const path = require('path'); + +// Gets the platform's www path. +function getPlatformWWWPath(context, platform) +{ + var platformPath = path.join(context.opts.projectRoot, 'platforms', platform); + var platformAPI = require(path.join(platformPath, 'cordova', 'Api')); + var platformAPIInstance = new platformAPI(); + return platformAPIInstance.locations.www; +} + +// Adds a file to save the contents of the NODEJS_MOBILE_BUILD_NATIVE_MODULES +// environment variable if it is set during the prepare step. +function saveBuildNativeModulesPreference(context, platform) +{ + var wwwPath = getPlatformWWWPath(context, platform); + var saveBuildNativeModulesPreferencePath = path.join(wwwPath, 'NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt'); + if (process.env.NODEJS_MOBILE_BUILD_NATIVE_MODULES !== undefined) { + fs.writeFileSync(saveBuildNativeModulesPreferencePath, process.env.NODEJS_MOBILE_BUILD_NATIVE_MODULES); + } +} + +module.exports = function(context) +{ + if (context.opts.platforms.indexOf('android') >= 0) { + saveBuildNativeModulesPreference(context, 'android'); + } + if (context.opts.platforms.indexOf('ios') >= 0) { + saveBuildNativeModulesPreference(context, 'ios'); + } +} diff --git a/install/hooks/ios/after-plugin-install.js b/install/hooks/ios/after-plugin-install.js index 79d09df..54820a8 100644 --- a/install/hooks/ios/after-plugin-install.js +++ b/install/hooks/ios/after-plugin-install.js @@ -20,7 +20,14 @@ module.exports = function(context) { var rebuildNativeModulesBuildPhaseScript = ` set -e if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then - NODEJS_MOBILE_BUILD_NATIVE_MODULES=0 +# If build native modules preference is not set, look for it in the project's +# www/NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt + PREFERENCE_FILE_PATH="$CODESIGNING_FOLDER_PATH/www/NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt" + if [ -f "$PREFERENCE_FILE_PATH" ]; then + NODEJS_MOBILE_BUILD_NATIVE_MODULES="$(cat $PREFERENCE_FILE_PATH | xargs)" + else + NODEJS_MOBILE_BUILD_NATIVE_MODULES=0 + fi fi if [ "1" != "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then exit 0; fi # Delete object files that may already come from within the npm package. @@ -66,7 +73,16 @@ popd var signNativeModulesBuildPhaseScript = ` set -e if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then - NODEJS_MOBILE_BUILD_NATIVE_MODULES=0 +# If build native modules preference is not set, look for it in the project's +# www/NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt + PREFERENCE_FILE_PATH="$CODESIGNING_FOLDER_PATH/www/NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt" + if [ -f "$PREFERENCE_FILE_PATH" ]; then + NODEJS_MOBILE_BUILD_NATIVE_MODULES="$(cat $PREFERENCE_FILE_PATH | xargs)" + # Remove the preference file so it doesn't get in the application package. + rm "$PREFERENCE_FILE_PATH" + else + NODEJS_MOBILE_BUILD_NATIVE_MODULES=0 + fi fi if [ "1" != "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then exit 0; fi # Delete object files diff --git a/plugin.xml b/plugin.xml index b2d31b6..6449ad2 100644 --- a/plugin.xml +++ b/plugin.xml @@ -29,6 +29,7 @@ + @@ -59,6 +60,7 @@ +