diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/AndroidConfiguration.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/AndroidConfiguration.kt index 6ff141045e7..0e5cf8f75f2 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/AndroidConfiguration.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/AndroidConfiguration.kt @@ -12,7 +12,8 @@ import org.gradle.api.Project fun Project.configureDevPorts(androidExt: BaseExtension) { val devServerPort = project.properties["reactNativeDevServerPort"]?.toString() ?: "8081" - val inspectorProxyPort = project.properties["reactNativeInspectorProxyPort"]?.toString() ?: devServerPort + val inspectorProxyPort = + project.properties["reactNativeInspectorProxyPort"]?.toString() ?: devServerPort androidExt.buildTypes.all { resValue("integer", "react_native_dev_server_port", devServerPort) diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactAppExtension.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactAppExtension.kt index 01343bcc126..09099f7aa09 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactAppExtension.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactAppExtension.kt @@ -8,9 +8,9 @@ package com.facebook.react import com.android.build.gradle.api.BaseVariant +import java.io.File import org.apache.tools.ant.taskdefs.condition.Os import org.gradle.api.Project -import java.io.File open class ReactAppExtension(private val project: Project) { var composeSourceMapsPath: String = "node_modules/react-native/scripts/compose-source-maps.js" @@ -39,36 +39,38 @@ open class ReactAppExtension(private val project: Project) { get() = detectEntryFile(entryFile = entryFile, reactRoot = reactRoot) internal val detectedCliPath: String - get() = detectCliPath( - projectDir = project.projectDir, - reactRoot = reactRoot, - preconfuredCliPath = cliPath - ) + get() = + detectCliPath( + projectDir = project.projectDir, reactRoot = reactRoot, preconfuredCliPath = cliPath) internal val osAwareHermesCommand: String get() = getOSAwareHermesCommand(hermesCommand) - private fun detectEntryFile(entryFile: File?, reactRoot: File): File = when { - System.getenv("ENTRY_FILE") != null -> File(System.getenv("ENTRY_FILE")) - entryFile != null -> entryFile - File(reactRoot, "index.android.js").exists() -> File(reactRoot, "index.android.js") - else -> File(reactRoot, "index.android.js") - } + private fun detectEntryFile(entryFile: File?, reactRoot: File): File = + when { + System.getenv("ENTRY_FILE") != null -> File(System.getenv("ENTRY_FILE")) + entryFile != null -> entryFile + File(reactRoot, "index.android.js").exists() -> File(reactRoot, "index.android.js") + else -> File(reactRoot, "index.android.js") + } - private fun detectCliPath(projectDir: File, reactRoot: File, preconfuredCliPath: String?): String { + private fun detectCliPath( + projectDir: File, + reactRoot: File, + preconfuredCliPath: String? + ): String { // 1. preconfigured path if (preconfuredCliPath != null) return preconfuredCliPath // 2. node module path - val nodeProcess = Runtime.getRuntime().exec( - arrayOf("node", "-e", "console.log(require('react-native/cli').bin);"), - emptyArray(), - projectDir - ) + val nodeProcess = + Runtime.getRuntime() + .exec( + arrayOf("node", "-e", "console.log(require('react-native/cli').bin);"), + emptyArray(), + projectDir) - val nodeProcessOutput = nodeProcess.inputStream.use { - it.bufferedReader().readText().trim() - } + val nodeProcessOutput = nodeProcess.inputStream.use { it.bufferedReader().readText().trim() } if (nodeProcessOutput.isNotEmpty()) { return nodeProcessOutput @@ -80,8 +82,9 @@ open class ReactAppExtension(private val project: Project) { return rootCliJs.absolutePath } - error("Couldn't determine CLI location. " + - "Please set `project.react.cliPath` to the path of the react-native cli.js") + error( + "Couldn't determine CLI location. " + + "Please set `project.react.cliPath` to the path of the react-native cli.js") } // Make sure not to inspect the Hermes config unless we need it, @@ -93,16 +96,15 @@ open class ReactAppExtension(private val project: Project) { } // Execution on Windows fails with / as separator - return hermesCommand - .replace("%OS-BIN%", getHermesOSBin()) - .replace('/', File.separatorChar) + return hermesCommand.replace("%OS-BIN%", getHermesOSBin()).replace('/', File.separatorChar) } private fun getHermesOSBin(): String { if (Os.isFamily(Os.FAMILY_WINDOWS)) return "win64-bin" if (Os.isFamily(Os.FAMILY_MAC)) return "osx-bin" if (Os.isOs(null, "linux", "amd64", null)) return "linux64-bin" - error("OS not recognized. Please set project.react.hermesCommand " + - "to the path of a working Hermes compiler.") + error( + "OS not recognized. Please set project.react.hermesCommand " + + "to the path of a working Hermes compiler.") } } diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactAppPlugin.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactAppPlugin.kt index 14265ca6647..7b06e9067d1 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactAppPlugin.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactAppPlugin.kt @@ -24,17 +24,13 @@ class ReactAppPlugin : Plugin { configureDevPorts(androidConfiguration) val isAndroidLibrary = plugins.hasPlugin("com.android.library") - val variants = if (isAndroidLibrary) { - extensions.getByType().libraryVariants - } else { - extensions.getByType().applicationVariants - } - variants.all { - configureReactTasks( - variant = this, - config = config - ) - } + val variants = + if (isAndroidLibrary) { + extensions.getByType().libraryVariants + } else { + extensions.getByType().applicationVariants + } + variants.all { configureReactTasks(variant = this, config = config) } } } } diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt index 82f8eb084a7..e518d780497 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt @@ -13,11 +13,11 @@ import com.android.build.gradle.api.LibraryVariant import com.android.build.gradle.internal.tasks.factory.dependsOn import com.facebook.react.tasks.BundleJsAndAssetsTask import com.facebook.react.tasks.HermesBinaryTask +import java.io.File import org.gradle.api.Project import org.gradle.api.tasks.Copy import org.gradle.kotlin.dsl.extra import org.gradle.kotlin.dsl.register -import java.io.File private const val REACT_GROUP = "react" @@ -34,8 +34,10 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactAppE val jsBundleFile = File(jsBundleDir, config.bundleAssetName) val jsSourceMapsDir = File(buildDir, "generated/sourcemaps/react/$targetPath") val jsIntermediateSourceMapsDir = File(buildDir, "intermediates/sourcemaps/react/$targetPath") - val jsPackagerSourceMapFile = File(jsIntermediateSourceMapsDir, "${config.bundleAssetName}.packager.map") - val jsCompilerSourceMapFile = File(jsIntermediateSourceMapsDir, "${config.bundleAssetName}.compiler.map") + val jsPackagerSourceMapFile = + File(jsIntermediateSourceMapsDir, "${config.bundleAssetName}.packager.map") + val jsCompilerSourceMapFile = + File(jsIntermediateSourceMapsDir, "${config.bundleAssetName}.compiler.map") val jsOutputSourceMapFile = File(jsSourceMapsDir, "${config.bundleAssetName}.map") // Additional node and packager commandline arguments @@ -46,88 +48,90 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactAppE val enableHermes = config.enableHermesForVariant(variant) val bundleEnabled = variant.checkBundleEnabled(config) - val bundleTask = tasks.register("createBundle${targetName}JsAndAssets") { - val task = this - task.group = REACT_GROUP - task.description = "create JS bundle and assets for $targetName." + val bundleTask = + tasks.register("createBundle${targetName}JsAndAssets") { + val task = this + task.group = REACT_GROUP + task.description = "create JS bundle and assets for $targetName." - task.reactRoot = config.reactRoot - task.sources = fileTree(config.reactRoot) { - setExcludes(config.inputExcludes) - } - task.execCommand = execCommand - task.bundleCommand = config.bundleCommand - task.devEnabled = !(variant.name in config.devDisabledInVariants || isRelease) - task.entryFile = config.detectedEntryFile + task.reactRoot = config.reactRoot + task.sources = fileTree(config.reactRoot) { setExcludes(config.inputExcludes) } + task.execCommand = execCommand + task.bundleCommand = config.bundleCommand + task.devEnabled = !(variant.name in config.devDisabledInVariants || isRelease) + task.entryFile = config.detectedEntryFile - val extraArgs = mutableListOf() + val extraArgs = mutableListOf() - if (config.bundleConfig != null) { - extraArgs.add("--config") - extraArgs.add(config.bundleConfig.orEmpty()) - } + if (config.bundleConfig != null) { + extraArgs.add("--config") + extraArgs.add(config.bundleConfig.orEmpty()) + } - // Hermes doesn't require JS minification. - if (enableHermes && !devEnabled) { - extraArgs.add("--minify") - extraArgs.add("false") - } + // Hermes doesn't require JS minification. + if (enableHermes && !devEnabled) { + extraArgs.add("--minify") + extraArgs.add("false") + } - extraArgs.addAll(config.extraPackagerArgs) + extraArgs.addAll(config.extraPackagerArgs) - task.extraArgs = emptyList() + task.extraArgs = emptyList() - task.jsBundleDir = jsBundleDir - task.jsBundleFile = jsBundleFile - task.resourcesDir = resourcesDir - task.jsIntermediateSourceMapsDir = jsIntermediateSourceMapsDir - task.jsSourceMapsDir = jsSourceMapsDir - task.jsSourceMapsFile = if (enableHermes) jsPackagerSourceMapFile else jsOutputSourceMapFile + task.jsBundleDir = jsBundleDir + task.jsBundleFile = jsBundleFile + task.resourcesDir = resourcesDir + task.jsIntermediateSourceMapsDir = jsIntermediateSourceMapsDir + task.jsSourceMapsDir = jsSourceMapsDir + task.jsSourceMapsFile = if (enableHermes) jsPackagerSourceMapFile else jsOutputSourceMapFile - task.enabled = bundleEnabled - } + task.enabled = bundleEnabled + } - val hermesTask = tasks.register("emit${targetName}HermesResources") { - val task = this - task.group = REACT_GROUP - task.description = "bundle hermes resources for $targetName" + val hermesTask = + tasks.register("emit${targetName}HermesResources") { + val task = this + task.group = REACT_GROUP + task.description = "bundle hermes resources for $targetName" - task.reactRoot = config.reactRoot - task.hermesCommand = config.osAwareHermesCommand - task.hermesFlags = if (isRelease) config.hermesFlagsRelease else config.hermesFlagsDebug - task.jsBundleFile = jsBundleFile - task.composeSourceMapsCommand = nodeExecutableAndArgs + config.composeSourceMapsPath - task.jsPackagerSourceMapFile = jsPackagerSourceMapFile - task.jsCompilerSourceMapFile = jsCompilerSourceMapFile - task.jsOutputSourceMapFile = jsOutputSourceMapFile + task.reactRoot = config.reactRoot + task.hermesCommand = config.osAwareHermesCommand + task.hermesFlags = if (isRelease) config.hermesFlagsRelease else config.hermesFlagsDebug + task.jsBundleFile = jsBundleFile + task.composeSourceMapsCommand = nodeExecutableAndArgs + config.composeSourceMapsPath + task.jsPackagerSourceMapFile = jsPackagerSourceMapFile + task.jsCompilerSourceMapFile = jsCompilerSourceMapFile + task.jsOutputSourceMapFile = jsOutputSourceMapFile - task.dependsOn(bundleTask) + task.dependsOn(bundleTask) - task.enabled = bundleEnabled && enableHermes - } + task.enabled = bundleEnabled && enableHermes + } - val aggregatedBundleTask = tasks.register("bundle${targetName}JsAndAssets") { - val task = this - task.group = REACT_GROUP - task.description = "bundle JS and resources for $targetName" + val aggregatedBundleTask = + tasks.register("bundle${targetName}JsAndAssets") { + val task = this + task.group = REACT_GROUP + task.description = "bundle JS and resources for $targetName" - task.dependsOn(bundleTask, hermesTask) + task.dependsOn(bundleTask, hermesTask) - // this was exposed before, do we still need it? - task.extra["generatedResFolders"] = files(resourcesDir).builtBy(task) - task.extra["generatedAssetsFolders"] = files(jsBundleDir).builtBy(task) - } + // this was exposed before, do we still need it? + task.extra["generatedResFolders"] = files(resourcesDir).builtBy(task) + task.extra["generatedAssetsFolders"] = files(jsBundleDir).builtBy(task) + } val generatedResFolders = files(resourcesDir).builtBy(aggregatedBundleTask) // Android configuration variant.registerGeneratedResFolders(generatedResFolders) - val packageTask = when (variant) { - is ApplicationVariant -> variant.packageApplicationProvider - is LibraryVariant -> variant.packageLibraryProvider - else -> tasks.named("package$targetName") - } + val packageTask = + when (variant) { + is ApplicationVariant -> variant.packageApplicationProvider + is LibraryVariant -> variant.packageLibraryProvider + else -> tasks.named("package$targetName") + } val mergeResourcesTask = variant.mergeResourcesProvider val mergeAssetsTask = variant.mergeAssetsProvider @@ -135,17 +139,18 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactAppE val resourcesDirConfigValue = config.resourcesDir[variant.name] if (resourcesDirConfigValue != null) { - val currentCopyResTask = tasks.register("copy${targetName}BundledResources") { - group = "react" - description = "copy bundled resources into custom location for $targetName." + val currentCopyResTask = + tasks.register("copy${targetName}BundledResources") { + group = "react" + description = "copy bundled resources into custom location for $targetName." - from(resourcesDir) - into(file(resourcesDirConfigValue)) + from(resourcesDir) + into(file(resourcesDirConfigValue)) - dependsOn(bundleTask) + dependsOn(bundleTask) - enabled = bundleEnabled - } + enabled = bundleEnabled + } packageTask.dependsOn(currentCopyResTask) preBundleTask.dependsOn(currentCopyResTask) @@ -153,31 +158,31 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactAppE packageTask.configure { if (config.enableVmCleanup) { - doFirst { - cleanupVMFiles(enableHermes, isRelease, targetPath) + doFirst { cleanupVMFiles(enableHermes, isRelease, targetPath) } + } + } + + val currentAssetsCopyTask = + tasks.register("copy${targetName}BundledJs") { + group = "react" + description = "copy bundled JS into $targetName." + + from(jsBundleDir) + + val jsBundleDirConfigValue = config.jsBundleDir[targetName] + if (jsBundleDirConfigValue != null) { + into(jsBundleDirConfigValue) + } else { + into(mergeAssetsTask.map { it.outputDir.get() }) + } + + dependsOn(mergeAssetsTask) + + enabled = bundleEnabled } - } - } - val currentAssetsCopyTask = tasks.register("copy${targetName}BundledJs") { - group = "react" - description = "copy bundled JS into $targetName." - - from(jsBundleDir) - - val jsBundleDirConfigValue = config.jsBundleDir[targetName] - if (jsBundleDirConfigValue != null) { - into(jsBundleDirConfigValue) - } else { - into(mergeAssetsTask.map { it.outputDir.get() }) - } - - dependsOn(mergeAssetsTask) - - enabled = bundleEnabled - } - - // mergeResources task runs before the bundle file is copied to the intermediate asset directory from Android plugin 4.1+. + // mergeResources task runs before the bundle file is copied to the intermediate asset directory + // from Android plugin 4.1+. // This ensures to copy the bundle file before mergeResources task starts mergeResourcesTask.dependsOn(currentAssetsCopyTask) packageTask.dependsOn(currentAssetsCopyTask) @@ -210,13 +215,14 @@ private fun Project.cleanupVMFiles(enableHermes: Boolean, isRelease: Boolean, ta // For JSC, delete all the libhermes* files include("**/libhermes*.so") } - }.visit { - val targetVariant = ".*/transforms/[^/]*/$targetPath/.*".toRegex() - val path = file.absolutePath.replace(File.separatorChar, '/') - if (path.matches(targetVariant) && file.isFile()) { - file.delete() - } } + .visit { + val targetVariant = ".*/transforms/[^/]*/$targetPath/.*".toRegex() + val path = file.absolutePath.replace(File.separatorChar, '/') + if (path.matches(targetVariant) && file.isFile()) { + file.delete() + } + } } private fun BaseVariant.checkBundleEnabled(config: ReactAppExtension): Boolean { diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/BundleJsAndAssetsTask.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/BundleJsAndAssetsTask.kt index 30e8e6fb908..086f2515f98 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/BundleJsAndAssetsTask.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/BundleJsAndAssetsTask.kt @@ -7,6 +7,7 @@ package com.facebook.react.tasks +import java.io.File import org.gradle.api.DefaultTask import org.gradle.api.file.FileTree import org.gradle.api.tasks.Input @@ -14,7 +15,6 @@ import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.TaskAction -import java.io.File open class BundleJsAndAssetsTask : DefaultTask() { internal lateinit var reactRoot: File @@ -22,29 +22,18 @@ open class BundleJsAndAssetsTask : DefaultTask() { @get:InputFiles @Suppress("UNUSED") // used to invalidate caches internal lateinit var sources: FileTree - @get:Input - internal lateinit var execCommand: List - @get:Input - internal lateinit var bundleCommand: String - @get:Input - internal var devEnabled: Boolean = true - @get:Input - internal lateinit var entryFile: File - @get:Input - internal var extraArgs: List = emptyList() + @get:Input internal lateinit var execCommand: List + @get:Input internal lateinit var bundleCommand: String + @get:Input internal var devEnabled: Boolean = true + @get:Input internal lateinit var entryFile: File + @get:Input internal var extraArgs: List = emptyList() - @get:OutputDirectory - internal lateinit var jsBundleDir: File - @get:OutputFile - internal lateinit var jsBundleFile: File - @get:OutputDirectory - internal lateinit var resourcesDir: File - @get:OutputDirectory - internal lateinit var jsIntermediateSourceMapsDir: File - @get:OutputDirectory - internal lateinit var jsSourceMapsDir: File - @get:OutputFile - internal lateinit var jsSourceMapsFile: File + @get:OutputDirectory internal lateinit var jsBundleDir: File + @get:OutputFile internal lateinit var jsBundleFile: File + @get:OutputDirectory internal lateinit var resourcesDir: File + @get:OutputDirectory internal lateinit var jsIntermediateSourceMapsDir: File + @get:OutputDirectory internal lateinit var jsSourceMapsDir: File + @get:OutputFile internal lateinit var jsSourceMapsFile: File @TaskAction fun run() { @@ -65,17 +54,22 @@ open class BundleJsAndAssetsTask : DefaultTask() { @Suppress("SpreadOperator") windowsAwareCommandLine( - *execCommand.toTypedArray(), - bundleCommand, - "--platform", "android", - "--dev", devEnabled, - "--reset-cache", - "--entry-file", entryFile, - "--bundle-output", jsBundleFile, - "--assets-dest", resourcesDir, - "--sourcemap-output", jsSourceMapsFile, - *extraArgs.toTypedArray() - ) + *execCommand.toTypedArray(), + bundleCommand, + "--platform", + "android", + "--dev", + devEnabled, + "--reset-cache", + "--entry-file", + entryFile, + "--bundle-output", + jsBundleFile, + "--assets-dest", + resourcesDir, + "--sourcemap-output", + jsSourceMapsFile, + *extraArgs.toTypedArray()) } } diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/HermesBinaryTask.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/HermesBinaryTask.kt index 8f0db469e95..0e641dea7ad 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/HermesBinaryTask.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/HermesBinaryTask.kt @@ -7,32 +7,25 @@ package com.facebook.react.tasks +import java.io.File import org.gradle.api.DefaultTask import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.TaskAction -import java.io.File open class HermesBinaryTask : DefaultTask() { internal lateinit var reactRoot: File - @get:Input - internal lateinit var hermesCommand: String - @get:Input - internal var hermesFlags: List = emptyList() - @get:InputFile - internal lateinit var jsBundleFile: File + @get:Input internal lateinit var hermesCommand: String + @get:Input internal var hermesFlags: List = emptyList() + @get:InputFile internal lateinit var jsBundleFile: File - @get:Input - internal lateinit var composeSourceMapsCommand: List - @get:Input - internal lateinit var jsPackagerSourceMapFile: File + @get:Input internal lateinit var composeSourceMapsCommand: List + @get:Input internal lateinit var jsPackagerSourceMapFile: File - @get:OutputFile - internal lateinit var jsCompilerSourceMapFile: File - @get:OutputFile - internal lateinit var jsOutputSourceMapFile: File + @get:OutputFile internal lateinit var jsCompilerSourceMapFile: File + @get:OutputFile internal lateinit var jsOutputSourceMapFile: File @TaskAction fun run() { @@ -51,12 +44,12 @@ open class HermesBinaryTask : DefaultTask() { project.exec { @Suppress("SpreadOperator") windowsAwareCommandLine( - hermesCommand, - "-emit-binary", - "-out", outputFile, - jsBundleFile, - *hermesFlags.toTypedArray() - ) + hermesCommand, + "-emit-binary", + "-out", + outputFile, + jsBundleFile, + *hermesFlags.toTypedArray()) } } @@ -66,11 +59,11 @@ open class HermesBinaryTask : DefaultTask() { @Suppress("SpreadOperator") windowsAwareCommandLine( - *composeSourceMapsCommand.toTypedArray(), - jsPackagerSourceMapFile, - jsCompilerSourceMapFile, - "-o", jsOutputSourceMapFile - ) + *composeSourceMapsCommand.toTypedArray(), + jsPackagerSourceMapFile, + jsCompilerSourceMapFile, + "-o", + jsOutputSourceMapFile) } }