From d2bb8c354a8d9b409c9e6ceb7fb1f788740721aa Mon Sep 17 00:00:00 2001 From: Pavel Marek Date: Fri, 18 Oct 2024 20:04:46 +0200 Subject: [PATCH] sbt picks up Scala case classes generated by Java annotation processor --- build.sbt | 5 +++++ project/Utils.scala | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 project/Utils.scala diff --git a/build.sbt b/build.sbt index 2671ddb09ccb..80008fd73010 100644 --- a/build.sbt +++ b/build.sbt @@ -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, diff --git a/project/Utils.scala b/project/Utils.scala new file mode 100644 index 000000000000..a18fd557cd17 --- /dev/null +++ b/project/Utils.scala @@ -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) + } +}