How to get scalacOptions value for metaproject in plugin?

In the plugin, ensime-sbtwe need to be able to get the compiler flags that the sbt process uses to compile the assembly definition (i.e., everything is under project).

We have an object State, but I see no way to get the compiler flags, where are they?

Note: these are not compilation flags for the projects themselves, I mean only for the definition of the assembly.

eg. let's say the project has this inproject/plugins.sbt

scalacOptions += "-Xfuture"

how can we read this from the plugin?

This is somewhat related to How to distribute version values ​​between project / plugins.sbt and project / Build.scala?

+4
source share
1 answer

. , - .

import sbt._
import Keys._

object MyPlugin extends AutoPlugin {

  object autoImport {
    val scalacOptions4Meta = SettingKey[Seq[String]]("scalacOptions4Meta")
    val mygenerator = TaskKey[Seq[File]]("mygenerator")
  }

  import autoImport._

  override def trigger = allRequirements

  override lazy val projectSettings = Seq(
    mygenerator := {
      val file = sourceManaged.value / "settings4Meta.scala"
      val opts = (scalacOptions in Compile).value
        .map(opt => "\"" + opt + "\"")
      val content = s"""
        import sbt._
        import Keys._

        object MyBuild extends Build {
          lazy val root = Project("root", file("."))
            .settings(
              MyPlugin.autoImport.scalacOptions4Meta := Seq(${opts.mkString(",")})
            )
        }"""
      IO.write(file, content)
      Seq(file)
    }
  )
}

/plugins.sbt:

addSbtPlugin("myplugin" % "myplugin" % "0.1-SNAPSHOT")

scalacOptions := Seq("-Xfuture")

sourceGenerators in Compile += mygenerator.taskValue

//plugins.sbt:

addSbtPlugin("myplugin" % "myplugin" % "0.1-SNAPSHOT")
+1

All Articles