-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathNpmExecRunner.kt
More file actions
158 lines (148 loc) · 6.86 KB
/
NpmExecRunner.kt
File metadata and controls
158 lines (148 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.github.gradle.node.npm.exec
import com.github.gradle.node.NodeExtension
import com.github.gradle.node.exec.ExecConfiguration
import com.github.gradle.node.exec.ExecRunner
import com.github.gradle.node.exec.NodeExecConfiguration
import com.github.gradle.node.npm.proxy.NpmProxy
import com.github.gradle.node.util.ProjectApiHelper
import com.github.gradle.node.util.zip
import com.github.gradle.node.variant.VariantComputer
import com.github.gradle.node.variant.computeNodeExec
import com.github.gradle.node.variant.computeNpmScriptFile
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.process.ExecResult
import java.io.File
import javax.inject.Inject
abstract class NpmExecRunner {
@get:Inject
abstract val providers: ProviderFactory
fun executeNpmCommand(
project: ProjectApiHelper,
extension: NodeExtension,
nodeExecConfiguration: NodeExecConfiguration,
variants: VariantComputer,
): ExecResult {
val npmExecConfiguration = NpmExecConfiguration(
command = "npm",
commandExecComputer = { variantComputer, nodeExtension, npmBinDir ->
variantComputer.computeNpmExec(
nodeExtension,
npmBinDir
)
}
)
return executeCommand(
project = project,
extension = extension,
nodeExecConfiguration = NpmProxy.addProxyEnvironmentVariables(
proxySettings = extension.nodeProxySettings.get(),
nodeExecConfiguration = nodeExecConfiguration
),
npmExecConfiguration = npmExecConfiguration,
variantComputer = variants
)
}
fun executeNpxCommand(
project: ProjectApiHelper,
extension: NodeExtension,
nodeExecConfiguration: NodeExecConfiguration,
variants: VariantComputer
): ExecResult {
val npxExecConfiguration = NpmExecConfiguration("npx") { variantComputer, nodeExtension, npmBinDir ->
variantComputer.computeNpxExec(nodeExtension, npmBinDir)
}
return executeCommand(project, extension, nodeExecConfiguration, npxExecConfiguration, variants)
}
private fun executeCommand(
project: ProjectApiHelper,
extension: NodeExtension,
nodeExecConfiguration: NodeExecConfiguration,
npmExecConfiguration: NpmExecConfiguration,
variantComputer: VariantComputer
): ExecResult {
val execConfiguration =
computeExecConfiguration(extension, npmExecConfiguration, nodeExecConfiguration, variantComputer).get()
val execRunner = ExecRunner()
return execRunner.execute(project, extension, execConfiguration)
}
private fun computeExecConfiguration(
extension: NodeExtension,
npmExecConfiguration: NpmExecConfiguration,
nodeExecConfiguration: NodeExecConfiguration,
variantComputer: VariantComputer
): Provider<ExecConfiguration> {
val additionalBinPathProvider = computeAdditionalBinPath(extension, variantComputer)
val executableAndScriptProvider = computeExecutable(extension, npmExecConfiguration, variantComputer)
return zip(additionalBinPathProvider, executableAndScriptProvider)
.map { (additionalBinPath, executableAndScript) ->
val argsPrefix =
if (executableAndScript.script != null) listOf(executableAndScript.script) else listOf()
val args = argsPrefix.plus(nodeExecConfiguration.command)
ExecConfiguration(
executable = executableAndScript.executable,
args = args,
additionalBinPaths = additionalBinPath,
environment = nodeExecConfiguration.environment,
workingDir = nodeExecConfiguration.workingDir,
ignoreExitValue = nodeExecConfiguration.ignoreExitValue,
execOverrides = nodeExecConfiguration.execOverrides,
)
}
}
private fun computeExecutable(
nodeExtension: NodeExtension,
npmExecConfiguration: NpmExecConfiguration,
variantComputer: VariantComputer
): Provider<ExecutableAndScript> {
val nodeDirProvider = nodeExtension.resolvedNodeDir
val npmDirProvider = variantComputer.computeNpmDir(nodeExtension, nodeDirProvider)
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider, nodeExtension.resolvedPlatform)
val npmBinDirProvider = variantComputer.computeNpmBinDir(npmDirProvider, nodeExtension.resolvedPlatform)
val nodeExecProvider = computeNodeExec(nodeExtension, nodeBinDirProvider)
val executableProvider =
npmExecConfiguration.commandExecComputer(variantComputer, nodeExtension, npmBinDirProvider)
val isWindows = nodeExtension.resolvedPlatform.get().isWindows()
val npmScriptFileProvider =
computeNpmScriptFile(nodeDirProvider, npmExecConfiguration.command, isWindows)
return zip(
nodeExtension.download,
nodeExtension.nodeProjectDir,
executableProvider,
nodeExecProvider,
npmScriptFileProvider,
).map { (download, nodeProjectDir, executable, nodeExec, npmScriptFile) ->
if (download) {
val localCommandScript = nodeProjectDir.dir("node_modules/npm/bin")
.file("${npmExecConfiguration.command}-cli.js").asFile
if (localCommandScript.exists()) {
return@map ExecutableAndScript(nodeExec, localCommandScript.absolutePath)
} else if (!File(executable).exists()) {
return@map ExecutableAndScript(nodeExec, npmScriptFile)
}
}
return@map ExecutableAndScript(executable)
}
}
private data class ExecutableAndScript(
val executable: String,
val script: String? = null,
)
private fun computeAdditionalBinPath(
nodeExtension: NodeExtension,
variantComputer: VariantComputer
): Provider<List<String>> {
return nodeExtension.download.flatMap { download ->
if (!download) {
providers.provider { listOf<String>() }
}
val nodeDirProvider = nodeExtension.resolvedNodeDir
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider, nodeExtension.resolvedPlatform)
val npmDirProvider = variantComputer.computeNpmDir(nodeExtension, nodeDirProvider)
val npmBinDirProvider = variantComputer.computeNpmBinDir(npmDirProvider, nodeExtension.resolvedPlatform)
zip(npmBinDirProvider, nodeBinDirProvider).map { (npmBinDir, nodeBinDir) ->
listOf(npmBinDir, nodeBinDir).map { file -> file.asFile.absolutePath }
}
}
}
}