Skip to content

Commit

Permalink
FIXED ysb33r#4. A make task can now set the environment under which m…
Browse files Browse the repository at this point in the history
…ake is executed
  • Loading branch information
ysb33r committed May 7, 2015
1 parent 6d08462 commit 8a537c0
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ apply plugin : 'org.ysb33r.gradletest'

group = 'org.ysb33r.gradle'
archivesBaseName = 'gnumake'
version = '1.0.1'
version = '1.0.2-SNAPSHOT'

sourceCompatibility = 1.6
targetCompatibility = 1.6
Expand Down
4 changes: 4 additions & 0 deletions src/gradleTest/basicTest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ gnumake {
executable "${projectDir}/${OperatingSystem.current().isWindows() ? 'make.bat' : 'make.sh'}"
}

make {
environment foo : 'bar'
}

task runGradleTest {
dependsOn makeClean
}
65 changes: 53 additions & 12 deletions src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuild.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class GnuMakeBuild extends DefaultTask {
@Input
boolean alwaysMake = false

/** Tell make that varibales from the environment takes precedence over variables defined
/** Tell make that variables from the environment takes precedence over variables defined
* inside makefile.
* This is equivalent of passing -e to make
*/
Expand Down Expand Up @@ -156,6 +156,46 @@ class GnuMakeBuild extends DefaultTask {
this.flags+= a
}

/** Returns the map of environment variables that will be added to the environment that make be executed within.
*
* @return A map of environment variables or null
* @since 1.0.2
*/
@Input
@Optional
@CompileDynamic
Map getEnvironment() {
this.environment
}

/** Resets the existing environment variables that is to be added to make environment.
* @code
* setEnvironment INCPATH : '/foo/bar'
* @endcode
* @since 1.0.2
*/
void setEnvironment(Map a) {
if(this.environment != null) {
this.environment.clear()
} else {
this.environment = [:]
}
}

/** Appends variables to the make environment.
* @code
* environment INCPATH : '/foo/bar', LIBPATH : '/lib/path'
* @endcode
* @since 1.0.2
*/
void environment(Map a) {
if(this.environment != null) {
this.environment+= a
} else {
this.environment= a
}
}

/** Arbitrary GNU Make command-line switches to pass. This allows flexibility to
* pass anything above and beyond basic functionality already supported in this
* task class. Supplied switches are passed as the last items on the command-line to
Expand Down Expand Up @@ -355,17 +395,6 @@ class GnuMakeBuild extends DefaultTask {
cmdargs
}

@TaskAction
@CompileDynamic
void exec() {

if(!executor) {
executor = new MakeExecutor(project)
}
buildCmdArgs()
execResult = executor.runMake(getExecutable(),cmdArgs,getWorkingDir())
}

@Deprecated
void setTasks(Object... targets_) {
logger.warn "'tasks/setTasks' is deprecated. Please use 'targets/setTargets' instead."
Expand Down Expand Up @@ -414,6 +443,17 @@ class GnuMakeBuild extends DefaultTask {
cmdargs = TaskUtils.buildCmdArgs(project,this,targets)
}

@TaskAction
@CompileDynamic
void exec() {

if(!executor) {
executor = new MakeExecutor(project)
}
buildCmdArgs()
execResult = executor.runMake(getExecutable(),cmdArgs,getWorkingDir(),environment)
}



private String executable
Expand All @@ -428,6 +468,7 @@ class GnuMakeBuild extends DefaultTask {
private List<String> cmdargs = []
private InputOutputMonitor inMonitor = new InputOutputMonitor(this,'inputs')
private InputOutputMonitor outMonitor = new InputOutputMonitor(this,'outputs')
private Map environment

Executor executor
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import org.gradle.process.ExecResult
* @author Schalk W. Cronjé
*/
interface Executor {
ExecResult runMake(final String exec,final List<String> cmdargs,final File wd)
ExecResult runMake(final String exec,final List<String> cmdargs,final File wd,final Map environment)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.gradle.process.ExecResult
class MakeExecutor implements Executor {
Project project

ExecResult runMake(final String exec,final List<String> cmdargs,final File wd=null) {
TaskUtils.runMake(project,exec,cmdargs,wd)
ExecResult runMake(final String exec,final List<String> cmdargs,final File wd=null,final Map environment=null) {
TaskUtils.runMake(project,exec,cmdargs,wd,environment)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TaskUtils {
execArgs + switches + includes + targets + flags + task.switches
}

static ExecResult runMake(Project project,final String exec,final List<String> cmdargs,final File wd=null) {
static ExecResult runMake(Project project,final String exec,final List<String> cmdargs,final File wd=null,Map env = null) {
project.exec {

executable = exec
Expand All @@ -57,6 +57,10 @@ class TaskUtils {
workingDir = wd
}

if(env) {
environment env
}

args = cmdargs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TrackerTask extends DefaultTask {
executor = new MakeExecutor(project)
}
def cmdargs = TaskUtils.buildCmdArgs(project,tracks,[target])
execResult = executor.runMake(tracks.executable,cmdargs,tracks.workingDir)
execResult = executor.runMake(tracks.executable,cmdargs,tracks.workingDir,tracks.environment)

}

Expand Down
28 changes: 23 additions & 5 deletions src/test/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuildSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ class GnuMakeBuildSpec extends spock.lang.Specification {
systemOut = null
}

def "Newly created Task will set executable to OS-specific value"() {
expect:
def "Defaults on newly created Task"() {

expect: 'executable to OS-specific value'
gnumake.executable == 'make'
}

def "Newly created Task will have empty command line"() {
expect:
and: 'empty command line'
gnumake.cmdArgs.size() == 0

and: 'no additional environment'
gnumake.environment == null
}

def "alwaysMake adds -B" () {
Expand Down Expand Up @@ -342,7 +344,23 @@ class GnuMakeBuildSpec extends spock.lang.Specification {
!project.tasks.make.inputs.files.isEmpty()
project.tasks.make.inputs.files.files.contains(propsFile)
!project.tasks.make.outputs.files.isEmpty()
}

def "Modifying the environment"() {
given:
project.allprojects {
make {
environment INCDIR : '/foo/path'
environment LIBDIR : '/foo/lib'
}
}
project.evaluate()
def env = project.tasks.make.environment

expect:
env.size() == 2
env.INCDIR == '/foo/path'
env.LIBDIR == '/foo/lib'
}
}

4 changes: 3 additions & 1 deletion src/test/groovy/org/ysb33r/gradle/gnumake/RunMakeSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class RunMakeSpec extends Specification {
make {
flags DESTDIR : 'foo/bar'
targets 'build','install'
environment 'INCAPATH' : 'PERU'
}
}

Expand All @@ -55,6 +56,7 @@ class RunMakeSpec extends Specification {
String output = systemOut.toString()

expect:
output.contains('fake-make build install DESTDIR=foo/bar') || output.contains('fake-make.bat build install DESTDIR=foo/bar')
output.contains('fake-make build install DESTDIR=foo/bar') || output.contains('fake-make.bat build install DESTDIR=foo/bar')
output.contains('INCAPATH=PERU')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ class FakeExecutor implements Executor {
String executable
List<String> cmdargs
File workingDir
Map environment

ExecResult runMake(String exec, List<String> cmdargs, File wd) {
ExecResult runMake(String exec, List<String> cmdargs, File wd,Map env) {
this.executable = exec
this.cmdargs = cmdargs
this.workingDir = wd
this.environment = env
return new FakeExecResult()
}
}
3 changes: 2 additions & 1 deletion src/test/resources/fake-make-scripts/fake-make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
@REM

@echo off
echo fake-make %1 %2 %3 %4 %5 %6 %7 %8 %9
echo fake-make %1 %2 %3 %4 %5 %6 %7 %8 %9
set
1 change: 1 addition & 0 deletions src/test/resources/fake-make-scripts/fake-make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
#

echo fake-make $@
env

0 comments on commit 8a537c0

Please sign in to comment.