Skip to content

Commit

Permalink
Move platform computation to NodeExtension
Browse files Browse the repository at this point in the history
This fixes #271 and starts a bunch of refactors, it
also re-introduces support for Gradle 6, and deprecates
the platform functionality of PlatformHelper (in it's
current state)
  • Loading branch information
deepy committed May 12, 2023
1 parent 0073845 commit 3ea8a87
Show file tree
Hide file tree
Showing 29 changed files with 343 additions and 209 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ jobs:
strategy:
matrix:
config:
- {os: ubuntu-latest, params: "'-PtestedGradleVersion=7.5.1|8.1.1' pnpmTests" }
- {os: ubuntu-latest, params: "'-PtestedGradleVersion=7.5.1|8.1.1' build" }
- {os: ubuntu-latest, params: "'-PtestedGradleVersion=6.9.4|7.5.1|8.1.1' pnpmTests" }
- {os: ubuntu-latest, params: "'-PtestedGradleVersion=6.9.4|7.5.1|8.1.1' build" }
- {os: windows-latest, params: "build pnpmTests" }
- {os: macos-latest, params: "build pnpmTests" }
steps:
Expand Down
37 changes: 22 additions & 15 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Changelog

## Version 4.x *(unreleased)*
## Version 5.x *(unreleased)*

