Skip to content

Instantly share code, notes, and snippets.

@freekh
Created December 3, 2013 13:12
Show Gist options
  • Save freekh/7768926 to your computer and use it in GitHub Desktop.
Save freekh/7768926 to your computer and use it in GitHub Desktop.
Emscripten 004
import sbt._
import sbt.Keys._
import java.io.File
import play.Keys._
import play.PlayExceptions._
import play.PlaySourceGenerators
object EmscriptenKeys {
val eccFiles = taskKey[PathFinder]("C/C++ files to be compiled")
val eccCompiler = settingKey[String]("Path to Emscripten compiler")
}
object EmscriptenSettings {
import EmscriptenKeys._
val settings = Seq(
eccCompiler := "emcc",
eccFiles <<= (sourceDirectory in Compile) map ( _ ** "*.cpp" ),
resourceGenerators in Compile <+= (eccFiles, eccCompiler, resourceManaged, cacheDirectory, state) map { (files, eccCompilerString, resourceManaged, cacheDirectory, state) =>
val emscriptenCompiler = new EmscriptenCompiler(eccCompilerString)
val results = files.get.flatMap{ f =>
val name = f.getName.replaceAll("""\..*?$""", "")
val outputDir = resourceManaged / "main" / "public" / name
val cacheFile = cacheDirectory / name
val (previousRelation, previousInfo) = Sync.readInfo(cacheFile)(FileInfo.lastModified.format)
val currentInfos = Map(f -> FileInfo.lastModified(f))
if (previousInfo != currentInfos) { //found new files
//delete current files:
IO.delete( (outputDir ** (name + ".*")).get)
emscriptenCompiler.compile(f, outputDir) match {
case Some(EmscriptenCompilerException(filename, lineNo, colNo, msg)) =>
throw PlaySourceGenerators.reportCompilationError(state,
AssetCompilationException(Some(file(filename)), msg, lineNo, colNo))
case _ =>
}
val outputFiles = (outputDir ** (name + ".*")).get
val relationship = Relation.empty[File, File] ++ outputFiles.map(generatedFile => f -> generatedFile)
Sync.writeInfo(cacheFile, relationship, currentInfos)(FileInfo.lastModified.format)
outputFiles
} else {
previousRelation._2s.toSeq
}
}
results
}
)
}
object EmscriptenPlugin extends Plugin {
import EmscriptenKeys._
override lazy val projectSettings: Seq[Setting[_]] = EmscriptenSettings.settings
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment