What does "<<=" do in sbt settings? - scala

I am learning to write some more advanced sbt build files, and I came across sbt-proguard's code:
binaryDeps <<= compile in Compile map { _.relations.allBinaryDeps.toSeq },
inputs <<= fullClasspath in Runtime map { _.files },
libraries <<= (binaryDeps, inputs) map { (deps, in) => deps filterNot in.toSet },
outputs <<= artifactPath map { Seq(_) },
I want to know what does <<= mean in this context?
How do I understand the map function at the 3rd line?

The <<= is a method on DefinableSetting (mixed in by TaskKey, InputKey, and SettingKey) which provides a way to initialise a build setting. It's described in the older docs here:
:= assigns a value, overwriting any existing value. <<= uses existing values to initialize a setting.
Essentially, in 0.12 (and current versions, for compatibility) it was a way to define a build setting in terms of some other build setting(s).
As #sjrd points out, in 0.13 a new task setting syntax was introduced allowing you to do this with := instead.
The map in the third line is creating a new settings value by getting only dependency items from binaryDeps which are not already in inputs, i.e. transform these two settings into this new one.

Related

sbt wrapper plugin and custom logic

I have developed a custom sbt plugin that imports the sbt native packager, and a bunch of other plugins.
This helps bootstrap a scala project easing the process of importing all plugins we use across the project.
I also have custom task for custom purposes, such a:
substituteTemplates
to do custom behaviour.
I'm also changing the behaviour of the plugin I'm importing to cope with my customizations, such as:
packageZipTarball in Universal <<= (packageZipTarball in Universal) dependsOn substituteTemplates.
Now, everything works but the user of my plugin has to do:
val example = (project in file(".")).enablePlugins(JavaAppPackaging).settings(...
if he does not do it the sbt will not compile, because sbt finds definitions of stuff (mappings in Universal and such... ) that do not exist.
Here is how I tried to solve it.
TAKE 1
Now, I'd like to have the possibility to cope with the fact that some plugins need to be enabled.
If I enable the plugins (for example JavaAppPackaging) then I do a bunch of other changes, otherwise I leave the settings untouched.
At first I tried with a custom settings key, such as:
val enableJavaAppPackaging = SettingKey[Boolean]("enableJavaAppPackaging","")
then, I tried to use it:
object MyWrapperPlugin extends AutoPlugin {
override def projectSettings: Seq[Setting[_]] = {
if (enableJavaAppPackaging.value) {
packageZipTarball in Universal <<= (packageZipTarball in Universal) dependsOn substituteTemplates,
mappings in Universal <++= templates.map {
_.keySet.map { k => file(s"target/templates/$k") -> k }.toList
},
// a lot of other stuff....
}
but this cannot work as sbt complains:
`value` can only be used within a task or setting macro, such as :=, +=, ++=, Def.task, or Def.setting
There is a way to do this kind of logic in a way that is OK also for sbt?
TAKE 2
If I cannot do this at least I'd like to turn the JavaApp PAckaging plugin by default, so not to do elsiffing at all.
object MyWrapperPlugin extends AutoPlugin {
override def requires: Plugins = AssemblyPlugin && DependencyGraphPlugin && JavaAppPackaging
but this does not work too.

What is the SBT := operator in build.sbt?

I'm new to Scala and SBT. I noticed an unfamiliar operator in the build.sbt of an open source project:
:=
Here are a couple examples of how it's used:
lazy val akkaApp = Project(id = "akka-app", base = file("akka-app"))
.settings(description := "Common Akka application stack: metrics, tracing, logging, and more.")
and it's used a few times in this larger code snippet:
lazy val jobServer = Project(id = "job-server", base = file("job-server"))
.settings(commonSettings)
.settings(revolverSettings)
.settings(assembly := null.asInstanceOf[File])
.settings(
description := "Spark as a Service: a RESTful job server for Apache Spark",
libraryDependencies ++= sparkDeps ++ slickDeps ++ cassandraDeps ++ securityDeps ++ coreTestDeps,
test in Test <<= (test in Test).dependsOn(packageBin in Compile in jobServerTestJar)
.dependsOn(clean in Compile in jobServerTestJar)
.dependsOn(buildPython in jobServerPython)
.dependsOn(clean in Compile in jobServerPython),
testOnly in Test <<= (testOnly in Test).dependsOn(packageBin in Compile in jobServerTestJar)
.dependsOn(clean in Compile in jobServerTestJar)
.dependsOn(buildPython in jobServerPython)
.dependsOn(clean in Compile in jobServerPython),
console in Compile <<= Defaults.consoleTask(fullClasspath in Compile, console in Compile),
fullClasspath in Compile <<= (fullClasspath in Compile).map { classpath =>
extraJarPaths ++ classpath
},
fork in Test := true
)
.settings(publishSettings)
.dependsOn(akkaApp, jobServerApi)
.disablePlugins(SbtScalariform)
My best guess is that it means "declare if not already declared".
The := has essentially nothing to do with the ordinary assignment operator =. It's not a built-in scala operator, but rather a family of methods/macros called :=. These methods (or macros) are members of classes such as SettingKey[T] (similarly for TaskKey[T] and InputKey[T]). They consume the right hand side of the key := value expression, and return instances of type Def.Setting[T] (or similarly, Tasks), where T is the type of the value represented by the key. They are usually written in infix notation. Without syntactic sugar, the invocations of these methods/macros would look as follows:
key.:=(value)
The constructed Settings and Tasks are in turn the basic building blocks of the build definition.
The important thing to understand here is that the keys on the left hand side are not some variables in a code block. Instead of merely representing a memory position in an active stack frame of a function call (as a simple variable would do), the keys on the left hand side are rather complex objects which can be inspected and passed around during the build process.

Using input args inside a TaskKey

I'm writing an sbt plugin, and have created a TaskKey that need to get parsed arguments
lazy val getManager = TaskKey[DeployManager]("Deploy manager")
lazy val getCustomConfig = InputKey[String]("Custom config")
...
getCustomConfig := {
spaceDelimited("<arg>").parsed(0)
}
getManager := {
val conf = configResource.evaluated
...
}
but I get this error during compilation:
`parsed` can only be used within an input task macro, such as := or Def.inputTask.
I can't define getManager as InputKey since I later use it's value many times, and for an inputKey the value gets created anew on each evaluation (and I need to use the same instance)
You cannot do what you want in a reasonable way in sbt. (And the type system nicely prevents you from doing that in this case).
Imagine that getManager is a TaskKey that takes parsed arguments (a note aside, the sbt way of naming this would probably be manager, get is implied).
I now decide that, for example, compile depends on getManager. If I type compile in the shell, what arguments should getManager parse?
There is no concept of arguments inside the sbt dependency tree. They are just a shallow (and IMHO somewhat hackish) addition to make for a nicer CLI.
If you want to make getManager configurable, you can add additional settings, getManager depends on and then use set on the command line to change these where necessary.
So in you case:
lazy val configResource = SettingKey[...]("Config resource")
getManager := {
val conf = configResource.value
// ...
}

Where are examples of `?`, `??`, `<++=`, `<+=` in build.sbt?

I'm reading the sbt document, found there are some special methods I've never used:
?
??
<++=
<+=
Where can I find any examples of them?
SBT 0.13 did a great job eliminating the need for these operators and simplify build definition to :=, += and ++= with the help of macros and the special "extractor" .value. So no need for these operators anymore. The only thing I'm still using is ~= were you can apply some function to the value of some setting, but it also can be expressed with := and .value
In your question, I think you've mixed two sets of operations - one with <+= and <++= that was or are about to be "deprecated" in favour of :=, += and ++=, and another with ? and ?? that's unfortunately not very often used since all can be expressed with :=, += and ++= (and people often find using 3 enough for their use cases).
Read the official documentation of sbt in More operations about ? and ??.
As for examples:
?
lazy val unintiedKey = settingKey[String]("Unitialized key")
lazy val someKey = settingKey[String]("Key to check the value of another")
someKey := unintiedKey.?.value getOrElse "new value"
What do you think is going to be printed out with show someKey given the above build.sbt?
> show someKey
[info] new value
When you add the following to the build.sbt to have the uninitedKey setting initialized:
unintiedKey := "Another value"
someKey changes, too:
> show unintiedKey
[info] Another value
> show someKey
[info] Another value
??
Let's define a build with the following build.sbt:
lazy val unintiedKey = settingKey[String]("Unitialized key")
lazy val someKey = settingKey[String]("Key to check the value of another")
someKey := (unintiedKey ?? "uninitedKey had no value").value
Guess what the value of someKey is going to be?
> show someKey
[info] uninitedKey had no value
The key to understand the operations (that make up the sbt.SettingKey API) is to understand what a setting is in sbt - it's a pair of a key and an initialization that gets transformed into a useable setting when a scope gets applied to it.

Run custom task automatically before/after standard task

I often want to do some customization before one of the standard tasks are run. I realize I can make new tasks that executes existing tasks in the order I want, but I find that cumbersome and the chance that a developer misses that he is supposed to run my-compile instead of compile is big and leads to hard to fix errors.
So I want to define a custom task (say prepare-app) and inject it into the dependency tree of the existing tasks (say package-bin) so that every time someone invokes package-bin my custom tasks is run right before it.
I tried doing this
def mySettings = {
inConfig(Compile)(Seq(prepareAppTask <<= packageBin in Compile map { (pkg: File) =>
// fiddle with the /target folder before package-bin makes it into a jar
})) ++
Seq(name := "my project", version := "1.0")
}
lazy val prepareAppTask = TaskKey[Unit]("prepare-app")
but it's not executed automatically by package-bin right before it packages the compile output into a jar. So how do I alter the above code to be run at the right time ?
More generally where do I find info about hooking into other tasks like compile and is there a general way to ensure that your own tasks are run before and after a standard tasks are invoked ?.
Extending an existing task is documented the SBT documentation for Tasks (look at the section Modifying an Existing Task).
Something like this:
compile in Compile <<= (compile in Compile) map { _ =>
// what you want to happen after compile goes here
}
Actually, there is another way - define your task to depend on compile
prepareAppTask := (whatever you want to do) dependsOn compile
and then modify packageBin to depend on that:
packageBin <<= packageBin dependsOn prepareAppTask
(all of the above non-tested, but the general thrust should work, I hope).
As an update for the previous answer by #Paul Butcher, this could be done in a bit different way in SBT 1.x versions since <<== is no longer supported. I took an example of a sample task to run before the compilation that I use in one of my projects:
lazy val wsdlImport = TaskKey[Unit]("wsdlImport", "Generates Java classes from WSDL")
wsdlImport := {
import sys.process._
"./wsdl/bin/wsdl_import.sh" !
// or do whatever stuff you need
}
(compile in Compile) := ((compile in Compile) dependsOn wsdlImport).value
This is very similar to how it was done before 1.x.
Also, there is another way suggested by official SBT docs, which is basically a composition of tasks (instead of dependencies hierarchy). Taking the same example as above:
(compile in Compile) := {
val w = wsdlImport.value
val c = (compile in Compile).value
// you can add more tasks to composition or perform some actions with them
c
}
It feels like giving more flexibility in some cases, though the first example looks a bit neater, as for me.
Tested on SBT 1.2.3 but should work with other 1.x as well.