tiagobento commented on code in PR #6901:
URL: https://github.com/apache/incubator-kie/pull/6901#discussion_r3805850719
##########
script/ci/CiComputeBuildScopes.java:
##########
@@ -93,53 +95,32 @@ public static void main(String[] args) throws Exception {
// 2. run dep-graph-extractor, writing dependency graph to file.
// Persist the graph to DEP_GRAPH_EXTRACTOR__OUTPUT_FILE when set so
downstream
// tools (CiSummary) can reuse it without re-invoking Maven.
- String graphFileEnv =
System.getenv("DEP_GRAPH_EXTRACTOR__OUTPUT_FILE");
+ String graphFileEnv = cfg("DEP_GRAPH_EXTRACTOR__OUTPUT_FILE");
Path graphFile = (graphFileEnv != null && !graphFileEnv.isBlank())
? Paths.get(graphFileEnv).toAbsolutePath()
: Files.createTempFile("dep-graph-", ".tsv");
- int rc = runMavenWithDepGraphExtractor(cwd, extractorJarPath,
graphFile, extraMavenArgs);
- if (!Files.isRegularFile(graphFile) || Files.size(graphFile) == 0) {
- System.err.println("dep-graph-extractor failed (mvn rc=" + rc +
")");
- System.exit(1);
- }
-
- // 3. parse graph
- Map<String, Path> gaToDir = new HashMap<>();
- Map<String, Set<String>> upstreamOf = new HashMap<>(); // ga ->
direct upstreams
- Map<String, Set<String>> downstreamOf = new HashMap<>(); // ga ->
direct downstreams
- try (BufferedReader r = Files.newBufferedReader(graphFile)) {
- String line;
- while ((line = r.readLine()) != null) {
- String[] parts = line.split("\t", -1);
- if (parts.length < 3) continue;
- switch (parts[0]) {
- case "P" -> {
- gaToDir.put(parts[1],
Paths.get(parts[2]).toAbsolutePath().normalize());
- upstreamOf.computeIfAbsent(parts[1], k -> new
HashSet<>());
- downstreamOf.computeIfAbsent(parts[1], k -> new
HashSet<>());
- }
- case "D" -> {
- upstreamOf.computeIfAbsent(parts[1], k -> new
HashSet<>()).add(parts[2]);
- downstreamOf.computeIfAbsent(parts[2], k -> new
HashSet<>()).add(parts[1]);
- }
- }
+ // Reading the reactor costs a full pass over every pom.xml, which is
a steep
+ // price to pay on every invocation of a local dev loop. When
+ // DEP_GRAPH_EXTRACTOR__REUSE_IF_FRESH is set, keep a stamp of the
reactor's
+ // poms next to the graph and re-extract only once one of them changes.
+ // Off by default, so CI always extracts from scratch.
+ Path stampFile = Paths.get(graphFile + ".stamp");
Review Comment:
Fixed in
[992c9fa](https://github.com/apache/incubator-kie/pull/6901/commits/992c9fa6189ed56404effadf3a1a8c6d005d8b37)
##########
script/ci/DepGraph.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+//JAVA 21
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * The reactor dependency graph, as written by the {@code dep-graph-extractor}
Maven
+ * extension and read by everything that needs to know how the modules relate.
+ *
+ * <p>Shared by {@code script/ci/CiComputeBuildScopes.java} and
+ * {@code script/dev/Dev.java} — pull it in with a jbang {@code //SOURCES}
line rather
+ * than copying it, so that CI and local builds cannot disagree about the
graph.
+ *
+ * <p>The file is a TSV of one-letter record types:
+ * <pre>
+ * P groupId:artifactId /abs/basedir
+ * D groupId:artifactId upstream-groupId:artifactId scope
+ * V groupId:artifactId version packaging
+ * L /abs/path/to/local/maven/repository
+ * B groupId:artifactId (an in-reactor BOM)
+ * </pre>
+ * Unknown record types are ignored, so the extractor can add more without
breaking
+ * anything that reads it.
+ */
+public final class DepGraph {
+
+ /** Module directory of each module. */
+ public final Map<String, Path> gaToDir = new LinkedHashMap<>();
+ /** Version of each module, when the extractor recorded it. */
+ public final Map<String, String> gaToVersion = new LinkedHashMap<>();
+ /** Maven packaging of each module, when the extractor recorded it. */
+ public final Map<String, String> gaToPackaging = new LinkedHashMap<>();
+ /** What each module depends on, directly. */
+ public final Map<String, Set<String>> upstreamOf = new LinkedHashMap<>();
+ /** What depends on each module, directly. */
+ public final Map<String, Set<String>> downstreamOf = new LinkedHashMap<>();
+ /** Every module directory, for telling a module's own files from a nested
module's. */
+ public final Set<Path> moduleDirs = new HashSet<>();
+ /** The modules that are BOMs. */
+ public final Set<String> boms = new LinkedHashSet<>();
+ /**
+ * Why each edge exists, keyed by {@code "dependent|dependency"} — a Maven
scope, or
+ * {@code parent} / {@code plugin} / {@code import}. Empty when the graph
was written
+ * by an extractor that predates the 4th {@code D} field.
+ *
+ * <p>Build scoping deliberately ignores this: a test-scope edge still
means the
+ * downstream module must be rebuilt. It is here for tooling that needs to
tell what
+ * ships from what is only tested, such as separating test-only modules.
+ */
+ public final Map<String, String> edgeScopes = new LinkedHashMap<>();
+ /** The local Maven repository this graph was read with, or null if not
recorded. */
+ public Path localRepo;
+
+ public static DepGraph parse(Path file) throws IOException {
+ DepGraph graph = new DepGraph();
+ if (!Files.isRegularFile(file)) {
+ return graph;
+ }
+ for (String line : Files.readAllLines(file)) {
+ String[] parts = line.split("\t", -1);
+ if (parts.length < 2) continue;
+ switch (parts[0]) {
+ case "P" -> {
+ if (parts.length < 3) break;
+ Path dir =
Paths.get(parts[2]).toAbsolutePath().normalize();
+ graph.gaToDir.put(parts[1], dir);
+ graph.moduleDirs.add(dir);
+ graph.upstreamOf.computeIfAbsent(parts[1], k -> new
LinkedHashSet<>());
+ graph.downstreamOf.computeIfAbsent(parts[1], k -> new
LinkedHashSet<>());
+ }
+ case "D" -> {
+ if (parts.length < 3) break;
+ graph.addEdge(parts[1], parts[2]);
+ if (parts.length >= 4 && !parts[3].isEmpty()) {
+ graph.edgeScopes.put(parts[1] + "|" + parts[2],
parts[3]);
+ }
+ }
+ case "V" -> {
+ if (parts.length < 4) break;
+ graph.gaToVersion.put(parts[1], parts[2]);
+ graph.gaToPackaging.put(parts[1], parts[3]);
+ }
+ case "L" -> graph.localRepo =
Paths.get(parts[1]).toAbsolutePath().normalize();
+ case "B" -> graph.boms.add(parts[1]);
+ default -> { /* ignore unknown record types */ }
Review Comment:
Fixed in
[992c9fa](https://github.com/apache/incubator-kie/pull/6901/commits/992c9fa6189ed56404effadf3a1a8c6d005d8b37)
##########
script/dev/Dev.java:
##########
@@ -0,0 +1,841 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+///usr/bin/env jbang "$0" "$@" ; exit $?
+//JAVA 21
+//SOURCES ../ci/CiComputeBuildScopes.java
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Builds only the Maven modules your changes affect, using the same scope
computation CI
+ * runs on pull requests ({@link CiComputeBuildScopes} and {@code DepGraph},
pulled in
+ * above via jbang {@code //SOURCES} rather than copied, so the two cannot
disagree).
+ *
+ * <p>Driven by four settings in {@code .kie-dev/config}: <b>since</b> (the
point in
+ * history to measure from), <b>uncommitted</b> (whether the working tree
counts),
+ * <b>breadth</b> (how far to fan out) and <b>upstream</b> (whether to rebuild
+ * dependencies first). The first run writes that file with sensible defaults.
+ *
+ * <p>See {@code docs/DEV.md}.
+ */
+public class Dev {
+
+ // ------------------------------------------------------------------
+ // settings and state
+ // ------------------------------------------------------------------
+
+ static final Path STATE_DIR = Paths.get(".kie-dev");
+ static final Path CONFIG_FILE = STATE_DIR.resolve("config.properties");
+ private static final Path GRAPH_FILE = STATE_DIR.resolve("dep-graph.tsv");
+ private static final Path CHANGED_FILES_FILE =
STATE_DIR.resolve("changed-files.txt");
+ private static final Path PL_UPSTREAM_FILE =
STATE_DIR.resolve("pl-upstream.txt");
+ private static final Path PL_AFFECTED_FILE =
STATE_DIR.resolve("pl-affected.txt");
+ private static final Path PL_CHANGED_FILE =
STATE_DIR.resolve("pl-changed.txt");
+ private static final Path MODULES_FILE =
STATE_DIR.resolve("modules-to-build.txt");
+ private static final Path UPSTREAM_FILE =
STATE_DIR.resolve("upstream-modules.txt");
+ private static final Path LAST_COMMAND_FILE =
STATE_DIR.resolve("last-maven-command.txt");
+
+ static final String KEY_SINCE = "since";
+ static final String KEY_UNCOMMITTED = "uncommitted";
+ static final String KEY_BREADTH = "breadth";
+ static final String KEY_UPSTREAM = "upstream";
+ /**
+ * The point in history a build measures from: any git ref. Used as
+ * {@code merge-base(<ref>, HEAD)}, the same three-dot comparison CI
makes, so that
+ * work that landed on the base branch after you started is not counted as
yours.
+ *
+ * <p>The default, {@code HEAD}, falls out of that with no special case:
its merge
+ * base with HEAD is HEAD, so nothing committed is included and you are
left with the
+ * working tree alone.
+ */
+ private static final String SINCE_DEFAULT = "HEAD";
+
+ /** How far a build fans out from what changed. */
+ static final String BREADTH_CHANGED = "changed";
+ private static final String BREADTH_AFFECTED = "affected";
+ private static final List<String> BREADTH_VALUES =
List.of(BREADTH_CHANGED, BREADTH_AFFECTED);
+
+ /** Whether to rebuild dependencies before the main build. */
+ private static final String UPSTREAM_AUTO = "auto";
+ private static final String UPSTREAM_ALWAYS = "always";
+ private static final String UPSTREAM_NEVER = "never";
+ private static final List<String> UPSTREAM_VALUES = List.of(UPSTREAM_AUTO,
UPSTREAM_ALWAYS, UPSTREAM_NEVER);
+
+ private static final List<String> BOOLEAN_VALUES = List.of("true",
"false");
+
+ /**
+ * The everyday case: what you have edited but not committed, and
everything
+ * downstream of it, which is what CI would check.
+ *
+ * <p>A {@link LinkedHashMap} rather than {@code Map.of} so that the
config file and
+ * the line printed on first run come out in a sensible order.
+ */
+ private static final Map<String, String> DEFAULTS = new
LinkedHashMap<>(Map.of()) {{
+ put(KEY_SINCE, SINCE_DEFAULT);
+ put(KEY_UNCOMMITTED, "true");
+ put(KEY_BREADTH, BREADTH_AFFECTED);
+ put(KEY_UPSTREAM, UPSTREAM_AUTO);
+ }};
+
Review Comment:
Fixed in
[992c9fa](https://github.com/apache/incubator-kie/pull/6901/commits/992c9fa6189ed56404effadf3a1a8c6d005d8b37)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]