Skip to content

Commit

Permalink
Merge pull request #179 from s0x/fix-path-issues
Browse files Browse the repository at this point in the history
Set PATH variable for node, npm and yarn
  • Loading branch information
Sten Roger Sandvik authored Jan 13, 2017
2 parents dbf3822 + f480150 commit 1039495
Show file tree
Hide file tree
Showing 12 changed files with 436 additions and 79 deletions.
4 changes: 2 additions & 2 deletions src/main/groovy/com/moowork/gradle/node/NodePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ class NodePlugin

private void configureNpmSetupTask()
{
this.npmSetupTask.configureNpmVersion( this.config.npmVersion )
this.npmSetupTask.configureVersion( this.config.npmVersion )
}

private void configureYarnSetupTask()
{
this.yarnSetupTask.configureYarnVersion( this.config.yarnVersion )
this.yarnSetupTask.configureVersion( this.config.yarnVersion )
}
}
56 changes: 31 additions & 25 deletions src/main/groovy/com/moowork/gradle/node/npm/NpmExecRunner.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,41 @@ class NpmExecRunner
@Override
protected ExecResult doExecute()
{
if ( this.ext.npmCommand == 'npm' && this.ext.variant.windows ) {
this.ext.npmCommand = 'npm.cmd'
}

if ( !this.ext.download )
def exec = this.variant.npmExec
def arguments = this.arguments

if ( this.ext.download )
{
return run( this.ext.npmCommand, this.arguments )
}
def npmBinDir = this.variant.npmBinDir.getAbsolutePath();

def String npmScriptFile = this.variant.npmScriptFile
def File localNpm = project.file( new File( this.ext.nodeModulesDir, 'node_modules/npm/bin/npm-cli.js' ) )
def File workNpm = project.file( new File( this.ext.npmWorkDir, 'node_modules/npm/bin/npm-cli.js' ) )
def nodeBinDir = this.variant.nodeBinDir.getAbsolutePath();

// Use npm specified by user if available
if ( workNpm.exists() )
{
npmScriptFile = workNpm.absolutePath
}
// Use locally-installed npm if available
else if ( localNpm.exists() ) {
npmScriptFile = localNpm.absolutePath
}
def path = npmBinDir + File.pathSeparator + nodeBinDir;

def runner = new NodeExecRunner( this.project )
runner.arguments = [npmScriptFile] + this.arguments
runner.environment = this.environment
runner.workingDir = this.workingDir
runner.execOverrides = this.execOverrides
runner.ignoreExitValue = this.ignoreExitValue
return runner.execute()
// Take care of Windows environments that may contain "Path" OR "PATH" - both existing
// possibly (but not in parallel as of now)
if ( environment['Path'] != null )
{
environment['Path'] = path + File.pathSeparator + environment['Path']
}
else
{
environment['PATH'] = path + File.pathSeparator + environment['PATH']
}

def File localNpm = project.file( new File( this.ext.nodeModulesDir, 'node_modules/npm/bin/npm-cli.js' ) )
if ( localNpm.exists() )
{
exec = this.variant.nodeExec
arguments = [localNpm.absolutePath] + arguments
}
else if ( !new File(exec).exists() )
{
exec = this.variant.nodeExec
arguments = [this.variant.npmScriptFile] + arguments
}
}
return run( exec, arguments )
}
}
81 changes: 73 additions & 8 deletions src/main/groovy/com/moowork/gradle/node/npm/NpmSetupTask.groovy
Original file line number Diff line number Diff line change
@@ -1,33 +1,98 @@
package com.moowork.gradle.node.npm

import com.moowork.gradle.node.task.SetupTask
import com.moowork.gradle.node.NodeExtension
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.process.ExecResult

