不同范围或任务的不同 scalac 选项? [英] Different scalac options for different scopes or tasks?

查看:34
本文介绍了不同范围或任务的不同 scalac 选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有 sbt 的编译器插件(我使用的是 0.13.5),在我的 build.sbt 中传递为:

I am trying to use a compiler plugin with sbt (I'm on 0.13.5), passed along in my build.sbt as:

autoCompilerPlugins := true
scalacOptions += "-Xplugin:myCompilerPluginJar.jar"

这有效,插件运行,但我真的想只在一些显式编译上运行插件(可能使用范围编译任务或自定义任务).

This works, the plugin runs, however I would really like to only run the plugin on some explicit compiles (perhaps with a scoped compile task or a custom task).

如果我尝试以下操作:

val PluginConfig = config("plugin-config") extend(Compile)
autoCompilerPlugins := true
scalacOptions in PluginConfig += "-Xplugin:myCompilerPluginJar.jar"

插件不在plugin-config:compile"上运行.事实上,如果我有

The plugin does not run on "plugin-config:compile". In fact if I have

scalacOptions in Compile += "-Xplugin:myCompilerPluginJar.jar"

插件仍然在test:compile"上运行或在任何其他范围内编译.我想我可能没有正确理解配置/范围.

The plugin still runs on "test:compile" or compile on any other scope. I would guess I am probably not understanding something correctly with the configs/scopes.

我也试过:

lazy val pluginCommand = Command.command("plugincompile") { state =>
  runTask(compile in Compile,
    append(Seq(scalacOptions in Compile += "Xplugin:myCompilerPluginJar.jar"), state)
  )
  state
}

commands += pluginCommand

但是该插件实际上并没有在该命令上运行,所以我可能还是没有理解那里的某些内容.

But the plugin doesn't actually run on that command, so again I am probably not understanding something there.

欢迎任何帮助.

推荐答案

所以我来到了 hacky 解决方案;我想我会在这里分享,以防其他人偶然发现这个问题.

So I have come to hacky solution; I thought I would share it here in case anyone else stumbles upon this question.

val safeCompile = TaskKey[Unit]("safeCompile", "Compiles, catching errors.")

safeCompile := (compile in Compile).result.value.toEither.fold(
  l => {
    println("Compilation failed.")
  }, r => {
    println("Compilation success. " + r)})

//Hack to allow "-deprecation" and "-unchecked" in scalacOptions by default
scalacOptions <<= scalacOptions map { current: Seq[String] =>
  val default = "-deprecation" :: "-unchecked" :: Nil
  if (current.contains("-Xplugin:myCompilerPluginJar.jar")) current else default
}

addCommandAlias("depcheck", "; set scalacOptions := Seq(\"-deprecation\", \"-unchecked\", \"-Xplugin:myCompilerPluginJar.jar\"); safeCompile; set scalacOptions := Seq(\"-deprecation\", \"-unchecked\")")

作为快速指南,此代码:

As a quick guide, this code:

  • 定义一个自定义任务safeCompile",该任务运行Compile:compile"任务,但即使出现错误也会成功(这是必需的,以便稍后定义的命令序列不会在编译失败时中断).
  • 声明scalacOptions"依赖于检查插件是否已打开的函数(如果已打开则保持选项不变),否则将选项设置为我想要的项目默认值(Seq("-deprecation", "-未选中")).这是一个 hack,因此这些设置在默认情况下处于启用状态,因此裸露的scalacOptions :=" 定义不会覆盖在别名命令序列中完成的设置.(使用 Seq.append 和 Seq.distinct 可能是解决这个棘手部分的更好方法).
  • 定义别名命令序列:打开插件、safeCompiles、关闭插件.

欢迎发表评论,如果你有更干净的工作,请分享!

Comments are welcome, and if you get something cleaner to work, please share!

这篇关于不同范围或任务的不同 scalac 选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