I mentioned it already in the previous post.
With SBuild 0.4, you have the ability to attach additional files to the context of a target execution.
So, if your target produces more than one file, this is the way to make SBuild aware of them.
The most important thing is, that attached files are exported to the direct dependent targets.
Those can access attached files either by using TargetContext.fileDependencies
to get all files including the attached ones or by using TargetRef.files
(or TargetRefs.files
) to selectively access files and attached files from only one (or some) dependencies.
Each target can attach files through its target context via TargetContext.attachFile
.
One typical use case for attached files is a target that generates or processes files.
The new scan scheme handler, I introduced in my last post, uses exactly that new feature.
All found files will be attached to the target context. Direct depedendent targets might access them.
The following code shows how to access files of dependencies:
val srcDir = "src/main/java"
val resDir = "src/main/resources"
Target("phony:print-files") dependsOn s"scan:${srcDir}" ~ s"scan:${resDir}" exec { ctx: TargetContext =>
println("All found files (via TargetContext.fileDependencies): " + ctx.fileDependencies.mkString(", "))
println("All sources (via TargetRef.files): " + s"scan:${srcDir}".files.mkString(", "))
println("All resources (via TargetRef.files): " + s"scan:${resDir}".files.mkString(", "))
}
The following code shows how you can attach files inside your target:
Target("phony:process-configs") dependsOn s"scan:src/main/config;regex=.*\.template$" exec { ctx: TargetContext =>
ctx.fileDependencies.foreach { file =>
val processedFile = ... // somehow generate or process the file
ctx.attachFile(processedFile)
}
}