Skip to content

Commit

Permalink
#67 Update the pull request #89 by applying all recent changes to the…
Browse files Browse the repository at this point in the history
… pnpm part.
  • Loading branch information
bsautel committed Apr 6, 2021
1 parent 678268f commit 93aefc2
Show file tree
Hide file tree
Showing 23 changed files with 480 additions and 259 deletions.
19 changes: 19 additions & 0 deletions src/main/kotlin/com/github/gradle/node/NodeExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,29 @@ open class NodeExtension(project: Project) {
*/
val distBaseUrl = project.objects.property<String>()

/**
* The command used to start npm (default npm)
*/
val npmCommand = project.objects.property<String>().convention("npm")

/**
* The command used to start npx (default npx)
*/
val npxCommand = project.objects.property<String>().convention("npx")

/**
* The command used to start yarn (default yarn)
*/
val yarnCommand = project.objects.property<String>().convention("yarn")

/**
* The command used to start pnpm (default pnpm)
*/
val pnpmCommand = project.objects.property<String>().convention("pnpm")

/**
* The command used to start pnpx (default pnpx)
*/
val pnpxCommand = project.objects.property<String>().convention("pnpx")

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.github.gradle.node.npm.exec

import com.github.gradle.node.NodeExtension
import com.github.gradle.node.variant.VariantComputer
import org.gradle.api.file.Directory
import org.gradle.api.provider.Provider

internal typealias CommandExecComputer = (variantComputer: VariantComputer, nodeExtension: NodeExtension,
npmBinDir: Provider<Directory>) -> Provider<String>
internal typealias CommandExecComputer = (variantComputer: VariantComputer, npmBinDir: Provider<Directory>) -> Provider<String>

internal data class NpmExecConfiguration(
val command: String,
val commandExecComputer: CommandExecComputer
val command: String,
val commandExecComputer: CommandExecComputer
)
95 changes: 54 additions & 41 deletions src/main/kotlin/com/github/gradle/node/npm/exec/NpmExecRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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.npm.proxy.NpmProxy.Companion.computeNpmProxyEnvironmentVariables
import com.github.gradle.node.util.ProjectApiHelper
import com.github.gradle.node.util.zip
import com.github.gradle.node.variant.VariantComputer
Expand All @@ -20,54 +19,66 @@ internal abstract class NpmExecRunner {

private val variantComputer = VariantComputer()

fun executeNpmCommand(project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration) {
val npmExecConfiguration = NpmExecConfiguration("npm"
) { variantComputer, nodeExtension, npmBinDir -> variantComputer.computeNpmExec(nodeExtension, npmBinDir) }
executeCommand(project, extension, addProxyEnvironmentVariables(extension, nodeExecConfiguration),
npmExecConfiguration)
fun executeNpmCommand(
project: ProjectApiHelper,
nodeExtension: NodeExtension,
nodeExecConfiguration: NodeExecConfiguration
) {
val npmExecConfiguration = NpmExecConfiguration(
"npm"
) { variantComputer, npmBinDir -> variantComputer.computeNpmExec(nodeExtension, npmBinDir) }
executeCommand(
project, nodeExtension, addProxyEnvironmentVariables(nodeExtension, nodeExecConfiguration),
npmExecConfiguration
)
}

private fun addProxyEnvironmentVariables(nodeExtension: NodeExtension,
nodeExecConfiguration: NodeExecConfiguration): NodeExecConfiguration {
if (NpmProxy.shouldConfigureProxy(System.getenv(), nodeExtension.nodeProxySettings.get())) {
val npmProxyEnvironmentVariables = computeNpmProxyEnvironmentVariables()
if (npmProxyEnvironmentVariables.isNotEmpty()) {
val environmentVariables =
nodeExecConfiguration.environment.plus(npmProxyEnvironmentVariables)
return nodeExecConfiguration.copy(environment = environmentVariables)
}
}
return nodeExecConfiguration
private fun addProxyEnvironmentVariables(
nodeExtension: NodeExtension,
nodeExecConfiguration: NodeExecConfiguration
): NodeExecConfiguration {
val environment = NpmProxy.addProxyEnvironmentVariables(nodeExtension, nodeExecConfiguration.environment)
return nodeExecConfiguration.copy(environment = environment)
}

fun executeNpxCommand(project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration) {
val npxExecConfiguration = NpmExecConfiguration("npx") { variantComputer, nodeExtension, npmBinDir ->
fun executeNpxCommand(
project: ProjectApiHelper,
nodeExtension: NodeExtension,
nodeExecConfiguration: NodeExecConfiguration
) {
val npxExecConfiguration = NpmExecConfiguration("npx") { variantComputer, npmBinDir ->
variantComputer.computeNpxExec(nodeExtension, npmBinDir)
}
executeCommand(project, extension, nodeExecConfiguration, npxExecConfiguration)
executeCommand(project, nodeExtension, nodeExecConfiguration, npxExecConfiguration)
}

private fun executeCommand(project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration,
npmExecConfiguration: NpmExecConfiguration) {
private fun executeCommand(
project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration,
npmExecConfiguration: NpmExecConfiguration
) {
val execConfiguration =
computeExecConfiguration(extension, npmExecConfiguration, nodeExecConfiguration).get()
computeExecConfiguration(extension, npmExecConfiguration, nodeExecConfiguration).get()
val execRunner = ExecRunner()
execRunner.execute(project, extension, execConfiguration)
}

private fun computeExecConfiguration(extension: NodeExtension, npmExecConfiguration: NpmExecConfiguration,
nodeExecConfiguration: NodeExecConfiguration): Provider<ExecConfiguration> {
private fun computeExecConfiguration(
extension: NodeExtension, npmExecConfiguration: NpmExecConfiguration,
nodeExecConfiguration: NodeExecConfiguration
): Provider<ExecConfiguration> {
val additionalBinPathProvider = computeAdditionalBinPath(extension)
val executableAndScriptProvider = computeExecutable(extension, npmExecConfiguration)
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(executableAndScript.executable, args, additionalBinPath,
nodeExecConfiguration.environment, nodeExecConfiguration.workingDir,
nodeExecConfiguration.ignoreExitValue, nodeExecConfiguration.execOverrides)
}
.map { (additionalBinPath, executableAndScript) ->
val argsPrefix =
if (executableAndScript.script != null) listOf(executableAndScript.script) else listOf()
val args = argsPrefix.plus(nodeExecConfiguration.command)
ExecConfiguration(
executableAndScript.executable, args, additionalBinPath,
nodeExecConfiguration.environment, nodeExecConfiguration.workingDir,
nodeExecConfiguration.ignoreExitValue, nodeExecConfiguration.execOverrides
)
}
}

private fun computeExecutable(nodeExtension: NodeExtension, npmExecConfiguration: NpmExecConfiguration):
Expand All @@ -78,16 +89,18 @@ internal abstract class NpmExecRunner {
val npmBinDirProvider = variantComputer.computeNpmBinDir(npmDirProvider)
val nodeExecProvider = variantComputer.computeNodeExec(nodeExtension, nodeBinDirProvider)
val executableProvider =
npmExecConfiguration.commandExecComputer(variantComputer, nodeExtension, npmBinDirProvider)
npmExecConfiguration.commandExecComputer(variantComputer, npmBinDirProvider)
val npmScriptFileProvider =
variantComputer.computeNpmScriptFile(nodeDirProvider, npmExecConfiguration.command)
return zip(nodeExtension.download, nodeExtension.nodeProjectDir, executableProvider, nodeExecProvider,
npmScriptFileProvider).map {
variantComputer.computeNpmScriptFile(nodeDirProvider, npmExecConfiguration.command)
return zip(
nodeExtension.download, nodeExtension.nodeProjectDir, executableProvider, nodeExecProvider,
npmScriptFileProvider
).map {
val (download, nodeProjectDir, executable, nodeExec,
npmScriptFile) = it
npmScriptFile) = it
if (download) {
val localCommandScript = nodeProjectDir.dir("node_modules/npm/bin")
.file("${npmExecConfiguration.command}-cli.js").asFile
.file("${npmExecConfiguration.command}-cli.js").asFile
if (localCommandScript.exists()) {
return@map ExecutableAndScript(nodeExec, localCommandScript.absolutePath)
} else if (!File(executable).exists()) {
Expand All @@ -99,8 +112,8 @@ internal abstract class NpmExecRunner {
}

private data class ExecutableAndScript(
val executable: String,
val script: String? = null
val executable: String,
val script: String? = null
)

private fun computeAdditionalBinPath(nodeExtension: NodeExtension): Provider<List<String>> {
Expand Down
48 changes: 31 additions & 17 deletions src/main/kotlin/com/github/gradle/node/npm/proxy/NpmProxy.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.gradle.node.npm.proxy

import com.github.gradle.node.NodeExtension
import java.net.URLEncoder
import java.util.stream.Collectors.toList
import java.util.stream.Stream
Expand All @@ -11,15 +12,28 @@ internal class NpmProxy {
// These are the environment variables that HTTPing applications checks, proxy is on and off.
// FTP skipped in hopes of a better future.
private val proxyVariables = listOf(
"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "PROXY"
"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "PROXY"
)

// And since npm also takes settings in the form of environment variables with the
// NPM_CONFIG_<setting> format, we should check those. Hopefully nobody does this.
private val npmProxyVariables = listOf(
"NPM_CONFIG_PROXY", "NPM_CONFIG_HTTPS-PROXY", "NPM_CONFIG_NOPROXY"
"NPM_CONFIG_PROXY", "NPM_CONFIG_HTTPS-PROXY", "NPM_CONFIG_NOPROXY"
)

fun addProxyEnvironmentVariables(
nodeExtension: NodeExtension,
environment: Map<String, String>
): Map<String, String> {
if (shouldConfigureProxy(System.getenv(), nodeExtension.nodeProxySettings.get())) {
val npmProxyEnvironmentVariables = computeNpmProxyEnvironmentVariables()
if (npmProxyEnvironmentVariables.isNotEmpty()) {
return environment.plus(npmProxyEnvironmentVariables)
}
}
return environment
}

fun computeNpmProxyEnvironmentVariables(): Map<String, String> {
val proxyEnvironmentVariables = computeProxyUrlEnvironmentVariables()
if (proxyEnvironmentVariables.isNotEmpty()) {
Expand Down Expand Up @@ -62,7 +76,7 @@ internal class NpmProxy {
val proxyPassword = System.getProperty("$proxyProto.proxyPassword")
if (proxyUser != null && proxyPassword != null) {
proxyArgs[proxyParam] =
"http://${encode(proxyUser)}:${encode(proxyPassword)}@$proxyHost:$proxyPort"
"http://${encode(proxyUser)}:${encode(proxyPassword)}@$proxyHost:$proxyPort"
} else {
proxyArgs[proxyParam] = "http://$proxyHost:$proxyPort"
}
Expand All @@ -84,21 +98,21 @@ internal class NpmProxy {

private fun computeProxyIgnoredHosts(): List<String> {
return Stream.of("http.nonProxyHosts", "https.nonProxyHosts")
.map { property ->
val propertyValue = System.getProperty(property)
if (propertyValue != null) {
val hosts = propertyValue.split("|")
return@map hosts
.map { host ->
if (host.contains(":")) host.split(":")[0]
else host
}
}
return@map listOf<String>()
.map { property ->
val propertyValue = System.getProperty(property)
if (propertyValue != null) {
val hosts = propertyValue.split("|")
return@map hosts
.map { host ->
if (host.contains(":")) host.split(":")[0]
else host
}
}
.flatMap(List<String>::stream)
.distinct()
.collect(toList())
return@map listOf<String>()
}
.flatMap(List<String>::stream)
.distinct()
.collect(toList())
}
}
}
Loading

0 comments on commit 93aefc2

Please sign in to comment.