Skip to content

Commit

Permalink
Remove the usage of Project.afterEvaluate in the plugin tasks. Issue #39
Browse files Browse the repository at this point in the history
.
  • Loading branch information
bsautel committed Oct 7, 2019
1 parent 1b842f6 commit 6efe0f4
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 62 deletions.
8 changes: 0 additions & 8 deletions src/main/groovy/com/moowork/gradle/node/NodePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,10 @@ class NodePlugin
private void addNpmRule()
{
// note this rule also makes it possible to specify e.g. "dependsOn npm_install"
def workingDir
this.project.afterEvaluate {
workingDir = this.project.node.nodeModulesDir
}
project.getTasks().addRule( 'Pattern: "npm_<command>": Executes an NPM command.' ) { String taskName ->
if ( taskName.startsWith( "npm_" ) )
{
NpmTask npmTask = project.getTasks().create( taskName, NpmTask.class )
if ( workingDir )
{
npmTask.afterEvaluate( workingDir )
}

String[] tokens = taskName.split( '_' ).tail() // all except first
npmTask.npmCommand = tokens
Expand Down
17 changes: 12 additions & 5 deletions src/main/groovy/com/moowork/gradle/node/exec/ExecRunner.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,13 @@ abstract class ExecRunner
def realExec = exec
def realArgs = args
def execEnvironment = computeExecEnvironment()
def execWorkingDir = computeWorkingDir()
return this.project.exec( {
it.executable = realExec
it.args = realArgs
it.environment = execEnvironment
it.ignoreExitValue = this.ignoreExitValue

if ( this.workingDir != null )
{
it.workingDir = this.workingDir
}
it.workingDir = execWorkingDir

if ( this.execOverrides != null )
{
Expand All @@ -72,6 +69,16 @@ abstract class ExecRunner
} )
}

private File computeWorkingDir()
{
File workingDir = this.workingDir != null ? this.workingDir : this.project.node.nodeModulesDir
if (!workingDir.exists())
{
workingDir.mkdirs()
}
return workingDir
}

private Map<Object, Object> computeExecEnvironment()
{
def environment = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class NodeExecRunner
{
if (this.ext.download)
{
return this.variant.nodeBinDir.getAbsolutePath()
return this.variant.nodeBinDir.getAbsolutePath()
}
return null
}
Expand Down
16 changes: 0 additions & 16 deletions src/main/groovy/com/moowork/gradle/node/npm/NpmTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,6 @@ class NpmTask
this.group = NodePlugin.NODE_GROUP
this.runner = new NpmExecRunner( this.project )
dependsOn( NpmSetupTask.NAME )

this.project.afterEvaluate {
afterEvaluate( this.project.node.nodeModulesDir )
}
}

void afterEvaluate(nodeModulesDir) {
if ( !this.runner.workingDir )
{
setWorkingDir( nodeModulesDir )
}

if ( !this.runner.workingDir.exists() )
{
this.runner.workingDir.mkdirs();
}
}

void setArgs( final Iterable<?> value )
Expand Down
16 changes: 0 additions & 16 deletions src/main/groovy/com/moowork/gradle/node/npm/NpxTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ class NpxTask
this.group = NodePlugin.NODE_GROUP
this.runner = new NpxExecRunner( this.project )
dependsOn( NpmSetupTask.NAME )

this.project.afterEvaluate {
afterEvaluate( this.project.node.nodeModulesDir )
}
}

void afterEvaluate(nodeModulesDir) {
if ( !this.runner.workingDir )
{
setWorkingDir( nodeModulesDir )
}

if ( !this.runner.workingDir.exists() )
{
this.runner.workingDir.mkdirs();
}
}

void setArgs( final Iterable<?> value )
Expand Down
13 changes: 0 additions & 13 deletions src/main/groovy/com/moowork/gradle/node/yarn/YarnTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ class YarnTask
this.group = NodePlugin.NODE_GROUP
this.runner = new YarnExecRunner( this.project )
dependsOn( YarnSetupTask.NAME )

this.project.afterEvaluate {
if ( !this.runner.workingDir )
{
def workingDir = this.project.node.nodeModulesDir
setWorkingDir( workingDir )
}

if ( !this.runner.workingDir.exists() )
{
this.runner.workingDir.mkdirs();
}
}
}

void setArgs( final Iterable<?> value )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,6 @@ class NpmTask_integTest
result8.task(":pwd").outcome == TaskOutcome.SUCCESS
def expectedWorkingDirectory = "${projectDir}${File.separator}build${File.separator}customWorkingDirectory"
result8.output.contains("Working directory is '${expectedWorkingDirectory}'")
new File(expectedWorkingDirectory).isDirectory()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,6 @@ class NpxTask_integTest
result8.task(":pwd").outcome == TaskOutcome.SUCCESS
def expectedWorkingDirectory = "${projectDir}${File.separator}build${File.separator}customWorkingDirectory"
result8.output.contains("Working directory is '${expectedWorkingDirectory}'")
new File(expectedWorkingDirectory).isDirectory()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@ class NodeTask_integTest
result9.task(":env").outcome == TaskOutcome.UP_TO_DATE

when:
def result10 = buildAndFail("env", "-DchangeWorkingDir=true", "--rerun-tasks")
def result10 = build("env", "-DchangeWorkingDir=true", "--rerun-tasks")

then:
result10.task(":nodeSetup").outcome == TaskOutcome.SUCCESS
result10.task(":env").outcome == TaskOutcome.FAILED
result10.output.contains("A problem occurred starting process")
result10.task(":env").outcome == TaskOutcome.SUCCESS
def expectedWorkingDir = "${projectDir}${File.separator}build${File.separator}notExisting"
result10.output.contains("Current working directory: ${expectedWorkingDir}")
new File(expectedWorkingDir).isDirectory()

when:
// Reset build arguments to ensure the next change is not up-to-date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,6 @@ class YarnTask_integTest
result8.task(":pwd").outcome == TaskOutcome.SUCCESS
def expectedWorkingDirectory = "${projectDir}${File.separator}build${File.separator}customWorkingDirectory"
result8.output.contains("Working directory is '${expectedWorkingDirectory}'")
new File(expectedWorkingDirectory).isDirectory()
}
}
2 changes: 2 additions & 0 deletions src/test/resources/fixtures/node-env/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ if (customEnvironmentVariable) {
} else {
console.log('No custom environment');
}

console.log(`Current working directory: ${process.cwd()}`);

0 comments on commit 6efe0f4

Please sign in to comment.