Skip to content

Commit e4b9b15

Browse files
authored
Merge pull request #202 from node-gradle/open-util-classes
Open utility classes for external consumption.
2 parents 8e9ff60 + eb7cbc7 commit e4b9b15

12 files changed

Lines changed: 77 additions & 63 deletions

File tree

src/main/kotlin/com/github/gradle/node/exec/ExecConfiguration.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.gradle.api.Action
44
import org.gradle.process.ExecSpec
55
import java.io.File
66

7-
internal data class ExecConfiguration(
7+
data class ExecConfiguration(
88
val executable: String,
99
val args: List<String> = listOf(),
1010
val additionalBinPaths: List<String> = listOf(),

src/main/kotlin/com/github/gradle/node/exec/ExecRunner.kt

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,36 @@ import com.github.gradle.node.util.ProjectApiHelper
55
import org.gradle.api.file.DirectoryProperty
66
import java.io.File
77

8-
internal class ExecRunner {
8+
/**
9+
* Helper function that will calculate the environment variables that should be used.
10+
*
11+
* This is operating-system aware and will check Path and PATH in additionalBinPaths on Windows
12+
*
13+
* @param execConfiguration configuration to get environment variables from
14+
*/
15+
fun computeEnvironment(execConfiguration: ExecConfiguration): Map<String, String> {
16+
val execEnvironment = mutableMapOf<String, String>()
17+
execEnvironment += System.getenv()
18+
execEnvironment += execConfiguration.environment
19+
if (execConfiguration.additionalBinPaths.isNotEmpty()) {
20+
// Take care of Windows environments that may contain "Path" OR "PATH" - both existing
21+
// possibly (but not in parallel as of now)
22+
val pathEnvironmentVariableName = if (execEnvironment["Path"] != null) "Path" else "PATH"
23+
val actualPath = execEnvironment[pathEnvironmentVariableName]
24+
val additionalPathsSerialized = execConfiguration.additionalBinPaths.joinToString(File.pathSeparator)
25+
execEnvironment[pathEnvironmentVariableName] =
26+
"${additionalPathsSerialized}${File.pathSeparator}${actualPath}"
27+
}
28+
return execEnvironment
29+
}
30+
31+
fun computeWorkingDir(nodeProjectDir: DirectoryProperty, execConfiguration: ExecConfiguration): File? {
32+
val workingDir = execConfiguration.workingDir ?: nodeProjectDir.get().asFile
33+
workingDir.mkdirs()
34+
return workingDir
35+
}
36+
37+
class ExecRunner {
938
fun execute(projectHelper: ProjectApiHelper, extension: NodeExtension, execConfiguration: ExecConfiguration) {
1039
projectHelper.exec {
1140
executable = execConfiguration.executable
@@ -16,26 +45,4 @@ internal class ExecRunner {
1645
execConfiguration.execOverrides?.execute(this)
1746
}
1847
}
19-
20-
private fun computeEnvironment(execConfiguration: ExecConfiguration): Map<String, String> {
21-
val execEnvironment = mutableMapOf<String, String>()
22-
execEnvironment += System.getenv()
23-
execEnvironment += execConfiguration.environment
24-
if (execConfiguration.additionalBinPaths.isNotEmpty()) {
25-
// Take care of Windows environments that may contain "Path" OR "PATH" - both existing
26-
// possibly (but not in parallel as of now)
27-
val pathEnvironmentVariableName = if (execEnvironment["Path"] != null) "Path" else "PATH"
28-
val actualPath = execEnvironment[pathEnvironmentVariableName]
29-
val additionalPathsSerialized = execConfiguration.additionalBinPaths.joinToString(File.pathSeparator)
30-
execEnvironment[pathEnvironmentVariableName] =
31-
"${additionalPathsSerialized}${File.pathSeparator}${actualPath}"
32-
}
33-
return execEnvironment
34-
}
35-
36-
private fun computeWorkingDir(nodeProjectDir: DirectoryProperty, execConfiguration: ExecConfiguration): File? {
37-
val workingDir = execConfiguration.workingDir ?: nodeProjectDir.get().asFile
38-
workingDir.mkdirs()
39-
return workingDir
40-
}
4148
}

src/main/kotlin/com/github/gradle/node/exec/NodeExecConfiguration.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.gradle.api.Action
44
import org.gradle.process.ExecSpec
55
import java.io.File
66

7-
internal data class NodeExecConfiguration(
7+
data class NodeExecConfiguration(
88
val command: List<String> = listOf(),
99
val environment: Map<String, String> = mapOf(),
1010
val workingDir: File? = null,

src/main/kotlin/com/github/gradle/node/exec/NodeExecRunner.kt

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,36 @@ import com.github.gradle.node.variant.VariantComputer
77
import org.gradle.api.file.Directory
88
import org.gradle.api.provider.Provider
99

10-
internal class NodeExecRunner {
10+
/**
11+
* This function is responsible for setting up the configuration used when running the tasks.
12+
*/
13+
fun buildExecConfiguration(nodeExtension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration):
14+
Provider<ExecConfiguration> {
15+
val variantComputer = VariantComputer()
16+
val nodeDirProvider = variantComputer.computeNodeDir(nodeExtension)
17+
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
18+
val executableProvider = variantComputer.computeNodeExec(nodeExtension, nodeBinDirProvider)
19+
val additionalBinPathProvider = computeAdditionalBinPath(nodeExtension, nodeBinDirProvider)
20+
return zip(executableProvider, additionalBinPathProvider)
21+
.map { (executable, additionalBinPath) ->
22+
ExecConfiguration(executable, nodeExecConfiguration.command, additionalBinPath,
23+
nodeExecConfiguration.environment, nodeExecConfiguration.workingDir,
24+
nodeExecConfiguration.ignoreExitValue, nodeExecConfiguration.execOverrides)
25+
}
26+
}
27+
28+
fun computeAdditionalBinPath(nodeExtension: NodeExtension, nodeBinDirProvider: Provider<Directory>):
29+
Provider<List<String>> {
30+
return zip(nodeExtension.download, nodeBinDirProvider)
31+
.map { (download, nodeBinDir) ->
32+
if (download) listOf(nodeBinDir.asFile.absolutePath) else listOf()
33+
}
34+
}
35+
36+
class NodeExecRunner {
1137
fun execute(project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration) {
1238
val execConfiguration = buildExecConfiguration(extension, nodeExecConfiguration).get()
1339
val execRunner = ExecRunner()
1440
execRunner.execute(project, extension, execConfiguration)
1541
}
16-
17-
private fun buildExecConfiguration(nodeExtension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration):
18-
Provider<ExecConfiguration> {
19-
val variantComputer = VariantComputer()
20-
val nodeDirProvider = variantComputer.computeNodeDir(nodeExtension)
21-
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
22-
val executableProvider = variantComputer.computeNodeExec(nodeExtension, nodeBinDirProvider)
23-
val additionalBinPathProvider = computeAdditionalBinPath(nodeExtension, nodeBinDirProvider)
24-
return zip(executableProvider, additionalBinPathProvider)
25-
.map { (executable, additionalBinPath) ->
26-
ExecConfiguration(executable, nodeExecConfiguration.command, additionalBinPath,
27-
nodeExecConfiguration.environment, nodeExecConfiguration.workingDir,
28-
nodeExecConfiguration.ignoreExitValue, nodeExecConfiguration.execOverrides)
29-
}
30-
}
31-
32-
private fun computeAdditionalBinPath(nodeExtension: NodeExtension, nodeBinDirProvider: Provider<Directory>):
33-
Provider<List<String>> {
34-
return zip(nodeExtension.download, nodeBinDirProvider)
35-
.map { (download, nodeBinDir) ->
36-
if (download) listOf(nodeBinDir.asFile.absolutePath) else listOf()
37-
}
38-
}
3942
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.gradle.api.provider.ProviderFactory
1414
import java.io.File
1515
import javax.inject.Inject
1616

17-
internal abstract class NpmExecRunner {
17+
abstract class NpmExecRunner {
1818
@get:Inject
1919
abstract val providers: ProviderFactory
2020

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import java.util.stream.Collectors.toList
55
import java.util.stream.Stream
66
import kotlin.text.Charsets.UTF_8
77

8-
internal class NpmProxy {
8+
class NpmProxy {
99

1010
companion object {
1111
// These are the environment variables that HTTPing applications checks, proxy is on and off.
@@ -28,6 +28,11 @@ internal class NpmProxy {
2828
return proxyEnvironmentVariables.toMap()
2929
}
3030

31+
/**
32+
* Helper function for deciding whether to set proxy settings or not.
33+
*
34+
* If you make use of
35+
*/
3136
fun shouldConfigureProxy(env: Map<String, String>, settings: ProxySettings): Boolean {
3237
if (settings == ProxySettings.FORCED) {
3338
return true

src/main/kotlin/com/github/gradle/node/util/ProjectApiHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interface ProjectApiHelper {
4343
fun exec(action: Action<ExecSpec>): ExecResult
4444
}
4545

46-
internal open class DefaultProjectApiHelper @Inject constructor(
46+
open class DefaultProjectApiHelper @Inject constructor(
4747
private val factory: ObjectFactory,
4848
private val execOperations: ExecOperations,
4949
private val fileSystemOperations: FileSystemOperations,
@@ -74,7 +74,7 @@ internal open class DefaultProjectApiHelper @Inject constructor(
7474
}
7575
}
7676

77-
internal open class LegacyProjectApiHelper(private val project: Project) : ProjectApiHelper {
77+
open class LegacyProjectApiHelper(private val project: Project) : ProjectApiHelper {
7878

7979
override fun fileTree(directory: Directory): ConfigurableFileTree {
8080
return project.fileTree(directory)

src/main/kotlin/com/github/gradle/node/variant/VariantComputer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.github.gradle.node.util.zip
77
import org.gradle.api.file.Directory
88
import org.gradle.api.provider.Provider
99

10-
internal class VariantComputer @JvmOverloads constructor(
10+
open class VariantComputer @JvmOverloads constructor(
1111
private val platformHelper: PlatformHelper = PlatformHelper.INSTANCE
1212
) {
1313
fun computeNodeDir(nodeExtension: NodeExtension): Provider<Directory> {

src/main/kotlin/com/github/gradle/node/yarn/exec/YarnExecRunner.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.gradle.api.provider.Provider
1313
import org.gradle.api.provider.ProviderFactory
1414
import javax.inject.Inject
1515

16-
internal abstract class YarnExecRunner {
16+
abstract class YarnExecRunner {
1717
@get:Inject
1818
abstract val providers: ProviderFactory
1919

src/test/groovy/com/github/gradle/node/SystemVersion_integTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ class SystemVersion_integTest extends AbstractIntegTest {
2929
result2.output.contains("Project repositories: 0")
3030

3131
when:
32-
def result3 = build(":npmHelp")
32+
def result3 = build(":npmVersion")
3333

3434
then:
35-
result3.task(":npmHelp").outcome == TaskOutcome.SUCCESS
36-
result3.output.contains("Usage: npm <command>")
35+
result3.task(":npmVersion").outcome == TaskOutcome.SUCCESS
36+
result3.output.contains(" npm: '")
3737

3838
when:
3939
def result4 = build(":npxHelp")
4040

4141
then:
4242
result4.task(":npxHelp").outcome == TaskOutcome.SUCCESS
43-
result4.output.contains("npx --shell-auto-fallback [shell]")
43+
result4.output.contains("Run a command from a local or remote npm package")
4444

4545
when:
4646
def result5 = build(":yarnHelp")

0 commit comments

Comments
 (0)