|
| 1 | +import org.apache.tools.ant.taskdefs.condition.Os |
| 2 | + |
| 3 | +def config = project.hasProperty("react") ? project.react : []; |
| 4 | + |
| 5 | +def bundleAssetName = config.bundleAssetName ?: "index.android.bundle" |
| 6 | +def entryFile = config.entryFile ?: "index.android.js" |
| 7 | + |
| 8 | +// because elvis operator |
| 9 | +def elvisFile(thing) { |
| 10 | + return thing ? file(thing) : null; |
| 11 | +} |
| 12 | + |
| 13 | +def reactRoot = elvisFile(config.root) ?: file("../../") |
| 14 | +def jsBundleDirDebug = elvisFile(config.jsBundleDirDebug) ?: |
| 15 | + file("$buildDir/intermediates/assets/debug") |
| 16 | +def jsBundleDirRelease = elvisFile(config.jsBundleDirRelease) ?: |
| 17 | + file("$buildDir/intermediates/assets/release") |
| 18 | +def resourcesDirDebug = elvisFile(config.resourcesDirDebug) ?: |
| 19 | + file("$buildDir/intermediates/res/merged/debug") |
| 20 | +def resourcesDirRelease = elvisFile(config.resourcesDirRelease) ?: |
| 21 | + file("$buildDir/intermediates/res/merged/release") |
| 22 | +def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"] |
| 23 | + |
| 24 | +def jsBundleFileDebug = file("$jsBundleDirDebug/$bundleAssetName") |
| 25 | +def jsBundleFileRelease = file("$jsBundleDirRelease/$bundleAssetName") |
| 26 | + |
| 27 | +task bundleDebugJsAndAssets(type: Exec) { |
| 28 | + // create dirs if they are not there (e.g. the "clean" task just ran) |
| 29 | + doFirst { |
| 30 | + jsBundleDirDebug.mkdirs() |
| 31 | + resourcesDirDebug.mkdirs() |
| 32 | + } |
| 33 | + |
| 34 | + // set up inputs and outputs so gradle can cache the result |
| 35 | + inputs.files fileTree(dir: reactRoot, excludes: inputExcludes) |
| 36 | + outputs.dir jsBundleDirDebug |
| 37 | + outputs.dir resourcesDirDebug |
| 38 | + |
| 39 | + // set up the call to the react-native cli |
| 40 | + workingDir reactRoot |
| 41 | + if (Os.isFamily(Os.FAMILY_WINDOWS)) { |
| 42 | + commandLine "cmd", "/c", "react-native", "bundle", "--platform", "android", "--dev", "true", "--entry-file", |
| 43 | + entryFile, "--bundle-output", jsBundleFileDebug, "--assets-dest", resourcesDirDebug |
| 44 | + } else { |
| 45 | + commandLine "react-native", "bundle", "--platform", "android", "--dev", "true", "--entry-file", |
| 46 | + entryFile, "--bundle-output", jsBundleFileDebug, "--assets-dest", resourcesDirDebug |
| 47 | + } |
| 48 | + |
| 49 | + enabled config.bundleInDebug ?: false |
| 50 | +} |
| 51 | + |
| 52 | +task bundleReleaseJsAndAssets(type: Exec) { |
| 53 | + // create dirs if they are not there (e.g. the "clean" task just ran) |
| 54 | + doFirst { |
| 55 | + jsBundleDirRelease.mkdirs() |
| 56 | + resourcesDirRelease.mkdirs() |
| 57 | + } |
| 58 | + |
| 59 | + // set up inputs and outputs so gradle can cache the result |
| 60 | + inputs.files fileTree(dir: reactRoot, excludes: inputExcludes) |
| 61 | + outputs.dir jsBundleDirRelease |
| 62 | + outputs.dir resourcesDirRelease |
| 63 | + |
| 64 | + // set up the call to the react-native cli |
| 65 | + workingDir reactRoot |
| 66 | + if (Os.isFamily(Os.FAMILY_WINDOWS)) { |
| 67 | + commandLine "cmd","/c", "react-native", "bundle", "--platform", "android", "--dev", "false", "--entry-file", |
| 68 | + entryFile, "--bundle-output", jsBundleFileRelease, "--assets-dest", resourcesDirRelease |
| 69 | + } else { |
| 70 | + commandLine "react-native", "bundle", "--platform", "android", "--dev", "false", "--entry-file", |
| 71 | + entryFile, "--bundle-output", jsBundleFileRelease, "--assets-dest", resourcesDirRelease |
| 72 | + } |
| 73 | + |
| 74 | + enabled config.bundleInRelease ?: true |
| 75 | +} |
| 76 | + |
| 77 | +gradle.projectsEvaluated { |
| 78 | + // hook bundleDebugJsAndAssets into the android build process |
| 79 | + bundleDebugJsAndAssets.dependsOn mergeDebugResources |
| 80 | + bundleDebugJsAndAssets.dependsOn mergeDebugAssets |
| 81 | + processDebugResources.dependsOn bundleDebugJsAndAssets |
| 82 | + |
| 83 | + // hook bundleReleaseJsAndAssets into the android build process |
| 84 | + bundleReleaseJsAndAssets.dependsOn mergeReleaseResources |
| 85 | + bundleReleaseJsAndAssets.dependsOn mergeReleaseAssets |
| 86 | + processReleaseResources.dependsOn bundleReleaseJsAndAssets |
| 87 | +} |
0 commit comments