2020-08-03 17:00:12 +03:00
|
|
|
import sbt.Keys._
|
|
|
|
import sbt._
|
|
|
|
import sbt.internal.util.ManagedLogger
|
|
|
|
|
2020-08-14 12:10:52 +03:00
|
|
|
object GenerateAST {
|
2020-08-03 17:00:12 +03:00
|
|
|
|
|
|
|
lazy val task = Def.task {
|
|
|
|
val log = state.value.log
|
2020-08-14 12:10:52 +03:00
|
|
|
val lib = baseDirectory.value.getParentFile.getParentFile
|
2020-08-03 17:00:12 +03:00
|
|
|
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")
|
|
|
|
|
2020-10-22 17:12:28 +03:00
|
|
|
Tracked.diffInputs(cache, FileInfo.lastModified)(Set(source)) {
|
|
|
|
source: ChangeReport[File] =>
|
|
|
|
val rustVersion = Cargo.rustVersion.value
|
|
|
|
if (source.modified.nonEmpty) {
|
2020-08-03 17:00:12 +03:00
|
|
|
output.getParentFile.mkdirs
|
2020-08-14 12:10:52 +03:00
|
|
|
generateAST(rustVersion, output, log)
|
2020-10-22 17:12:28 +03:00
|
|
|
}
|
2020-08-03 17:00:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Seq(output)
|
|
|
|
}
|
|
|
|
|
2020-10-22 17:12:28 +03:00
|
|
|
/** Generates the Scala AST in the specified file. All errors are reported in
|
2020-08-03 17:00:12 +03:00
|
|
|
* stderr and raise a runtime exception.
|
|
|
|
*
|
|
|
|
* @param out the file where the generated AST is going to be placed
|
|
|
|
*/
|
2020-10-22 17:12:28 +03:00
|
|
|
def generateAST(rustVersion: String, 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-03 17:00:12 +03:00
|
|
|
|
2020-08-14 12:10:52 +03:00
|
|
|
log.info(s"Generating Scala AST from Rust definitions.")
|
2020-08-03 17:00:12 +03:00
|
|
|
|
2020-10-22 17:12:28 +03:00
|
|
|
try Cargo.run(args, rustVersion, log)
|
|
|
|
catch {
|
2020-08-03 17:00:12 +03:00
|
|
|
case ex: RuntimeException =>
|
|
|
|
log.error(s"Generation of the Scala AST failed.")
|
|
|
|
throw ex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|