I have an SBT task that I want available across multiple "parallel" configuration scopes, where the dependencies have scope specifiers automatically applied based on the scope the task is invoked in.
I have something that works--whereby a separate task is instantiated per configuration scope--but it feels clumsy and want to know if there's a better way with less boilerplate.
In my specific use case I'm using sbt-native-packager
and defining package preparation tasks with OS-specific settings. Specifically, I'd like to be able to define unpack
without specifying the config scope, and then invoke windows:unpack
and have the config scope selector appropriately applied to the declared dependencies.
...
// Task and setting definitions
lazy val unpackSrc = settingKey[File]("Name of file we wish to unpack.")
lazy val unpackDstName = settingKey[File]("Unpacking destination.")
lazy val unpack = taskKey[Set[File]]("Unpack `unpackSrc` to `unpackDst`")
...
// OS specific settings via config scopes
unpackSrc in Windows := target.value / "foo-bar-windows.zip"
unpackSrc in Linux := target.value / "foo-bar-linux.zip"
unpackDst in Windows := target.value / "extract" / "windows"
unpackDst in Linux := target.value / "extract" / "linux"
// Task definition.
// This is what I wish I could do, but can't since it assumes global scope:
// unpack <<= (unpackSrc, unpackDst) map { (src, dst) ⇒
// IO.unzip(src, dst)
// }
// Workaround is to create a task generating function...
def unpackTask(conf: Configuration) = Def.task {
val src = (unpackSrc in conf).value
val dst = (unpackDst in conf).value
val s = streams.value
IO.unzip(src, dst)
}
// ... and invoke per configuration.
// This seems clumsy to me, but can't figure out a better way.
unpack in Windows <<= unpackTask(Windows)
unpack in Linux <<= unpackTask(Linux)
Is there some way have defining a task and it's dependencies, and having it work across scopes with proper scope selection?