There is a useful SBT plugin for formatting license headers:
https://github.com/Banno/sbt-license-plugin
As of 0.1.0, it only formats "scalaSource in Compile":
object Plugin extends sbt.Plugin {
import LicenseKeys._
object LicenseKeys {
lazy val formatLicenseHeaders = TaskKey[Unit]("formatLicenseHeaders", "Includes the license header to source files")
…
}
def licenseSettings = Seq(
formatLicenseHeaders <<= formatLicenseHeadersTask,
…
)
…
private def formatLicenseHeadersTask =
(streams, scalaSource in Compile, license in formatLicenseHeaders, removeExistingHeaderBlock in formatLicenseHeaders) map {
(out, sourceDir, lic, removeHeader) =>
modifySources(sourceDir, lic, removeHeader, out.log)
}
I wrote a pull request where I generalized this to format both java and scala sources used for both compilation and testing here:
https://github.com/Banno/sbt-license-plugin/blob/master/src/main/scala/license/plugin.scala
private def formatLicenseHeadersTask = Def.task[Unit] {
formatLicenseHeadersTask1.value
formatLicenseHeadersTask2.value
formatLicenseHeadersTask3.value
formatLicenseHeadersTask4.value
}
The tasks formatLicenseHeadersTask1234 correspond to the combinations of {scala,java}Source and {Compile,Test}.
This works but it's really ugly. Is there a way to write the same thing with loops as sketched below?
for {
src <- Seq( scalaSource, javaSource )
kind <- Seq( Compile, Test )
…
}
Related
I'm writing a Scala program and I want it to work with two version of a big library.
This big library's version 2 changes the API very slightly (only one class constructor signature has an extra parameter).
// Lib v1
class APIClass(a: String, b:Integer){
...
}
// Lib v2
class APIClass(a: String, b: Integer, c: String){
...
}
// And my code extends APIClass.. And I have no #IFDEF
class MyClass() extends APIClass("x", 1){ // <-- would be APIClass("x", 1, "y") in library v2
...
}
I really don't want to branch my code. Because then I'd need to maintain two branches, and tomorrow 3,4,..branches for tiny API changes :(
Ideally we'd have a simple preprocessor in Scala, but the idea was rejected long ago by Scala community.
A thing I don't really couldn't grasp is: can Scalameta help simulating a preprocessor in this case? I.e. parsing two source files conditionally to - say - an environmental variable known at compile time?
If not, how would you approach this real life problem?
1. C++ preprocessors can be used with Java/Scala if you run cpp before javac or scalac (also there is Manifold).
2. If you really want to have conditional compilation in Scala you can use macro annotation (expanding at compile time)
macros/src/main/scala/extendsAPIClass.scala
import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
#compileTimeOnly("enable macro paradise")
class extendsAPIClass extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro ExtendsAPIClassMacro.impl
}
object ExtendsAPIClassMacro {
def impl(c: blackbox.Context)(annottees: c.Tree*): c.Tree = {
import c.universe._
annottees match {
case q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" :: tail =>
def updateParents(parents: Seq[Tree], args: Seq[Tree]) =
q"""${tq"APIClass"}(..$args)""" +: parents.filter { case tq"scala.AnyRef" => false; case _ => true }
val parents1 = sys.env.get("LIB_VERSION") match {
case Some("1") => updateParents(parents, Seq(q""" "x" """, q"1"))
case Some("2") => updateParents(parents, Seq(q""" "x" """, q"1", q""" "y" """))
case None => parents
}
q"""
$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents1 { $self => ..$stats }
..$tail
"""
}
}
}
core/src/main/scala/MyClass.scala (if LIB_VERSION=2)
#extendsAPIClass
class MyClass
//Warning:scalac: {
// class MyClass extends APIClass("x", 1, "y") {
// def <init>() = {
// super.<init>();
// ()
// }
// };
// ()
//}
build.sbt
ThisBuild / name := "macrosdemo"
lazy val commonSettings = Seq(
scalaVersion := "2.13.2",
organization := "com.example",
version := "1.0.0",
scalacOptions ++= Seq(
"-Ymacro-debug-lite",
"-Ymacro-annotations",
),
)
lazy val macros: Project = (project in file("macros")).settings(
commonSettings,
libraryDependencies ++= Seq(
scalaOrganization.value % "scala-reflect" % scalaVersion.value,
)
)
lazy val core: Project = (project in file("core")).aggregate(macros).dependsOn(macros).settings(
commonSettings,
)
)
3. Alternatively you can use Scalameta for code generation (at the time before compile time)
build.sbt
ThisBuild / name := "scalametacodegendemo"
lazy val commonSettings = Seq(
scalaVersion := "2.13.2",
organization := "com.example",
version := "1.0.0",
)
lazy val common = project
.settings(
commonSettings,
)
lazy val in = project
.dependsOn(common)
.settings(
commonSettings,
)
lazy val out = project
.dependsOn(common)
.settings(
sourceGenerators in Compile += Def.task {
Generator.gen(
inputDir = sourceDirectory.in(in, Compile).value,
outputDir = sourceManaged.in(Compile).value
)
}.taskValue,
commonSettings,
)
project/build.sbt
libraryDependencies += "org.scalameta" %% "scalameta" % "4.3.10"
project/Generator.scala
import sbt._
object Generator {
def gen(inputDir: File, outputDir: File): Seq[File] = {
val finder: PathFinder = inputDir ** "*.scala"
for(inputFile <- finder.get) yield {
val inputStr = IO.read(inputFile)
val outputFile = outputDir / inputFile.toURI.toString.stripPrefix(inputDir.toURI.toString)
val outputStr = Transformer.transform(inputStr)
IO.write(outputFile, outputStr)
outputFile
}
}
}
project/Transformer.scala
import scala.meta._
object Transformer {
def transform(input: String): String = {
val (v1on, v2on) = sys.env.get("LIB_VERSION") match {
case Some("1") => (true, false)
case Some("2") => (false, true)
case None => (false, false)
}
var v1 = false
var v2 = false
input.tokenize.get.filter(_.text match {
case "// Lib v1" =>
v1 = true
false
case "// End Lib v1" =>
v1 = false
false
case "// Lib v2" =>
v2 = true
false
case "// End Lib v2" =>
v2 = false
false
case _ => (v1on && v1) || (v2on && v2) || (!v1 && !v2)
}).mkString("")
}
}
common/src/main/scala/com/api/APIClass.scala
package com.api
class APIClass(a: String, b: Integer, c: String)
in/src/main/scala/com/example/MyClass.scala
package com.example
import com.api.APIClass
// Lib v1
class MyClass extends APIClass("x", 1)
// End Lib v1
// Lib v2
class MyClass extends APIClass("x", 1, "y")
// End Lib v2
out/target/scala-2.13/src_managed/main/scala/com/example/MyClass.scala
(after sbt out/compile if LIB_VERSION=2)
package com.example
import com.api.APIClass
class MyClass extends APIClass("x", 1, "y")
Macro annotation to override toString of Scala function
How to merge multiple imports in scala?
I see some options but none if them is "conditional compilation"
you can create 2 modules in your build - they would have a shared source directory and each of them you have a source directory for code specific to it. Then you would publish 2 versions of your whole library
create 3 modules - one with your library and an abstract class/trait that it would talk to/through and 2 other with version-specific implementation of the trait
The problem is - what if you build the code against v1 and user provided v2? Or the opposite? You emitted the bytecode but JVM expects something else and it all crashes.
Virtually every time you have such compatibility breaking changes, library either refuses to update or fork. Not because you wouldn't be able to generate 2 versions - you would. Problem is in the downstream - how would your users deal with this situation. If you are writing an application you can commit to one of these. If you are writing library and you don't want to lock users to your choices... you have to publish separate version for each choice.
Theoretically you could create one project, with 2 modules, which share the same code and use different branches like #ifdef macros in C++ using Scala macros or Scalameta - but that is a disaster if you want to use IDE or publish sourcecode that your users can use in IDE. No source to look at. No way to jump to the definition's source. Disassembled byte code at best.
So the solution that you simply have separate source directories for mismatching versions is much easier to read, write and maintain in a long run.
Depending on a setting I need to call either package or assembly but obviously the assembly task is from sbt-assembly plugin that may not be added. My task looks something like this
lazy val useAssembly = settingKey[Boolean]("Use assembly")
lazy val myTask = Def.task {
val file = if (useAssembly.value) {
// somehow call assembly here
} else {
(`package` in Compile).value
}
// Do other stuff
}
PluginsDebug.autoPluginMap may be used to get available auto plugins. Create project/AssemblyIsAvailable.scala like so
package sbt.internal
/** If sbt-assembly is loaded, then it should be listed as sbtassembly.AssemblyPlugin **/
object AssemblyIsAvailable {
def apply(state: sbt.State): Boolean = {
PluginsDebug
.autoPluginMap(state)
.values
.toList
.map(_.label)
.exists(_.contains("ssembly"))
}
}
then define dynamic task assemblyOrDefaultPackage to be able to use conditional task evaluation within a task:
lazy val assemblyOrDefaultPackage = Def.taskDyn {
if (AssemblyIsAvailable(state.value))
Def.task { assembly.value }
else
Def.task { (Compile / Keys.`package`).value }
}
Now assemblyOrDefaultPackage can be evaluated within another task like so
lazy val myTask = Def.task {
val file = assemblyOrDefaultPackage.value
// Do other stuff
}
Alternatively, define custom command assemblyOrDefaultPackage in build.sbt like so
commands += Command.command("assemblyOrDefaultPackage") { state =>
(if (AssemblyIsAvailable(state)) "assembly" else "package") :: state
}
Now executing sbt assemblyOrDefaultPackage should package with sbt-assembly if available, otherwise fallback to default packaging.
Here is my plugin code. It defines a master lint task that is triggered from the CLI like so: sbt api/lint jobs/lint. It calls out to some project-specific linters and some build-wide linters. The build-wide linter runs scalafix, but if I call lint multiple times from the CLI, as above (for multiple projects), then scalafix is run multiple times.
How do I make scalafix (and scalafixLinter) run only one time for a given sbt invocation? I thought sbt caches task results, but it seems to not be working here.
object LinterPlugin extends AutoPlugin {
object autoImport {
lazy val scalafixLinter = taskKey[Unit]("Run scalafix on all scala code")
lazy val lint = taskKey[Unit]("Run all linters")
}
override val buildSettings = Seq(
scalafixLinter := {
Def.taskDyn {
if (...) {
Def.task {
// run scalafix
(Compile / scalafix).toTask("").value
(Test / scalafix).toTask("").value
}
} else {
Def.task {}
}
}.all(ScopeFilter(inAnyProject)).value // run on all projects
}
)
override val projectSettings = Seq(
lint := {
// run all the linters
otherLinters.value
scalafixLinter.value
}
)
}
You could use a FileFunction.cache method. Though that only works for files.
I've build a litte object, which can interpret scala code on the fly and catches a value out of it.
object Interpreter {
import scala.tools.nsc._
import scala.tools.nsc.interpreter._
class Dummy
val settings = new Settings
settings.usejavacp.value = false
settings.embeddedDefaults[Dummy] // to make imain useable with sbt.
val imain = new IMain(settings)
def run(code: String, returnId: String) = {
this.imain.beQuietDuring{
this.imain.interpret(code)
}
val ret = this.imain.valueOfTerm(returnId)
this.imain.reset()
ret
}
}
object Main {
def main(args: Array[String]) {
println(Interpreter.run("val x = 1", "x"))
}
}
In a pure sbt environment or called by the scala interpreter this code works fine! But if I run this in a simple play (version 2.2.2) application, it gets a null pointer at val ret = this.imain.valueOfTerm(returnId).
play uses also a modified sbt, therefor it should probably work. What does play do that this code doesn't work anymore? Any ideas how to get this code to work in play?
Note
That's the used build.sbt:
name := "Test"
version := "1.0"
scalaVersion := "2.10.3"
libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value
Alternatively I tried this implementation, but it doesen't solve the problem either:
object Interpreter2 {
import scala.tools.nsc._
import scala.tools.nsc.interpreter._
import play.api._
import play.api.Play.current
val settings: Settings = {
lazy val urls = java.lang.Thread.currentThread.getContextClassLoader match {
case cl: java.net.URLClassLoader => cl.getURLs.toList
case _ => sys.error("classloader is not a URLClassLoader")
}
lazy val classpath = urls map {_.toString}
val tmp = new Settings()
tmp.bootclasspath.value = classpath.distinct mkString java.io.File.pathSeparator
tmp
}
val imain = new IMain(settings)
def run(code: String, returnId: String) = {
this.imain.beQuietDuring {
this.imain.interpret(code)
}
val ret = this.imain.valueOfTerm(returnId)
this.imain.reset()
ret
}
}
Useful links I found to make this second implementation:
scala.tools.nsc.IMain within Play 2.1
How to set up classpath for the Scala interpreter in a managed environment?
https://groups.google.com/forum/#!topic/scala-user/wV86VwnKaVk
https://github.com/gourlaysama/play-repl-example/blob/master/app/REPL.scala#L18
https://gist.github.com/mslinn/7205854
After spending a few hours on this issue myself, here is a solution that I came up with. It works both inside SBT and outside. It is also expected to work in a variety of managed environments (like OSGi):
private def getClasspathUrls(classLoader: ClassLoader, acc: List[URL]): List[URL] = {
classLoader match {
case null => acc
case cl: java.net.URLClassLoader => getClasspathUrls(classLoader.getParent, acc ++ cl.getURLs.toList)
case c => LOGGER.error("classloader is not a URLClassLoader and will be skipped. ClassLoader type that was skipped is " + c.getClass)
getClasspathUrls(classLoader.getParent, acc)
}
}
val classpathUrls = getClasspathUrls(this.getClass.getClassLoader, List())
val classpathElements = classpathUrls map {url => url.toURI.getPath}
val classpath = classpathElements mkString java.io.File.pathSeparator
val settings = new Settings
settings.bootclasspath.value = classpath
val imain = new IMain(settings)
// use imain to interpret code. It should be able to access all your application classes as well as dependent libraries.
It's because play uses the "fork in run" feature from sbt. This feature starts a new JVM and this causes that this failure appears:
[info] Failed to initialize compiler: object scala.runtime in compiler mirror not found.
[info] ** Note that as of 2.8 scala does not assume use of the java classpath.
[info] ** For the old behavior pass -usejavacp to scala, or if using a Settings
[info] ** object programatically, settings.usejavacp.value = true.
See: http://www.scala-sbt.org/release/docs/Detailed-Topics/Forking
I want a program I'm building to be able to report its own version at runtime (e.g. scala myprog.jar --version). Traditionally in a maven project, I'd use resource filtering (pom.xml -> file.properties -> read value at runtime). I know there's sbt-filter-plugin to emulate this functionality, but I'm curious if there's a more standard / preferred / clever way of doing this in SBT.
tl;dr how can I read the version number defined in build.sbt at runtime?
Update...
https://github.com/ritschwumm/xsbt-reflect (mentioned above) is Obsolete, but there is this cool SBT release tool that can automatically manage versions and more: https://github.com/sbt/sbt-release.
Alternatively, if you want a quick fix you can get version from manifest like this:
val version: String = getClass.getPackage.getImplementationVersion
This value will be equal to version setting in your project which you set either in build.sbt or Build.scala.
Another Update ...
Buildinfo SBT plugin can generate a class with version number based on build.sbt:
/** This object was generated by sbt-buildinfo. */
case object BuildInfo {
/** The value is "helloworld". */
val name: String = "helloworld"
/** The value is "0.1-SNAPSHOT". */
val version: String = "0.1-SNAPSHOT"
/** The value is "2.10.3". */
val scalaVersion: String = "2.10.3"
/** The value is "0.13.2". */
val sbtVersion: String = "0.13.2"
override val toString: String = "name: %s, version: %s, scalaVersion: %s, sbtVersion: %s" format (name, version, scalaVersion, sbtVersion)
}
See the docs on how to enable it here: https://github.com/sbt/sbt-buildinfo/.
Use the xsbt-reflect plugin. It will generate a source file that contains, among other things, the project version number.
In general, without any plugins, you can do something like this:
sourceGenerators in Compile += Def.task {
val file = (sourceManaged in Compile).value / "foo" / "bar" / "BuildInfo.scala"
IO.write(
file,
s"""package foo.bar
|object BuildInfo {
| val Version = "${version.value}"
|}""".stripMargin
)
Seq(file)
}.taskValue
And then do with foo.bar.BuildInfo.Version constant whatever you like.
Or more general:
def generateBuildInfo(packageName: String,
objectName: String = "BuildInfo"): Setting[_] =
sourceGenerators in Compile += Def.task {
val file =
packageName
.split('.')
.foldLeft((sourceManaged in Compile).value)(_ / _) / s"$objectName.scala"
IO.write(
file,
s"""package $packageName
|object $objectName {
| val Version = "${version.value}"
|}""".stripMargin
)
Seq(file)
}.taskValue
Example:
settings(generateBuildInfo("foo.bar"))
You can even change this to pass object properties as a Map[String, String] and generate the object appropriately.
I ended up making the build system (I use a Makefile on top of sbt) prepare a src/main/resources/version.txt file for Scala to read.
In the Makefile:
$(RESOURCES_VERSION): build.sbt
grep "^version := " $< | cut -f2 -d\" > $#
In Scala:
val version: String = {
val src = Source.fromURL( getClass.getResource("/version.txt") )
src.getLines.next // just take the first line
}
This works for me.
It's curious that such a needed feature (I would think) is not easily available in Scala. A very simple sbt plugin just for this would be welcome.