Search code examples
scalapropertiessbtsequentialsbt-assembly

Sequential input task in SBT


I am trying to write an SBT task that may be used like this:

> deploy key1=value1 key2=value2 ...

and does the following:

  1. reads a default properties file
  2. adds the parsed keys and values to the properties object
  3. writes the new properties object to resources/config.properties
  4. packages a fat .jar withsbt-assembly
  5. writes the default properties to resources/config.properties

I have been trying to achieve it with Def.sequential, but I cant seem to find a way to use it withinputKey. Mybuild.sbtlooks like this:

val config = inputKey[Unit] (
  "Set configuration options before deployment.")
val deploy = inputKey[Unit](
  "assemble fat .jar with configuration options")
val defaultProperties = settingKey[Properties](
  "default application properties.")
val propertiesPath = settingKey[File]("path to config.properties")
val writeDefaultProperties = taskKey[Unit]("write default properties file.")
val parser = (((' ' ~> StringBasic) <~ '=') ~ StringBasic).+

lazy val root = (project in file("."))
  .settings(
    propertiesPath := {
      val base = (resourceDirectory in Compile).value
      base / "config.properties"
    },
    defaultProperties := {
      val path = propertiesPath.value
      val defaultConfig = new Properties
      IO.load(defaultConfig, path)
      defaultConfig
    },
    config := {
      val path = propertiesPath.value
      val defaultConfig = defaultProperties.value
      val options = parser.parsed
      val deployConfig = new Properties
      deployConfig.putAll(defaultConfig)
      options.foreach(option =>
        deployConfig
          .setProperty(option._1, option._2))
        IO.write(deployConfig, "", path)
    },
    writeDefaultProperties := {
      val default = defaultProperties.value
      val path = propertiesPath.value
      IO.write(default, "", path)
    },
    deploy := Def.sequential(
      config.parsed, // does not compile
      assembly, 
      writeDefaultProperties),
    ...)

Can I makeDef.sequential work with input keys, or do I need to do something more involved?


Solution

  • See Defining a sequential task with Def.sequential. It uses scalastyle as an example:

      (scalastyle in Compile).toTask("")