Skip to content

Commit ab81294

Browse files
committed
Add option for disabling task rules #116
1 parent 946ba86 commit ab81294

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Version 3.x *(unreleased)*
44

5+
## Version 3.6.0 *(unreleased)*
6+
* Allow task rules to be disabled [#116](https://github.com/node-gradle/gradle-node-plugin/issues/116)
7+
58
## Version 3.5.1 *(2022-12-26)*
69
* Fix configuration cache support in pnpm
710

src/main/kotlin/com/github/gradle/node/NodeExtension.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ open class NodeExtension(project: Project) {
137137
@Deprecated("Deprecated in version 3.0, please use nodeProjectDir now")
138138
val nodeModulesDir = nodeProjectDir
139139

140+
/**
141+
* Create rules for automatic task creation
142+
*
143+
* Disabling this will prevent the npm_ npx_ yarn_ pnpm_ tasks from being
144+
* automatically created.
145+
* It's recommended to turn this off after you've gotten comfortable
146+
* with the plugin and register your own tasks instead of relying on the rule.
147+
*/
148+
val enableTaskRules = project.objects.property<Boolean>().convention(true)
149+
140150
init {
141151
distBaseUrl.set("https://nodejs.org/dist")
142152
}

src/main/kotlin/com/github/gradle/node/NodePlugin.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.github.gradle.node.yarn.task.YarnSetupTask
1515
import com.github.gradle.node.yarn.task.YarnTask
1616
import org.gradle.api.Plugin
1717
import org.gradle.api.Project
18+
import org.gradle.api.provider.Property
1819
import org.gradle.kotlin.dsl.create
1920
import org.gradle.kotlin.dsl.named
2021
import org.gradle.kotlin.dsl.register
@@ -30,9 +31,9 @@ class NodePlugin : Plugin<Project> {
3031
project.extensions.create<PackageJsonExtension>(PackageJsonExtension.NAME, project)
3132
addGlobalTypes()
3233
addTasks()
33-
addNpmRule()
34-
addPnpmRule()
35-
addYarnRule()
34+
addNpmRule(nodeExtension.enableTaskRules)
35+
addPnpmRule(nodeExtension.enableTaskRules)
36+
addYarnRule(nodeExtension.enableTaskRules)
3637
project.afterEvaluate {
3738
if (nodeExtension.download.get()) {
3839
nodeExtension.distBaseUrl.orNull?.let { addRepository(it, nodeExtension.allowInsecureProtocol.orNull) }
@@ -64,10 +65,10 @@ class NodePlugin : Plugin<Project> {
6465
project.tasks.register<YarnSetupTask>(YarnSetupTask.NAME)
6566
}
6667

67-
private fun addNpmRule() { // note this rule also makes it possible to specify e.g. "dependsOn npm_install"
68+
private fun addNpmRule(enableTaskRules: Property<Boolean>) { // note this rule also makes it possible to specify e.g. "dependsOn npm_install"
6869
project.tasks.addRule("Pattern: \"npm_<command>\": Executes an NPM command.") {
6970
val taskName = this
70-
if (taskName.startsWith("npm_")) {
71+
if (taskName.startsWith("npm_") && enableTaskRules.get()) {
7172
project.tasks.create<NpmTask>(taskName) {
7273
val tokens = taskName.split("_").drop(1) // all except first
7374
npmCommand.set(tokens)
@@ -79,10 +80,10 @@ class NodePlugin : Plugin<Project> {
7980
}
8081
}
8182

82-
private fun addPnpmRule() { // note this rule also makes it possible to specify e.g. "dependsOn npm_install"
83+
private fun addPnpmRule(enableTaskRules: Property<Boolean>) { // note this rule also makes it possible to specify e.g. "dependsOn npm_install"
8384
project.tasks.addRule("Pattern: \"pnpm_<command>\": Executes an PNPM command.") {
8485
val taskName = this
85-
if (taskName.startsWith("pnpm_")) {
86+
if (taskName.startsWith("pnpm_") && enableTaskRules.get()) {
8687
project.tasks.register<PnpmTask>(taskName) {
8788
val tokens = taskName.split("_").drop(1) // all except first
8889
pnpmCommand.set(tokens)
@@ -94,10 +95,10 @@ class NodePlugin : Plugin<Project> {
9495
}
9596
}
9697

97-
private fun addYarnRule() { // note this rule also makes it possible to specify e.g. "dependsOn yarn_install"
98+
private fun addYarnRule(enableTaskRules: Property<Boolean>) { // note this rule also makes it possible to specify e.g. "dependsOn yarn_install"
9899
project.tasks.addRule("Pattern: \"yarn_<command>\": Executes an Yarn command.") {
99100
val taskName = this
100-
if (taskName.startsWith("yarn_")) {
101+
if (taskName.startsWith("yarn_") && enableTaskRules.get()) {
101102
project.tasks.create<YarnTask>(taskName) {
102103
val tokens = taskName.split("_").drop(1) // all except first
103104
yarnCommand.set(tokens)

src/test/groovy/com/github/gradle/node/npm/task/NpmRule_integTest.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@ class NpmRule_integTest extends AbstractIntegTest {
2727
gv << GRADLE_VERSIONS_UNDER_TEST
2828
}
2929
30+
def 'rules can be disabled (#gv.version)'() {
31+
given:
32+
gradleVersion = gv
33+
writeBuild('''
34+
plugins {
35+
id 'com.github.node-gradle.node'
36+
}
37+
38+
node {
39+
enableTaskRules = false
40+
}
41+
''')
42+
writeEmptyPackageJson()
43+
44+
when:
45+
def result = buildAndFail('npm_install')
46+
47+
then:
48+
result.output.contains("Task 'npm_install' not found")
49+
50+
where:
51+
gv << GRADLE_VERSIONS_UNDER_TEST
52+
}
53+
3054
def 'can configure npm_ rule task (#gv.version)'() {
3155
given:
3256
gradleVersion = gv

0 commit comments

Comments
 (0)