diff --git a/sbt-twirl/src/main/scala/play/twirl/sbt/SbtTwirl.scala b/sbt-twirl/src/main/scala/play/twirl/sbt/SbtTwirl.scala index ec28f47f..57ae4398 100644 --- a/sbt-twirl/src/main/scala/play/twirl/sbt/SbtTwirl.scala +++ b/sbt-twirl/src/main/scala/play/twirl/sbt/SbtTwirl.scala @@ -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") @@ -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) @@ -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, diff --git a/sbt-twirl/src/main/scala/play/twirl/sbt/TemplateCompiler.scala b/sbt-twirl/src/main/scala/play/twirl/sbt/TemplateCompiler.scala index 4eae4262..fccd3751 100644 --- a/sbt-twirl/src/main/scala/play/twirl/sbt/TemplateCompiler.scala +++ b/sbt-twirl/src/main/scala/play/twirl/sbt/TemplateCompiler.scala @@ -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, @@ -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, @@ -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 + } } } } diff --git a/sbt-twirl/src/sbt-test/twirl/compile/build.sbt b/sbt-twirl/src/sbt-test/twirl/compile/build.sbt index 2208e4b2..4a4706ce 100644 --- a/sbt-twirl/src/sbt-test/twirl/compile/build.sbt +++ b/sbt-twirl/src/sbt-test/twirl/compile/build.sbt @@ -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")) diff --git a/sbt-twirl/src/sbt-test/twirl/compile/src/test/scala/test/Test.scala b/sbt-twirl/src/sbt-test/twirl/compile/src/test/scala/test/Test.scala index fb6b8cdd..1d6d7c1e 100644 --- a/sbt-twirl/src/sbt-test/twirl/compile/src/test/scala/test/Test.scala +++ b/sbt-twirl/src/sbt-test/twirl/compile/src/test/scala/test/Test.scala @@ -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") }