lev
lev

Reputation: 4127

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)

Upvotes: 0

Views: 517

Answers (1)

gzm0
gzm0

Reputation: 14842

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
  // ...
}

Upvotes: 2

Related Questions