Skip to content

Commit

Permalink
Add just bump-version
Browse files Browse the repository at this point in the history
- script finds and bumps any besom dependency in any project.scala file to given version and updated version.txt
  • Loading branch information
pawelprazak committed Dec 19, 2023
1 parent e4d124a commit 3df40e6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ compile-scripts: publish-local-codegen
clean-scripts:
scala-cli --power clean scripts
bump-version new-version:
scala-cli run scripts/Version.scala -- bump {{new-version}}
####################
# Troubleshooting
####################
Expand Down
72 changes: 72 additions & 0 deletions scripts/Version.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//> using scala 3.3.1

//> using dep com.lihaoyi::os-lib:0.9.2

import scala.sys.exit
import scala.util.CommandLineParser.FromString
import scala.util.matching.Regex

@main def main(command: String, other: String*): Unit =
if os.pwd.last != "besom" then
println("You have to run this command from besom project root directory!")
sys.exit(1)

val besomVersion = os.read(os.pwd / "version.txt").trim

val besomDependencyPattern: Regex =
("""^(//> using (?:test\.)?dep ["]{0,1}org.virtuslab::besom-[a-z]+:(?:[0-9\.]+-core\.)?)""" + besomVersion + """(["]{0,1})$""").r

val projectFiles: Map[os.Path, String] =
os.walk(os.pwd)
.filter(_.last == "project.scala")
.filterNot(_.startsWith(os.pwd / ".out"))
.map((f: os.Path) => f -> os.read(f))
.toMap

command match
case "show" =>
projectFiles
.foreachEntry { case (f, content) =>
content.linesIterator.zipWithIndex.foreach { case (line, index) =>
if besomDependencyPattern.matches(line) then
println(s"$f:$index:\n$line\n")
}
}
case "bump" =>
val newBesomVersion =
if other.isDefinedAt(0) then other.toVector(0)
else {
println("You have to provide new besom version as the second argument.")
sys.exit(1)
}

projectFiles
.collect { case (path, content) if content.linesIterator.exists(besomDependencyPattern.matches) => path -> content }
.foreachEntry { case (path, content) =>
val newContent = content.linesIterator
.map {
case besomDependencyPattern(prefix, suffix) => prefix + newBesomVersion + suffix
case line => line
}
.mkString("\n") + "\n"

os.write.over(path, newContent)
println(s"Updated $path")
}

// TODO: automatically update provider versions based on Packages.scala

os.write.over(os.pwd / "version.txt", newBesomVersion)
println(s"Updated version.txt")
case _ =>
println(
s"""Usage: version <command>
|
|Commands:
| show - show all project.scala files with besom dependency
| bump <new_version> - bump besom version in all project.scala files
|""".stripMargin
)
exit(1)
end match
end main

0 comments on commit 3df40e6

Please sign in to comment.