Skip to content

Commit

Permalink
Fix configuration-cache support in Gradle 8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
deepy committed Aug 2, 2023
1 parent f327f5a commit 0125322
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 96 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## Version 5.x *(unreleased)*
## Version 6.x *(unreleased)*
* Removed deprecated `nodeModulesDir` from `NodeExtension`
* The resolved/computed node directory and platform are stored in `resolvedNodeDir` and `resolvedPlatform` on `NodeExtension`
* Fixes configuration-cache issue in Gradle 8.4

## Version 5.0.0 *(2023-05-13)*
* Support configuration-cache on Gradle 8.1 [#271](https://github.com/node-gradle/gradle-node-plugin/issues/271)
Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/com/github/gradle/node/NodeExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ open class NodeExtension(project: Project) {
*/
val oldNpm = project.objects.property<Boolean>().convention(false)

@Suppress("unused")
@Deprecated("Deprecated in version 3.0, please use nodeProjectDir now")
val nodeModulesDir = nodeProjectDir

/**
* Create rules for automatic task creation
*
Expand All @@ -152,13 +148,25 @@ open class NodeExtension(project: Project) {
/**
* Computed path to nodejs directory
*/
@Deprecated(message = "replaced with resolvedNodeDir", replaceWith = ReplaceWith("resolvedNodeDir"))
val computedNodeDir = project.objects.directoryProperty()

/**
* Computed path to nodejs directory
*/
val resolvedNodeDir = project.objects.directoryProperty()

/**
* Operating system and architecture
*/
@Deprecated(message = "replaced with resolvedPlatform", replaceWith = ReplaceWith("resolvedPlatform"))
val computedPlatform = project.objects.property<Platform>()

/**
* Operating system and architecture
*/
val resolvedPlatform = project.objects.property<Platform>()

init {
distBaseUrl.set("https://nodejs.org/dist")
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/com/github/gradle/node/NodePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ class NodePlugin : Plugin<Project> {
}
}

@SuppressWarnings("deprecation")
private fun configureNodeExtension(extension: NodeExtension) {
addPlatform(extension)
extension.computedNodeDir.set(computeNodeDir(extension))
extension.computedNodeDir.finalizeValueOnRead()
with(extension.resolvedNodeDir) {
set(computeNodeDir(extension))
finalizeValueOnRead()
}
with(extension.computedNodeDir) {
convention(extension.resolvedNodeDir)
finalizeValueOnRead()
}
}

private fun addPlatform(extension: NodeExtension) {
Expand All @@ -77,7 +84,8 @@ class NodePlugin : Plugin<Project> {
val name = System.getProperty("os.name")
val arch = System.getProperty("os.arch")
val platform = parsePlatform(name, arch, uname)
extension.computedPlatform.set(platform)
extension.resolvedPlatform.set(platform)
extension.computedPlatform.convention(extension.resolvedPlatform)
}

private fun addGlobalTypes() {
Expand Down Expand Up @@ -167,7 +175,7 @@ class NodePlugin : Plugin<Project> {

private fun configureNodeSetupTask(nodeExtension: NodeExtension) {
project.tasks.withType<NodeSetupTask>().configureEach {
nodeDir.set(nodeExtension.computedNodeDir)
nodeDir.set(nodeExtension.resolvedNodeDir)
val archiveFileProvider = computeNodeArchiveDependency(nodeExtension)
.map { nodeArchiveDependency ->
resolveNodeArchiveFile(nodeArchiveDependency)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/github/gradle/node/exec/NodeExecRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ fun buildExecConfiguration(
nodeExecConfiguration: NodeExecConfiguration,
variantComputer: VariantComputer
):
Provider<ExecConfiguration> {
val nodeDirProvider = nodeExtension.computedNodeDir
Provider<ExecConfiguration> {
val nodeDirProvider = nodeExtension.resolvedNodeDir
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
val executableProvider = computeNodeExec(nodeExtension, nodeBinDirProvider)
val additionalBinPathProvider = computeAdditionalBinPath(nodeExtension, nodeBinDirProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ abstract class NpmExecRunner {
npmExecConfiguration: NpmExecConfiguration,
variantComputer: VariantComputer
):
Provider<ExecutableAndScript> {
val nodeDirProvider = nodeExtension.computedNodeDir
Provider<ExecutableAndScript> {
val nodeDirProvider = nodeExtension.resolvedNodeDir
val npmDirProvider = variantComputer.computeNpmDir(nodeExtension, nodeDirProvider)
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
val npmBinDirProvider = variantComputer.computeNpmBinDir(npmDirProvider)
val nodeExecProvider = computeNodeExec(nodeExtension, nodeBinDirProvider)
val executableProvider =
npmExecConfiguration.commandExecComputer(variantComputer, nodeExtension, npmBinDirProvider)
val isWindows = nodeExtension.computedPlatform.get().isWindows()
val isWindows = nodeExtension.resolvedPlatform.get().isWindows()
val npmScriptFileProvider =
computeNpmScriptFile(nodeDirProvider, npmExecConfiguration.command, isWindows)
return zip(
Expand Down Expand Up @@ -120,7 +120,7 @@ abstract class NpmExecRunner {
if (!download) {
providers.provider { listOf<String>() }
}
val nodeDirProvider = variantComputer.computeNodeDir(nodeExtension)
val nodeDirProvider = nodeExtension.resolvedNodeDir
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
val npmDirProvider = variantComputer.computeNpmDir(nodeExtension, nodeDirProvider)
val npmBinDirProvider = variantComputer.computeNpmBinDir(npmDirProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class NpmSetupTask : BaseTask() {

@get:OutputDirectory
val npmDir by lazy {
val nodeDir = variantComputer.computeNodeDir(nodeExtension)
val nodeDir = nodeExtension.resolvedNodeDir
variantComputer.computeNpmDir(nodeExtension, nodeDir)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ abstract class PnpmExecRunner {
variantComputer: VariantComputer
):
Provider<ExecutableAndScript> {
val nodeDirProvider = variantComputer.computeNodeDir(nodeExtension)
val nodeDirProvider = nodeExtension.resolvedNodeDir
val pnpmDirProvider = variantComputer.computePnpmDir(nodeExtension)
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
val pnpmBinDirProvider = variantComputer.computePnpmBinDir(pnpmDirProvider)
Expand Down Expand Up @@ -105,7 +105,7 @@ abstract class PnpmExecRunner {
if (!download) {
providers.provider { listOf<String>() }
}
val nodeDirProvider = variantComputer.computeNodeDir(nodeExtension)
val nodeDirProvider = nodeExtension.resolvedNodeDir
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
val pnpmDirProvider = variantComputer.computePnpmDir(nodeExtension)
val pnpmBinDirProvider = variantComputer.computePnpmBinDir(pnpmDirProvider)
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/github/gradle/node/task/NodeSetupTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class NodeSetupTask : BaseTask() {

private fun unpackNodeArchive() {
val archiveFile = nodeArchiveFile.get().asFile
val nodeDirProvider = nodeExtension.computedNodeDir
val nodeDirProvider = nodeExtension.resolvedNodeDir
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
val archivePath = nodeDirProvider.map { it.dir("../") }
if (archiveFile.name.endsWith("zip")) {
Expand All @@ -84,15 +84,15 @@ abstract class NodeSetupTask : BaseTask() {

private fun fixBrokenSymlink(name: String, nodeBinDirPath: Path, nodeDirProvider: Provider<Directory>) {
val script = nodeBinDirPath.resolve(name)
val scriptFile = computeNpmScriptFile(nodeDirProvider, name, nodeExtension.computedPlatform.get().isWindows())
val scriptFile = computeNpmScriptFile(nodeDirProvider, name, nodeExtension.resolvedPlatform.get().isWindows())
if (Files.deleteIfExists(script)) {
Files.createSymbolicLink(script, nodeBinDirPath.relativize(Paths.get(scriptFile.get())))
}
}

private fun setExecutableFlag() {
if (!nodeExtension.computedPlatform.get().isWindows()) {
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeExtension.computedNodeDir)
if (!nodeExtension.resolvedPlatform.get().isWindows()) {
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeExtension.resolvedNodeDir)
val nodeExecProvider = computeNodeExec(nodeExtension, nodeBinDirProvider)
File(nodeExecProvider.get()).setExecutable(true)
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/kotlin/com/github/gradle/node/variant/VariantComputer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fun computeNodeExec(nodeExtension: NodeExtension, nodeBinDirProvider: Provider<D
return zip(nodeExtension.download, nodeBinDirProvider).map {
val (download, nodeBinDir) = it
if (download) {
val nodeCommand = if (nodeExtension.computedPlatform.get().isWindows()) "node.exe" else "node"
val nodeCommand = if (nodeExtension.resolvedPlatform.get().isWindows()) "node.exe" else "node"
nodeBinDir.dir(nodeCommand).asFile.absolutePath
} else "node"
}
Expand All @@ -28,8 +28,8 @@ fun computeNpmScriptFile(nodeDirProvider: Provider<Directory>, command: String,
}

fun computeNodeDir(nodeExtension: NodeExtension): Provider<Directory> {
val osName = nodeExtension.computedPlatform.get().name
val osArch = nodeExtension.computedPlatform.get().arch
val osName = nodeExtension.resolvedPlatform.get().name
val osArch = nodeExtension.resolvedPlatform.get().arch
return computeNodeDir(nodeExtension, osName, osArch)
}

Expand All @@ -46,9 +46,9 @@ fun computeNodeDir(nodeExtension: NodeExtension, osName: String, osArch: String)
* Essentially: org.nodejs:node:$version:$osName-$osArch@tar.gz
*/
fun computeNodeArchiveDependency(extension: NodeExtension): Provider<String> {
val osName = extension.computedPlatform.get().name
val osArch = extension.computedPlatform.get().arch
val type = if (extension.computedPlatform.get().isWindows()) "zip" else "tar.gz"
val osName = extension.resolvedPlatform.get().name
val osArch = extension.resolvedPlatform.get().arch
val type = if (extension.resolvedPlatform.get().isWindows()) "zip" else "tar.gz"
return extension.version.map { version -> "org.nodejs:node:$version:$osName-$osArch@$type" }
}

Expand Down Expand Up @@ -112,7 +112,7 @@ open class VariantComputer constructor(
fun computeNpmExec(nodeExtension: NodeExtension, npmBinDirProvider: Provider<Directory>): Provider<String> {
return zip(nodeExtension.download, nodeExtension.npmCommand, npmBinDirProvider).map {
val (download, npmCommand, npmBinDir) = it
val command = if (nodeExtension.computedPlatform.get().isWindows()) {
val command = if (nodeExtension.resolvedPlatform.get().isWindows()) {
npmCommand.mapIf({ it == "npm" }) { "npm.cmd" }
} else npmCommand
if (download) npmBinDir.dir(command).asFile.absolutePath else command
Expand All @@ -132,7 +132,7 @@ open class VariantComputer constructor(
fun computeNpxExec(nodeExtension: NodeExtension, npmBinDirProvider: Provider<Directory>): Provider<String> {
return zip(nodeExtension.download, nodeExtension.npxCommand, npmBinDirProvider).map {
val (download, npxCommand, npmBinDir) = it
val command = if (nodeExtension.computedPlatform.get().isWindows()) {
val command = if (nodeExtension.resolvedPlatform.get().isWindows()) {
npxCommand.mapIf({ it == "npx" }) { "npx.cmd" }
} else npxCommand
if (download) npmBinDir.dir(command).asFile.absolutePath else command
Expand All @@ -155,7 +155,7 @@ open class VariantComputer constructor(
fun computePnpmExec(nodeExtension: NodeExtension, pnpmBinDirProvider: Provider<Directory>): Provider<String> {
return zip(nodeExtension.pnpmCommand, nodeExtension.download, pnpmBinDirProvider).map {
val (pnpmCommand, download, pnpmBinDir) = it
val command = if (nodeExtension.computedPlatform.get().isWindows()) {
val command = if (nodeExtension.resolvedPlatform.get().isWindows()) {
pnpmCommand.mapIf({ it == "pnpm" }) { "pnpm.cmd" }
} else pnpmCommand
if (download) pnpmBinDir.dir(command).asFile.absolutePath else command
Expand All @@ -178,7 +178,7 @@ open class VariantComputer constructor(
fun computeYarnExec(nodeExtension: NodeExtension, yarnBinDirProvider: Provider<Directory>): Provider<String> {
return zip(nodeExtension.yarnCommand, nodeExtension.download, yarnBinDirProvider).map {
val (yarnCommand, download, yarnBinDir) = it
val command = if (nodeExtension.computedPlatform.get().isWindows()) {
val command = if (nodeExtension.resolvedPlatform.get().isWindows()) {
yarnCommand.mapIf({ it == "yarn" }) { "yarn.cmd" }
} else yarnCommand
if (download) yarnBinDir.dir(command).asFile.absolutePath else command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ abstract class YarnExecRunner {
nodeExecConfiguration: NodeExecConfiguration,
variantComputer: VariantComputer
): ExecResult {
val nodeDirProvider = variantComputer.computeNodeDir(nodeExtension)
val nodeDirProvider = nodeExtension.resolvedNodeDir
val yarnDirProvider = variantComputer.computeYarnDir(nodeExtension)
val yarnBinDirProvider = variantComputer.computeYarnBinDir(yarnDirProvider)
val yarnExecProvider = variantComputer.computeYarnExec(nodeExtension, yarnBinDirProvider)
val additionalBinPathProvider =
computeAdditionalBinPath(nodeExtension, nodeDirProvider, yarnBinDirProvider, variantComputer)
val execConfiguration = ExecConfiguration(yarnExecProvider.get(),
nodeExecConfiguration.command, additionalBinPathProvider.get(),
addNpmProxyEnvironment(nodeExtension, nodeExecConfiguration), nodeExecConfiguration.workingDir,
nodeExecConfiguration.ignoreExitValue, nodeExecConfiguration.execOverrides)
computeAdditionalBinPath(nodeExtension, nodeDirProvider, yarnBinDirProvider, variantComputer)
val execConfiguration = ExecConfiguration(
yarnExecProvider.get(),
nodeExecConfiguration.command, additionalBinPathProvider.get(),
addNpmProxyEnvironment(nodeExtension, nodeExecConfiguration), nodeExecConfiguration.workingDir,
nodeExecConfiguration.ignoreExitValue, nodeExecConfiguration.execOverrides
)
val execRunner = ExecRunner()

return execRunner.execute(project, nodeExtension, execConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class NpmInstallTaskTest extends AbstractTaskTest {
def "exec npm install task with configured proxy"() {
given:
props.setProperty('os.name', 'Linux')
nodeExtension.computedPlatform.set(PlatformHelperKt.parsePlatform("Linux", "x86_64", {}))
nodeExtension.resolvedPlatform.set(PlatformHelperKt.parsePlatform("Linux", "x86_64", {}))
GradleProxyHelper.setHttpsProxyHost("my-super-proxy.net")
GradleProxyHelper.setHttpsProxyPort(11235)

Expand All @@ -34,7 +34,7 @@ class NpmInstallTaskTest extends AbstractTaskTest {
def "exec npm install task with configured proxy but disabled"() {
given:
props.setProperty('os.name', 'Linux')
nodeExtension.computedPlatform.set(PlatformHelperKt.parsePlatform("Linux", "x86_64", {}))
nodeExtension.resolvedPlatform.set(PlatformHelperKt.parsePlatform("Linux", "x86_64", {}))
GradleProxyHelper.setHttpsProxyHost("my-super-proxy.net")
GradleProxyHelper.setHttpsProxyPort(11235)
nodeExtension.nodeProxySettings.set(ProxySettings.OFF)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NpmSetupTaskTest extends AbstractTaskTest {
def "disable npmSetup task when no npm version is specified"() {
given:
props.setProperty('os.name', 'Linux')
nodeExtension.computedPlatform.set(PlatformHelperKt.parsePlatform("Linux", "x86_64", {}))
nodeExtension.resolvedPlatform.set(PlatformHelperKt.parsePlatform("Linux", "x86_64", {}))

def task = project.tasks.create('simple', NpmSetupTask)
mockPlatformHelper(task)
Expand All @@ -27,7 +27,7 @@ class NpmSetupTaskTest extends AbstractTaskTest {
def "exec npmSetup task (version specified)"() {
given:
props.setProperty('os.name', 'Linux')
nodeExtension.computedPlatform.set(PlatformHelperKt.parsePlatform("Linux", "x86_64", {}))
nodeExtension.resolvedPlatform.set(PlatformHelperKt.parsePlatform("Linux", "x86_64", {}))
nodeExtension.npmVersion.set('6.4.1')
def task = project.tasks.create('simple', NpmSetupTask)
Expand All @@ -50,7 +50,7 @@ class NpmSetupTaskTest extends AbstractTaskTest {
def "exec npmSetup task with proxy configured"() {
given:
props.setProperty('os.name', 'Linux')
nodeExtension.computedPlatform.set(PlatformHelperKt.parsePlatform("Linux", "x86_64", {}))
nodeExtension.resolvedPlatform.set(PlatformHelperKt.parsePlatform("Linux", "x86_64", {}))
nodeExtension.npmVersion.set('6.4.1')
GradleProxyHelper.setHttpProxyHost("my-proxy.net")
GradleProxyHelper.setHttpProxyPort(1234)
Expand Down
Loading

0 comments on commit 0125322

Please sign in to comment.