enso/project/GenerateAST.scala
2021-12-27 17:56:35 +01:00

43 lines
1.2 KiB
Scala

import sbt.Keys._
import sbt._
import sbt.internal.util.ManagedLogger
object GenerateAST {
lazy val task = Def.task {
val log = state.value.log
val lib = baseDirectory.value.getParentFile.getParentFile
val source = lib / "rust/ast/src/ast.rs"
val output = sourceManaged.value / "main/org/enso/ast/Ast.scala"
val cache = streams.value.cacheStoreFactory.make("ast_source")
Tracked.diffInputs(cache, FileInfo.lastModified)(Set(source)) {
source: ChangeReport[File] =>
if (source.modified.nonEmpty) {
output.getParentFile.mkdirs
generateAST(output, log)
}
}
Seq(output)
}
/** Generates the Scala AST in the specified file. All errors are reported in
* stderr and raise a runtime exception.
*
* @param out the file where the generated AST is going to be placed
*/
def generateAST(out: File, log: ManagedLogger): Unit = {
val args = s"run -p ast -- --generate-scala-ast $out"
log.info(s"Generating Scala AST from Rust definitions.")
try Cargo.run(args, log)
catch {
case ex: RuntimeException =>
log.error(s"Generation of the Scala AST failed.")
throw ex
}
}
}