-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbump-version.sc
executable file
·38 lines (27 loc) · 1.03 KB
/
bump-version.sc
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
//> using scala 3.5.2
val file = if args.length == 0 then "index.html" else args(0)
println(s"Bumping version in $file")
extension (s: String)
def loadLines(enc: String = "UTF-8"): Vector[String] =
var result = Vector.empty[String]
val source = scala.io.Source.fromFile(s, enc)
try result = source.getLines().toVector finally source.close()
result
def incrementVersion =
val key = "version="
val i = s.indexOf(key)
if i < 0 then s
else
val start = i + key.length
val n = s.drop(start).takeWhile(_.isDigit).toIntOption.getOrElse(0)
val current = n.toString
val next = (n + 1).toString
println(s"changing from $key$current to $key$next")
s.patch(start, next, current.length)
def saveIn(fileName: String, enc: String = "UTF-8"): Unit =
val f = java.io.File(fileName)
val pw = java.io.PrintWriter(f, enc)
try pw.write(s) finally pw.close()
val lines = file.loadLines()
val updated = lines.map(_.incrementVersion).mkString("\n")
updated.saveIn(file)