有没有办法让gradle执行$ path中的命令行 [英] Is there a way to get gradle to exec a command line in $path

查看:183
本文介绍了有没有办法让gradle执行$ path中的命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

pre code任务compileCss(type:Exec){
def dir =
if(System.properties ['os.name']。toLowerCase()。contains('windows')){
dir =C:\\\\\\\\\\\\\\\\\\\ 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 

任务checkForSassInPath(类型: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在路径中找不到sass;


然后你可以这样做:

 任务compileCss(类型:Exec,dependsOn:checkForSassInPath){
def dir
if(Os.isFamily(Os.FAMILY_WINDOWS) ){
dir = project.sassInPath? sass.bat:C:\\\\\\\\\\\\\\\\\\\\\\\' sass:/ usr / bin / sass
}
commandLine dir,'--update','source.scss:dest.css'
}


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?

解决方案

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

这篇关于有没有办法让gradle执行$ path中的命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