我有一个SBT任务,我希望在多个" parallel"配置范围,其中依赖项根据调用任务的范围自动应用范围说明符。
我有一些有用的东西 - 每个配置范围都会实例化一个单独的任务 - 但它感觉很笨拙,并想知道是否有更好的方法,而且样板更少。
在我的特定用例中,我使用sbt-native-packager
并使用特定于操作系统的设置定义包准备任务。具体来说,我希望能够在不指定配置范围的情况下定义unpack
,然后调用windows:unpack
并将配置范围选择器适当地应用于声明的依赖关系。
...
// 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)
是否有某种方法可以定义任务及其依赖关系,并使其在适当范围选择的范围内工作?