Conditional scalacSettings / settingKey

本秂侑毒 提交于 2019-12-24 01:09:29

问题


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?

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.


回答1:


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)

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
} 

This way we can use sbt test during development, while our CI hooks into sbt validate which uses stateWithStrictScalacSettings.



来源:https://stackoverflow.com/questions/54309976/conditional-scalacsettings-settingkey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!