Skip to content

Commit

Permalink
sbt picks up Scala case classes generated by Java annotation processor
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Oct 18, 2024
1 parent 1fc0497 commit d2bb8c3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3101,6 +3101,11 @@ lazy val `runtime-parser` =
frgaalJavaCompilerSetting,
annotationProcSetting,
commands += WithDebugCommand.withDebug,
// Java annotation processor in `runtime-parser-processor` generates scala
// case classes. This setting ensures that all the generated `*.scala` files
// are picked by the sbt for compilation.
Compile / managedSources ++=
Utils.listAllGeneratedScalaFiles.value,
fork := true,
libraryDependencies ++= Seq(
"junit" % "junit" % junitVersion % Test,
Expand Down
36 changes: 36 additions & 0 deletions project/Utils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sbt.*
import sbt.Keys.*

import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.{FileVisitResult, Files, Path, SimpleFileVisitor}
import scala.collection.mutable.ListBuffer

object Utils {

/** Recursively lists all `*.scala` files in the `target/src_managed` directory.
* Should be put as a dependency of `Compile / managedSources` task to ensure
* that sbt picks up any scala sources generated by the Java annotation processor.
* @return
*/
def listAllGeneratedScalaFiles(): Def.Initialize[Task[Seq[File]]] = Def.task {
val srcManagedDir = (Compile / sourceManaged).value
val scalaFiles = ListBuffer[Path]()

Files.walkFileTree(
srcManagedDir.toPath,
new SimpleFileVisitor[Path]() {
override def visitFile(
file: Path,
attrs: BasicFileAttributes
): FileVisitResult = {
if (file.toString.endsWith(".scala")) {
scalaFiles += file
}
FileVisitResult.CONTINUE
}
}
)

scalaFiles.map(_.toFile)
}
}

0 comments on commit d2bb8c3

Please sign in to comment.