如何更改 SBT 命令中的设置? [英] How to change setting inside SBT command?
问题描述
我想要一个命令 publish-snapshot
来运行 publish
任务并修改 version
设置(该设置将被计算命令执行时).
I want to have a command publish-snapshot
that would run the publish
task with modified version
setting (that setting is to be computed at the time of execution of the command).
我想出了如何在命令中获取 version
的当前值,并且 Project.runTask("task", "scope", ...)
似乎正确调用 publish
任务.
I figured out how to get the current value of the version
inside command, and Project.runTask("task", "scope", ...)
seems to be a right call for invoking the publish
task.
唯一让我感到困惑的是如何使用新的版本值修改 State
实例.我所有的尝试似乎对原始版本设置没有任何作用.
The only thing that I'm confused with is how to modify the State
instance with a new version value. All my attempts seem to do nothing to the original version setting.
我最后一次尝试:
val printVers = TaskKey[Unit]("printvers")
val printVersTask = TaskKey[Unit]("printvers") <<= {version map println}
def publishSnapshot = Command.command("publish-snapshot") { state =>
val newState = SessionSettings.reapply(state.get(sessionSettings).get.appendRaw(version := "???"), state)
Project.runTask(printVers in Compile, newState, true)
state
}
lazy val root = Project("main", file("."),
settings =
Defaults.defaultSettings ++
Seq(printVersTask)).settings(commands += publishSnapshot)
有什么办法可以解决这种行为吗?
Is there some way to fix that behavior?
推荐答案
这实际上对我不起作用.我正在使用 SBT 0.13.7
This actually did not work for me. I'm using SBT 0.13.7
将我必须做的事情调整到上面的例子中,我不得不做这样的事情:
Adapting what I had to do to the above example, I had to do something like:
def publishSnapshot = Command.command("publish-snapshot") { state =>
val extracted = Project extract state
val newState = extracted.append(Seq(version := "newVersion"), state)
val (s, _) = Project.extract(newState).runTask(publish in Compile, newState)
s
}
或者也可以这样做:
def publishSnapshot = Command.command("publish-snapshot") { state =>
val newState =
Command.process("""set version := "newVersion" """, state)
val (s, _) = Project.extract(newState).runTask(publish in Compile, newState)
s
}
这篇关于如何更改 SBT 命令中的设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!