【问题标题】:Conditional scalacSettings / settingKey条件scalacSettings / settingKey
【发布时间】:2019-02-04 08:44:40
【问题描述】:

当我发出自己的命令 validate 时,我希望我的 scalacSettings 更加严格(更多 linting)。

实现这一目标的最佳方法是什么?

一个新的作用域 (strict) 确实有效,但它需要在您发出 test 时编译两次项目。所以这不是一个选择。

【问题讨论】:

  • 你尝试过scalacSettings in validate ++= Seq(...)这样的事情吗?
  • 这个信息对stackoverflow.com/questions/26940253/…有帮助吗?
  • validate 不是作用域,而是命令别名: private def cmdAlias(name: String, commands: List[String]) = addCommandAlias(name, s";${commands.mkString( ";")}") cmdAlias("validateCoverage", scalafmtCheckAlias ++ coverageAlias)
  • 不确定别名的使用方式。你能把validate 转换成task 试试吗?
  • validate 在 CI 中运行构建时正在使用,并且应该严格 lint。虽然开发编译设置的限制较少,因此不具有侵入性。我不想在开发新功能时被未使用的导入编译错误所困扰:-)

标签: sbt


【解决方案1】:

SBT 自定义 command 允许临时修改构建状态,可以在命令完成后丢弃:

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 转换:

commands += Command.command("validate") { state =>
  """set scalacOptions in Compile := Seq("-Ywarn-unused:imports", "-Xfatal-warnings", "...")""" :: 
  "test" :: state
} 

这样我们可以在开发过程中使用sbt test,而我们的CI挂钩到使用stateWithStrictScalacSettingssbt validate

【讨论】:

  • 我想要一个严格的编译变体我已经到了这个程度: def strictCompile: Command = Command.command("strictCompile") { state => val extract = Project.extract(state) val ref = extract .get(thisProjectRef) val s = extract.appendWithoutSession(Seq(Compile / scalacOptions := baseScalacSettings ++ strictScalacSettings),state) Project.extract(s).runAggregated(Compile / compile in ref, s) s } 这是一个实现它使用 runAggregated (用于多个子模块)。它编译,但它不应用严格的编译设置
  • 将项目轴设置为ThisBuild,如下所示:extracted.appendWithoutSession(Seq(ThisBuild / Compile / scalacOptions := baseScalacSettings ++ strictScalacSettings),state)
  • 我明白了,聪明。当您在 projectSettings 内使用它时,它不起作用 AutoPlugin (SBT 1.x)
  • 有没有办法调试正在发生的事情? sbt inspect strictCompile 显示这是一个命令,但不显示正在做什么。我也使用sbt clean debug strictCompile,它应用了baseScalacOptions(在projectSettings 中定义),但strictScalacOptions 仍然被排除在外。
猜你喜欢
  • 2013-01-18
  • 1970-01-01
  • 2017-02-25
  • 2014-12-12
  • 2019-05-28
  • 2017-05-28
  • 2019-06-09
  • 2015-06-27
  • 2014-06-19
相关资源
最近更新 更多