问题
I want to write a gradle script which will run ruby (sass). The way I do it now is
task compileCss (type: Exec){
def dir = ""
if (System.properties['os.name'].toLowerCase().contains('windows')) {
dir = "C:\\ruby22\\bin\\sass.bat"
} else {
dir = "/usr/bin/sass"
}
commandLine dir, '--update', 'source.scss:dest.css'
}
I don't like this code for the obvious concern that someone may install ruby in a "non-standard" directory (such as /usr/local/bin :) ).
Is there a way to see if sass is in path and use that particular sass?
回答1:
Create a task to look for sass in the path:
import org.apache.tools.ant.taskdefs.condition.Os
task checkForSassInPath(type: Exec) {
def windows = Os.isFamily(Os.FAMILY_WINDOWS)
executable = windows ? "where" : "which"
args = [(windows ? "sass.bat" : "sass")]
ignoreExitValue = true
standardOutput = new ByteArrayOutputStream()
errorOutput = new ByteArrayOutputStream()
}
checkForSassInPath << {
project.ext.sassInPath = (execResult.exitValue == 0)
if (!project.sassInPath ) {
logger.warn "Cannot find sass in path";
}
}
Then you can do:
task compileCss (type: Exec, dependsOn: checkForSassInPath){
def dir
if ( Os.isFamily(Os.FAMILY_WINDOWS) ) {
dir = project.sassInPath ? "sass.bat" : "C:\\ruby22\\bin\\sass.bat"
} else {
dir = project.sassInPath ? "sass" : "/usr/bin/sass"
}
commandLine dir, '--update', 'source.scss:dest.css'
}
来源:https://stackoverflow.com/questions/36273690/is-there-a-way-to-get-gradle-to-exec-a-command-line-in-path