From ff19463e25690c8385544fd74b938f99260f17b4 Mon Sep 17 00:00:00 2001 From: Jaime Bernardo Date: Wed, 21 Feb 2018 20:04:09 +0000 Subject: [PATCH] android: use gradle tasks inputs and output Defines the native modules tasks inputs and outputs so that native modules are only rebuilt if the nodejs-project has changed. Also avoids rebuilding the NDK standalone toolchain if it's already there. --- src/android/build.gradle | 43 +++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/android/build.gradle b/src/android/build.gradle index 1026e76..2b17acd 100644 --- a/src/android/build.gradle +++ b/src/android/build.gradle @@ -64,10 +64,6 @@ if (shouldRebuildNativeModules==null) { if ("1".equals(shouldRebuildNativeModules)) { - task CleanNPMTempBuildDir (type: Delete) { - delete "${rootProject.buildDir}/nodejs-native-assets-temp-build/" - } - android.defaultConfig.ndk.abiFilters.each { abi_name -> String temp_arch = { switch (abi_name) { @@ -137,22 +133,39 @@ if ("1".equals(shouldRebuildNativeModules)) { throw new GradleException("Unsupported opperating system for nodejs-mobile native builds: ${OperatingSystem.current().getName()}") } - task "CopyNodeProjectAssets${abi_name}" (type:Copy) { - dependsOn "CleanNPMTempBuildDir" + task "CopyNodeProjectAssets${abi_name}" { description = "Copying node assets to build native modules for ${abi_name}." - from "${project.projectDir}/assets/www/nodejs-project/" - into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/" + inputs.files fileTree ( + dir: "${project.projectDir}/assets/www/nodejs-project/" + ).exclude({ + details -> // We shouldn't need to rebuild native code if there are only changes in the Node.js project javascript files. + !details.isDirectory() && + details.getPath().endsWith('.js') && + !details.getPath().startsWith('node_modules/') + }) + outputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp" + doLast { + delete "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/" + copy { + from "${project.projectDir}/assets/www/nodejs-project/" + into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/" + } + new File("${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp").text = "${new Date().format('yyyy-MM-dd HH:mm:ss')}" + } } task "MakeToolchain${abi_name}" (type:Exec) { - dependsOn "CopyNodeProjectAssets${abi_name}" description = "Building a native toolchain to compile nodejs-mobile native modules for ${abi_name}." executable = "${ndk_bundle_path}/build/tools/make-standalone-toolchain.sh" args "--toolchain=${temp_toolchain_name}-${temp_cc_ver}", "--arch=${temp_arch}", "--install-dir=${standalone_toolchain}", "--stl=libc++", "--force", "--platform=android-22" + outputs.file "${standalone_toolchain}" } task "BuildNpmModules${abi_name}" (type:Exec) { dependsOn "MakeToolchain${abi_name}" + dependsOn "CopyNodeProjectAssets${abi_name}" + inputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp" + outputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/" description = "Building native modules for ${abi_name}." workingDir "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/" commandLine 'npm', '--verbose', 'rebuild', '--build-from-source' @@ -170,12 +183,9 @@ if ("1".equals(shouldRebuildNativeModules)) { environment ('LINK',"${npm_toolchain_link}") environment ('GYP_DEFINES',"${npm_gyp_defines}") } - task "CleanFinalNpmAssets${abi_name}" (type: Delete) { - delete "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/" - } - task "CopyBuiltNpmAssets${abi_name}" (type:Copy) { + + task "CopyBuiltNpmAssets${abi_name}" (type:Sync) { dependsOn "BuildNpmModules${abi_name}" - dependsOn "CleanFinalNpmAssets${abi_name}" description = "Copying node assets with build native modules for ${abi_name}." from "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/" into "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/" @@ -186,7 +196,12 @@ if ("1".equals(shouldRebuildNativeModules)) { task "GenerateNodeNativeAssetsLists${abi_name}" { dependsOn "CopyBuiltNpmAssets${abi_name}" description "Generates a list for runtime copying" + inputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/" + outputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list" + outputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list" doLast{ + delete "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list" + delete "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list" String file_list = ""; String dir_list = "";