Reputation: 1624
Is there a way to list the tags associated with a Task in sbt
?
inspect
and show
don’t seem to have anything there.
Upvotes: 2
Views: 279
Reputation: 48420
The cool and powerful aspect of sbt is that the build definition it generates is a regular Scala application meaning we can inspect its objects, like we would in any other Scala application, by simply invoking member methods to query their state. Executing sbt
starts the REPL for the special DSL build language, however we can drop to a lower level by executing
sbt consoleProject
to start true Scala REPL:
starts the Scala interpreter with access to your project definition and to sbt...
consoleProject
can be useful for creating and modifying your build in the same way that the Scala interpreter is normally used to explore writing code. Note that this gives you raw access to your build.
There exists a public tags
method
final case class Task[T](info: Info[T], work: Action[T]) {
...
def tags: TagMap = info get tagsKey getOrElse TagMap.empty
}
so there must be a way to invoke it (even if there might not be a ready top level command for it such as inspect
). Say we have the following tagged task definition in build.sbt
lazy val hello = taskKey[Unit]("Vulcan greeting")
hello := Def.task(println("Live long and prosper")).tag(Tags.CPU, Tags.Compile).value
After executing consoleProject
our build definition is imported
scala> import _root_.scala.xml.{TopScope=>$scope}
import _root_.sbt._
import _root_.sbt.Keys._
import _root_.sbt.nio.Keys._
import _root_.sbt.ScriptedPlugin.autoImport._
import _root_.sbt.plugins.IvyPlugin
import _root_.sbt.plugins.JvmPlugin
import _root_.sbt.plugins.CorePlugin
import _root_.sbt.ScriptedPlugin
import _root_.sbt.plugins.SbtPlugin
import _root_.sbt.plugins.SemanticdbPlugin
import _root_.sbt.plugins.JUnitXmlReportPlugin
import _root_.sbt.plugins.Giter8TemplatePlugin
import $d408b7d79eabe42459a4.root
import currentState._
import extracted._
import cpHelpers._
Now we can make use of Extracted#get
to get the TaskKey
and explore it like so
scala> extracted.get(hello).tags
res1: sbt.ConcurrentRestrictions.TagMap = Map(Tag(cpu) -> 1, Tag(compile) -> 1)
Furthermore, note the import $d408b7d79eabe42459a4
. We can use this object to access regular val/def members, for example, say we had defined in build.sbt
def helloTask = Def.task { println("Live long and prosper") } tag(Tags.CPU, Tags.Compile)
then we could access helloTask
like so
scala> $d408b7d79eabe42459a4.helloTask.evaluate(structure.data).tags
res0: sbt.ConcurrentRestrictions.TagMap = Map(Tag(cpu) -> 1, Tag(compile) -> 1)
Both approaches show the required Map(Tag(cpu) -> 1, Tag(compile) -> 1)
.
Addressing the comment compileTask
does not seem to be tagged thus
scala> get(Compile/compile).tags
res8: sbt.ConcurrentRestrictions.TagMap = Map()
however, for example, updateFull
task is indeed tagged
updateFull := (updateTask tag (Tags.Update, Tags.Network)).value
hence
scala> get(updateFull).tags
res9: sbt.ConcurrentRestrictions.TagMap = Map(Tag(update) -> 1, Tag(network) -> 1)
Upvotes: 3