How to correctly filter sbt source generator files for compilation?

Viewed 86

I'm using an external library as a Scala source generator. The library spits out both .scala files and resources in a path provided.

If I simply adapt the library as a Task[Seq[File]]:

Compile / sourceGenerators += Def.task {
   SomeCodeGenLib.genCode(
     (Compile / resourceDirectory).value / "schema.yaml",
     (Compile / sourceManaged).value
   )
}.taskValue,

… the resource files are treated as Scala sources and passed to scalac which fails compilation.

So I simply tried to add includeFilter or excludeFilter, but these seem only to work on unmanagedSource, not managed:

// These settings does not work:
managedSources / includeFilter := "*.scala" || "*.java",

sbt also complains these settings keys aren't used by anyone else:

[warn] there are 2 keys that are not used by any other settings/tasks:
[warn]  
[warn] * api / managedSources / excludeFilter
[warn]   +- /Users/viktor/dev/…… / build.sbt:192
[warn] * api / managedSources / includeFilter
[warn]   +- /Users/viktor/dev/…… / build.sbt:193

So next idea is to filter the files within the task:

Compile / sourceGenerators += Def.task {
   val generatedFiles = SomeCodeGenLib.genCode(
     (Compile / resourceDirectory).value / "schema.yaml",
     (Compile / sourceManaged).value
   )
   generatedFiles.filter(f => f.getName().endsWith(".scala")) 
}.taskValue

…and that's the only way I seem to get anywhere. But is this really the right way?

0 Answers
Related