Skip to content

Commit

Permalink
twirl that works with scala API
Browse files Browse the repository at this point in the history
  • Loading branch information
rockjam committed Apr 30, 2018
1 parent 0ea7902 commit 0daf9ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 4 additions & 1 deletion twirllib/src/mill/twirllib/TwirlModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ trait TwirlModule extends mill.Module {
MavenRepository("https://repo1.maven.org/maven2")
),
Lib.depToDependency(_, "2.12.4"),
Seq(ivy"com.typesafe.play::twirl-compiler:${twirlVersion()}")
Seq(
ivy"com.typesafe.play::twirl-compiler:${twirlVersion()}",
ivy"org.scala-lang.modules::scala-parser-combinators:1.0.5"
)
)
}

Expand Down
21 changes: 16 additions & 5 deletions twirllib/src/mill/twirllib/TwirlWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TwirlWorker {
case Some((sig, instance)) if sig == classloaderSig => instance
case _ =>
val cl = new URLClassLoader(twirlClasspath.map(_.toIO.toURI.toURL).toArray)
val twirlCompilerClass = cl.loadClass("play.japi.twirl.compiler.TwirlCompiler")
val twirlCompilerClass = cl.loadClass("play.twirl.compiler.TwirlCompiler")
// Use the Java API (available in Twirl 1.3+)
// Using reflection on a method with "Seq[String] = Nil" parameter type does not seem to work.

Expand All @@ -34,8 +34,16 @@ class TwirlWorker {
classOf[java.io.File],
classOf[java.io.File],
classOf[java.lang.String],
classOf[util.Collection[java.lang.String]],
classOf[util.List[java.lang.String]])
cl.loadClass("scala.collection.Seq"),
cl.loadClass("scala.collection.Seq"),
cl.loadClass("scala.io.Codec"),
classOf[Boolean]
)

val defaultAdditionalImportsMethod = twirlCompilerClass.getMethod("compile$default$5")
val defaultConstructorAnnotationsMethod = twirlCompilerClass.getMethod("compile$default$6")
val defaultCodecMethod = twirlCompilerClass.getMethod("compile$default$7")
val defaultFlagMethod = twirlCompilerClass.getMethod("compile$default$8")

val instance = new TwirlWorkerApi {
override def compileTwirl(source: File,
Expand All @@ -48,8 +56,11 @@ class TwirlWorker {
sourceDirectory,
generatedDirectory,
formatterType,
additionalImports.asJava,
constructorAnnotations.asJava)
defaultAdditionalImportsMethod.invoke(null),
defaultConstructorAnnotationsMethod.invoke(null),
defaultCodecMethod.invoke(null),
defaultFlagMethod.invoke(null)
)
}
}
twirlInstanceCache = Some((classloaderSig, instance))
Expand Down

0 comments on commit 0daf9ca

Please sign in to comment.