Skip to content

Commit

Permalink
Add option for disabling task rules #116
Browse files Browse the repository at this point in the history
  • Loading branch information
deepy committed Feb 17, 2023
1 parent 946ba86 commit ab81294
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Version 3.x *(unreleased)*

## Version 3.6.0 *(unreleased)*
* Allow task rules to be disabled [#116](https://github.com/node-gradle/gradle-node-plugin/issues/116)

## Version 3.5.1 *(2022-12-26)*
* Fix configuration cache support in pnpm

Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/github/gradle/node/NodeExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ open class NodeExtension(project: Project) {
@Deprecated("Deprecated in version 3.0, please use nodeProjectDir now")
val nodeModulesDir = nodeProjectDir

/**
* Create rules for automatic task creation
*
* Disabling this will prevent the npm_ npx_ yarn_ pnpm_ tasks from being
* automatically created.
* It's recommended to turn this off after you've gotten comfortable
* with the plugin and register your own tasks instead of relying on the rule.
*/
val enableTaskRules = project.objects.property<Boolean>().convention(true)

init {
distBaseUrl.set("https://nodejs.org/dist")
}
Expand Down
19 changes: 10 additions & 9 deletions src/main/kotlin/com/github/gradle/node/NodePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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.register
Expand All @@ -30,9 +31,9 @@ class NodePlugin : Plugin<Project> {
project.extensions.create<PackageJsonExtension>(PackageJsonExtension.NAME, project)
addGlobalTypes()
addTasks()
addNpmRule()
addPnpmRule()
addYarnRule()
addNpmRule(nodeExtension.enableTaskRules)
addPnpmRule(nodeExtension.enableTaskRules)
addYarnRule(nodeExtension.enableTaskRules)
project.afterEvaluate {
if (nodeExtension.download.get()) {
nodeExtension.distBaseUrl.orNull?.let { addRepository(it, nodeExtension.allowInsecureProtocol.orNull) }
Expand Down Expand Up @@ -64,10 +65,10 @@ class NodePlugin : Plugin<Project> {
project.tasks.register<YarnSetupTask>(YarnSetupTask.NAME)
}

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

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

private fun addYarnRule() { // note this rule also makes it possible to specify e.g. "dependsOn yarn_install"
private fun addYarnRule(enableTaskRules: Property<Boolean>) { // note this rule also makes it possible to specify e.g. "dependsOn yarn_install"
project.tasks.addRule("Pattern: \"yarn_<command>\": Executes an Yarn command.") {
val taskName = this
if (taskName.startsWith("yarn_")) {
if (taskName.startsWith("yarn_") && enableTaskRules.get()) {
project.tasks.create<YarnTask>(taskName) {
val tokens = taskName.split("_").drop(1) // all except first
yarnCommand.set(tokens)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ class NpmRule_integTest extends AbstractIntegTest {
gv << GRADLE_VERSIONS_UNDER_TEST
}
def 'rules can be disabled (#gv.version)'() {
given:
gradleVersion = gv
writeBuild('''
plugins {
id 'com.github.node-gradle.node'
}

node {
enableTaskRules = false
}
''')
writeEmptyPackageJson()
when:
def result = buildAndFail('npm_install')
then:
result.output.contains("Task 'npm_install' not found")
where:
gv << GRADLE_VERSIONS_UNDER_TEST
}
def 'can configure npm_ rule task (#gv.version)'() {
given:
gradleVersion = gv
Expand Down

0 comments on commit ab81294

Please sign in to comment.