|
| 1 | +package com.github.gradle.node.bun.exec |
| 2 | + |
| 3 | +import com.github.gradle.node.NodeExtension |
| 4 | +import com.github.gradle.node.exec.ExecConfiguration |
| 5 | +import com.github.gradle.node.exec.ExecRunner |
| 6 | +import com.github.gradle.node.exec.NodeExecConfiguration |
| 7 | +import com.github.gradle.node.npm.exec.NpmExecConfiguration |
| 8 | +import com.github.gradle.node.npm.proxy.NpmProxy |
| 9 | +import com.github.gradle.node.util.ProjectApiHelper |
| 10 | +import com.github.gradle.node.util.zip |
| 11 | +import com.github.gradle.node.variant.VariantComputer |
| 12 | +import com.github.gradle.node.variant.computeNodeExec |
| 13 | +import org.gradle.api.provider.Provider |
| 14 | +import org.gradle.api.provider.ProviderFactory |
| 15 | +import org.gradle.process.ExecResult |
| 16 | +import javax.inject.Inject |
| 17 | + |
| 18 | +abstract class BunExecRunner { |
| 19 | + @get:Inject |
| 20 | + abstract val providers: ProviderFactory |
| 21 | + |
| 22 | + fun executeBunCommand(project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration, variants: VariantComputer): ExecResult { |
| 23 | + val bunExecConfiguration = NpmExecConfiguration("bun" |
| 24 | + ) { variantComputer, nodeExtension, binDir -> variantComputer.computeBunExec(nodeExtension, binDir) } |
| 25 | + |
| 26 | + val enhancedNodeExecConfiguration = NpmProxy.addProxyEnvironmentVariables(extension.nodeProxySettings.get(), nodeExecConfiguration) |
| 27 | + val execConfiguration = computeExecConfiguration(extension, bunExecConfiguration, enhancedNodeExecConfiguration, variants).get() |
| 28 | + return ExecRunner().execute(project, extension, execConfiguration) |
| 29 | + } |
| 30 | + |
| 31 | + fun executeBunxCommand(project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration, variants: VariantComputer): ExecResult { |
| 32 | + val bunExecConfiguration = NpmExecConfiguration("bunx") { variantComputer, nodeExtension, bunBinDir -> |
| 33 | + variantComputer.computeBunxExec(nodeExtension, bunBinDir) |
| 34 | + } |
| 35 | + |
| 36 | + val enhancedNodeExecConfiguration = NpmProxy.addProxyEnvironmentVariables(extension.nodeProxySettings.get(), nodeExecConfiguration) |
| 37 | + val execConfiguration = computeExecConfiguration(extension, bunExecConfiguration, enhancedNodeExecConfiguration, variants).get() |
| 38 | + return ExecRunner().execute(project, extension, execConfiguration) |
| 39 | + } |
| 40 | + |
| 41 | + private fun computeExecConfiguration(extension: NodeExtension, bunExecConfiguration: NpmExecConfiguration, |
| 42 | + nodeExecConfiguration: NodeExecConfiguration, |
| 43 | + variantComputer: VariantComputer): Provider<ExecConfiguration> { |
| 44 | + val additionalBinPathProvider = computeAdditionalBinPath(extension, variantComputer) |
| 45 | + val executableAndScriptProvider = computeExecutable(extension, bunExecConfiguration, variantComputer) |
| 46 | + return zip(additionalBinPathProvider, executableAndScriptProvider) |
| 47 | + .map { (additionalBinPath, executableAndScript) -> |
| 48 | + val argsPrefix = |
| 49 | + if (executableAndScript.script != null) listOf(executableAndScript.script) else listOf() |
| 50 | + val args = argsPrefix.plus(nodeExecConfiguration.command) |
| 51 | + ExecConfiguration(executableAndScript.executable, args, additionalBinPath, |
| 52 | + nodeExecConfiguration.environment, nodeExecConfiguration.workingDir, |
| 53 | + nodeExecConfiguration.ignoreExitValue, nodeExecConfiguration.execOverrides) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private fun computeExecutable( |
| 58 | + nodeExtension: NodeExtension, |
| 59 | + bunExecConfiguration: NpmExecConfiguration, |
| 60 | + variantComputer: VariantComputer |
| 61 | + ): |
| 62 | + Provider<ExecutableAndScript> { |
| 63 | + val nodeDirProvider = nodeExtension.resolvedNodeDir |
| 64 | + val bunDirProvider = variantComputer.computeBunDir(nodeExtension) |
| 65 | + val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider, nodeExtension.resolvedPlatform) |
| 66 | + val bunBinDirProvider = variantComputer.computeBunBinDir(bunDirProvider, nodeExtension.resolvedPlatform) |
| 67 | + val nodeExecProvider = computeNodeExec(nodeExtension, nodeBinDirProvider) |
| 68 | + val executableProvider = |
| 69 | + bunExecConfiguration.commandExecComputer(variantComputer, nodeExtension, bunBinDirProvider) |
| 70 | + |
| 71 | + return zip(nodeExtension.download, nodeExtension.nodeProjectDir, executableProvider, nodeExecProvider).map { |
| 72 | + val (download, nodeProjectDir, executable, nodeExec) = it |
| 73 | + if (download) { |
| 74 | + val localCommandScript = nodeProjectDir.dir("node_modules/bun/bin") |
| 75 | + .file("${bunExecConfiguration.command}.js").asFile |
| 76 | + if (localCommandScript.exists()) { |
| 77 | + return@map ExecutableAndScript(nodeExec, localCommandScript.absolutePath) |
| 78 | + } |
| 79 | + } |
| 80 | + return@map ExecutableAndScript(executable) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private data class ExecutableAndScript( |
| 85 | + val executable: String, |
| 86 | + val script: String? = null |
| 87 | + ) |
| 88 | + |
| 89 | + private fun computeAdditionalBinPath(nodeExtension: NodeExtension, variantComputer: VariantComputer): Provider<List<String>> { |
| 90 | + return nodeExtension.download.flatMap { download -> |
| 91 | + if (!download) { |
| 92 | + providers.provider { listOf<String>() } |
| 93 | + } |
| 94 | + val bunDirProvider = variantComputer.computeBunDir(nodeExtension) |
| 95 | + val bunBinDirProvider = variantComputer.computeBunBinDir(bunDirProvider, nodeExtension.resolvedPlatform) |
| 96 | + bunBinDirProvider.map { file -> listOf(file.asFile.absolutePath) } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments