Skip to content

Commit

Permalink
Initial version of Dart compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
nelson.silva committed Nov 17, 2011
1 parent 0dad42d commit b365ca5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
64 changes: 64 additions & 0 deletions framework/play/src/main/scala/play/core/dart/DartCompiler.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package play.core.dart

import com.google.common.io.CharStreams
import com.google.dart.compiler.ast.DartUnit
import com.google.dart.compiler._
import com.google.dart.compiler.{ DartCompiler => Compiler }
import com.google.dart.compiler.CommandLineOptions.CompilerOptions
import java.io._
import play.api._

object DartCompiler {
def compile(source: File): (String, Seq[File]) = {

val options = new CompilerOptions()
var config: CompilerConfiguration = new DefaultCompilerConfiguration(options);

val outputDirectory = config.getOutputDirectory
val provider = new DefaultDartArtifactProvider(outputDirectory)

val listener = new DartCompilerListener {

var errors = List[CompilationError]()

override def onError(event: DartCompilationError) {
errors ::= CompilationError(event.getMessage, event.getLineNumber - 1, event.getColumnNumber - 1)
}

override def unitCompiled(unit: DartUnit) {}
}

// Compile the Dart application and its dependencies.
val lib = new UrlLibrarySource(source);
config = new DelegatingCompilerConfiguration(config) {
override def expectEntryPoint = true
}

try {

Compiler.compileLib(lib, config, provider, listener) match {
case null =>
val r = provider.getArtifactReader(lib, "", config.getBackends.get(0).getAppExtension);
val js = CharStreams.toString(r);
(js, Seq(source))
case _ =>
val err = listener.errors.head
throw CompilationException(err.message, source, err.atLine, err.atColumn)
}
} catch {
case e: IOException =>
throw PlayException("Compilation error", e.getMessage)
}

}
}

case class CompilationError(message: String, atLine: Int, atColumn: Int)

case class CompilationException(message: String, dartFile: File, atLine: Int, atColumn: Int)
extends PlayException("Compilation error", message) with PlayException.ExceptionSource {
def line = Some(atLine)
def position = Some(atColumn)
def input = Some(scalax.file.Path(dartFile))
def sourceName = Some(dartFile.getAbsolutePath)
}
7 changes: 7 additions & 0 deletions framework/play/src/main/scala/play/sbt/SbtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ object PlayProject extends Plugin {
{ name => name.replace(".coffee", ".js") },
{ (coffeeFile, minify) => (play.core.coffeescript.CoffeescriptCompiler.compile(coffeeFile), Seq(coffeeFile)) })

val DartCompiler = AssetsCompiler("dart",
{ assets => (assets ** "*.dart") },
{ name => name.replace(".dart", ".js") },
{ (dartFile, minify) => play.core.dart.DartCompiler.compile(dartFile) })

// ----- Post compile (need to be refactored and fully configurable)

val PostCompile = (sourceDirectory in Compile, dependencyClasspath in Compile, compile in Compile, javaSource in Compile, sourceManaged in Compile, classDirectory in Compile) map { (src, deps, analysis, javaSrc, srcManaged, classes) =>
Expand Down Expand Up @@ -977,6 +982,8 @@ object PlayProject extends Plugin {

resourceGenerators in Compile <+= JavascriptCompiler,

resourceGenerators in Compile <+= DartCompiler,

minify := false,

playResourceDirectories := Seq.empty[File],
Expand Down
8 changes: 7 additions & 1 deletion framework/project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ object PlayBuild extends Build {
"org.specs2" %% "specs2" % "1.6.1" % "test",
"com.novocode" % "junit-interface" % "0.7" % "test",
"org.seleniumhq.selenium" % "selenium-chrome-driver" % "2.11.0" % "test",
"org.seleniumhq.selenium" % "selenium-htmlunit-driver" % "2.11.0" % "test"
"org.seleniumhq.selenium" % "selenium-htmlunit-driver" % "2.11.0" % "test",
// Dart dependencies
"com.google.dart" % "compiler" % "r1556",
"com.google.dart" % "corelib" % "r1556",
"com.google.dart" % "htmllib" % "r1556",
"com.google.dart" % "jsonlib" % "r1556",
"args4j" % "args4j" % "2.0.12"
)

val templatesDependencies = Seq(
Expand Down

0 comments on commit b365ca5

Please sign in to comment.