2017-08-24 15 views
0

シェルexecのようにgradle execを動作させる方法はありますか?つまり、パス内の実行可能ファイルを理解していますか?execがスクリプトで動作しないのはなぜですか?

私たちは、WindowsとUNIX上で動作する必要があるコードと、両方のマシンでは明らかに異なる多くのスクリプトを持っています。

npmCommand = Os.isFamily(Os.FAMILY_WINDOWS) ? 'npm.cmd' : '/usr/local/bin/npm'

をしてから、そのコマンドを実行します - いくつかのスクリプトのパスは必ずしも石で設定されていないが - 、それだけで恐ろしいコードです:私はこのようなハックを行うことができますが。

問題を解決する方法はありますか?つまり、execタスクを拡張してパス内の実行可能ファイルを見つけて実行しますか?

+0

もちろん、標準の$ PATH値を取得し、groovyを使ってそれを処理することができます。 Btw、可能[duplicate](https://stackoverflow.com/questions/36273690/is-there-a-way-to-get-gradle-to-exec-a-command-line-in-path)。 – slesh

答えて

0

私はより良い方法をみたい - しかし、私は、私はあなたがこのように使用することを我々の共通に入れて簡単な関数作ら:

// 
// find a command with this name in the path 
// 
String command(String name) 
{ 
    def onWindows = (System.env.PATH==null); 
    def pathBits = onWindows ? System.env.Path.split(";") : System.env.PATH.split(":"); 
    def isMatch = onWindows ? {path -> 
       for (String extension : System.env.PATHEXT.split(";")) 
       { 
        File theFile = new File(path, name + extension); 
        if (theFile.exists()) 
         return theFile; 
       } 
       return null; 
     } : {path -> def file = new File(path,name);if (file.exists() && file.canExecute()) return file;return null;} 

    def foundLocal = isMatch(file(".")); 
    if (foundLocal) 
     return foundLocal; 
    for (String pathBit : pathBits) 
    { 
     def found = isMatch(pathBit); 
     if (found) 
      return found; 
    } 
    throw new RuntimeException("Failed to find " + name + " in the path") 
} 
:関数は次のようになります

exec { 
    commandLine command("npm"), "install" 
} 

関連する問題