From 3b76a1e450f8d03b2912ee4bc5dea8bd5732ab78 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Thu, 8 Mar 2018 00:19:34 -0800 Subject: Add support for multiple boilerplate source directories --- build.sbt | 2 +- src/main/scala-sbt-0.13/Compat.scala | 4 +-- src/main/scala-sbt-1.0/Compat.scala | 7 ++-- .../spray/boilerplate/BoilerplatePlugin.scala | 38 +++++++++++++--------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/build.sbt b/build.sbt index 5c6ab0a..f70201b 100644 --- a/build.sbt +++ b/build.sbt @@ -4,7 +4,7 @@ lazy val root = (project in file(".")) crossSbtVersions := Vector("0.13.16", "1.0.0"), name := "sbt-boilerplate", organization := "io.spray", - version := "0.6.2-SNAPSHOT", + version := "0.7.0-SNAPSHOT", description := "An SBT plugin for simple generation of boilerplate", startYear := Some(2012), homepage := Some(url("http://github.com/sbt/sbt-boilerplate")), diff --git a/src/main/scala-sbt-0.13/Compat.scala b/src/main/scala-sbt-0.13/Compat.scala index 6138f7c..1756b20 100644 --- a/src/main/scala-sbt-0.13/Compat.scala +++ b/src/main/scala-sbt-0.13/Compat.scala @@ -10,12 +10,12 @@ import sbt._ import Keys._ object Compat { - private val boilerplateSource = settingKey[File]("Default directory containing boilerplate template sources.") + private val boilerplateSourceDirectories = settingKey[Seq[File]]("Directories containing boilerplate template sources.") private val inputFilter = "*.template" def allPaths(f: File) = f.*** def watchSourceSettings = Def.settings { - Seq(watchSources in Defaults.ConfigGlobal ++= ((boilerplateSource.value ** inputFilter) --- (boilerplateSource.value ** excludeFilter.value ** inputFilter)).get) + Seq(watchSources in Defaults.ConfigGlobal ++= ((boilerplateSourceDirectories.value ** inputFilter) --- (boilerplateSourceDirectories.value ** excludeFilter.value ** inputFilter)).get) } } diff --git a/src/main/scala-sbt-1.0/Compat.scala b/src/main/scala-sbt-1.0/Compat.scala index f1a9d47..ca7cf43 100644 --- a/src/main/scala-sbt-1.0/Compat.scala +++ b/src/main/scala-sbt-1.0/Compat.scala @@ -11,20 +11,21 @@ import Keys._ import sbt.internal.io.Source object Compat { - private val boilerplateSource = settingKey[File]("Default directory containing boilerplate template sources.") + private val boilerplateSourceDirectories = settingKey[Seq[File]]("Directories containing boilerplate template sources.") private val inputFilter = """.*\.template""".r def allPaths(f: File) = f.allPaths def watchSourceSettings = Def.settings { Seq( - watchSources in Defaults.ConfigGlobal += + watchSources in Defaults.ConfigGlobal ++= boilerplateSourceDirectories.value map { dir => new Source( - boilerplateSource.value, + dir, new NameFilter { override def accept(name: String): Boolean = inputFilter.pattern.matcher(name).matches() }, NothingFilter) + } ) } } diff --git a/src/main/scala/spray/boilerplate/BoilerplatePlugin.scala b/src/main/scala/spray/boilerplate/BoilerplatePlugin.scala index 50bcfd5..b84b428 100644 --- a/src/main/scala/spray/boilerplate/BoilerplatePlugin.scala +++ b/src/main/scala/spray/boilerplate/BoilerplatePlugin.scala @@ -18,6 +18,8 @@ object BoilerplatePlugin extends AutoPlugin { object autoImport { val boilerplateGenerate = taskKey[Seq[File]]("Generates boilerplate from template files") + val boilerplateSourceDirectories = settingKey[Seq[File]]("Directories containing boilerplate template sources.") + @deprecated("Use boilerplateSourceDirectories instead.", "0.7.0") val boilerplateSource = settingKey[File]("Default directory containing boilerplate template sources.") val boilerplateSignature = settingKey[String]( "Function that creates signature string to prepend to the generated file (given an input file name). " + @@ -34,27 +36,33 @@ object BoilerplatePlugin extends AutoPlugin { Compat.watchSourceSettings ++ Seq( boilerplateSource := sourceDirectory.value / "boilerplate", - boilerplateGenerate := generateFromTemplates(streams.value, boilerplateSignature.value, boilerplateSource.value, sourceManaged.value), + boilerplateSourceDirectories := Seq(boilerplateSource.value), + boilerplateGenerate := + generateFromTemplates(streams.value, boilerplateSignature.value, boilerplateSourceDirectories.value, sourceManaged.value), mappings in packageSrc ++= managedSources.value pair (Path.relativeTo(sourceManaged.value) | Path.flat), sourceGenerators += boilerplateGenerate) } - def generateFromTemplates(streams: TaskStreams, signature: String, sourceDir: File, targetDir: File): Seq[File] = { - val files = sourceDir ** "*.template" - streams.log.debug(s"Found ${files.get.size} template files in $sourceDir.") - - def changeExtension(f: File): File = { - val (_, name) = f.getName.reverse.span(_ != '.') - val strippedName = name.drop(1).reverse.toString - val newName = - if (!strippedName.contains(".")) s"$strippedName.scala" - else strippedName - new File(f.getParent, newName) + def generateFromTemplates(streams: TaskStreams, signature: String, sourceDirs: Seq[File], targetDir: File): Seq[File] = { + def generate(sourceDir: File): Seq[(File, File)] = { + val files = sourceDir ** "*.template" + streams.log.debug(s"Found ${files.get.size} template files in $sourceDir.") + + def changeExtension(f: File): File = { + val (_, name) = f.getName.reverse.span(_ != '.') + val strippedName = name.drop(1).reverse.toString + val newName = + if (!strippedName.contains(".")) s"$strippedName.scala" + else strippedName + new File(f.getParent, newName) + } + + (files pair Path.rebase(sourceDir, targetDir)).map { + case (orig, target) ⇒ (orig, changeExtension(target)) + } } - val mapping = (files pair Path.rebase(sourceDir, targetDir)).map { - case (orig, target) ⇒ (orig, changeExtension(target)) - } + val mapping = sourceDirs.flatMap(generate) val newFiles = mapping.map(_._2) clearTargetDir(streams, targetDir, signature, newFiles) -- cgit v1.2.3