Skip to content

Commit 28cb4fb

Browse files
committed
Automate version updates
1 parent 76dc293 commit 28cb4fb

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

.github/workflows/build-all-samples.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ jobs:
1313
distribution: temurin
1414
- name: 'Build all samples using JShell'
1515
run: java src/Builder.java
16+
- name: 'Check automation for updating versions'
17+
run: java src/Updater.java 42

src/Updater.java

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* http://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
// default package
12+
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.util.List;
17+
import java.util.regex.Pattern;
18+
19+
/**
20+
* Updates the versions of JUnit Platform artifacts in all sample projects.
21+
*/
22+
@SuppressWarnings({"WeakerAccess", "SameParameterValue"})
23+
class Updater {
24+
25+
private static final String VERSION_REGEX = "([0-9.]+)";
26+
27+
public static void main(String[] args) throws Exception {
28+
new Updater(args[0]).update();
29+
}
30+
31+
private final String jupiterVersion;
32+
33+
public Updater(String jupiterVersion) {
34+
this.jupiterVersion = jupiterVersion;
35+
}
36+
37+
void update() throws IOException {
38+
var gradleBomReplacement = new Replacement("org.junit:junit-bom:" + VERSION_REGEX, VersionType.BOM);
39+
var mavenBomReplacement = new Replacement(
40+
Pattern.compile("""
41+
\\s*<groupId>org.junit</groupId>
42+
\\s*<artifactId>junit-bom</artifactId>
43+
\\s*<version>([0-9.]+)</version>
44+
""", Pattern.MULTILINE),
45+
VersionType.BOM
46+
);
47+
48+
update(Path.of("junit5-jupiter-extensions/build.gradle"), List.of(gradleBomReplacement));
49+
update(Path.of("junit5-jupiter-starter-ant/build.sh"), List.of(
50+
new Replacement("junit_platform_version='" + VERSION_REGEX + "'", VersionType.PLATFORM)
51+
));
52+
update(Path.of("junit5-jupiter-starter-bazel/MODULE.bazel"), List.of(
53+
new Replacement("JUNIT_JUPITER_VERSION = \"" + VERSION_REGEX + '"', VersionType.JUPITER),
54+
new Replacement("JUNIT_PLATFORM_VERSION = \"" + VERSION_REGEX + '"', VersionType.PLATFORM)
55+
));
56+
update(Path.of("junit5-jupiter-starter-gradle/build.gradle"), List.of(gradleBomReplacement));
57+
update(Path.of("junit5-jupiter-starter-gradle-groovy/build.gradle"), List.of(gradleBomReplacement));
58+
update(Path.of("junit5-jupiter-starter-gradle-kotlin/build.gradle.kts"), List.of(gradleBomReplacement));
59+
update(Path.of("junit5-jupiter-starter-maven/pom.xml"), List.of(mavenBomReplacement));
60+
update(Path.of("junit5-jupiter-starter-maven-kotlin/pom.xml"), List.of(mavenBomReplacement));
61+
update(Path.of("junit5-jupiter-starter-sbt/build.sbt"), List.of(
62+
new Replacement("\"org.junit.jupiter\" % \"junit-jupiter\" % \"" + VERSION_REGEX + '"', VersionType.JUPITER),
63+
new Replacement("\"org.junit.platform\" % \"junit-platform-launcher\" % \"" + VERSION_REGEX + '"', VersionType.PLATFORM)
64+
));
65+
update(Path.of("junit5-migration-gradle/build.gradle"), List.of(gradleBomReplacement));
66+
update(Path.of("junit5-migration-gradle/README.md"), List.of(
67+
new Replacement("org.junit.jupiter:junit-jupiter:" + VERSION_REGEX, VersionType.JUPITER),
68+
new Replacement("org.junit.vintage:junit-vintage-engine:" + VERSION_REGEX, VersionType.VINTAGE)
69+
));
70+
update(Path.of("junit5-modular-world/compile.jsh"), List.of(
71+
new Replacement("platformVersion = \"" + VERSION_REGEX + '"', VersionType.PLATFORM),
72+
new Replacement("jupiterVersion = \"" + VERSION_REGEX + '"', VersionType.JUPITER),
73+
new Replacement("vintageVersion = \"" + VERSION_REGEX + '"', VersionType.VINTAGE)
74+
));
75+
update(Path.of("junit5-multiple-engines/build.gradle.kts"), List.of(
76+
new Replacement("junitBomVersion = \"" + VERSION_REGEX + '"', VersionType.BOM)
77+
));
78+
}
79+
80+
void update(Path path, List<Replacement> replacements) throws IOException {
81+
System.out.printf("Updating %s...", path);
82+
System.out.flush();
83+
int matches = 0;
84+
var content = new StringBuilder(Files.readString(path));
85+
for (var replacement : replacements) {
86+
var minIndex = 0;
87+
var matcher = replacement.regex.matcher(content);
88+
while (matcher.find(minIndex)) {
89+
matches++;
90+
int start = matcher.start(1);
91+
int end = matcher.end(1);
92+
int oldLength = end - start;
93+
String newVersion = replacement.newVersion(jupiterVersion);
94+
content.replace(start, end, newVersion);
95+
minIndex = end + newVersion.length() - oldLength;
96+
}
97+
}
98+
Files.writeString(path, content);
99+
System.out.printf(" %d%n", matches);
100+
if (matches == 0) {
101+
throw new IllegalStateException("No matches found in " + path);
102+
}
103+
}
104+
105+
record Replacement(Pattern regex, VersionType versionType) {
106+
Replacement(String regex, VersionType versionType) {
107+
this(Pattern.compile(regex), versionType);
108+
}
109+
110+
String newVersion(String jupiterVersion) {
111+
return switch (versionType) {
112+
case BOM, JUPITER, VINTAGE -> jupiterVersion;
113+
case PLATFORM -> jupiterVersion.replaceFirst("\\d+\\.", "1.");
114+
};
115+
}
116+
}
117+
118+
enum VersionType {
119+
BOM,
120+
JUPITER,
121+
PLATFORM,
122+
VINTAGE
123+
}
124+
}

0 commit comments

Comments
 (0)