aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/cc/spray/boilerplate/BoilerplatePlugin.scala
blob: 0d8b9c3d4304546dc580d6b4bb53e892c484a61b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package cc.spray.boilerplate

import sbt._
import Keys._

object BoilerplatePlugin extends Plugin {
  object Boilerplate {
    val boilerplateGenerate = TaskKey[Seq[File]]("boilerplate-generate", "Generates boilerplate from template files")

    val settings = seq(
      sourceDirectory in boilerplateGenerate <<= (sourceDirectory in Compile) / "boilerplate",

      target in boilerplateGenerate <<= (sourceManaged in Compile),

      boilerplateGenerate <<= (streams, sourceDirectory in boilerplateGenerate, target in boilerplateGenerate) map Generator.generateFromTemplates,

      (sourceGenerators in Compile) <+= boilerplateGenerate,
      (managedSourceDirectories in Compile) <+= target in boilerplateGenerate,

      // watch sources support
      includeFilter in boilerplateGenerate := "*.template",
      excludeFilter in boilerplateGenerate <<= excludeFilter in Global,
      watch(sourceDirectory in boilerplateGenerate, includeFilter in boilerplateGenerate, excludeFilter in boilerplateGenerate),

      // add managed sources to the packaged sources
      mappings in (Compile, packageSrc) <++=
        (sourceManaged in Compile, managedSources in Compile) map { (base, srcs) =>
          (srcs x (Path.relativeTo(base) | Path.flat))
        }
    )

    def watch(sourceDirKey: SettingKey[File], filterKey: SettingKey[FileFilter], excludeKey: SettingKey[FileFilter]) =
      watchSources <++= (sourceDirKey, filterKey, excludeKey) map descendents
    def descendents(sourceDir: File, filt: FileFilter, excl: FileFilter) =
      sourceDir.descendantsExcept(filt, excl).get
  }
}