From 4b0fed833bd73b33f1556d4d5604f9023795b37e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Schalk=20W=2E=20Cronj=C3=A9?= Date: Tue, 28 Apr 2015 20:54:12 +0100 Subject: [PATCH] Added support for noExecArgs. Fixed unit test failure --- .../ysb33r/gradle/gnumake/GnuMakeBuild.groovy | 57 ++++++++++-------- .../gradle/gnumake/GnuMakeExtension.groovy | 2 + .../gradle/gnumake/GnuMakePlugin.groovy | 2 +- .../gradle/gnumake/internal/TaskUtils.groovy | 22 +++++-- .../gradle/gnumake/GnuMakeBuildSpec.groovy | 20 ++++++- .../ysb33r/gradle/gnumake/RunMakeSpec.groovy | 60 +++++++++++++++++++ .../resources/fake-make-scripts/fake-make.bat | 2 +- .../resources/fake-make-scripts/fake-make.sh | 2 +- 8 files changed, 131 insertions(+), 36 deletions(-) create mode 100644 src/test/groovy/org/ysb33r/gradle/gnumake/RunMakeSpec.groovy diff --git a/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuild.groovy b/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuild.groovy index 5ca92d2..36d6ee2 100644 --- a/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuild.groovy +++ b/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuild.groovy @@ -30,6 +30,7 @@ import org.ysb33r.gradle.gnumake.internal.Executor import org.ysb33r.gradle.gnumake.internal.InputOutputMonitor import org.ysb33r.gradle.gnumake.internal.MakeExecutor import org.ysb33r.gradle.gnumake.internal.Rules +import org.ysb33r.gradle.gnumake.internal.TaskUtils /** * A wrapper task for calling GNU Make. This is useful for migrating legacy builds @@ -56,7 +57,6 @@ class GnuMakeBuild extends DefaultTask { * This is equivalent of passing -B to make */ @Input - @Optional boolean alwaysMake = false /** Tell make that varibales from the environment takes precedence over variables defined @@ -64,30 +64,32 @@ class GnuMakeBuild extends DefaultTask { * This is equivalent of passing -e to make */ @Input - @Optional boolean environmentOverrides = false /** Tell make to ignore all errors in commands executed to remake files. - * This is equivalant to passing -i to make + * This is equivalent to passing -i to make */ @Input - @Optional boolean ignoreErrors = false /** Set make concurrency level. * This is equivalent of passing -j to make. */ @Input - @Optional Integer jobs = 1 /** Tell make to carry on as far as possible after errors. * This is equivalant to passing -k to make */ @Input - @Optional boolean keepGoing = false + /** If set to {@code true} then {@code gnumake.execArgs} are not inherited. + * Default is to inherit. + */ + @Input + boolean noExecArgs = false + /** List of targets to execute * @@ -125,7 +127,7 @@ class GnuMakeBuild extends DefaultTask { @Optional @CompileDynamic Map getFlags() { - if( defaultFlags && project.extensions.findByName('gnumake') ) { + if( defaultFlags && project.extensions.findByName(GnuMakeExtension.EXTENSION_NAME) ) { project.gnumake.defaultFlags + this.flags } else { this.flags @@ -188,7 +190,7 @@ class GnuMakeBuild extends DefaultTask { @Input @CompileDynamic String getExecutable() { - this.executable ?: project.extensions.getByName('gnumake').executable + this.executable ?: project.extensions.getByName(GnuMakeExtension.EXTENSION_NAME)?.executable } /** The make executable. @@ -258,11 +260,14 @@ class GnuMakeBuild extends DefaultTask { /** Makefile to use. If not supplied will resort to the default behaviour of make, * which is usually to look for a file called Makefile or GNUMakefile. * This is the equivalent of passing -f to make. + * + * @return Returns the makefile or null */ @Input + @Optional @CompileDynamic String getMakefile() { - this.makefile ?: project.extensions.getByName('gnumake').makefile + this.makefile ?: project.extensions.findByName(GnuMakeExtension.EXTENSION_NAME)?.makefile } /** Makefile to use. If not supplied will resort to the default behaviour of make, @@ -355,7 +360,7 @@ class GnuMakeBuild extends DefaultTask { executor = new MakeExecutor(project) } buildCmdArgs() - execResult = executor.runMake(executable,cmdArgs,getWorkingDir()) + execResult = executor.runMake(getExecutable(),cmdArgs,getWorkingDir()) } @Deprecated @@ -403,22 +408,22 @@ class GnuMakeBuild extends DefaultTask { @CompileDynamic @PackageScope List buildCmdArgs() { - - cmdargs = project.extensions.getByName('gnumake').execArgs + [ - [alwaysMake, '-B'], - [environmentOverrides, '-e'], - [ignoreErrors, '-i'], - [keepGoing, '-k'], - [jobs > 1, '-j', jobs as String], - [makefile, '-f', "${makefile.toString()}"], - [chDir, '-C', "${chDir.toString()}"], - - ].collectMany { it.head() ? it.tail() : [] } + - - (getIncludeDirs().files.collectMany { ['-I', "${it.toString()}"] }) + - targets + - flags.collect { k, v -> "$k=$v" } + - switches + cmdargs = TaskUtils.buildCmdArgs(project,this,targets) +// cmdargs = project.extensions.getByName(GnuMakeExtension.EXTENSION_NAME).execArgs + [ +// [alwaysMake, '-B'], +// [environmentOverrides, '-e'], +// [ignoreErrors, '-i'], +// [keepGoing, '-k'], +// [jobs > 1, '-j', jobs as String], +// [makefile, '-f', "${makefile.toString()}"], +// [chDir, '-C', "${chDir.toString()}"], +// +// ].collectMany { it.head() ? it.tail() : [] } + +// +// (getIncludeDirs().files.collectMany { ['-I', "${it.toString()}"] }) + +// targets + +// flags.collect { k, v -> "$k=$v" } + +// switches } diff --git a/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakeExtension.groovy b/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakeExtension.groovy index e47dcea..9fa547c 100644 --- a/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakeExtension.groovy +++ b/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakeExtension.groovy @@ -25,6 +25,8 @@ import org.gradle.util.CollectionUtils */ class GnuMakeExtension { + static final String EXTENSION_NAME = 'gnumake' + String executable = 'make' String makefile diff --git a/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakePlugin.groovy b/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakePlugin.groovy index a6855e8..f5fc83a 100644 --- a/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakePlugin.groovy +++ b/src/main/groovy/org/ysb33r/gradle/gnumake/GnuMakePlugin.groovy @@ -25,7 +25,7 @@ import org.gradle.api.Project class GnuMakePlugin implements Plugin { void apply(Project project) { - project.extensions.create('gnumake', GnuMakeExtension, project) + project.extensions.create(GnuMakeExtension.EXTENSION_NAME, GnuMakeExtension, project) project.tasks.create( 'make', GnuMakeBuild ).configure { description "Runs a GNU Make process" group "GNU Make tasks" diff --git a/src/main/groovy/org/ysb33r/gradle/gnumake/internal/TaskUtils.groovy b/src/main/groovy/org/ysb33r/gradle/gnumake/internal/TaskUtils.groovy index 38ccd8e..f576560 100644 --- a/src/main/groovy/org/ysb33r/gradle/gnumake/internal/TaskUtils.groovy +++ b/src/main/groovy/org/ysb33r/gradle/gnumake/internal/TaskUtils.groovy @@ -12,9 +12,12 @@ // ============================================================================ // +package org.ysb33r.gradle.gnumake.internal + import org.gradle.api.Project import org.gradle.process.ExecResult import org.ysb33r.gradle.gnumake.GnuMakeBuild +import org.ysb33r.gradle.gnumake.GnuMakeExtension /** * @author Schalk W. Cronjé @@ -22,7 +25,13 @@ import org.ysb33r.gradle.gnumake.GnuMakeBuild class TaskUtils { static List buildCmdArgs(Project project,GnuMakeBuild task,List targets) { - project.extensions.getByName('gnumake').execArgs + [ + List execArgs = [] + + if (!task.noExecArgs) { + execArgs = project.extensions.findByName(GnuMakeExtension.EXTENSION_NAME)?.execArgs ?: [] + } + + def switches = [ [task.alwaysMake, '-B'], [task.environmentOverrides, '-e'], [task.ignoreErrors, '-i'], @@ -30,12 +39,13 @@ class TaskUtils { [task.jobs > 1, '-j', task.jobs.toString()], [task.makefile, '-f', "${task.makefile.toString()}"], [task.chDir, '-C', "${task.chDir?.absolutePath}" ], - ].collectMany { it.head() ? it.tail() : [] } + + ].collectMany { it.head() ? it.tail() : [] } + + def includes = (task.includeDirs.files.collectMany { ['-I', "${it.absolutePath}"] }) + + def flags = task.flags.collect { k, v -> "$k=$v" } - (task.includeDirs.files.collectMany { ['-I', "${it.absolutePath}"] }) + - targets + - task.flags.collect { k, v -> "$k=$v" } + - task.switches + execArgs + switches + includes + targets + flags + task.switches } static ExecResult runMake(Project project,final String exec,final List cmdargs,final File wd=null) { diff --git a/src/test/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuildSpec.groovy b/src/test/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuildSpec.groovy index 1404ae2..b7e89c2 100644 --- a/src/test/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuildSpec.groovy +++ b/src/test/groovy/org/ysb33r/gradle/gnumake/GnuMakeBuildSpec.groovy @@ -12,6 +12,7 @@ // ============================================================================ // +import org.ysb33r.gradle.gnumake.internal.FakeExecutor import spock.lang.* import org.ysb33r.gradle.gnumake.GnuMakeBuild import org.gradle.api.Project @@ -160,7 +161,7 @@ class GnuMakeBuildSpec extends spock.lang.Specification { expect: gnumake.cmdArgs.size() == 2 gnumake.cmdArgs[0] == '-C' - gnumake.cmdArgs[1] == "./change/here" + gnumake.cmdArgs[1] == project.file('./change/here').absolutePath } @@ -300,6 +301,23 @@ class GnuMakeBuildSpec extends spock.lang.Specification { gnumake.cmdArgs.join(' ') == '-k gmake -j 2 clean install' } + def "It is not necessary to set makefile"() { + given: + project.allprojects { + make { + executable 'foo' + targets 'clean' + } + } + + project.tasks.make.executor = new FakeExecutor() + project.evaluate() + project.tasks.make.execute() + + expect: + project.tasks.make.didWork + } + def "Monitor input sources and output folders"() { given: File srcDir = new File('src').absoluteFile diff --git a/src/test/groovy/org/ysb33r/gradle/gnumake/RunMakeSpec.groovy b/src/test/groovy/org/ysb33r/gradle/gnumake/RunMakeSpec.groovy new file mode 100644 index 0000000..eba36a6 --- /dev/null +++ b/src/test/groovy/org/ysb33r/gradle/gnumake/RunMakeSpec.groovy @@ -0,0 +1,60 @@ +// +// ============================================================================ +// (C) Copyright Schalk W. Cronje 2013-2015 +// +// This software is licensed under the Apache License 2.0 +// See http://www.apache.org/licenses/LICENSE-2.0 for license details +// +// Unless required by applicable law or agreed to in writing, software distributed under the License is +// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and limitations under the License. +// +// ============================================================================ +// + +package org.ysb33r.gradle.gnumake + +import org.gradle.api.Project +import org.gradle.internal.os.OperatingSystem +import org.gradle.testfixtures.ProjectBuilder +import spock.lang.Specification + + +/** + * @author Schalk W. Cronjé + */ +class RunMakeSpec extends Specification { + + static final File MAKESCRIPT = new File( "src/test/resources/fake-make-scripts/${OperatingSystem.current().isWindows() ? 'fake-make.bat' : 'fake-make.sh'}" ) + Project project = ProjectBuilder.builder().build() + + void captureStdOut() { + } + + + def "Check that Make is invoked with the correct arguments"() { + given: + def systemOut = new ByteArrayOutputStream() + System.out = new PrintStream(systemOut) + + project.allprojects { + apply plugin : 'org.ysb33r.gnumake' + + gnumake { + executable MAKESCRIPT.absolutePath + } + + make { + flags DESTDIR : 'foo/bar' + targets 'build','install' + } + } + + project.evaluate() + project.tasks.make.execute() + String output = systemOut.toString() + + expect: + output.contains('fake-make build install DESTDIR=foo/bar') + } +} \ No newline at end of file diff --git a/src/test/resources/fake-make-scripts/fake-make.bat b/src/test/resources/fake-make-scripts/fake-make.bat index 3920937..fb7afea 100644 --- a/src/test/resources/fake-make-scripts/fake-make.bat +++ b/src/test/resources/fake-make-scripts/fake-make.bat @@ -13,4 +13,4 @@ @REM @echo off -echo %1 %2 %3 %4 %5 %6 %7 %8 %9 \ No newline at end of file +echo fake-make %1 %2 %3 %4 %5 %6 %7 %8 %9 \ No newline at end of file diff --git a/src/test/resources/fake-make-scripts/fake-make.sh b/src/test/resources/fake-make-scripts/fake-make.sh index ce4c30e..a1eab9f 100755 --- a/src/test/resources/fake-make-scripts/fake-make.sh +++ b/src/test/resources/fake-make-scripts/fake-make.sh @@ -13,4 +13,4 @@ # ============================================================================ # -echo $@ +echo fake-make $@