Is there a way to get gradle to exec a command line in $path

两盒软妹~` 提交于 2019-12-12 03:36:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!