条件 scalacSettings/settingKey [英] Conditional scalacSettings / settingKey
问题描述
当我发出自己的命令 validate
时,我希望我的 scalacSettings
更加严格(更多 linting).
I want my scalacSettings
to be more strict (more linting) when I issue my own command validate
.
实现这一目标的最佳方法是什么?
What is the best way to achieve that?
新的作用域 (strict
) 确实有效,但是当您发出 test
时,它需要两次编译项目.所以这不是一个选择.
A new scope (strict
) did work, but it requires to compile the project two times when you issue test
. So that's not a option.
推荐答案
SBT 自定义 command 允许临时修改构建状态,可以在命令完成后丢弃:
SBT custom command allows for temporary modification of build state which can be discarded after command finishes:
def validate: Command = Command.command("validate") { state =>
import Project._
val stateWithStrictScalacSettings =
extract(state).appendWithSession(
Seq(Compile / scalacOptions ++= Seq(
"-Ywarn-unused:imports",
"-Xfatal-warnings",
"...",
))
,state
)
val (s, _) = extract(stateWithStrictScalacSettings).runTask(Test / test, stateWithStrictScalacSettings)
s
}
commands ++= Seq(validate)
或更简洁地使用 ::
State
转换的便捷方法:
or more succinctly using ::
convenience method for State
transformations:
commands += Command.command("validate") { state =>
"""set scalacOptions in Compile := Seq("-Ywarn-unused:imports", "-Xfatal-warnings", "...")""" ::
"test" :: state
}
这样我们就可以在开发过程中使用sbt test
,而我们的CI挂钩到使用stateWithStrictScalacSettings
的sbt validate
.
This way we can use sbt test
during development, while our CI hooks into sbt validate
which uses stateWithStrictScalacSettings
.
这篇关于条件 scalacSettings/settingKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!