1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import org.apache.tools.ant.taskdefs.condition.Os
|
7 |
|
8 | def config = project.hasProperty("react") ? project.react : [];
|
9 |
|
10 | def cliPath = config.cliPath ?: "node_modules/react-native/cli.js"
|
11 | def composeSourceMapsPath = config.composeSourceMapsPath ?: "node_modules/react-native/scripts/compose-source-maps.js"
|
12 | def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
|
13 | def entryFile = config.entryFile ?: "index.android.js"
|
14 | def bundleCommand = config.bundleCommand ?: "bundle"
|
15 | def reactRoot = file(config.root ?: "../../")
|
16 | def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"]
|
17 | def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ;
|
18 | def enableVmCleanup = config.enableVmCleanup == null ? true : config.enableVmCleanup
|
19 | def hermesCommand = config.hermesCommand ?: "../../node_modules/hermes-engine/%OS-BIN%/hermes"
|
20 |
|
21 | def reactNativeDevServerPort() {
|
22 | def value = project.getProperties().get("reactNativeDevServerPort")
|
23 | return value != null ? value : "8081"
|
24 | }
|
25 |
|
26 | def reactNativeInspectorProxyPort() {
|
27 | def value = project.getProperties().get("reactNativeInspectorProxyPort")
|
28 | return value != null ? value : reactNativeDevServerPort()
|
29 | }
|
30 |
|
31 | def getHermesOSBin() {
|
32 | if (Os.isFamily(Os.FAMILY_WINDOWS)) return "win64-bin";
|
33 | if (Os.isFamily(Os.FAMILY_MAC)) return "osx-bin";
|
34 | if (Os.isOs(null, "linux", "amd64", null)) return "linux64-bin";
|
35 | throw new Exception("OS not recognized. Please set project.ext.react.hermesCommand " +
|
36 | "to the path of a working Hermes compiler.");
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 | def getHermesCommand = {
|
42 |
|
43 | if (!hermesCommand.contains("%OS-BIN%")) {
|
44 | return hermesCommand
|
45 | }
|
46 |
|
47 |
|
48 | return hermesCommand
|
49 | .replaceAll("%OS-BIN%", getHermesOSBin())
|
50 | .replace('/' as char, File.separatorChar);
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 | def enableHermesForVariant = config.enableHermesForVariant ?: {
|
56 | def variant -> config.enableHermes ?: false
|
57 | }
|
58 |
|
59 | android {
|
60 | buildTypes.all {
|
61 | resValue "integer", "react_native_dev_server_port", reactNativeDevServerPort()
|
62 | resValue "integer", "react_native_inspector_proxy_port", reactNativeInspectorProxyPort()
|
63 | }
|
64 | }
|
65 |
|
66 | afterEvaluate {
|
67 | def isAndroidLibrary = plugins.hasPlugin("com.android.library")
|
68 | def variants = isAndroidLibrary ? android.libraryVariants : android.applicationVariants
|
69 | variants.all { def variant ->
|
70 |
|
71 | def targetName = variant.name.capitalize()
|
72 | def targetPath = variant.dirName
|
73 |
|
74 |
|
75 | def jsBundleDir = file("$buildDir/generated/assets/react/${targetPath}")
|
76 | def resourcesDir = file("$buildDir/generated/res/react/${targetPath}")
|
77 |
|
78 | def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
|
79 | def jsSourceMapsDir = file("$buildDir/generated/sourcemaps/react/${targetPath}")
|
80 | def jsIntermediateSourceMapsDir = file("$buildDir/intermediates/sourcemaps/react/${targetPath}")
|
81 | def jsPackagerSourceMapFile = file("$jsIntermediateSourceMapsDir/${bundleAssetName}.packager.map")
|
82 | def jsCompilerSourceMapFile = file("$jsIntermediateSourceMapsDir/${bundleAssetName}.compiler.map")
|
83 | def jsOutputSourceMapFile = file("$jsSourceMapsDir/${bundleAssetName}.map")
|
84 |
|
85 |
|
86 | def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
|
87 | def extraPackagerArgs = config.extraPackagerArgs ?: []
|
88 | def npx = Os.isFamily(Os.FAMILY_WINDOWS) ? "npx.cmd" : "npx"
|
89 |
|
90 | def execCommand = []
|
91 |
|
92 | if (config.cliPath || config.nodeExecutableAndArgs) {
|
93 | if (Os.isFamily(Os.FAMILY_WINDOWS)) {
|
94 | execCommand.addAll(["cmd", "/c", *nodeExecutableAndArgs, cliPath])
|
95 | } else {
|
96 | execCommand.addAll([*nodeExecutableAndArgs, cliPath])
|
97 | }
|
98 | } else {
|
99 | execCommand.addAll([npx, "react-native"])
|
100 | }
|
101 |
|
102 | def enableHermes = enableHermesForVariant(variant)
|
103 |
|
104 | def currentBundleTask = tasks.create(
|
105 | name: "bundle${targetName}JsAndAssets",
|
106 | type: Exec) {
|
107 | group = "react"
|
108 | description = "bundle JS and assets for ${targetName}."
|
109 |
|
110 |
|
111 | doFirst {
|
112 | jsBundleDir.deleteDir()
|
113 | jsBundleDir.mkdirs()
|
114 | resourcesDir.deleteDir()
|
115 | resourcesDir.mkdirs()
|
116 | jsIntermediateSourceMapsDir.deleteDir()
|
117 | jsIntermediateSourceMapsDir.mkdirs()
|
118 | jsSourceMapsDir.deleteDir()
|
119 | jsSourceMapsDir.mkdirs()
|
120 | }
|
121 |
|
122 |
|
123 | inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
|
124 | outputs.dir(jsBundleDir)
|
125 | outputs.dir(resourcesDir)
|
126 |
|
127 |
|
128 | workingDir(reactRoot)
|
129 |
|
130 |
|
131 | def devEnabled = !(config."devDisabledIn${targetName}"
|
132 | || targetName.toLowerCase().contains("release"))
|
133 |
|
134 | def extraArgs = extraPackagerArgs;
|
135 |
|
136 | if (bundleConfig) {
|
137 | extraArgs = extraArgs.clone()
|
138 | extraArgs.add("--config");
|
139 | extraArgs.add(bundleConfig);
|
140 | }
|
141 |
|
142 | commandLine(*execCommand, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
|
143 | "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir,
|
144 | "--sourcemap-output", enableHermes ? jsPackagerSourceMapFile : jsOutputSourceMapFile, *extraArgs)
|
145 |
|
146 |
|
147 | if (enableHermes) {
|
148 | doLast {
|
149 | def hermesFlags;
|
150 | def hbcTempFile = file("${jsBundleFile}.hbc")
|
151 | exec {
|
152 | if (targetName.toLowerCase().contains("release")) {
|
153 |
|
154 | hermesFlags = config.hermesFlagsRelease
|
155 | if (hermesFlags == null) hermesFlags = ["-O", "-output-source-map"]
|
156 | } else {
|
157 | hermesFlags = config.hermesFlagsDebug
|
158 | if (hermesFlags == null) hermesFlags = []
|
159 | }
|
160 |
|
161 | if (Os.isFamily(Os.FAMILY_WINDOWS)) {
|
162 | commandLine("cmd", "/c", getHermesCommand(), "-emit-binary", "-out", hbcTempFile, jsBundleFile, *hermesFlags)
|
163 | } else {
|
164 | commandLine(getHermesCommand(), "-emit-binary", "-out", hbcTempFile, jsBundleFile, *hermesFlags)
|
165 | }
|
166 | }
|
167 | ant.move(
|
168 | file: hbcTempFile,
|
169 | toFile: jsBundleFile
|
170 | );
|
171 | if (hermesFlags.contains("-output-source-map")) {
|
172 | ant.move(
|
173 |
|
174 | file: "${jsBundleFile}.hbc.map",
|
175 | tofile: jsCompilerSourceMapFile
|
176 | );
|
177 | exec {
|
178 |
|
179 |
|
180 |
|
181 | workingDir(reactRoot)
|
182 | if (Os.isFamily(Os.FAMILY_WINDOWS)) {
|
183 | commandLine("cmd", "/c", *nodeExecutableAndArgs, composeSourceMapsPath, jsPackagerSourceMapFile, jsCompilerSourceMapFile, "-o", jsOutputSourceMapFile)
|
184 | } else {
|
185 | commandLine(*nodeExecutableAndArgs, composeSourceMapsPath, jsPackagerSourceMapFile, jsCompilerSourceMapFile, "-o", jsOutputSourceMapFile)
|
186 | }
|
187 | }
|
188 | }
|
189 | }
|
190 | }
|
191 |
|
192 | enabled config."bundleIn${targetName}" != null
|
193 | ? config."bundleIn${targetName}"
|
194 | : config."bundleIn${variant.buildType.name.capitalize()}" != null
|
195 | ? config."bundleIn${variant.buildType.name.capitalize()}"
|
196 | : targetName.toLowerCase().contains("release")
|
197 | }
|
198 |
|
199 |
|
200 | variant.ext.bundleJsAndAssets = currentBundleTask
|
201 | currentBundleTask.ext.generatedResFolders = files(resourcesDir).builtBy(currentBundleTask)
|
202 | currentBundleTask.ext.generatedAssetsFolders = files(jsBundleDir).builtBy(currentBundleTask)
|
203 |
|
204 |
|
205 | if (variant.respondsTo("registerGeneratedResFolders")) {
|
206 | variant.registerGeneratedResFolders(currentBundleTask.generatedResFolders)
|
207 | } else {
|
208 | variant.registerResGeneratingTask(currentBundleTask)
|
209 | }
|
210 | variant.mergeResourcesProvider.get().dependsOn(currentBundleTask)
|
211 |
|
212 |
|
213 | def packageTask = variant.hasProperty("packageApplication")
|
214 | ? variant.packageApplicationProvider.get()
|
215 | : tasks.findByName("package${targetName}")
|
216 | if (variant.hasProperty("packageLibrary")) {
|
217 | packageTask = variant.packageLibrary
|
218 | }
|
219 |
|
220 |
|
221 | def buildPreBundleTask = tasks.findByName("build${targetName}PreBundle")
|
222 |
|
223 | def resourcesDirConfigValue = config."resourcesDir${targetName}"
|
224 | if (resourcesDirConfigValue) {
|
225 | def currentCopyResTask = tasks.create(
|
226 | name: "copy${targetName}BundledResources",
|
227 | type: Copy) {
|
228 | group = "react"
|
229 | description = "copy bundled resources into custom location for ${targetName}."
|
230 |
|
231 | from(resourcesDir)
|
232 | into(file(resourcesDirConfigValue))
|
233 |
|
234 | dependsOn(currentBundleTask)
|
235 |
|
236 | enabled(currentBundleTask.enabled)
|
237 | }
|
238 |
|
239 | packageTask.dependsOn(currentCopyResTask)
|
240 | if (buildPreBundleTask != null) {
|
241 | buildPreBundleTask.dependsOn(currentCopyResTask)
|
242 | }
|
243 | }
|
244 |
|
245 | def currentAssetsCopyTask = tasks.create(
|
246 | name: "copy${targetName}BundledJs",
|
247 | type: Copy) {
|
248 | group = "react"
|
249 | description = "copy bundled JS into ${targetName}."
|
250 |
|
251 | if (config."jsBundleDir${targetName}") {
|
252 | from(jsBundleDir)
|
253 | into(file(config."jsBundleDir${targetName}"))
|
254 | } else {
|
255 | into ("$buildDir/intermediates")
|
256 | into ("assets/${targetPath}") {
|
257 | from(jsBundleDir)
|
258 | }
|
259 |
|
260 |
|
261 | into ("merged_assets/${variant.name}/merge${targetName}Assets/out") {
|
262 | from(jsBundleDir)
|
263 | }
|
264 |
|
265 |
|
266 | into ("merged_assets/${variant.name}/out") {
|
267 | from(jsBundleDir)
|
268 | }
|
269 | }
|
270 |
|
271 |
|
272 | dependsOn(variant.mergeAssetsProvider.get())
|
273 |
|
274 | enabled(currentBundleTask.enabled)
|
275 | }
|
276 |
|
277 | packageTask.dependsOn(currentAssetsCopyTask)
|
278 | if (buildPreBundleTask != null) {
|
279 | buildPreBundleTask.dependsOn(currentAssetsCopyTask)
|
280 | }
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | def isRelease = targetName.toLowerCase().contains("release")
|
289 | def libDir = "$buildDir/intermediates/transforms/"
|
290 | def vmSelectionAction = {
|
291 | fileTree(libDir).matching {
|
292 | if (enableHermes) {
|
293 |
|
294 | include "**/libjsc*.so"
|
295 |
|
296 | if (isRelease) {
|
297 |
|
298 | include '**/libhermes-inspector.so'
|
299 | include '**/libhermes-executor-debug.so'
|
300 | } else {
|
301 |
|
302 |
|
303 | include '**/libhermes-executor-release.so'
|
304 | }
|
305 | } else {
|
306 |
|
307 | include "**/libhermes*.so"
|
308 | }
|
309 | }.visit { details ->
|
310 | def targetVariant = ".*/transforms/[^/]*/${targetPath}/.*"
|
311 | def path = details.file.getAbsolutePath().replace(File.separatorChar, '/' as char)
|
312 | if (path.matches(targetVariant) && details.file.isFile()) {
|
313 | details.file.delete()
|
314 | }
|
315 | }
|
316 | }
|
317 |
|
318 | if (enableVmCleanup) {
|
319 | def task = tasks.findByName("package${targetName}")
|
320 | task.doFirst(vmSelectionAction)
|
321 | }
|
322 | }
|
323 | }
|