Skip to content

Commit

Permalink
android: 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
so it can read by Gradle. Useful for using Android Studio on macOS,
since windowed applications don't have access to the shell
environment variables unless they are started from the shell.
  • Loading branch information
jaimecbernardo committed Feb 27, 2018
1 parent 7fec514 commit a603da6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
29 changes: 25 additions & 4 deletions install/hooks/android/after-prepare-create-macOS-builder-helper.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
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 helper script to run "npm rebuild" with the current PATH.
// This workaround is needed for Android Studio on macOS when it is not started
// from the command line, as npm probably won't be in the PATH at build time.
function buildMacOSHelperNpmBuildScript(context, platform)
{
var platformPath = path.join(context.opts.projectRoot, 'platforms', platform);
var platformAPI = require(path.join(platformPath, 'cordova', 'Api'));
var platformAPIInstance = new platformAPI();
var wwwPath = platformAPIInstance.locations.www;
var wwwPath = getPlatformWWWPath(context, platform);
var helperMacOSBuildScriptPath = path.join(wwwPath, 'build-native-modules-MacOS-helper-script.sh');
fs.writeFileSync( helperMacOSBuildScriptPath,`#!/bin/bash
export PATH=$PATH:${process.env.PATH}
Expand All @@ -18,11 +24,26 @@ 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');
}
}
11 changes: 11 additions & 0 deletions src/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,23 @@ cdvPluginPostBuildExtras += { ->
android.aaptOptions.ignoreAssetsPattern += ":!*.gz";
// The MacOS builder helper script is not needed inside the APK.
android.aaptOptions.ignoreAssetsPattern += ":!build-native-modules-MacOS-helper-script.sh";
// The file that caches the value of NODEJS_MOBILE_BUILD_NATIVE_MODULES is not needed inside the APK.
android.aaptOptions.ignoreAssetsPattern += ":!NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt";
};

import org.gradle.internal.os.OperatingSystem;

String shouldRebuildNativeModules = System.getenv('NODEJS_MOBILE_BUILD_NATIVE_MODULES');

if (shouldRebuildNativeModules==null) {
// If the environment variable is not set right now, check if it has been saved to a file during
// the cordova prepare phase.
def nativeModulesPreferenceFile = file("${project.projectDir}/assets/www/NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt");
if (nativeModulesPreferenceFile.exists()) {
shouldRebuildNativeModules=nativeModulesPreferenceFile.text;
}
}

if (shouldRebuildNativeModules==null) {
// If build native modules preference is not set, try to find .gyp files to turn it on.
shouldRebuildNativeModules="0";
Expand Down

0 comments on commit a603da6

Please sign in to comment.