2016-03-29 198 views
0

我想寫一個運行ruby(sass)的gradle腳本。我做的方式是現在

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?

回答

0

創建任務來尋找路徑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' 
} 
相關問題