是否可以在Sbt任务中执行命令?如果是这样,怎么样?因为命令需要一个国家,我怎么能得到一个?
我正在尝试覆盖默认任务,这是我尝试过的内容
dist := {
println("Turning coverage off")
Command.process("coverageOff")
dist.value
}
Command.process的签名是(string, state) => _
我还没弄明白在哪里获得州
答案 0 :(得分:6)
是的,您可以在任务中运行命令。以下是我目前为实现这一目标所做的工作。首先,在构建中定义以下方法:
/**
* 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
}
希望这有帮助!
答案 1 :(得分:0)
在获得gitter的帮助之后,它无法实现,但是可以反过来,在命令中调用任务。
因此,如果您的用例是顺序运行命令和任务(反之亦然),您可以执行类似的操作
lazy val newCommand = Command.command("name") { state =>
val newState = Command.process("comandName", state)
// run task
newState
}
答案 2 :(得分:0)
State可以通过评估state.value
要通过任务访问当前状态,请使用
state
任务作为 输入。例如,
myTask := ... state.value ...
像这样可以在任务中使用Command.process
调用命令
dist := {
println("Turning coverage off")
Command.process("coverageOff", state.value)
dist.value
}