Skip to content

Commit 41f002b

Browse files
committed
Insert version in the right place in add-version workflow
1 parent 5014cec commit 41f002b

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

.github/workflows/add-version.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
- name: Commit new version support
7070
run: |
7171
gpg --list-keys
72-
echo "${{inputs.scala_version}}" >> scala-versions
72+
sbt "addVersion ${{inputs.scala_version}}"
7373
sbt "generateAll;++${{inputs.scala_version}} test"
7474
git add --all
7575
git commit -m "Add ${{inputs.scala_version}} support"

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,4 @@ val betterToString =
9999
.aggregate(plugin, tests)
100100
.enablePlugins(NoPublishPlugin)
101101
.enablePlugins(ReadmePlugin)
102+
.enablePlugins(AddVersionPlugin)

project/AddVersionPlugin.scala

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import sbt.AutoPlugin
2+
3+
import sbt.Def
4+
import sbt._
5+
6+
object AddVersionPlugin extends AutoPlugin {
7+
8+
val addVersion = inputKey[Unit]("Write the given version to scala-versions")
9+
10+
case class ParsedVersion(mainPart: String, rcPart: Option[String]) {
11+
12+
def render: String =
13+
rcPart match {
14+
case Some(rc) => s"$mainPart-$rc"
15+
case None => mainPart
16+
}
17+
18+
}
19+
20+
object ParsedVersion {
21+
22+
// if mainPart is the same, RCs go first
23+
implicit val ordering: Ordering[ParsedVersion] = Ordering.by { v =>
24+
(
25+
v.mainPart,
26+
v.rcPart match {
27+
case Some(rc) => s"0$rc" // prepend 0 to RCs to sort them before non-RCs
28+
case None => "1" // non-RCs come after RCs
29+
}
30+
)
31+
}
32+
33+
def parse(version: String): ParsedVersion = {
34+
val parts = version.split("-")
35+
val mainPart = parts.head
36+
val rcPart = if (parts.length > 1) Some(parts(1)) else None
37+
ParsedVersion(mainPart, rcPart)
38+
}
39+
40+
}
41+
42+
override def projectSettings: Seq[Def.Setting[_]] = Seq(
43+
addVersion := {
44+
// Read the version from the input
45+
val version = Def.spaceDelimited("<version>").parsed.mkString(" ")
46+
47+
val path = file("scala-versions")
48+
val currentVersions = IO
49+
.read(path)
50+
.split("\n")
51+
.map(_.trim)
52+
.filterNot(_.isEmpty)
53+
.toSet
54+
55+
val newVersions = (currentVersions + version).toSeq.map(ParsedVersion.parse).sorted.map(_.render)
56+
IO.write(path, newVersions.mkString("\n") + "\n")
57+
}
58+
)
59+
60+
}

0 commit comments

Comments
 (0)