enso/project/GenerateAST.scala

43 lines
1.2 KiB
Scala
Raw Normal View History

import sbt.Keys._
import sbt._
import sbt.internal.util.ManagedLogger
2020-08-14 12:10:52 +03:00
object GenerateAST {
lazy val task = Def.task {
val log = state.value.log
2020-08-14 12:10:52 +03:00
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 = {
2020-08-14 12:10:52 +03:00
val args = s"run -p ast -- --generate-scala-ast $out"
2020-08-14 12:10:52 +03:00
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
}
}
}