Skip to content

Commit

Permalink
ios: cache native modules override preference
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jaimecbernardo committed Jun 26, 2018
1 parent 3d430ec commit 29240cd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
15 changes: 0 additions & 15 deletions install/hooks/android/after-prepare-create-macOS-builder-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
32 changes: 32 additions & 0 deletions install/hooks/both/after-prepare-native-modules-preference.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
20 changes: 18 additions & 2 deletions install/hooks/ios/after-plugin-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<hook type="after_plugin_install" src="install/hooks/ios/after-plugin-install.js" />
<hook type="before_plugin_uninstall" src="install/hooks/ios/before-plugin-uninstall.js" />
<hook type="after_prepare" src="install/hooks/both/after-prepare-patch-npm-packages.js" />
<hook type="after_prepare" src="install/hooks/both/after-prepare-native-modules-preference.js" />

<config-file target="config.xml" parent="/*">
<feature name="NodeJS">
Expand Down Expand Up @@ -59,6 +60,7 @@
<hook type="after_prepare" src="install/hooks/android/after-prepare-build-node-assets-lists.js" />
<hook type="after_prepare" src="install/hooks/both/after-prepare-patch-npm-packages.js" />
<hook type="after_prepare" src="install/hooks/android/after-prepare-create-macOS-builder-helper.js" />
<hook type="after_prepare" src="install/hooks/both/after-prepare-native-modules-preference.js" />

<config-file target="res/xml/config.xml" parent="/*">
<feature name="NodeJS">
Expand Down

0 comments on commit 29240cd

Please sign in to comment.