Skip to content

Commit

Permalink
make this an AutoPlugin, fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
jrudolph committed Mar 1, 2016
1 parent 6ccb744 commit 11b21a1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ This now expands correctly to

Put

addSbtPlugin("io.spray" % "sbt-boilerplate" % "0.5.9")
addSbtPlugin("io.spray" % "sbt-boilerplate" % "0.6.0")

into your `plugins.sbt` and add
into your `plugins.sbt`. sbt-boilerplate is an `AutoPlugin` which needs to be enabled using

Boilerplate.settings

to your `build.sbt`.
```scala
enablePlugins(spray.boilerplate.BoilerplatePlugin)
```

The templates have to be put into the `src/main/boilerplate` directory and the file name
must end with `.template`. The generated files will be put into the same hierarchy as they
Expand All @@ -93,8 +93,7 @@ filename has no extension ".scala" is added automatically.

## Known issues

* The maximum number of arguments, 22, is hard-coded.
* Instances for 0 arguments have to be supplied manually.
* Instances for zero arguments have to be supplied manually.

## Projects using sbt-boilerplate

Expand Down
85 changes: 45 additions & 40 deletions src/main/scala/spray/boilerplate/BoilerplatePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,56 @@ package spray.boilerplate
import sbt._
import Keys._

object BoilerplatePlugin extends Plugin {
object Boilerplate {
object BoilerplatePlugin extends AutoPlugin {
override def trigger: PluginTrigger = noTrigger
override def `requires`: Plugins = empty

object autoImport {
val boilerplateGenerate = taskKey[Seq[File]]("Generates boilerplate from template files")
val boilerplateSource = settingKey[File]("Default directory containing boilerplate template sources.")
}

import autoImport._

override def projectSettings: Seq[Def.Setting[_]] =
inConfig(Compile)(rawBoilerplateSettings) ++ inConfig(Test)(rawBoilerplateSettings)

private def rawBoilerplateSettings: Seq[Setting[_]] = {
val inputFilter = "*.template"
Seq(
boilerplateSource := sourceDirectory.value / "boilerplate",
watchSources in Defaults.ConfigGlobal ++= ((boilerplateSource.value ** inputFilter) --- (boilerplateSource.value ** excludeFilter.value ** inputFilter)).get,
boilerplateGenerate := generateFromTemplates(streams.value, boilerplateSource.value, sourceManaged.value),
mappings in packageSrc ++= managedSources.value pair (Path.relativeTo(sourceManaged.value) | Path.flat),
sourceGenerators <+= boilerplateGenerate)
}

def generateFromTemplates(streams: TaskStreams, sourceDir: File, targetDir: File): Seq[File] = {
val files = sourceDir ** "*.template"

private def rawBoilerplateSettings: Seq[Setting[_]] = {
val inputFilter = "*.template"
Seq(
boilerplateSource := sourceDirectory.value / "boilerplate",
watchSources in Defaults.ConfigGlobal ++= ((boilerplateSource.value ** inputFilter) --- (boilerplateSource.value ** excludeFilter.value ** inputFilter)).get,
boilerplateGenerate := generateFromTemplates(streams.value, boilerplateSource.value, sourceManaged.value),
mappings in packageSrc ++= managedSources.value x (Path.relativeTo(sourceManaged.value) | Path.flat),
sourceGenerators <+= boilerplateGenerate)
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)
}

val settings =
inConfig(Compile)(rawBoilerplateSettings) ++ inConfig(Test)(rawBoilerplateSettings)

def generateFromTemplates(streams: TaskStreams, sourceDir: File, targetDir: File): Seq[File] = {
val files = sourceDir ** "*.template"

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)
}

val mapping = (files x rebase(sourceDir, targetDir)).map {
case (orig, target) (orig, changeExtension(target))
}

mapping foreach {
case (templateFile, target)
if (templateFile.lastModified > target.lastModified) {
streams.log.info("Generating '%s'" format target.getName)
val template = IO.read(templateFile)
IO.write(target, Generator.generateFromTemplate(template, 22))
} else
streams.log.debug("Template '%s' older than target. Ignoring." format templateFile.getName)
}

mapping.map(_._2)
val mapping = (files pair rebase(sourceDir, targetDir)).map {
case (orig, target) (orig, changeExtension(target))
}

mapping foreach {
case (templateFile, target)
if (templateFile.lastModified > target.lastModified) {
streams.log.info("Generating '%s'" format target.getName)
val template = IO.read(templateFile)
IO.write(target, Generator.generateFromTemplate(template, 22))
} else
streams.log.debug("Template '%s' older than target. Ignoring." format templateFile.getName)
}

mapping.map(_._2)
}
}

0 comments on commit 11b21a1

Please sign in to comment.