aboutsummaryrefslogtreecommitdiff
path: root/stage2/plugins/GeneratedSections.scala
blob: dc065cf37ac86a3b2d0809149bb2a64bf0626040 (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
package cbt
import java.nio.file.Files._
trait GeneratedSections extends BaseBuild{
  def generatedSectionStartMarker( name: String ) = s"AUTO GENERATED SECTION BEGIN: $name "
  def generatedSectionEndMarker( name: String ) = s"AUTO GENERATED SECTION END: $name "
  assert(
    generatedSectionStartMarker("foo").endsWith(" "),
    "generatedSectionStartMarker needs to end with a space character"
  )
  assert(
    generatedSectionEndMarker("foo").endsWith(" "),
    "generatedSectionEndMarker needs to end with a space character"
  )

  def replacements: Seq[(String, String)]

  def generate = {
    def replaceSections(subject: String, sections: Seq[(String, String)]): String = {
      sections.headOption.map{
        case (name, replacement) =>
          replaceSections(
            s"(?s)(\n[^\n]*AUTO GENERATED SECTION BEGIN: $name [^\n]*\n).*(\n[^\n]*AUTO GENERATED SECTION END: $name [^\n]*\n)"
              .r.replaceAllIn( subject, m => m.group(1) ++ replacement ++ m.group(2) ),
            sections.tail
          )
      }.getOrElse(subject)
    }

    val updated = lib.transformFiles( sourceFiles, replaceSections( _, replacements ) )

    logger.log("generated-sections","Updated:" + updated.map(_ ++ "\n").mkString)
  }
}