Michal Rus
Michal Rus

Reputation: 1848

Map over SBT's Setting while keeping scopes?

In a plugin, I've got a SettingKey, say:

val age = settingKey[Int]("An age")

Users can define this age in different scopes in their build.sbt's:

age in Somewhere := 13

age in Whatever := 55

Now, in the plugin, I'd like to map all their definitions like so:

someOtherKey in ___ := if ((age in ___).value <= 10) "young" else "old"

... for each scope ___ that the age is defined in by the user.

Now, I don't know the scopes beforehand!

I think this used to be done with mapReferenced but it got deprecated in 0.13.2. What's the proper way now?

Upvotes: 1

Views: 211

Answers (1)

jsuereth
jsuereth

Reputation: 5624

Well, you can use derived settings, which are an advanced and undocumented API. The idea is that if underlying keys exist, then your setting will automatically be pushed into a given configuration. We use it for things like testOptions internally, but it hasn't been fully fleshed out in 0.13 series. Here's an example:

inScope(Global)(Seq(
   Def.derive(someOtherKey := if(age.value <= 10) "young" else "old")
}

You can also set up derivation filters, such that e.g. you can only derive the setting into a project scope:

inScope(Global)(Seq(
   Def.derive(someOtherKey := if(age.value <= 10) "young" else "old", filter = _.project.isSelect)
}

Feel free to tamper around with the API. As I stated, it's experimental and we're still working out the kinks.

Upvotes: 1

Related Questions