## Version 5.0.0 *(unreleased)*
* Support configuration-cache on Gradle 8.1 [#271](https://github.com/node-gradle/gradle-node-plugin/issues/271)
* Store node directories on NodeExtension
* Deprecate parts of PlatformHelper that require executing commands during configuration
* Re-introduce support for Gradle 6.6 and newer

## Version 4.0.0 *(2023-04-23)*
* **This version is unfortunately broken, please upgrade directly to 5.0.0**
* Drop support for versions lower than Gradle 7.5.1
* Rename `package.json` extension to `packageJson` [#232](https://github.com/node-gradle/gradle-node-plugin/issues/232)
* Support configuration-cache on Gradle 8.1 [#271](https://github.com/node-gradle/gradle-node-plugin/issues/271)
Expand Down Expand Up @@ -39,7 +46,7 @@
## Version 3.1.0 *(2021-05-07)*
* Add support for Linux Power PC [#166](https://github.com/node-gradle/gradle-node-plugin/issues/166) (thanks [akatona84](https://github.com/akatona84) for the [pull request](https://github.com/node-gradle/gradle-node-plugin/issues/167))
* Internal test suite now running against Gradle 7
* Adds `yarn.lock` as an input to `NpmInstallTask` [#155](https://github.com/node-gradle/gradle-node-plugin/issues/155)
* Adds `yarn.lock` as an input to `NpmInstallTask` [#155](https://github.com/node-gradle/gradle-node-plugin/issues/155)

## Version 3.0.1 *(2021-02-09)*
* Fixes a bug with the task rule preventing it from being configured [#145](https://github.com/node-gradle/gradle-node-plugin/issues/145)
Expand Down Expand Up @@ -74,15 +81,15 @@ Here is what changed:
* `com.moowork.gradle.node` (and all children) renamed to `com.github.gradle.node`
* `com.moowork.gradle.node.npm` renamed to `com.github.gradle.node.npm.task`
* `com.moowork.gradle.node.yarn` renamed to `com.github.gradle.node.yarn.task`
* All the configuration properties (the `node` extension and all tasks) are now some
[lazy properties](https://docs.gradle.org/current/userguide/lazy_configuration.html#lazy_properties) as recommended by
Gradle. This makes this plugin fully compatible with lazy configuration (tasks will be configured only if they need to
* All the configuration properties (the `node` extension and all tasks) are now some
[lazy properties](https://docs.gradle.org/current/userguide/lazy_configuration.html#lazy_properties) as recommended by
Gradle. This makes this plugin fully compatible with lazy configuration (tasks will be configured only if they need to
run and configuration properties are read only at runtime if needed and not at configuration time).
* Thanks to the Kotlin rewrite, some properties now have a stronger typing.
* `nodeModulesDir` option was renamed to `nodeProjectDir` (name more explicit and less confusing)
(issue [#99](https://github.com/node-gradle/gradle-node-plugin/issues/99)). The former name still works but is
(issue [#99](https://github.com/node-gradle/gradle-node-plugin/issues/99)). The former name still works but is
deprecated.
* Change the syntax to configure `nodeModulesOutputFilter` on `npmInstall` and `yarn` tasks. It also affects Groovy DSL
* Change the syntax to configure `nodeModulesOutputFilter` on `npmInstall` and `yarn` tasks. It also affects Groovy DSL
users. Use now `nodeModulesOutputFilter { ... }` instead of `nodeModulesOutputFilter = { ... }`.

## Version 2.2.4 *(2020-05-18)*
Expand All @@ -91,7 +98,7 @@ users. Use now `nodeModulesOutputFilter { ... }` instead of `nodeModulesOutputFi

## Version 2.2.3 *(2020-02-28)*

The previous release (2.2.2) was released by error from the development branch which contains an entire Kotlin rewrite
The previous release (2.2.2) was released by error from the development branch which contains an entire Kotlin rewrite
of the plugin code and many backward compatibility breaks. This new version replaces the previous one and adds one fix.
* Make npm and npx symlinks relative. PR #68
* NpmSetupTask does not work when using separate http and https proxy settings #69
Expand All @@ -110,10 +117,10 @@ of the plugin code and many backward compatibility breaks. This new version repl
* `NodeTask`'s `script` now has relative path sensitivity (issue [#41](https://github.com/node-gradle/gradle-node-plugin/issues/41))
* No longer consider the working dir as an input for all tasks (issue [#40](https://github.com/node-gradle/gradle-node-plugin/issues/40))
* Explicitly exclude the `execOverrides` option of tasks from the inputs (issue [#40](https://github.com/node-gradle/gradle-node-plugin/issues/40))
* Add the ability to remove some files of the `node_modules` directory from the `NpmInstallTask` and `YarnInstallTask`
* Add the ability to remove some files of the `node_modules` directory from the `NpmInstallTask` and `YarnInstallTask`
outputs from the task output ; this is necessary when some tasks change some files of the `node_modules` directory ;
the `NpmInstallTask` and `YarnInstallTask` are never up-to-date in this case
(issue [#38](https://github.com/node-gradle/gradle-node-plugin/issues/38))
(issue [#38](https://github.com/node-gradle/gradle-node-plugin/issues/38))
* Deprecate the usage of `NodeTask` with a `script` which is a directory ; Node.js supports that and looks for an
`index.js` file in the directory but this is not compliant with a correct input/output declaration (issue [#41](https://github.com/node-gradle/gradle-node-plugin/issues/41))
* No longer use `Project.afterEvaluate` as a first step to support lazy tasks configuration (issue [#39](https://github.com/node-gradle/gradle-node-plugin/issues/39))
Expand All @@ -126,17 +133,17 @@ of the plugin code and many backward compatibility breaks. This new version repl
* Improve the inputs declarations of the `YarnTask`

## Version 2.1.0 *(2019-09-19)*
* Adds NpxTask for making use of https://www.npmjs.com/package/npx PR #32
* Improved up-to-date checks PR #32
* Adds NpxTask for making use of https://www.npmjs.com/package/npx PR #32
* Improved up-to-date checks PR #32
* Support ARM even if the JDK reports aarch64 #33
* Setting distBaseUrl to null disables repository adding PR #25
* Setting distBaseUrl to null disables repository adding PR #25

## Version 2.0.0 *(2019-07-29)*
* Only support Gradle 5.x officially.
* Drop support for grunt/gulp plugins.

## Version 1.5.1 *(2019-06-19)*
* Fix inputs/outputs for NpmInstallTask/YarnInstallTask.
* Fix inputs/outputs for NpmInstallTask/YarnInstallTask.

## Version 1.5.0 *(2019-06-19)*
* Backport from srs: Added gradle build cache support for npm install (bjornmagnusson)
Expand Down Expand Up @@ -195,7 +202,7 @@ Version 0.14 *(2016-11-29)*
Version 0.13 *(2016-06-27)*
---------------------------

* Bumped gradle wrapper version to 2.14
* Bumped gradle wrapper version to 2.14
* Implement ARM compatibility _(madmas)_
* Allow node modules to be used when calling npm_run _(jmcampanini)_
* Updated plugin versions and test versions
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![License](https://img.shields.io/github/license/node-gradle/gradle-node-plugin.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)
![Version](https://img.shields.io/badge/Version-4.0.0-orange.svg)

This plugin enables you to use a lot of [Node.js](https://nodejs.org)-based technologies as part of your
This plugin enables you to use a lot of [Node.js](https://nodejs.org)-based technologies as part of your
build without having Node.js installed locally on your system. It integrates the following Node.js-based system
with Gradle:

Expand All @@ -18,14 +18,14 @@ with Gradle:
The plugin is published in the [Gradle plugins portal](https://plugins.gradle.org/plugin/com.github.node-gradle.node)
with the `com.github.node-gradle.node` identifier.

It supports Gradle 7.5.1+ and Node.js 10+, for Gradle 5.6.4 support use version 3.x
It supports Gradle 6.6+ and Node.js 10+, for Gradle 5.6.4 support use version 3.x

## Documentation

⚠️ This is the documentation of the development version. See below in the releases history to read the
⚠️ This is the documentation of the development version. See below in the releases history to read the
documentation of the version you're using.

Here's how you get started using this plugin. If you do not find what you are looking for, please add an
Here's how you get started using this plugin. If you do not find what you are looking for, please add an
issue to [GitHub Issues](https://github.com/node-gradle/gradle-node-plugin/issues).

* [Installation](docs/installation.md)
Expand Down Expand Up @@ -88,7 +88,7 @@ To run the tests against all Gradle versions, use the following option (it is do

## Contributing

Contributions are always welcome! If you'd like to contribute (and we hope you do) please send
Contributions are always welcome! If you'd like to contribute (and we hope you do) please send
one of the existing contributors a nudge.

## <a name="support"></a> Support this project :heart:
Expand Down
7 changes: 4 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ repositories {
}

dependencies {
api("com.fasterxml.jackson.core:jackson-databind:2.13.2.2")
implementation("com.fasterxml.jackson.core:jackson-databind:2.14.2")
testImplementation(platform("org.junit:junit-bom:5.6.2"))
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
Expand Down Expand Up @@ -76,7 +76,8 @@ tasks.withType(Test::class) {
)

val processorsCount = Runtime.getRuntime().availableProcessors()
maxParallelForks = if (processorsCount > 2) processorsCount.div(2) else processorsCount
val safeMaxForks = if (processorsCount > 2) processorsCount.div(2) else processorsCount
maxParallelForks = safeMaxForks
testLogging {
events = setOf(TestLogEvent.SKIPPED, TestLogEvent.FAILED)
exceptionFormat = TestExceptionFormat.FULL
Expand Down Expand Up @@ -152,7 +153,7 @@ tasks.jacocoTestReport {
tasks.withType<org.jetbrains.dokka.gradle.DokkaTask>().configureEach {
dokkaSourceSets {
named("main") {
jdkVersion.set(8)
jdkVersion.set(compatibilityVersion.majorVersion.toInt())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class GradleVersionData {
.findAll { !it.rcFor || it.activeRc } // filter out inactive rcs
.findAll { !it.milestoneFor } // filter out milestones
.<String, VersionNumber, String>collectEntries { [(it.version): VersionNumber.parse(it.version as String)] }
.findAll { it.value.major >= 5 } // only 5.6 and above
.findAll { !(it.value.major == 5 && it.value.minor < 6) } // only 5.6 and above
.findAll { it.value.major >= 6 } // only 6.9 and above
.findAll { !(it.value.major == 6 && it.value.minor < 6.9) } // only 6.9 and above
.inject([] as List<Map.Entry<String, VersionNumber>>) { releasesToTest, version -> // only test against latest patch versions
if (!releasesToTest.any { it.value.major == version.value.major && it.value.minor == version.value.minor }) {
releasesToTest + version
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
org.gradle.unsafe.configuration-cache=true
systemProp.gradle.enterprise.testretry.enabled=false
systemProp.org.gradle.unsafe.kotlin.assignment=true
systemProp.org.gradle.unsafe.kotlin.assignment=true
14 changes: 13 additions & 1 deletion src/main/kotlin/com/github/gradle/node/NodeExtension.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.gradle.node

import com.github.gradle.node.npm.proxy.ProxySettings
import com.github.gradle.node.util.Platform
import org.gradle.api.Project
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.getByType
Expand Down Expand Up @@ -147,6 +148,17 @@ open class NodeExtension(project: Project) {
*/
val enableTaskRules = project.objects.property<Boolean>().convention(true)


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

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

init {
distBaseUrl.set("https://nodejs.org/dist")
}
Expand Down Expand Up @@ -180,7 +192,7 @@ open class NodeExtension(project: Project) {

@JvmStatic
fun create(project: Project): NodeExtension {
return project.extensions.create(NAME, project)
return project.extensions.create<NodeExtension>(NAME, project, )
}
}
}
60 changes: 45 additions & 15 deletions src/main/kotlin/com/github/gradle/node/NodePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@ import com.github.gradle.node.pnpm.task.PnpmSetupTask
import com.github.gradle.node.pnpm.task.PnpmTask
import com.github.gradle.node.task.NodeSetupTask
import com.github.gradle.node.task.NodeTask
import com.github.gradle.node.util.NodeVersionSource
import com.github.gradle.node.util.*
import com.github.gradle.node.variant.computeNodeArchiveDependency
import com.github.gradle.node.variant.computeNodeDir
import com.github.gradle.node.yarn.task.YarnInstallTask
import com.github.gradle.node.yarn.task.YarnSetupTask
import com.github.gradle.node.yarn.task.YarnTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.of
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.*
import org.gradle.util.GradleVersion
import java.io.ByteArrayOutputStream
import java.io.File

class NodePlugin : Plugin<Project> {
private lateinit var project: Project

override fun apply(project: Project) {
if (GradleVersion.current() < MINIMAL_SUPPORTED_GRADLE_VERSION) {
project.logger.error("This version of the plugin requires $MINIMAL_SUPPORTED_GRADLE_VERSION or newer.")
}
// if (GradleVersion.current() < MINIMAL_SUPPORTED_GRADLE_VERSION) {
// project.logger.error("This version of the plugin requires $MINIMAL_SUPPORTED_GRADLE_VERSION or newer.")
// }
this.project = project
val nodeExtension = NodeExtension.create(project)
configureNodeExtension(nodeExtension)
project.extensions.create<PackageJsonExtension>(PackageJsonExtension.NAME, project)
addGlobalTypes()
addTasks()
Expand All @@ -47,6 +48,38 @@ class NodePlugin : Plugin<Project> {
}
}

private fun configureNodeExtension(extension: NodeExtension) {
addPlatform(extension)
extension.computedNodeDir.set(computeNodeDir(extension))
extension.computedNodeDir.finalizeValueOnRead()
}

private fun addPlatform(extension: NodeExtension) {
val uname = {
if (GradleVersion.current() >= GradleVersion.version("7.5")) {
val cmd = project.providers.exec {
this.executable = "uname"
this.args = listOf("-m")
}
cmd.standardOutput.asText.get().trim()
} else {
val out = ByteArrayOutputStream()
val cmd = project.exec {
this.executable = "uname"
this.args = listOf("-m")
this.standardOutput = out
}

cmd.assertNormalExitValue()
out.toString().trim()
}
}
val name = System.getProperty("os.name")
val arch = System.getProperty("os.arch")
val platform = parsePlatform(name, arch, uname)
extension.computedPlatform.set(platform)
}

private fun addGlobalTypes() {
addGlobalType<NodeTask>()
addGlobalType<NpmTask>()
Expand Down Expand Up @@ -133,12 +166,9 @@ class NodePlugin : Plugin<Project> {
}

private fun configureNodeSetupTask(nodeExtension: NodeExtension) {
val versionSource = project.providers.of(NodeVersionSource::class) {
parameters.nodeVersion.set(nodeExtension.version)
}
project.tasks.named<NodeSetupTask>(NodeSetupTask.NAME) {
val nodeArchiveDependencyProvider = versionSource.get()
val archiveFileProvider = nodeArchiveDependencyProvider
project.tasks.withType<NodeSetupTask>().configureEach {
nodeDir.set(nodeExtension.computedNodeDir)
val archiveFileProvider = computeNodeArchiveDependency(nodeExtension)
.map { nodeArchiveDependency ->
resolveNodeArchiveFile(nodeArchiveDependency)
}
Expand All @@ -154,7 +184,7 @@ class NodePlugin : Plugin<Project> {
}

companion object {
val MINIMAL_SUPPORTED_GRADLE_VERSION: GradleVersion = GradleVersion.version("7.5.1")
val MINIMAL_SUPPORTED_GRADLE_VERSION: GradleVersion = GradleVersion.version("6.6")
const val NODE_GROUP = "Node"
const val NPM_GROUP = "npm"
const val PNPM_GROUP = "pnpm"
Expand Down
19 changes: 13 additions & 6 deletions src/main/kotlin/com/github/gradle/node/exec/NodeExecRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@ import com.github.gradle.node.NodeExtension
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 org.gradle.api.file.Directory
import org.gradle.api.provider.Provider
import org.gradle.process.ExecResult

/**
* This function is responsible for setting up the configuration used when running the tasks.
*/
fun buildExecConfiguration(nodeExtension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration, variantComputer: VariantComputer):
Provider<ExecConfiguration> {
val nodeDirProvider = variantComputer.computeNodeDir(nodeExtension)
fun buildExecConfiguration(
nodeExtension: NodeExtension,
nodeExecConfiguration: NodeExecConfiguration,
variantComputer: VariantComputer
):
Provider<ExecConfiguration> {
val nodeDirProvider = nodeExtension.computedNodeDir
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
val executableProvider = variantComputer.computeNodeExec(nodeExtension, nodeBinDirProvider)
val executableProvider = computeNodeExec(nodeExtension, nodeBinDirProvider)
val additionalBinPathProvider = computeAdditionalBinPath(nodeExtension, nodeBinDirProvider)
return zip(executableProvider, additionalBinPathProvider)
.map { (executable, additionalBinPath) ->
ExecConfiguration(executable, nodeExecConfiguration.command, additionalBinPath,
ExecConfiguration(
executable, nodeExecConfiguration.command, additionalBinPath,
nodeExecConfiguration.environment, nodeExecConfiguration.workingDir,
nodeExecConfiguration.ignoreExitValue, nodeExecConfiguration.execOverrides)
nodeExecConfiguration.ignoreExitValue, nodeExecConfiguration.execOverrides
)
}
}

Expand Down
Loading

0 comments on commit 3ea8a87

Please sign in to comment.