Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ 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() {

init {
group = "react"
}

@get:Inject abstract val execOperations: ExecOperations
@get:Inject abstract val workerExecutor: WorkerExecutor

@get:Internal abstract val root: DirectoryProperty

Expand Down Expand Up @@ -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),
)
)
}
}
}
}
Expand All @@ -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<Any>) {
execOperations.exec { exec ->
exec.workingDir(root.get().asFile)
exec.commandLine(command)
}
}

internal fun getBundleCommand(bundleFile: File, sourceMapFile: File): List<Any> {
val rootFile = root.get().asFile
val commandLine =
Expand Down Expand Up @@ -181,19 +177,14 @@ abstract class BundleHermesCTask : DefaultTask() {
hermesCommand: String,
bytecodeFile: File,
bundleFile: File,
): List<Any> {
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<Any> =
buildHermescCommand(
root.get().asFile,
hermesCommand,
bytecodeFile,
bundleFile,
hermesFlags.get(),
)

internal fun getComposeSourceMapsCommand(
composeScript: File,
Expand All @@ -212,3 +203,70 @@ abstract class BundleHermesCTask : DefaultTask() {
)
}
}

private fun buildHermescCommand(
root: File,
hermesCommand: String,
bytecodeFile: File,
bundleFile: File,
hermesFlags: List<String>,
): List<Any> =
windowsAwareCommandLine(
hermesCommand,
"-w",
"-emit-binary",
"-max-diagnostic-width=80",
"-out",
bytecodeFile.cliPath(root),
bundleFile.cliPath(root),
*hermesFlags.toTypedArray(),
)

internal abstract class BundleHermesWorkAction : WorkAction<BundleHermesWorkAction.Parameters> {
interface Parameters : WorkParameters {
val root: DirectoryProperty
val bundleCommand: ListProperty<Any>
val bundleFile: RegularFileProperty
val hermesCommand: Property<String>
val hermesFlags: ListProperty<String>
val bytecodeFile: RegularFileProperty
val composeCommand: ListProperty<Any>
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<Any>) {
execOperations.exec { exec ->
exec.workingDir(parameters.root.get().asFile)
exec.commandLine(command)
}
}
}