我只在需要时才尝试在项目中生成源。就我而言,“需要”是指任何相关项目已更改。
我有始终在运行生成代码的工作代码:
def runCodeGeneration(project: Project) = sourceGenerators in Compile += Def.taskDyn {
val dir = (sourceManaged in Compile).value
Def.task {
(project / run in Compile).
toTask(" " + dir.getAbsolutePath).
value
collectFiles(dir)
}
}.taskValue
lazy val doObject = (project in file("components/objects")).
dependsOn(doSpec).
settings(
runCodeGeneration(doSpec)
)
lazy val doSpec = (project in file("components/spec"))
从this question看来,我可以使用(managedClasspath in Compile) map { cp => cp.files }
在依赖关系中获取实际文件
适应this answer,我正在尝试类似的事情
def runCodeGeneration(project: Project) = {
val dir = (sourceManaged in Compile).value
(project / run in Compile).
toTask(" " + dir.getAbsolutePath).
value
collectFiles(dir)
}
def checkCodeGeneration(project: Project) = sourceGenerators in Compile += Def task {
val cachedFun = FileFunction.cached(
streams.value.cacheDirectory / "doGeneration"
) { (in: Set[File]) =>
runCodeGeneration(project).toSet
}
cachedFun((managedClasspath in Compile) map { cp => cp.files }).toSeq
}.taskValue
lazy val doObject = (project in file("components/objects")).
dependsOn(doSpec).
settings(
checkCodeGeneration(doSpec)
)
那失败了:
/Users/me/build.sbt:146: error: type mismatch;
found : sbt.Def.Initialize[sbt.Task[Seq[java.io.File]]]
required: Set[java.io.File]
cachedFun((managedClasspath in Compile) map { cp => cp.files }).toSeq
^
[error] sbt.compiler.EvalException: Type error in expression
[error] sbt.compiler.EvalException: Type error in expression
[error] Use 'last' for the full log.
似乎我只是添加一些.taskValue
和.toSet
来将实际值解包为所需的类型,但我不知道将它们放在哪里。
我将如何进行这项工作?