From a181c2855d87582912299844a744c9853feb6acf Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Thu, 2 Jan 2025 14:15:39 +0100 Subject: [PATCH 1/2] Add support for excluding projects when building --- src/Builder.java | 53 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/src/Builder.java b/src/Builder.java index bf6704fc..ce313961 100644 --- a/src/Builder.java +++ b/src/Builder.java @@ -14,8 +14,10 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Set; /** * Platform-agnostic builder @@ -23,8 +25,11 @@ @SuppressWarnings({ "WeakerAccess", "SameParameterValue" }) class Builder { + private static final java.lang.String EXCLUDE_OPTION = "--exclude="; + public static void main(String[] args) { - var status = new Builder().build(); + var excludedProjects = determineExcludedProjects(args); + var status = new Builder().build(excludedProjects); if (status != 0) { throw new AssertionError("Expected exit status of zero, but got: " + status); } @@ -32,38 +37,56 @@ public static void main(String[] args) { int status = 0; - int build() { + int build(Set excludedProjects) { System.out.printf("|%n| Building all samples...%n|%n"); run(".", "java", "--version"); checkLicense("src/eclipse-public-license-2.0.java", ".java", ".kt", ".scala", ".groovy"); // jupiter-starter if (!isWindows()) { // TODO https://github.com/junit-team/junit5-samples/issues/66 - run("junit5-jupiter-starter-ant", "build.sh"); + runProject(excludedProjects, "junit5-jupiter-starter-ant", "build.sh"); } - run("junit5-jupiter-starter-gradle", "gradlew", "test"); - run("junit5-jupiter-starter-gradle-groovy", "gradlew", "test"); - run("junit5-jupiter-starter-gradle-kotlin", "gradlew", "test"); - run("junit5-jupiter-starter-maven", "mvnw", "--batch-mode", "clean", "test"); - run("junit5-jupiter-starter-maven-kotlin", "mvnw", "--batch-mode", "clean", "test"); - run("junit5-jupiter-starter-bazel", "bazel", "test", "//...", "--test_output", "all"); - run("junit5-jupiter-starter-sbt", "sbt", "test"); + runProject(excludedProjects, "junit5-jupiter-starter-gradle", "gradlew", "test"); + runProject(excludedProjects, "junit5-jupiter-starter-gradle-groovy", "gradlew", "test"); + runProject(excludedProjects, "junit5-jupiter-starter-gradle-kotlin", "gradlew", "test"); + runProject(excludedProjects, "junit5-jupiter-starter-maven", "mvnw", "--batch-mode", "clean", "test"); + runProject(excludedProjects, "junit5-jupiter-starter-maven-kotlin", "mvnw", "--batch-mode", "clean", "test"); + runProject(excludedProjects, "junit5-jupiter-starter-bazel", "bazel", "test", "//...", "--test_output", "all"); + runProject(excludedProjects, "junit5-jupiter-starter-sbt", "sbt", "test"); // jupiter-extensions - run("junit5-jupiter-extensions", "gradlew", "test"); + runProject(excludedProjects, "junit5-jupiter-extensions", "gradlew", "test"); // migration - run("junit5-migration-gradle", "gradlew", "test"); - run("junit5-migration-maven", "mvnw", "--batch-mode", "clean", "test"); - run("junit5-multiple-engines", "gradlew", "test"); + runProject(excludedProjects, "junit5-migration-gradle", "gradlew", "test"); + runProject(excludedProjects, "junit5-migration-maven", "mvnw", "--batch-mode", "clean", "test"); + runProject(excludedProjects, "junit5-multiple-engines", "gradlew", "test"); // modular - run("junit5-modular-world", "jshell", "build.jsh"); + runProject(excludedProjects, "junit5-modular-world", "jshell", "build.jsh"); System.out.printf("%n%n%n|%n| Done. Build exits with status = %d.%n|%n", status); return status; } + private static Set determineExcludedProjects(String[] args) { + Set excludedProjects = new HashSet<>(); + for (var arg : args) { + if (arg.startsWith(EXCLUDE_OPTION)) { + excludedProjects.addAll(Set.of(arg.substring(EXCLUDE_OPTION.length()).split(","))); + } + } + return Set.copyOf(excludedProjects); + } + + void runProject(Set excludedProjects, String project, String executable, String... args) { + if (excludedProjects.contains(project)) { + System.out.printf("%n%n%n|%n| %s is excluded.%n|%n", project); + return; + } + run(project, executable, args); + } + void run(String directory, String executable, String... args) { if (status != 0) { return; From 42e1f7fafaf7d7a9b85c3120ff41d01bbc6318e2 Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Thu, 2 Jan 2025 15:06:10 +0100 Subject: [PATCH 2/2] Add script to inject staging repo --- src/StagingRepoInjector.java | 110 +++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/StagingRepoInjector.java diff --git a/src/StagingRepoInjector.java b/src/StagingRepoInjector.java new file mode 100644 index 00000000..9023f6d9 --- /dev/null +++ b/src/StagingRepoInjector.java @@ -0,0 +1,110 @@ +/* + * Copyright 2015-2024 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * https://www.eclipse.org/legal/epl-v20.html + */ + +// default package + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.function.Consumer; + +public class StagingRepoInjector { + + public static void main(String[] args) throws Exception { + new StagingRepoInjector(args[0]).appendAfter(); + } + + private final String stagingRepoUrl; + + public StagingRepoInjector(String stagingRepoUrl) { + this.stagingRepoUrl = stagingRepoUrl; + } + + private void appendAfter() throws Exception { + + appendAfter("junit5-jupiter-extensions/build.gradle", "mavenCentral()", + "%n\tmaven { url = '%s' }".formatted(stagingRepoUrl)); + + replace("junit5-jupiter-starter-ant/build.sh", "https://repo1.maven.org/maven2", stagingRepoUrl); + + appendAfter("junit5-jupiter-starter-bazel/MODULE.bazel", "\"https://repo1.maven.org/maven2\",", + "%n \"%s\",".formatted(stagingRepoUrl)); + + appendAfter("junit5-jupiter-starter-gradle/build.gradle", "mavenCentral()", + "%n\tmaven { url = '%s' }".formatted(stagingRepoUrl)); + + appendAfter("junit5-jupiter-starter-gradle-groovy/build.gradle", "mavenCentral()", + "%n\tmaven { url = '%s' }".formatted(stagingRepoUrl)); + + appendAfter("junit5-jupiter-starter-gradle-kotlin/build.gradle.kts", "mavenCentral()", + "%n\tmaven(url = \"%s\")".formatted(stagingRepoUrl)); + + var mavenPomRepo = """ + + + \t + \t\t + \t\t\tcentral-staging + \t\t\t%s + \t\t + \t + """.stripIndent().stripTrailing().formatted(stagingRepoUrl); + + appendAfter("junit5-jupiter-starter-maven/pom.xml", "", mavenPomRepo); + + appendAfter("junit5-jupiter-starter-maven-kotlin/pom.xml", "", mavenPomRepo); + + appendAtEnd("junit5-jupiter-starter-sbt/build.sbt", + "%nresolvers += \"central-staging\" at \"%s\"%n".formatted(stagingRepoUrl)); + + appendAfter("junit5-migration-gradle/build.gradle", "mavenCentral()", + "%n\tmaven { url = '%s' }".formatted(stagingRepoUrl)); + + appendAfter("junit5-migration-maven/pom.xml", "", mavenPomRepo); + + replace("junit5-modular-world/BUILDING", "\"https://repo1.maven.org/maven2\"", + "group.startsWith(\"org.junit\") ? \"%s\" : \"https://repo1.maven.org/maven2\"".formatted(stagingRepoUrl)); + + appendAfter("junit5-multiple-engines/build.gradle.kts", "mavenCentral()", + "%n\tmaven(url = \"%s\")".formatted(stagingRepoUrl)); + } + + void appendAfter(String path, String token, String addedContent) throws IOException { + process(path, content -> { + if (content.indexOf(token) == -1) { + throw new IllegalStateException("Token not found in " + path); + } + content.insert(content.indexOf(token) + token.length(), addedContent); + }); + } + + void replace(String path, String token, String replacement) throws IOException { + process(path, content -> { + if (content.indexOf(token) == -1) { + throw new IllegalStateException("Token not found in " + path); + } + content.replace(content.indexOf(token), content.indexOf(token) + token.length(), replacement); + }); + } + + void appendAtEnd(String path, String addedContent) throws IOException { + process(path, content -> content.append(addedContent)); + } + + void process(String path, Consumer modification) throws IOException { + System.out.printf("Processing %s...", path); + System.out.flush(); + var file = Path.of(path); + var content = new StringBuilder(Files.readString(file)); + modification.accept(content); + Files.writeString(file, content); + System.out.println(" OK"); + } +}