Skip to content

Commit 0fcfa30

Browse files
committed
wip fixes...
1 parent f98dcfa commit 0fcfa30

File tree

9 files changed

+163
-199
lines changed

9 files changed

+163
-199
lines changed

src/main/kotlin/com/github/gradle/node/npm/exec/NpmExecRunner.kt

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,34 +110,19 @@ abstract class NpmExecRunner {
110110
val nodeExecProvider = computeNodeExec(nodeExtension, nodeBinDirProvider)
111111
val executableProvider =
112112
npmExecConfiguration.commandExecComputer(variantComputer, nodeExtension, npmBinDirProvider)
113+
val isWindows = nodeExtension.resolvedPlatform.get().isWindows()
113114
val npmScriptFileProvider =
114-
computeNpmScriptFile(nodeDirProvider, npmExecConfiguration.command, nodeExtension.resolvedPlatform)
115-
return computeExecutable(
116-
npmExecConfiguration.command,
117-
nodeExtension,
118-
executableProvider,
119-
nodeExecProvider,
120-
npmScriptFileProvider
121-
)
122-
}
123-
124-
private fun computeExecutable(
125-
command: String,
126-
nodeExtension: NodeExtension,
127-
executableProvider: Provider<String>,
128-
nodeExecProvider: Provider<String>,
129-
npmScriptFileProvider: Provider<String>,
130-
): Provider<ExecutableAndScript> {
115+
computeNpmScriptFile(nodeDirProvider, npmExecConfiguration.command, isWindows)
131116
return zip(
132117
nodeExtension.download,
133118
nodeExtension.nodeProjectDir,
134119
executableProvider,
135120
nodeExecProvider,
136-
npmScriptFileProvider
121+
npmScriptFileProvider,
137122
).map { (download, nodeProjectDir, executable, nodeExec, npmScriptFile) ->
138123
if (download) {
139124
val localCommandScript = nodeProjectDir.dir("node_modules/npm/bin")
140-
.file("${command}-cli.js").asFile
125+
.file("${npmExecConfiguration.command}-cli.js").asFile
141126
if (localCommandScript.exists()) {
142127
return@map ExecutableAndScript(nodeExec, localCommandScript.absolutePath)
143128
} else if (!File(executable).exists()) {

src/main/kotlin/com/github/gradle/node/npm/exec/NpmExecSource.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ abstract class NpmExecSource @Inject internal constructor(
3434
abstract class Parameters internal constructor() : NpmExecSpec(), ValueSourceParameters {
3535
abstract val ignoreExitValue: Property<Boolean>
3636
abstract val workingDir: DirectoryProperty
37+
abstract val coreNpmCommand: Property<String>
3738
abstract val npmCommand: ListProperty<String>
3839
}
3940

4041

41-
private val npmCommand get() = parameters.npmCommand.get()
42+
private val coreNpmCommand get() = parameters.coreNpmCommand.get()
43+
private val execNpmCommand get() = parameters.npmCommand.get()
4244
private val nodeProxySettings get() = parameters.nodeProxySettings.get()
4345
private val npmVersion get() = parameters.npmVersion.get()
4446
private val npmWorkDir get() = parameters.npmWorkDir.get()
@@ -52,7 +54,7 @@ abstract class NpmExecSource @Inject internal constructor(
5254

5355

5456
override fun obtain(): NpmExecResult {
55-
val command = parameters.npmCommand.get().plus(parameters.arguments.get())
57+
val command = execNpmCommand.plus(parameters.arguments.get())
5658
val nodeExecConfiguration =
5759
NodeExecConfiguration(
5860
command = command,
@@ -263,14 +265,14 @@ abstract class NpmExecSource @Inject internal constructor(
263265
computeProductBinDir(npmDirProvider, platform)
264266

265267
/**
266-
* Get the expected node binary name, npm.cmd on Windows and npm everywhere else.
268+
* Get the expected node binary name, `npm.cmd` on Windows and `npm` everywhere else.
267269
*
268-
* Can be overridden by setting npmCommand.
270+
* Can be overridden by setting `npmCommand`.
269271
*/
270272
private fun computeNpmExec(npmBinDirProvider: Directory): String {
271273
return computeExec(
272274
binDirProvider = npmBinDirProvider,
273-
configurationCommand = npmCommand.single(),
275+
configurationCommand = coreNpmCommand,
274276
)
275277
}
276278

src/main/kotlin/com/github/gradle/node/npm/proxy/NpmProxy.kt

Lines changed: 130 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -4,164 +4,155 @@ import com.github.gradle.node.exec.NodeExecConfiguration
44
import java.net.URLEncoder
55
import kotlin.text.Charsets.UTF_8
66

7-
object NpmProxy {
8-
9-
// companion object {
10-
// These are the environment variables that HTTPing applications checks, proxy is on and off.
11-
// FTP skipped in hopes of a better future.
12-
private val proxyVariables = listOf(
13-
"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "PROXY"
14-
)
15-
16-
// And since npm also takes settings in the form of environment variables with the
17-
// NPM_CONFIG_<setting> format, we should check those. Hopefully nobody does this.
18-
// Windows will let you set environment variables with hyphens in them, but shells
19-
// on Linux will fight you. So you'll have to be pretty sneaky to do this.
20-
// I'm adding both here "just in case".
21-
private val npmProxyVariables = listOf(
22-
"NPM_CONFIG_PROXY", "NPM_CONFIG_HTTPS-PROXY", "NPM_CONFIG_HTTPS_PROXY", "NPM_CONFIG_NOPROXY"
23-
)
24-
25-
/**
26-
* Creates a map of environment variables with proxy settings.
27-
*
28-
* Will return an empty map if none are set.
29-
*/
30-
fun computeNpmProxyEnvironmentVariables(): Map<String, String> {
31-
val proxyEnvironmentVariables = computeProxyUrlEnvironmentVariables()
32-
if (proxyEnvironmentVariables.isNotEmpty()) {
33-
addProxyIgnoredHostsEnvironmentVariable(proxyEnvironmentVariables)
34-
}
35-
return proxyEnvironmentVariables.toMap()
36-
}
7+
class NpmProxy {
8+
9+
companion object {
10+
// These are the environment variables that HTTPing applications checks, proxy is on and off.
11+
// FTP skipped in hopes of a better future.
12+
private val proxyVariables = listOf(
13+
"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "PROXY"
14+
)
3715

38-
/**
39-
* Helper function for deciding whether proxy settings need to be set or not.
40-
*/
41-
fun shouldConfigureProxy(env: Map<String, String>, settings: ProxySettings): Boolean {
42-
if (settings == ProxySettings.FORCED) {
43-
return true
44-
} else if (settings == ProxySettings.SMART) {
45-
return !hasProxyConfiguration(env)
16+
// And since npm also takes settings in the form of environment variables with the
17+
// NPM_CONFIG_<setting> format, we should check those. Hopefully nobody does this.
18+
// Windows will let you set environment variables with hyphens in them, but shells
19+
// on Linux will fight you. So you'll have to be pretty sneaky to do this.
20+
// I'm adding both here "just in case".
21+
private val npmProxyVariables = listOf(
22+
"NPM_CONFIG_PROXY", "NPM_CONFIG_HTTPS-PROXY", "NPM_CONFIG_HTTPS_PROXY", "NPM_CONFIG_NOPROXY"
23+
)
24+
25+
/**
26+
* Creates a map of environment variables with proxy settings.
27+
*
28+
* Will return an empty map if none are set.
29+
*/
30+
fun computeNpmProxyEnvironmentVariables(): Map<String, String> {
31+
val proxyEnvironmentVariables = computeProxyUrlEnvironmentVariables()
32+
if (proxyEnvironmentVariables.isNotEmpty()) {
33+
addProxyIgnoredHostsEnvironmentVariable(proxyEnvironmentVariables)
34+
}
35+
return proxyEnvironmentVariables.toMap()
4636
}
4737

48-
return false
49-
}
38+
/**
39+
* Helper function for deciding whether proxy settings need to be set or not.
40+
*/
41+
fun shouldConfigureProxy(env: Map<String, String>, settings: ProxySettings): Boolean {
42+
if (settings == ProxySettings.FORCED) {
43+
return true
44+
} else if (settings == ProxySettings.SMART) {
45+
return !hasProxyConfiguration(env)
46+
}
5047

51-
/**
52-
* Returns true if the given map of environment variables has any
53-
* proxy settings configured.
54-
*
55-
* @param env map of environment variables
56-
*/
57-
fun hasProxyConfiguration(env: Map<String, String>): Boolean {
58-
return env.keys.any {
59-
proxyVariables.contains(it.toUpperCase()) || npmProxyVariables.contains(it.toUpperCase())
48+
return false
6049
}
61-
}
6250

63-
/**
64-
* Get a list of all known keys that affect the proxy configuration
65-
*/
66-
fun getKnownProxyConfigurationKeys(): Set<String> {
67-
return proxyVariables.plus(npmProxyVariables).toSet()
68-
}
51+
/**
52+
* Returns true if the given map of environment variables has any
53+
* proxy settings configured.
54+
*
55+
* @param env map of environment variables
56+
*/
57+
fun hasProxyConfiguration(env: Map<String, String>): Boolean {
58+
return env.keys.any {
59+
proxyVariables.contains(it.toUpperCase()) || npmProxyVariables.contains(it.toUpperCase())
60+
}
61+
}
6962

70-
/**
71-
* Creates a new NodeExecConfiguration with the proxy environment variables configured
72-
*/
73-
fun addProxyEnvironmentVariables(
74-
proxySettings: ProxySettings,
75-
nodeExecConfiguration: NodeExecConfiguration,
76-
environment: Map<String, String> = System.getenv()
77-
): NodeExecConfiguration {
78-
val environmentVariables = createProxyEnvironmentVariables(
79-
proxySettings,
80-
nodeExecConfiguration.environment,
81-
environment,
82-
)
83-
return nodeExecConfiguration.copy(environment = environmentVariables)
84-
}
63+
/**
64+
* Get a list of all known keys that affect the proxy configuration
65+
*/
66+
fun getKnownProxyConfigurationKeys(): Set<String> {
67+
return proxyVariables.plus(npmProxyVariables).toSet()
68+
}
69+
70+
/**
71+
* Creates a new NodeExecConfiguration with the proxy environment variables configured
72+
*/
73+
fun addProxyEnvironmentVariables(
74+
proxySettings: ProxySettings,
75+
nodeExecConfiguration: NodeExecConfiguration,
76+
environment: Map<String, String> = System.getenv()
77+
): NodeExecConfiguration {
78+
val environmentVariables = createProxyEnvironmentVariables(
79+
proxySettings,
80+
nodeExecConfiguration.environment,
81+
environment,
82+
)
83+
return nodeExecConfiguration.copy(environment = environmentVariables)
84+
}
8585

86-
fun createProxyEnvironmentVariables(
87-
proxySettings: ProxySettings,
88-
nodeExecConfigurationEnvironment: Map<String, String>,
89-
environment: Map<String, String> = System.getenv()
90-
): Map<String, String> {
91-
if (shouldConfigureProxy(environment, proxySettings)) {
92-
val npmProxyEnvironmentVariables = computeNpmProxyEnvironmentVariables()
93-
val environmentVariablesToUnset =
94-
if (proxySettings == ProxySettings.FORCED) getKnownProxyConfigurationKeys()
95-
else emptySet()
96-
if (npmProxyEnvironmentVariables.isNotEmpty()) {
97-
val environmentVariables =
98-
nodeExecConfigurationEnvironment
99-
.minus(environmentVariablesToUnset)
100-
.plus(npmProxyEnvironmentVariables)
101-
return environmentVariables
86+
fun createProxyEnvironmentVariables(
87+
proxySettings: ProxySettings,
88+
nodeExecConfigurationEnvironment: Map<String, String>,
89+
environment: Map<String, String> = System.getenv()
90+
): Map<String, String> {
91+
if (shouldConfigureProxy(environment, proxySettings)) {
92+
val npmProxyEnvironmentVariables = computeNpmProxyEnvironmentVariables()
93+
val environmentVariablesToUnset =
94+
if (proxySettings == ProxySettings.FORCED) getKnownProxyConfigurationKeys()
95+
else emptySet()
96+
if (npmProxyEnvironmentVariables.isNotEmpty()) {
97+
val environmentVariables =
98+
nodeExecConfigurationEnvironment
99+
.minus(environmentVariablesToUnset)
100+
.plus(npmProxyEnvironmentVariables)
101+
return environmentVariables
102+
}
102103
}
104+
return emptyMap()
103105
}
104-
return emptyMap()
105-
}
106106

107-
private fun computeProxyUrlEnvironmentVariables(): MutableMap<String, String> {
108-
val proxyArgs = mutableMapOf<String, String>()
109-
for ((proxyProto, proxyParam) in
110-
listOf(arrayOf("http", "HTTP_PROXY"), arrayOf("https", "HTTPS_PROXY"))) {
111-
var proxyHost = System.getProperty("$proxyProto.proxyHost")
112-
val proxyPort = System.getProperty("$proxyProto.proxyPort")
113-
if (proxyHost != null && proxyPort != null) {
114-
proxyHost = proxyHost.replace("^https?://".toRegex(), "")
115-
val proxyUser = System.getProperty("$proxyProto.proxyUser")
116-
val proxyPassword = System.getProperty("$proxyProto.proxyPassword")
117-
if (proxyUser != null && proxyPassword != null) {
118-
proxyArgs[proxyParam] =
119-
"http://${encode(proxyUser)}:${encode(proxyPassword)}@$proxyHost:$proxyPort"
120-
} else {
121-
proxyArgs[proxyParam] = "http://$proxyHost:$proxyPort"
107+
private fun computeProxyUrlEnvironmentVariables(): MutableMap<String, String> {
108+
val proxyArgs = mutableMapOf<String, String>()
109+
for ((proxyProto, proxyParam) in
110+
listOf(arrayOf("http", "HTTP_PROXY"), arrayOf("https", "HTTPS_PROXY"))) {
111+
var proxyHost = System.getProperty("$proxyProto.proxyHost")
112+
val proxyPort = System.getProperty("$proxyProto.proxyPort")
113+
if (proxyHost != null && proxyPort != null) {
114+
proxyHost = proxyHost.replace("^https?://".toRegex(), "")
115+
val proxyUser = System.getProperty("$proxyProto.proxyUser")
116+
val proxyPassword = System.getProperty("$proxyProto.proxyPassword")
117+
if (proxyUser != null && proxyPassword != null) {
118+
proxyArgs[proxyParam] =
119+
"http://${encode(proxyUser)}:${encode(proxyPassword)}@$proxyHost:$proxyPort"
120+
} else {
121+
proxyArgs[proxyParam] = "http://$proxyHost:$proxyPort"
122+
}
122123
}
123124
}
125+
return proxyArgs
124126
}
125-
return proxyArgs
126-
}
127127

128-
private fun encode(value: String): String {
129-
return URLEncoder.encode(value, UTF_8.toString())
130-
}
128+
private fun encode(value: String): String {
129+
return URLEncoder.encode(value, UTF_8.toString())
130+
}
131131

132-
private fun addProxyIgnoredHostsEnvironmentVariable(
133-
proxyEnvironmentVariables: MutableMap<String, String>,
134-
) {
135-
val proxyIgnoredHosts = computeProxyIgnoredHosts()
136-
if (proxyIgnoredHosts.isNotEmpty()) {
137-
proxyEnvironmentVariables["NO_PROXY"] = proxyIgnoredHosts.joinToString(", ")
132+
private fun addProxyIgnoredHostsEnvironmentVariable(
133+
proxyEnvironmentVariables: MutableMap<String, String>,
134+
) {
135+
val proxyIgnoredHosts = computeProxyIgnoredHosts()
136+
if (proxyIgnoredHosts.isNotEmpty()) {
137+
proxyEnvironmentVariables["NO_PROXY"] = proxyIgnoredHosts.joinToString(", ")
138+
}
138139
}
139-
}
140140

141-
private fun computeProxyIgnoredHosts(): List<String> {
142-
return listOf("http.nonProxyHosts", "https.nonProxyHosts")
143-
.map { property ->
144-
val propertyValue = System.getProperty(property)
145-
if (propertyValue != null) {
146-
val hosts = propertyValue.split("|")
147-
return@map hosts
148-
.map { host ->
149-
if (host.contains(":")) host.split(":")[0]
150-
else host
141+
private fun computeProxyIgnoredHosts(): List<String> {
142+
return listOf("http.nonProxyHosts", "https.nonProxyHosts")
143+
.map { property ->
144+
val propertyValue = System.getProperty(property)
145+
if (propertyValue != null) {
146+
val hosts = propertyValue.split("|")
147+
return@map hosts.map { host ->
148+
host.substringBefore(":")
151149
}
150+
}
151+
return@map listOf()
152152
}
153-
return@map listOf()
154-
}
155-
.flatten()
156-
.distinct()
157-
// .collect(toList())
158-
}
159-
160-
// }
161-
@Deprecated(
162-
"Replace with regular object",
163-
ReplaceWith("NpmProxy", imports = ["com.github.gradle.node.npm.proxy.NpmProxy"])
164-
)
165-
object Companion
153+
.flatten()
154+
.distinct()
155+
}
166156

157+
}
167158
}

0 commit comments

Comments
 (0)