2016-11-22 155 views
1

可以在Sbt任務中執行命令嗎?如果是這樣,怎麼樣?正如司令部要求的一個國家,我怎麼能獲得一個?在Sbt中,如何在任務中執行命令

我試圖重寫默認任務,這裏就是我試過

dist := { 
    println("Turning coverage off") 
    Command.process("coverageOff") 
    dist.value 
} 

Command.process的簽名是(string, state) => _

我還沒有弄清楚哪裏獲得國家

回答

5

是的,你可以運行一個任務中的命令。這是我目前正在做的事情。首先,在生成定義以下方法:

/** 
    * Convert the given command string to a release step action, preserving and  invoking remaining commands 
    * Note: This was copied from https://github.com/sbt/sbt-release/blob/663cfd426361484228a21a1244b2e6b0f7656bdf/src/main/scala/ReleasePlugin.scala#L99-L115 
    */ 
def runCommandAndRemaining(command: String): State => State = { st: State => 
    import sbt.complete.Parser 
    @annotation.tailrec 
    def runCommand(command: String, state: State): State = { 
    val nextState = Parser.parse(command, state.combinedParser) match { 
     case Right(cmd) => cmd() 
     case Left(msg) => throw sys.error(s"Invalid programmatic input:\n$msg") 
    } 
    nextState.remainingCommands.toList match { 
     case Nil => nextState 
     case head :: tail => runCommand(head, nextState.copy(remainingCommands = tail)) 
    } 
    } 
    runCommand(command, st.copy(remainingCommands = Nil)).copy(remainingCommands = st.remainingCommands) 
} 

然後,只需使用上述定義的實用程序在任務中調用任何命令,例如,runCommandAndRemaining("+myProject/publishLocal")(state.value)

在特定情況下,應該歸結爲

dist := { 
    val log = streams.value.log 
    log.debug("Turning coverage off") 
    runCommandAndRemaining("coverageOff")(state.value) 
    dist.value 
} 

希望這有助於!

0

從gitter獲得一些幫助後,這是不可能的,但是可以做相反的事情,在命令中調用任務。

所以,如果你的使用情況是運行命令和任務順序(反之亦然),你可以做這樣的事情

lazy val newCommand = Command.command("name") { state => 
    val newState = Command.process("comandName", state) 
    // run task 
    newState 
}