/**
* npm install that only gets executed if gradle decides so.
**/
class NpmSetupTask
extends NpmTask
extends DefaultTask
{
public final static String NAME = 'npmSetup'

private NodeExtension config

protected Iterable<?> args = []

private ExecResult result

public NpmSetupTask()
{
dependsOn( SetupTask.NAME )

this.group = 'Node'
this.description = 'Setup a specific version of npm to be used by the build.'
this.enabled = false
}

@Input
public Set<String> getInput()
{
def set = new HashSet<>()
set.add( getConfig().download )
set.add( getConfig().npmVersion )
set.add( getConfig().npmWorkDir )
return set
}

this.project.afterEvaluate {
getOutputs().dir( this.project.node.npmWorkDir )
setWorkingDir( this.project.node.nodeModulesDir )
@OutputDirectory
public File getNpmDir()
{
return getVariant().npmDir
}

@Internal
ExecResult getResult()
{
return this.result
}

@Internal
protected getConfig()
{
if ( this.config != null)
{
return this.config
}

this.config = NodeExtension.get( this.project )
return this.config
}

@Internal
protected getVariant()
{
return getConfig().variant
}

@Internal
void setArgs( final Iterable<?> value )
{
this.args = value
}

@TaskAction
void exec() {
def runner = new NpmExecRunner( this.project )
runner.arguments.addAll( this.args )

this.result = runner.execute()
}

void configureNpmVersion( String npmVersion )
void configureVersion( String npmVersion )
{
if ( !npmVersion.isEmpty() )
{
logger.debug( "Setting npmVersion to ${npmVersion}" )
setArgs( ['install', '--prefix', this.project.node.npmWorkDir, "npm@${npmVersion}"] )
setEnabled( true )
getInputs().property( 'npmVersion', npmVersion )
setArgs( ['install', '--global', '--prefix', getVariant().npmDir, "npm@${npmVersion}"] )
enabled = true
}
}
}
3 changes: 1 addition & 2 deletions src/main/groovy/com/moowork/gradle/node/npm/NpmTask.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.moowork.gradle.node.npm

import com.moowork.gradle.node.task.SetupTask
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction
Expand All @@ -20,7 +19,7 @@ class NpmTask
public NpmTask()
{
this.runner = new NpmExecRunner( this.project )
dependsOn( SetupTask.NAME )
dependsOn( NpmSetupTask.NAME )

this.project.afterEvaluate {
if ( !this.runner.workingDir )
Expand Down
24 changes: 21 additions & 3 deletions src/main/groovy/com/moowork/gradle/node/variant/Variant.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,33 @@ class Variant
{
def boolean windows

/* Node */

def String nodeExec

def String npmScriptFile

def File nodeDir

def File nodeBinDir

/* NPM */

def String npmExec

def File npmDir

def File nodeBinDir
def File npmBinDir

def String nodeExec
/* Yarn */

def String npmScriptFile
def String yarnExec

def File yarnDir

def File yarnBinDir

/* Dependencies */

def String archiveDependency

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,28 @@ class VariantBuilder

def variant = new Variant()
variant.windows = platformHelper.isWindows()

variant.nodeDir = getNodeDir( osName, osArch )
variant.npmDir = ext.npmVersion ? getNpmDir() : variant.nodeDir
variant.yarnDir = getYarnDir()

variant.nodeBinDir = variant.nodeDir
variant.npmBinDir = variant.npmDir
variant.yarnBinDir = variant.yarnDir

variant.nodeExec = "node"
variant.npmExec = this.ext.npmCommand
variant.yarnExec = this.ext.yarnCommand

if ( variant.windows )
{
variant.nodeBinDir = variant.nodeDir
if (variant.npmExec == 'npm') {
variant.npmExec = 'npm.cmd'
}
if (variant.yarnExec == 'yarn') {
variant.yarnExec = 'yarn.cmd'
}

if (hasWindowsZip())
{
variant.archiveDependency = getArchiveDependency( osName, osArch, 'zip' )
Expand All @@ -41,18 +58,28 @@ class VariantBuilder
variant.archiveDependency = getArchiveDependency( 'linux', 'x86', 'tar.gz' )
variant.exeDependency = getExeDependency()
}
variant.npmDir = new File( variant.nodeBinDir, 'node_modules' )
variant.nodeExec = new File( variant.nodeBinDir, 'node.exe' ).absolutePath
variant.npmScriptFile = new File( variant.nodeDir , 'node_modules/npm/bin/npm-cli.js')
}
else
{
variant.nodeBinDir = new File( variant.nodeDir, 'bin' )
variant.nodeBinDir = new File( variant.nodeBinDir, 'bin' )
variant.npmBinDir = new File( variant.npmBinDir, 'bin' )
variant.yarnBinDir = new File( variant.yarnBinDir, 'bin' )
variant.archiveDependency = getArchiveDependency( osName, osArch, 'tar.gz' )
variant.npmDir = new File( variant.nodeDir, 'lib/node_modules' )
variant.nodeExec = new File( variant.nodeBinDir, 'node' ).absolutePath
variant.npmScriptFile = new File( variant.nodeDir , 'lib/node_modules/npm/bin/npm-cli.js')
}

if (this.ext.download)
{
if (variant.nodeExec == "node" && variant.windows) {
variant.nodeExec = "node.exe"
}

variant.nodeExec = new File( variant.nodeBinDir, variant.nodeExec ).absolutePath
variant.npmExec = new File( variant.npmBinDir, variant.npmExec ).absolutePath
variant.yarnExec = new File( variant.yarnBinDir, variant.yarnExec ).absolutePath
}

variant.npmScriptFile = new File( variant.npmDir, 'npm/bin/npm-cli.js' ).absolutePath
return variant
}

Expand Down Expand Up @@ -117,4 +144,24 @@ class VariantBuilder
def dirName = "node-v${version}-${osName}-${osArch}"
return new File( this.ext.workDir, dirName )
}

private File getNpmDir()
{
def version = this.ext.npmVersion
return new File(this.ext.npmWorkDir, "npm-v${version}")
}

private File getYarnDir()
{
def dirname = "yarn"
if (this.ext.yarnVersion)
{
dirname += "-v${this.ext.yarnVersion}"
}
else
{
dirname += "-latest"
}
return new File( this.ext.yarnWorkDir, dirname )
}
}
34 changes: 20 additions & 14 deletions src/main/groovy/com/moowork/gradle/node/yarn/YarnExecRunner.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@ class YarnExecRunner
@Override
protected ExecResult doExecute()
{
if ( this.ext.yarnCommand == 'yarn' && this.ext.variant.windows ) {
this.ext.yarnCommand = 'yarn.cmd'
}

if ( !this.ext.download )
if ( this.ext.download )
{
return run( this.ext.yarnCommand, this.arguments )
def yarnBinDir = this.variant.yarnBinDir.getAbsolutePath();

def npmBinDir = this.variant.npmBinDir.getAbsolutePath();

def nodeBinDir = this.variant.nodeBinDir.getAbsolutePath();

def path = yarnBinDir + File.pathSeparator + npmBinDir + File.pathSeparator + nodeBinDir;

// Take care of Windows environments that may contain "Path" OR "PATH" - both existing
// possibly (but not in parallel as of now)
if ( this.environment['Path'] != null )
{
this.environment['Path'] = path + File.pathSeparator + this.environment['Path']
}
else
{
this.environment['PATH'] = path + File.pathSeparator + this.environment['PATH']
}
}

def String yarnScriptFile = "${this.project.node.yarnWorkDir}/node_modules/yarn/bin/yarn.js"
def runner = new NodeExecRunner( this.project )
runner.arguments = [yarnScriptFile] + this.arguments
runner.environment = this.environment
runner.workingDir = this.workingDir
runner.execOverrides = this.execOverrides
runner.ignoreExitValue = this.ignoreExitValue
return runner.execute()
return run( this.variant.yarnExec, this.arguments )
}
}
Loading

0 comments on commit 1039495

Please sign in to comment.