aboutsummaryrefslogtreecommitdiff
path: root/stage2
diff options
context:
space:
mode:
authorChristopher Vogt <oss.nsp@cvogt.org>2016-09-26 19:57:44 +0000
committerChristopher Vogt <oss.nsp@cvogt.org>2016-09-26 22:57:40 -0400
commitdd96e989d81180d4bbb8b30846a53c8239dfe3ac (patch)
tree3430078bd2e9fb2944c27eb87ae3fd53a5eb8937 /stage2
parentd23dd6040fdba7b201e5f15d7027030d5259ef8e (diff)
downloadcbt-dd96e989d81180d4bbb8b30846a53c8239dfe3ac.tar.gz
cbt-dd96e989d81180d4bbb8b30846a53c8239dfe3ac.tar.bz2
cbt-dd96e989d81180d4bbb8b30846a53c8239dfe3ac.zip
Dotty plugin and example project.
Diffstat (limited to 'stage2')
-rw-r--r--stage2/plugins/Dotty.scala107
1 files changed, 107 insertions, 0 deletions
diff --git a/stage2/plugins/Dotty.scala b/stage2/plugins/Dotty.scala
new file mode 100644
index 0000000..1e5addb
--- /dev/null
+++ b/stage2/plugins/Dotty.scala
@@ -0,0 +1,107 @@
+package cbt
+import java.io.File
+import java.net.URL
+import java.nio.file.Files
+import java.nio.file.attribute.FileTime
+
+trait Dotty extends BaseBuild{
+ override def scalacOptions: Seq[String] = Seq()
+
+ private object compileCache extends Cache[Option[File]]
+ override def compile: Option[File] = compileCache{
+ new DottyLib(logger).compile(
+ context.cbtHasChanged,
+ needsUpdate || context.parentBuild.map(_.needsUpdate).getOrElse(false),
+ sourceFiles, compileTarget, compileStatusFile, dependencyClasspath ++ compileClasspath,
+ context.paths.mavenCache, scalacOptions, context.classLoaderCache
+ )
+ }
+}
+
+class DottyLib(logger: Logger){
+ val lib = new Lib(logger)
+ import lib._
+
+ def compile(
+ cbtHasChanged: Boolean,
+ needsRecompile: Boolean,
+ files: Seq[File],
+ compileTarget: File,
+ statusFile: File,
+ classpath: ClassPath,
+ mavenCache: File,
+ scalacOptions: Seq[String] = Seq(),
+ classLoaderCache: ClassLoaderCache
+ ): Option[File] = {
+
+ if(classpath.files.isEmpty)
+ throw new Exception("Trying to compile with empty classpath. Source files: " ++ files.toString)
+
+ if( files.isEmpty ){
+ None
+ }else{
+ if( needsRecompile ){
+ def Resolver(urls: URL*) = MavenResolver(cbtHasChanged, mavenCache, urls: _*)
+ val dotty = Resolver(mavenCentral).bindOne(
+ MavenDependency("ch.epfl.lamp","dotty_2.11","0.1-20160925-b2b475d-NIGHTLY")
+ )
+
+ val cp = classpath ++ dotty.classpath
+
+ val start = System.currentTimeMillis
+
+ val _class = "dotty.tools.dotc.Main"
+ val dualArgs =
+ Seq(
+ "-d", compileTarget.toString
+ )
+ val singleArgs = scalacOptions.map( "-S" ++ _ )
+
+ val code =
+ try{
+ System.err.println("Compiling with Dotty to " ++ compileTarget.toString)
+ compileTarget.mkdirs
+ redirectOutToErr{
+ lib.runMain(
+ _class,
+ dualArgs ++ singleArgs ++ Seq(
+ "-classpath", cp.string // let's put cp last. It so long
+ ) ++ files.map(_.toString),
+ dotty.classLoader(classLoaderCache)
+ )
+ }
+ } catch {
+ case e: Exception =>
+ System.err.println(red("Dotty crashed. Try running it by hand:"))
+ System.out.println(s"""
+java -cp \\
+${dotty.classpath.strings.mkString(":\\\n")} \\
+\\
+${_class} \\
+\\
+${dualArgs.grouped(2).map(_.mkString(" ")).mkString(" \\\n")} \\
+\\
+${singleArgs.mkString(" \\\n")} \\
+\\
+-classpath \\
+${cp.strings.mkString(":\\\n")} \\
+\\
+${files.sorted.mkString(" \\\n")}
+"""
+ )
+ ExitCode.Failure
+ }
+
+ if(code == ExitCode.Success){
+ // write version and when last compilation started so we can trigger
+ // recompile if cbt version changed or newer source files are seen
+ write(statusFile, "")//cbtVersion.getBytes)
+ Files.setLastModifiedTime(statusFile.toPath, FileTime.fromMillis(start) )
+ } else {
+ System.exit(code.integer) // FIXME: let's find a better solution for error handling. Maybe a monad after all.
+ }
+ }
+ Some( compileTarget )
+ }
+ }
+}