Skip to content

Commit

Permalink
Move comment handling into generator
Browse files Browse the repository at this point in the history
  • Loading branch information
taig committed Jan 22, 2025
1 parent 1201c9c commit b48bfd3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package io.taig.blowout

import sbt.File

final case class BlowoutGenerator(target: File, content: () => String)
final case class BlowoutGenerator(
target: File,
content: () => String,
comment: BlowoutComment = BlowoutComment.Disabled
)

object BlowoutGenerator {
def lzy(target: File, content: => String): BlowoutGenerator = BlowoutGenerator(target, () => content)
def lzy(target: File, content: => String, comment: BlowoutComment = BlowoutComment.Disabled): BlowoutGenerator =
BlowoutGenerator(target, () => content, comment)

def strict(target: File, content: String): BlowoutGenerator = BlowoutGenerator(target, () => content)
def strict(target: File, content: String, comment: BlowoutComment = BlowoutComment.Disabled): BlowoutGenerator =
BlowoutGenerator(target, () => content, comment)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package io.taig.blowout
import sbt._

trait BlowoutKeys {
lazy val blowoutHeader = settingKey[File => Option[String]](
"Optional comment header that is added to installed files, depending on their file extension"
)
lazy val blowoutHeader = settingKey[List[String]]("Optional comment header that is added to installed files")

lazy val blowoutGenerators = settingKey[List[BlowoutGenerator]](
"Generators that describe where files should be installed to by blowoutGenerate and what content they should have"
Expand Down
19 changes: 8 additions & 11 deletions modules/core/src/main/scala/io/taig/blowout/BlowoutPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ object BlowoutPlugin extends AutoPlugin {
object autoImport extends BlowoutKeys {
type BlowoutGenerator = io.taig.blowout.BlowoutGenerator
val BlowoutGenerator = io.taig.blowout.BlowoutGenerator

type BlowoutComment = io.taig.blowout.BlowoutComment
val BlowoutComment = io.taig.blowout.BlowoutComment
}

import autoImport._
Expand All @@ -21,18 +24,12 @@ object BlowoutPlugin extends AutoPlugin {

override def projectSettings: Seq[Def.Setting[_]] = Def.settings(
blowoutGenerators := Nil,
blowoutHeader := { file =>
val message = List(
"This file was automatically generated by sbt-blowout and should not be edited manually.",
"Instead, run blowoutGenerate after making the desired changes to your build definition."
)

PartialFunction.condOpt((file.name, file.ext)) { case ("Dockerfile", "") | (_, "conf" | "yml" | "yaml") =>
message.map("# " + _).mkString("\n")
}
},
blowoutHeader := List(
"This file was automatically generated by sbt-blowout and should not be edited manually.",
"Instead, run blowoutGenerate after making the desired changes to your build definition."
),
blowoutRender := { generator =>
blowoutHeader.value(generator.target).map(_ + "\n").getOrElse("") + generator.content()
Some(blowoutHeader.value).filter(_.nonEmpty).flatMap(generator.comment).getOrElse("") + generator.content()
},
blowoutGenerate := {
blowoutGenerators.value.map { generator =>
Expand Down
15 changes: 15 additions & 0 deletions modules/core/src/main/scala/io/taig/blowout/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.taig

package object blowout {
type BlowoutComment = List[String] => Option[String]

object BlowoutComment {
val Disabled: BlowoutComment = _ => None

def prefixed(prefix: String): BlowoutComment = lines =>
Some(lines.map(line => s"$prefix$line").mkString("", "\n", "\n"))

val Hash: BlowoutComment = prefixed("# ")
val Slash: BlowoutComment = prefixed("// ")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import sbt.File

object BlowoutJsonGenerator {
def apply(target: File, content: () => Json, printer: Printer = Printer.spaces2): BlowoutGenerator =
BlowoutGenerator(target, () => printer.print(content()))
BlowoutGenerator(target, () => printer.print(content()), comment = BlowoutComment.Disabled)

def lzy(target: File, content: => Json, printer: Printer = Printer.spaces2): BlowoutGenerator =
BlowoutGenerator(target, () => printer.print(content))
BlowoutGenerator(target, () => printer.print(content), comment = BlowoutComment.Disabled)

def strict(target: File, content: Json, printer: Printer = Printer.spaces2): BlowoutGenerator =
BlowoutJsonGenerator(target, () => content, printer)
BlowoutGenerator.strict(target, printer.print(content), comment = BlowoutComment.Disabled)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ import sbt.File
object BlowoutYamlGenerator {
val DefaultPrinter: Printer = Printer.spaces2.copy(preserveOrder = true, splitLines = false)

def apply(target: File, content: () => Json, printer: Printer = DefaultPrinter): BlowoutGenerator =
BlowoutGenerator(target, () => printer.pretty(content()))
def apply(
target: File,
content: () => Json,
printer: Printer = DefaultPrinter,
comment: BlowoutComment = BlowoutComment.Hash
): BlowoutGenerator = BlowoutGenerator(target, () => printer.pretty(content()), comment)

def lzy(target: File, content: => Json, printer: Printer = DefaultPrinter): BlowoutGenerator =
BlowoutGenerator(target, () => printer.pretty(content))
def lzy(
target: File,
content: => Json,
printer: Printer = DefaultPrinter,
comment: BlowoutComment = BlowoutComment.Hash
): BlowoutGenerator = BlowoutGenerator(target, () => printer.pretty(content), comment)

def strict(target: File, content: Json, printer: Printer = DefaultPrinter): BlowoutGenerator =
BlowoutYamlGenerator(target, () => content, printer)
def strict(
target: File,
content: Json,
printer: Printer = DefaultPrinter,
comment: BlowoutComment = BlowoutComment.Hash
): BlowoutGenerator = BlowoutGenerator.strict(target, printer.pretty(content), comment)
}

0 comments on commit b48bfd3

Please sign in to comment.