From edae6754449d653405c4355e2a151176713d4b1f Mon Sep 17 00:00:00 2001 From: Bartosz Spyrko-Smietanko Date: Fri, 19 Jan 2024 17:51:39 +0000 Subject: [PATCH] [SET-679] Add FIRST merge strategy --- pom.xml | 2 + .../manifest/merge/ManifestMergeCommand.java | 12 ++++-- .../manifest/merge/VersionMergeStrategy.java | 41 +++++++++++++++++++ src/main/resources/UsageMessages.properties | 12 +++++- .../merge/VersionMergeStrategyTest.java | 22 ++++++++++ 5 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/wildfly/prospero/extras/manifest/merge/VersionMergeStrategy.java create mode 100644 src/test/java/org/wildfly/prospero/extras/manifest/merge/VersionMergeStrategyTest.java diff --git a/pom.xml b/pom.xml index 7763083..de81e0d 100644 --- a/pom.xml +++ b/pom.xml @@ -392,6 +392,8 @@ 11 11 + 11 + 11 diff --git a/src/main/java/org/wildfly/prospero/extras/manifest/merge/ManifestMergeCommand.java b/src/main/java/org/wildfly/prospero/extras/manifest/merge/ManifestMergeCommand.java index 6840ba2..e3bd94d 100644 --- a/src/main/java/org/wildfly/prospero/extras/manifest/merge/ManifestMergeCommand.java +++ b/src/main/java/org/wildfly/prospero/extras/manifest/merge/ManifestMergeCommand.java @@ -3,7 +3,6 @@ import org.wildfly.channel.ChannelManifest; import org.wildfly.channel.ChannelManifestMapper; import org.wildfly.channel.Stream; -import org.wildfly.channel.version.VersionMatcher; import picocli.CommandLine; import java.nio.file.Path; @@ -14,7 +13,7 @@ import java.util.concurrent.Callable; import java.util.stream.Collectors; -@CommandLine.Command(name = "merge") +@CommandLine.Command(name = "manifest-merge") public class ManifestMergeCommand implements Callable { @CommandLine.Parameters(index = "0") Path manifestOne; @@ -22,6 +21,12 @@ public class ManifestMergeCommand implements Callable { @CommandLine.Parameters(index = "1") Path manifestTwo; + @CommandLine.Option(names = {"--mode"}, defaultValue = "LATEST") + VersionMergeStrategy.Strategies mergeStrategy; + + @CommandLine.Option(names = {"-h", "--help"}, usageHelp = true) + boolean help; + @Override public Integer call() throws Exception { @@ -39,7 +44,8 @@ public Integer call() throws Exception { if (!presentKeys.containsKey(key)) { merged.add(s); } else { - if (VersionMatcher.COMPARATOR.compare(s.getVersion(), presentKeys.get(key).getVersion()) > 0) { + final String version = mergeStrategy.merge(presentKeys.get(key).getVersion(), s.getVersion()); + if (!version.equals(presentKeys.get(key).getVersion())) { merged.remove(presentKeys.get(key)); merged.add(s); } diff --git a/src/main/java/org/wildfly/prospero/extras/manifest/merge/VersionMergeStrategy.java b/src/main/java/org/wildfly/prospero/extras/manifest/merge/VersionMergeStrategy.java new file mode 100644 index 0000000..cdb8453 --- /dev/null +++ b/src/main/java/org/wildfly/prospero/extras/manifest/merge/VersionMergeStrategy.java @@ -0,0 +1,41 @@ +package org.wildfly.prospero.extras.manifest.merge; + +import org.wildfly.channel.version.VersionMatcher; + +interface VersionMergeStrategy { + enum Strategies implements VersionMergeStrategy { + LATEST(new LatestMergeStrategy()), + FIRST(new FirstMergeStrategy()); + + private final VersionMergeStrategy mergeStrategy; + + Strategies(VersionMergeStrategy mergeStrategy) { + this.mergeStrategy = mergeStrategy; + } + + public String merge(String v1, String v2) { + return mergeStrategy.merge(v1, v2); + } + } + String merge(String v1, String v2); +} + +class FirstMergeStrategy implements VersionMergeStrategy { + + @Override + public String merge(String v1, String v2) { + return v1; + } +} + +class LatestMergeStrategy implements VersionMergeStrategy { + + @Override + public String merge(String v1, String v2) { + if (VersionMatcher.COMPARATOR.compare(v2, v1) > 0) { + return v2; + } else { + return v1; + } + } +} diff --git a/src/main/resources/UsageMessages.properties b/src/main/resources/UsageMessages.properties index 922f862..cf60379 100644 --- a/src/main/resources/UsageMessages.properties +++ b/src/main/resources/UsageMessages.properties @@ -6,4 +6,14 @@ feature-packs=Comma-separated list of Galleon feature packs to resolve the artif fp-mapper=Method to be used to resolve the artifacts from Galleon feature packs. Available values are ZIP and OFFLINER include-sources=If the source jars are available, download and include them in the generated repository. include-poms=Download and include artifacts' poms in the generated repository. -out=Path to the generated repository \ No newline at end of file +out=Path to the generated repository + +tools.manifest-merge.usage.header=Merges streams from two manifests. +tools.manifest-merge.usage.description.0=Prints a manifest containing streams from both input manifests. If the same\ + stream is available in both input manifests, the conflict is resolved using a merge strategy. +tools.manifest-merge.usage.description.1=The LATEST merge strategy compares the versions and picks the latest stream. +tools.manifest-merge.usage.description.2=The FIRST merge strategy chooses the stream from ``. +mode=merge strategy to use. The default strategy is ${DEFAULT-VALUE}. + +usage.parameterListHeading = %nPositional parameters:%n +usage.optionListHeading = %nOptions:%n diff --git a/src/test/java/org/wildfly/prospero/extras/manifest/merge/VersionMergeStrategyTest.java b/src/test/java/org/wildfly/prospero/extras/manifest/merge/VersionMergeStrategyTest.java new file mode 100644 index 0000000..c9712ba --- /dev/null +++ b/src/test/java/org/wildfly/prospero/extras/manifest/merge/VersionMergeStrategyTest.java @@ -0,0 +1,22 @@ +package org.wildfly.prospero.extras.manifest.merge; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class VersionMergeStrategyTest { + + @Test + public void testLatestVersionMerge() { + final VersionMergeStrategy latest = new LatestMergeStrategy(); + assertEquals("1.2.4", latest.merge("1.2.3", "1.2.4")); + assertEquals("1.2.4", latest.merge("1.2.4", "1.2.3")); + } + + @Test + public void testFirstVersionMerge() { + final VersionMergeStrategy first = new FirstMergeStrategy(); + assertEquals("1.2.3", first.merge("1.2.3", "1.2.4")); + assertEquals("1.2.4", first.merge("1.2.4", "1.2.3")); + } +} \ No newline at end of file