diff --git a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/BundleHermesCTask.kt b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/BundleHermesCTask.kt index 444352dff4d5..f1dcdb0a261b 100644 --- a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/BundleHermesCTask.kt +++ b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/BundleHermesCTask.kt @@ -21,6 +21,9 @@ import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property import org.gradle.api.tasks.* import org.gradle.process.ExecOperations +import org.gradle.workers.WorkAction +import org.gradle.workers.WorkParameters +import org.gradle.workers.WorkerExecutor abstract class BundleHermesCTask : DefaultTask() { @@ -28,7 +31,7 @@ abstract class BundleHermesCTask : DefaultTask() { group = "react" } - @get:Inject abstract val execOperations: ExecOperations + @get:Inject abstract val workerExecutor: WorkerExecutor @get:Internal abstract val root: DirectoryProperty @@ -92,33 +95,33 @@ abstract class BundleHermesCTask : DefaultTask() { val bundleFile = File(jsBundleDir.get().asFile, bundleAssetFilename) val packagerSourceMap = resolvePackagerSourceMapFile(bundleAssetFilename) - val bundleCommand = getBundleCommand(bundleFile, packagerSourceMap) - runCommand(bundleCommand) - - if (hermesEnabled.get()) { - val detectedHermesCommand = detectOSAwareHermesCommand(root.get().asFile, hermesCommand.get()) - val bytecodeFile = File("${bundleFile}.hbc") - val outputSourceMap = resolveOutputSourceMap(bundleAssetFilename) - val compilerSourceMap = resolveCompilerSourceMap(bundleAssetFilename) - - val hermesCommand = getHermescCommand(detectedHermesCommand, bytecodeFile, bundleFile) - runCommand(hermesCommand) - bytecodeFile.moveTo(bundleFile) - - if (hermesFlags.get().contains("-output-source-map")) { - val hermesTempSourceMapFile = File("$bytecodeFile.map") - hermesTempSourceMapFile.moveTo(compilerSourceMap) - - val reactNativeDir = reactNativeDir.get().asFile - val composeScriptFile = File(reactNativeDir, "scripts/compose-source-maps.js") - val composeSourceMapsCommand = - getComposeSourceMapsCommand( - composeScriptFile, - packagerSourceMap, - compilerSourceMap, - outputSourceMap, - ) - runCommand(composeSourceMapsCommand) + // Release the project lock while the external commands run. + workerExecutor.noIsolation().submit(BundleHermesWorkAction::class.java) { work -> + work.root.set(root) + work.bundleCommand.set(getBundleCommand(bundleFile, packagerSourceMap)) + work.bundleFile.set(bundleFile) + + if (hermesEnabled.get()) { + val bytecodeFile = File("${bundleFile}.hbc") + work.hermesCommand.set(hermesCommand) + work.hermesFlags.set(hermesFlags) + work.bytecodeFile.set(bytecodeFile) + + if (hermesFlags.get().contains("-output-source-map")) { + val compilerSourceMap = resolveCompilerSourceMap(bundleAssetFilename) + val composeScriptFile = + File(reactNativeDir.get().asFile, "scripts/compose-source-maps.js") + work.hermesTempSourceMap.set(File("$bytecodeFile.map")) + work.compilerSourceMap.set(compilerSourceMap) + work.composeCommand.set( + getComposeSourceMapsCommand( + composeScriptFile, + packagerSourceMap, + compilerSourceMap, + resolveOutputSourceMap(bundleAssetFilename), + ) + ) + } } } } @@ -136,13 +139,6 @@ abstract class BundleHermesCTask : DefaultTask() { internal fun resolveCompilerSourceMap(bundleAssetName: String) = File(jsIntermediateSourceMapsDir.get().asFile, "$bundleAssetName.compiler.map") - private fun runCommand(command: List) { - execOperations.exec { exec -> - exec.workingDir(root.get().asFile) - exec.commandLine(command) - } - } - internal fun getBundleCommand(bundleFile: File, sourceMapFile: File): List { val rootFile = root.get().asFile val commandLine = @@ -181,19 +177,14 @@ abstract class BundleHermesCTask : DefaultTask() { hermesCommand: String, bytecodeFile: File, bundleFile: File, - ): List { - val rootFile = root.get().asFile - return windowsAwareCommandLine( - hermesCommand, - "-w", - "-emit-binary", - "-max-diagnostic-width=80", - "-out", - bytecodeFile.cliPath(rootFile), - bundleFile.cliPath(rootFile), - *hermesFlags.get().toTypedArray(), - ) - } + ): List = + buildHermescCommand( + root.get().asFile, + hermesCommand, + bytecodeFile, + bundleFile, + hermesFlags.get(), + ) internal fun getComposeSourceMapsCommand( composeScript: File, @@ -212,3 +203,70 @@ abstract class BundleHermesCTask : DefaultTask() { ) } } + +private fun buildHermescCommand( + root: File, + hermesCommand: String, + bytecodeFile: File, + bundleFile: File, + hermesFlags: List, +): List = + windowsAwareCommandLine( + hermesCommand, + "-w", + "-emit-binary", + "-max-diagnostic-width=80", + "-out", + bytecodeFile.cliPath(root), + bundleFile.cliPath(root), + *hermesFlags.toTypedArray(), + ) + +internal abstract class BundleHermesWorkAction : WorkAction { + interface Parameters : WorkParameters { + val root: DirectoryProperty + val bundleCommand: ListProperty + val bundleFile: RegularFileProperty + val hermesCommand: Property + val hermesFlags: ListProperty + val bytecodeFile: RegularFileProperty + val composeCommand: ListProperty + val hermesTempSourceMap: RegularFileProperty + val compilerSourceMap: RegularFileProperty + } + + @get:Inject abstract val execOperations: ExecOperations + + override fun execute() { + runCommand(parameters.bundleCommand.get()) + val configuredHermesCommand = parameters.hermesCommand.orNull ?: return + val root = parameters.root.get().asFile + val bytecodeFile = parameters.bytecodeFile.get().asFile + val bundleFile = parameters.bundleFile.get().asFile + + val hermesCommand = detectOSAwareHermesCommand(root, configuredHermesCommand) + runCommand( + buildHermescCommand( + root, + hermesCommand, + bytecodeFile, + bundleFile, + parameters.hermesFlags.get(), + ) + ) + bytecodeFile.moveTo(bundleFile) + + val composeCommand = parameters.composeCommand.getOrElse(emptyList()) + if (composeCommand.isNotEmpty()) { + parameters.hermesTempSourceMap.get().asFile.moveTo(parameters.compilerSourceMap.get().asFile) + runCommand(composeCommand) + } + } + + private fun runCommand(command: List) { + execOperations.exec { exec -> + exec.workingDir(parameters.root.get().asFile) + exec.commandLine(command) + } + } +}