| 1 |
#!/usr/bin/env amm |
| 2 |
// This script requires Ammonite, running Scala 2.13 and Java 11+ |
| 3 |
|
| 4 |
import $ivy.`info.picocli:picocli:4.7+` |
| 5 |
|
| 6 |
import java.nio.file.Files |
| 7 |
import java.nio.file.Path |
| 8 |
import java.nio.file.StandardCopyOption |
| 9 |
import java.util.Optional |
| 10 |
import java.util.concurrent.Callable |
| 11 |
import picocli.CommandLine |
| 12 |
import picocli.CommandLine.Command |
| 13 |
import picocli.CommandLine.{Option => PicoOption} |
| 14 |
import picocli.CommandLine.Parameters |
| 15 |
import scala.io.Source |
| 16 |
import scala.jdk.StreamConverters._ |
| 17 |
import scala.util.Using |
| 18 |
|
| 19 |
sealed abstract class Subheader(val text: String) |
| 20 |
object Subheader { |
| 21 |
final case object Gameplay extends Subheader("Gameplay") |
| 22 |
final case object Interface extends Subheader("User Interface") |
| 23 |
final case object Internal extends Subheader("Internal") |
| 24 |
final case object Documentation |
| 25 |
extends Subheader("Documentation / Translation") |
| 26 |
|
| 27 |
val list = Seq( |
| 28 |
Subheader.Gameplay, |
| 29 |
Subheader.Interface, |
| 30 |
Subheader.Internal, |
| 31 |
Subheader.Documentation |
| 32 |
) |
| 33 |
|
| 34 |
def fromString(text: String): Subheader = text match { |
| 35 |
case Gameplay.text => Gameplay |
| 36 |
case Interface.text => Interface |
| 37 |
case Internal.text => Internal |
| 38 |
case Documentation.text => Documentation |
| 39 |
case "Documentation" | "Translation" => Documentation |
| 40 |
case _ => throw new IllegalArgumentException(s"unknown subheader: $text") |
| 41 |
} |
| 42 |
} |
| 43 |
implicit def SubheaderIsOrdered(subheader: Subheader): Ordered[Subheader] = { |
| 44 |
new Ordered[Subheader]() { |
| 45 |
def compare(other: Subheader): Int = { |
| 46 |
Subheader.list.indexOf(subheader).compare( Subheader.list.indexOf(other)) |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
final class Entry( |
| 52 |
val text: Seq[String], |
| 53 |
val version: String, |
| 54 |
val subheader: Subheader |
| 55 |
) {} |
| 56 |
|
| 57 |
@Command(name = "apply-changelog") |
| 58 |
object ApplyChangelog extends java.util.concurrent.Callable[Int] { |
| 59 |
def apply(): Int = { |
| 60 |
val clDir = Path.of("CHANGELOG.d") |
| 61 |
val changeFiles = Files.list(clDir).toScala(LazyList) |
| 62 |
.filter(_ != clDir.resolve("apply-changelog.sc")) // skip this script |
| 63 |
.filter(_ != clDir.resolve("template.md")) // skip the template |
| 64 |
val entries = changeFiles |
| 65 |
.flatMap(p => |
| 66 |
Source.fromFile(p.toFile).getLines |
| 67 |
.map(_.stripTrailing) |
| 68 |
// do actual parsing |
| 69 |
.scanLeft[(Seq[String], Option[String], Option[Subheader])]( |
| 70 |
(Seq(), None, None) |
| 71 |
) {(s, l) => |
| 72 |
if(l.startsWith("## ")) // version header |
| 73 |
(Seq(), Some(l.substring(3)), None) |
| 74 |
else if(l.startsWith("###### ")) // subheader |
| 75 |
(Seq(), s._2, Some(Subheader.fromString(l.substring(7)))) |
| 76 |
else if(l.startsWith("- ")) // start entry |
| 77 |
(Seq(l), s._2, s._3) |
| 78 |
else if(l.startsWith(" ") && s._1.nonEmpty) // continue entry |
| 79 |
(s._1 :+ l, s._2, s._3) |
| 80 |
else if(l == "") // blank line |
| 81 |
(Seq(), s._2, s._3) |
| 82 |
else // bad line |
| 83 |
throw new RuntimeException(s"line has bad format: '$l'") |
| 84 |
} |
| 85 |
// drop non-entries |
| 86 |
.filter(_._1.nonEmpty) |
| 87 |
// drop entries that are continued later |
| 88 |
.concat(Seq((Seq(), None, None))) // pad for sliding |
| 89 |
.sliding(2) |
| 90 |
.filter(l => l(0)._1.sizeIs >= l(1)._1.size) |
| 91 |
.map(_(0)) |
| 92 |
// assume Unreleased version |
| 93 |
.map(l => (l._1, l._2.getOrElse("Unreleased"), l._3)) |
| 94 |
// ensure subheader is specified |
| 95 |
.map(l => (l._1, l._2, l._3.getOrElse{ |
| 96 |
throw new RuntimeException("entry does not have a subheader")})) |
| 97 |
// convert to an Entry object |
| 98 |
.map(l => new Entry(l._1, l._2, l._3)) |
| 99 |
) |
| 100 |
|
| 101 |
// only support adding to the 'Unreleased' version for now |
| 102 |
entries.map(_.version).find(_ != "Unreleased").foreach(v => |
| 103 |
throw new RuntimeException(s"only 'Unreleased' version supported: '$v'")) |
| 104 |
|
| 105 |
entries.map(_.version).find(_ == "Change or delete me.").foreach{t => |
| 106 |
throw new RuntimeException(s"encountered placeholder entry: '$t'") |
| 107 |
} |
| 108 |
|
| 109 |
val groupedEntries = entries.groupBy(_.subheader) |
| 110 |
val targetSubheaders = Subheader.list |
| 111 |
.filter(h => groupedEntries.contains(h)) |
| 112 |
|
| 113 |
Using.resources( |
| 114 |
Files.newBufferedReader(Path.of("CHANGELOG.md")), |
| 115 |
Files.newBufferedWriter(Path.of("CHANGELOG_new.md")) |
| 116 |
) {(clOld, clNew) => |
| 117 |
var curVersion: Option[String] = None |
| 118 |
var curHeader: Option[Subheader] = None |
| 119 |
var curLine: String = clOld.readLine() |
| 120 |
var blanks: Int = 0 |
| 121 |
def nextLine() = { |
| 122 |
if(curLine == "") { |
| 123 |
blanks += 1 |
| 124 |
} |
| 125 |
else if(curLine != null) { |
| 126 |
(0 until blanks).foreach(_ => clNew.newLine()) |
| 127 |
blanks = 0 |
| 128 |
clNew.write(curLine) |
| 129 |
clNew.newLine() |
| 130 |
} |
| 131 |
curLine = clOld.readLine() |
| 132 |
curLine |
| 133 |
} |
| 134 |
|
| 135 |
for(targetHeader <- targetSubheaders) { |
| 136 |
// seek to the version |
| 137 |
if(!curVersion.exists(_ == "Unreleased")) { |
| 138 |
while(curLine != null && !curLine.startsWith("## ")) { |
| 139 |
nextLine() |
| 140 |
} |
| 141 |
if(curLine == "## Unreleased") { |
| 142 |
nextLine() |
| 143 |
} |
| 144 |
else { |
| 145 |
clNew.write("## Unreleased") |
| 146 |
clNew.newLine() |
| 147 |
blanks = 2 // in case we're prepending to the beginning of the file |
| 148 |
} |
| 149 |
curVersion = Some("Unreleased") |
| 150 |
curHeader = None |
| 151 |
} |
| 152 |
|
| 153 |
// seek to the subheader |
| 154 |
while(!curHeader.exists(_ == targetHeader)) { |
| 155 |
while(curLine != null && |
| 156 |
!curLine.startsWith("## ") && |
| 157 |
!curLine.startsWith("###### ") |
| 158 |
) { |
| 159 |
nextLine() |
| 160 |
} |
| 161 |
|
| 162 |
if(curLine.startsWith("###### ")) { |
| 163 |
curHeader = Some(Subheader.fromString(curLine.substring(7))) |
| 164 |
} |
| 165 |
else if(curLine.startsWith("## ")) { |
| 166 |
curHeader = None |
| 167 |
} |
| 168 |
|
| 169 |
if(curHeader.exists(_ <= targetHeader)) { |
| 170 |
do { |
| 171 |
nextLine() |
| 172 |
} while(curLine != null && curLine.matches("[- ] .+")) |
| 173 |
} |
| 174 |
else { |
| 175 |
clNew.newLine() |
| 176 |
clNew.write("###### " + targetHeader.text) |
| 177 |
clNew.newLine() |
| 178 |
curHeader = Some(targetHeader) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
groupedEntries(targetHeader).foreach(_.text.foreach{t => |
| 183 |
clNew.write(t) |
| 184 |
clNew.newLine() |
| 185 |
}) |
| 186 |
} |
| 187 |
|
| 188 |
(0 until blanks).foreach(_ => clNew.newLine()) |
| 189 |
clNew.write(curLine) |
| 190 |
clNew.newLine() |
| 191 |
clOld.transferTo(clNew) |
| 192 |
} |
| 193 |
|
| 194 |
Files.move( |
| 195 |
Path.of("CHANGELOG_new.md"), |
| 196 |
Path.of("CHANGELOG.md"), |
| 197 |
StandardCopyOption.REPLACE_EXISTING |
| 198 |
) |
| 199 |
|
| 200 |
changeFiles.foreach(Files.delete(_)) |
| 201 |
|
| 202 |
0 |
| 203 |
} |
| 204 |
|
| 205 |
def call = apply |
| 206 |
} |
| 207 |
|
| 208 |
@main |
| 209 |
def main(args: String*): Unit = |
| 210 |
System.exit(new CommandLine(ApplyChangelog) |
| 211 |
.execute(args: _*)) |