Skip to content

Commit

Permalink
[SET-679] Add FIRST merge strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
spyrkob committed Jan 19, 2024
1 parent 5a2ddc6 commit edae675
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@
<configuration>
<source>11</source>
<target>11</target>
<testSource>11</testSource>
<testTarget>11</testTarget>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,14 +13,20 @@
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

@CommandLine.Command(name = "merge")
@CommandLine.Command(name = "manifest-merge")
public class ManifestMergeCommand implements Callable<Integer> {
@CommandLine.Parameters(index = "0")
Path manifestOne;

@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 {

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
12 changes: 11 additions & 1 deletion src/main/resources/UsageMessages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 `<manifestOne>`.
mode=merge strategy to use. The default strategy is ${DEFAULT-VALUE}.
usage.parameterListHeading = %nPositional parameters:%n
usage.optionListHeading = %nOptions:%n
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit edae675

Please sign in to comment.