How to enable compiler plugin from libraryDependencies?

I have a compiler plugin in library dependencies and would like to enable it. Sort of

autoCompilerPlugins := true

libraryDependencies += compilerPlugin(update.value.allModules.find(_.name contains "continuations-plugin").get)

scalacOptions += "-P:continuations:enable"

gives

/Users/luc/scala/release-sanity-check/build.sbt:20: error: A setting cannot depend on a task
libraryDependencies += compilerPlugin(update.value.allModules.find(_.name contains "continuations-plugin").get)
                                  ^

Can I do this with a custom task?

val addContinuationsPlugin = taskKey[Unit]("Add continuations plugin")

addContinuationsPlugin := {
  val plugin = update.value.allModules.find(_.name contains "continuations-plugin")
  // add plugin?
}

Repository in question: https://github.com/scala/scala-dist-smoketest

+4
source share
3 answers

You probably want something like this (see sbt API )

scalacOptions ++= {
  val compileConfig = update.value.configurations.find(_.configuration == "compile").get
  val pluginModule = compileConfig.modules.find(_.module.name contains "continuations-plugin").get
  val pluginFile = pluginModule.artifacts.head._2
  Seq(s"-Xplugin:${pluginFile.getCanonicalPath}", "-P:continuations:enable")
}

The compilePlugin method is designed to facilitate the task of using Ivy to resolve plugins, and you can do it right.

№ 2 compiler-plugin->compiler-plugin , ( compiler-plugin->default(compile)). , , № 2 ( ).

+3

, , Typesafe, . ( .)

, libraryDependencies, . , -, . project/plugins.sbt project/Build.scala?

, sbt-buildinfo, - . , -. , , Ivy -, scala -dist externalDependencyClasspath in BogusConfig. # 1.

//buildinfo.sbt

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.3.2")

//Dependencies.scala

package metabuild

import sbt._

object Dependencies {
  def scalaDistVersion = sys.props("project.version")
}

object Configs {
  lazy val BogusConfig = config("bogus")
}

/plugins.sbt

import metabuild.Dependencies._
import metabuild.Configs._

lazy val continuationPluginVersion = taskKey[String]("version of the continuation plugin")

lazy val build = (project in file(".")).
  configs(BogusConfig).
  settings(inConfig(BogusConfig)(Defaults.configSettings): _*).
  settings(buildInfoSettings: _*).
  settings(addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.6.4"): _*).
  settings(
    resolvers += "jgit-repo" at "http://download.eclipse.org/jgit/maven",
    libraryDependencies += "org.scala-lang" % "scala-dist" % scalaDistVersion % BogusConfig,
    sourceGenerators in Compile <+= buildInfo,
    buildInfoKeys := Seq[BuildInfoKey](
      "scalaDistVersion" -> scalaDistVersion,
      continuationPluginVersion
    ),
    buildInfoPackage := "metabuild",
    continuationPluginVersion := {
        val bogusClasspath = (externalDependencyClasspath in BogusConfig).value
        val f = (bogusClasspath find { _.data.getName contains "continuations-plugin" }).get.data
        f.getName.replaceAllLiterally("scala-continuations-plugin_" + scalaDistVersion + "-", "").replaceAllLiterally(".jar", "")
    }
  )

build.sbt

import metabuild.BuildInfo._

// versionWithGit

version := scalaDistVersion

scalaVersion := version.value

libraryDependencies += "org.scala-lang" % "scala-dist" % version.value

autoCompilerPlugins := true

libraryDependencies +=
  compilerPlugin("org.scala-lang.plugins" % ("scala-continuations-plugin_" + version.value) % continuationPluginVersion)

scalacOptions += "-P:continuations:enable"
+2

" ".

sbt, build.sbt :

autoCompilerPlugins := true

addCompilerPlugin, ( libraryDependencies) plugin->default(compile).

addCompilerPlugin("org.scala-tools.sxr" %% "sxr" % "0.3.0")

:

scalacOptions :=
    scalacOptions.value :+ ("-Psxr:base-directory:" + (scalaSource in Compile).value.getAbsolutePath)
+1

Source: https://habr.com/ru/post/1548465/


All Articles