Skip to content

Commit

Permalink
fallback when extension does not match (playframework#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocadaruma committed Sep 3, 2016
1 parent 50dd0fa commit f36defd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions sbt-twirl/src/main/scala/play/twirl/sbt/SbtTwirl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object Import {
object TwirlKeys {
val twirlVersion = SettingKey[String]("twirl-version", "Twirl version used for twirl-api dependency")
val templateFormats = SettingKey[Map[String, String]]("twirl-template-formats", "Defined twirl template formats")
val formatFallback = SettingKey[Option[String]]("twirl-format-fallback", "Defined twirl template format fallback for extension")
val templateImports = SettingKey[Seq[String]]("twirl-template-imports", "Extra imports for twirl templates")
val constructorAnnotations = SettingKey[Seq[String]]("twirl-constructor-annotations", "Annotations added to constructors in injectable templates")
@deprecated("No longer supported", "1.2.0")
Expand Down Expand Up @@ -64,6 +65,7 @@ object SbtTwirl extends AutoPlugin {

def defaultSettings: Seq[Setting[_]] = Seq(
templateFormats := defaultFormats,
formatFallback := None,
templateImports := TwirlCompiler.DefaultImports,
constructorAnnotations := Nil,
sourceEncoding := scalacEncoding(scalacOptions.value)
Expand Down Expand Up @@ -98,6 +100,7 @@ object SbtTwirl extends AutoPlugin {
(sourceDirectories in compileTemplates).value,
(target in compileTemplates).value,
templateFormats.value,
formatFallback.value,
templateImports.value,
constructorAnnotations.value,
(includeFilter in compileTemplates).value,
Expand Down
25 changes: 18 additions & 7 deletions sbt-twirl/src/main/scala/play/twirl/sbt/TemplateCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ object TemplateCompiler {
codec: Codec,
useOldParser: Boolean,
log: Logger): Seq[File] =
compile(sourceDirectories, targetDirectory, templateFormats, templateImports, Nil, includeFilter, excludeFilter,
codec, log)
compile(sourceDirectories, targetDirectory, templateFormats, None, templateImports, Nil, includeFilter, excludeFilter,
codec, log)

def compile(
sourceDirectories: Seq[File],
targetDirectory: File,
templateFormats: Map[String, String],
formatFallback: Option[String],
templateImports: Seq[String],
constructorAnnotations: Seq[String],
includeFilter: FileFilter,
Expand All @@ -35,7 +36,7 @@ object TemplateCompiler {

try {
syncGenerated(targetDirectory, codec)
val templates = collectTemplates(sourceDirectories, templateFormats, includeFilter, excludeFilter)
val templates = collectTemplates(sourceDirectories, templateFormats, formatFallback, includeFilter, excludeFilter)
for ((template, sourceDirectory, extension, format) <- templates) {
val imports = formatImports(templateImports, extension)
TwirlCompiler.compile(template, sourceDirectory, targetDirectory, format, imports, constructorAnnotations,
Expand All @@ -53,14 +54,24 @@ object TemplateCompiler {
generatedFiles(targetDirectory).map(GeneratedSource(_, codec)).foreach(_.sync)
}

def collectTemplates(sourceDirectories: Seq[File], templateFormats: Map[String, String], includeFilter: FileFilter, excludeFilter: FileFilter): Seq[(File, File, String, String)] = {
def collectTemplates(
sourceDirectories: Seq[File],
templateFormats: Map[String, String],
formatFallback: Option[String],
includeFilter: FileFilter,
excludeFilter: FileFilter): Seq[(File, File, String, String)] = {

sourceDirectories flatMap { sourceDirectory =>
(sourceDirectory ** includeFilter).get flatMap { file =>
val ext = file.name.split('.').last
if (!excludeFilter.accept(file) && templateFormats.contains(ext))
Some((file, sourceDirectory, ext, templateFormats(ext)))
else

if (!excludeFilter.accept(file)) {
templateFormats.get(ext).orElse(formatFallback).map { format =>
(file, sourceDirectory, ext, format)
}
} else {
None
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion sbt-twirl/src/sbt-test/twirl/compile/build.sbt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
lazy val root = (project in file(".")).enablePlugins(SbtTwirl)
lazy val root = (project in file("."))
.enablePlugins(SbtTwirl)
.settings(TwirlKeys.formatFallback := Some("play.twirl.api.HtmlFormat"))
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ object Test extends App {
test(a.b.html.c.render("world"), "\nHello, world.\n")

test(html.template.render("42"), "\nAnswer: 42\n")

test(conf.template.render("42"), "\nConf: 42\n")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@(a: String)
Conf: @a

0 comments on commit f36defd

Please sign in to comment.