Skip to content

Commit

Permalink
Merge pull request #55 from danslapman/feature/custom-extension
Browse files Browse the repository at this point in the history
Allow customization of target files' extension
  • Loading branch information
mkurz authored Nov 17, 2024
2 parents f17fee0 + db943a0 commit af2bf34
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main/scala/spray/boilerplate/BoilerplatePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object BoilerplatePlugin extends AutoPlugin {
val boilerplateSignature = settingKey[String](
"Function that creates signature string to prepend to the generated file (given an input file name). " +
"Will be used to detect boilerplate-generated files")
val boilerplateGeneratedExtension = settingKey[String]("Extension of generated source files")
}

import autoImport._
Expand All @@ -34,20 +35,21 @@ object BoilerplatePlugin extends AutoPlugin {
Compat.watchSourceSettings ++
Seq(
boilerplateSource := sourceDirectory.value / "boilerplate",
boilerplateGenerate := generateFromTemplates(streams.value, boilerplateSignature.value, boilerplateSource.value, sourceManaged.value),
boilerplateGeneratedExtension := "scala",
boilerplateGenerate := generateFromTemplates(streams.value, boilerplateSignature.value, boilerplateSource.value, sourceManaged.value, boilerplateGeneratedExtension.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] = {
def generateFromTemplates(streams: TaskStreams, signature: String, sourceDir: File, targetDir: File, extension: String): 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"
if (!strippedName.contains(".")) s"$strippedName.$extension"
else strippedName
new File(f.getParent, newName)
}
Expand Down

0 comments on commit af2bf34

Please sign in to comment.