gradle - 有没有办法让gradle在$ path中执行命令行

标签 gradle path

我想编写一个gradle脚本来运行ruby(sass)。我现在的方式是

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'
}

我不喜欢此代码,因为有人担心可能将ruby安装在“非标准”目录(例如/ usr / local / bin :)中)。

有没有办法查看sass是否在路径中并使用该特定的sass?

最佳答案

创建一个任务以在路径中查找sass:

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";
    }
}

然后,您可以执行以下操作:
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'
}

关于gradle - 有没有办法让gradle在$ path中执行命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36273690/

相关文章:

testing - Gradle + Robolectric + Espresso : can't run separately

android - 计算堆栈大小时,Android,gradle,Proguard错误:

eclipse - 将项目转换为Gradle项目后,为什么为什么缺少CacheConfiguration.getMaxEntriesLocalHeap()方法?

java - 读取java中另一个包中的图像路径

windows - 使用批处理文件向系统变量路径添加一行

python - 未找到命令 : django

Gradle:防止执行根项目的 allProjects 中定义的任务

bash - 指定任务的绝对路径的运行gradle会将名称解释为命令

css - 特定路径未按预期使用 Dashoffset 进行动画处理

linux - 可执行文件可以动态解析其在文件系统上的位置或其实际的 "resting place"与用户的工作目录吗?