Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key in Configuration : how to list Configurations and Keys?

Tags:

sbt

The sbt in Action book introduces a concept of Key in Configuration

It then lists the default configurations:

  • Compile
  • Test
  • Runtime
  • IntegrationTest

Q1) Is it possible to print out a list of all Configurations from a sbt session? If not, can I find information on Configurations in the sbt documentation?

Q2) For a particular Configuration, e.g. 'Compile', is it possible to print out a list of Keys for the Configuration from a sbt session? If not, can I find information on a Configuration's Keys in the sbt documentation?

like image 285
Chris Snow Avatar asked Sep 12 '25 20:09

Chris Snow


1 Answers

List of all configurations

For this you can use a setting like so:

val allConfs = settingKey[List[String]]("Returns all configurations for the current project")

val root = (project in file("."))
  .settings(
     name := "scala-tests",
     allConfs := {
       configuration.all(ScopeFilter(inAnyProject, inAnyConfiguration)).value.toList
         .map(_.name)
     }

This shows the name of all configurations. You can access more details about each configuration inside the map.

Output from the interactive sbt console:

> allConfs
[info] * provided
[info] * test
[info] * compile
[info] * runtime
[info] * optional

If all you want is to print them you can have a settingKey[Unit] and use println inside the setting definition.

List of all the keys in a configuration

For this we need a task (there might be other ways, but I haven't explored, in sbt I'm satisfied if something works... ) and a parser to parse user input.

All join the above setting in this snippet:

import sbt._
import sbt.Keys._
import complete.DefaultParsers._

val allConfs = settingKey[List[String]]("Returns all configurations for the current project")
val allKeys = inputKey[List[String]]("Prints all keys of a given configuration")

val root = (project in file("."))
  .settings(
     name := "scala-tests",
     allConfs := {
       configuration.all(ScopeFilter(inAnyProject, inAnyConfiguration)).value.toList
         .map(_.name)
     },
     allKeys := {
       val configHints = s"One of: ${
         configuration.all(ScopeFilter(inAnyProject, inAnyConfiguration)).value.toList.mkString(" ")
       }"
       val configs = spaceDelimited(configHints).parsed.map(_.toLowerCase).toSet
       val extracted: Extracted = Project.extract(state.value)
       val l = extracted.session.original.toList
         .filter(set => set.key.scope.config.toOption.map(_.name.toLowerCase)
           .exists(configs.contains))
         .map(_.key.key.label)
       l
     }
   )

Now you can use it like:

$ sbt "allKeys compile"

If you are in interactive mode you can press tab after allKeys to see the prompt:

> allKeys
One of: provided test compile runtime optional

Since allKeys is a task it's output won't appear on the sbt console if you just "return it" but you can print it.

like image 159
pedromss Avatar answered Sep 15 '25 13:09

pedromss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!