cchantep
cchantep

Reputation: 9168

How to use the values of TaskKey and SettingKey to define setting?

Is it possible to compute both values from TaskKey (e.g. unmanagedClasspath) and SettingKey (e.g. baseDirectory), in order to define other setting SettingKey?

Current goal would be to define sourceGenerators according to defined dependencies (classpath) and other settings (baseDirectory, sourceManaged, ...).

Using only SettingKey can be done as following, but it doesn't work as-is to include TaskKey in the process:

sourceGenerators in Compile <+= (baseDirectory in Compile).
  zip(sourceManaged in Compile).map(settingsValues ⇒ ???)

Upvotes: 1

Views: 1621

Answers (2)

lpiepiora
lpiepiora

Reputation: 13749

First things first: It's not possible to set a SettingKey using TaskKeys. See .sbt Build Definition and More Kinds of Setting from the official documentation of sbt.

You could solve it by having following code (in sbt 0.12.4 and lower versions).

sourceGenerators in Compile <+= (baseDirectory, sourceManaged, managedClasspath in Compile) map { (b, s, c) =>
  Seq[File]()
}

However I highly recommend using the newer versions of sbt that offer many simplifications for defining such relationship between settings and tasks (remember that tasks can be based upon the values of settings, but not vice versa).

For example using 0.13.5, you could do it as follows:

sourceGenerators in Compile += Def.task {
  val b = baseDirectory.value
  val s = sourceManaged.value
  val c = (managedClasspath in Compile).value
  Seq[File]()
}.taskValue

Obviously, in this overly simplistic example the values b, s and c are discarded since they're not used to compute the last expression Seq[File]() that becomes the result value.

Upvotes: 2

cchantep
cchantep

Reputation: 9168

As a temporary solution, I define a custom TaskKey assigned with a tuple gathering values from other SettingKey & TaskKey, and then I assign sourceGenerators mapping over this custom key. I don't know whether this a more direct way, not requiring to define such temporary custom key.

val generatorsConfig = TaskKey[(File, File, Classpath)](
  "reactive-mongo-generators-config", "Source generators configuration")

generatorsConfig := (
  (baseDirectory in Compile).value,
  (sourceManaged in Compile).value, 
  (managedClasspath in Compile).value)

sourceGenerators in Compile <+= generatorsConfig map { d =>
  val (base, managed, cp) = d
  Seq[File]() // Define according base, managed and classpath
}

Upvotes: 0

Related Questions