From 096bbd2faadeec44c229258d9ace3aaa841e9ce1 Mon Sep 17 00:00:00 2001 From: Bartosz Spyrko-Smietanko Date: Tue, 16 Jan 2024 14:29:06 +0000 Subject: [PATCH] Convert versions in manifest to versionPatterns --- .../org/wildfly/prospero/extras/Main.java | 3 + .../manifest/toopen/ConvertToOpenCommand.java | 101 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/main/java/org/wildfly/prospero/extras/manifest/toopen/ConvertToOpenCommand.java diff --git a/src/main/java/org/wildfly/prospero/extras/Main.java b/src/main/java/org/wildfly/prospero/extras/Main.java index 9d71547..ead962c 100644 --- a/src/main/java/org/wildfly/prospero/extras/Main.java +++ b/src/main/java/org/wildfly/prospero/extras/Main.java @@ -21,6 +21,7 @@ import org.wildfly.prospero.extras.manifest.diff.ManifestsDiffCommand; import org.wildfly.prospero.extras.manifest.download.DownloadDiffCommand; import org.wildfly.prospero.extras.manifest.merge.ManifestMergeCommand; +import org.wildfly.prospero.extras.manifest.toopen.ConvertToOpenCommand; import org.wildfly.prospero.extras.repository.create.DownloadRepositoryCommand; import picocli.CommandLine; @@ -41,6 +42,8 @@ private static CommandLine createCommandLine() { commandLine.addSubcommand(new DownloadRepositoryCommand()); + commandLine.addSubcommand(new ConvertToOpenCommand()); + commandLine.setUsageHelpAutoWidth(true); return commandLine; } diff --git a/src/main/java/org/wildfly/prospero/extras/manifest/toopen/ConvertToOpenCommand.java b/src/main/java/org/wildfly/prospero/extras/manifest/toopen/ConvertToOpenCommand.java new file mode 100644 index 0000000..1dae25b --- /dev/null +++ b/src/main/java/org/wildfly/prospero/extras/manifest/toopen/ConvertToOpenCommand.java @@ -0,0 +1,101 @@ +package org.wildfly.prospero.extras.manifest.toopen; + +import org.apache.commons.io.FileUtils; +import org.wildfly.channel.ChannelManifest; +import org.wildfly.channel.ChannelManifestMapper; +import org.wildfly.channel.Stream; +import picocli.CommandLine; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.concurrent.Callable; +import java.util.regex.MatchResult; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@CommandLine.Command(name = "manifest-to-open") +public class ConvertToOpenCommand implements Callable { + + @CommandLine.Option(names = {"--source-manifest", "-s"}) + private Path sourceManifestPath; + + @CommandLine.Option(names = {"--output-manifest", "-o"}) + private Path outputManifestPath; + + public static void main(String[] args) throws Exception { + new ConvertToOpenCommand().call(); + } + @Override + public Integer call() throws Exception { + + final Path sourceManifestPath = Path.of("/Users/spyrkob/workspaces/set/prospero/prospero-extras/eap8-manifest-proposed.yaml"); + + final ChannelManifest sourceManifest = ChannelManifestMapper + .fromString(FileUtils.readFileToString(sourceManifestPath.toFile(), StandardCharsets.UTF_8)); + + final Map> streamsByGroup = new TreeMap<>(); + for (Stream stream : sourceManifest.getStreams()) { + if (!streamsByGroup.containsKey(stream.getGroupId())) { + streamsByGroup.put(stream.getGroupId(), new ArrayList<>()); + } + streamsByGroup.get(stream.getGroupId()).add(stream); + } + + final List res = new ArrayList<>(); + for (Map.Entry> entry : streamsByGroup.entrySet()) { + + final Stream firstStream = entry.getValue().get(0); + if (entry.getValue().stream().allMatch(s->s.getVersion().equals(firstStream.getVersion()))) { + res.add(new Stream( + firstStream.getGroupId(), + "*", + toPattern(firstStream.getVersion()) + )); + } else { + for (Stream stream : entry.getValue()) { + res.add(new Stream( + stream.getGroupId(), + stream.getArtifactId(), + toPattern(stream.getVersion()) + )); + } + } + } + + final ChannelManifest openManifest = new ChannelManifest( + sourceManifest.getSchemaVersion(), + sourceManifest.getName(), + sourceManifest.getDescription(), + res); + + Files.writeString(outputManifestPath, ChannelManifestMapper.toYaml(openManifest), StandardCharsets.UTF_8); + + return 0; + } + + private static Pattern toPattern(String version) { + final Pattern versionPattern = Pattern.compile("\\d+\\.\\d+\\.(.*)[.|-]redhat-(\\d+)"); + final Matcher matcher = versionPattern.matcher(version); + + if (!matcher.matches()) { + throw new IllegalArgumentException(String.format("Version %s doesn't match", version)); + } + // TODO try converting this using the pattern + final MatchResult matchResult = matcher.toMatchResult(); + final int microStart = matchResult.start(0); + final int microEnd = matchResult.end(0); + final int suffixStart = matchResult.start(1); + final int suffixEnd = matchResult.end(1); + +// version.substring(0, microStart) + "\\d+" + version.substring(0, microEnd) + + final String[] parts = version.split("\\."); + + return Pattern.compile(parts[0] + "\\." + parts[1] + "\\." + "\\d+" + ".*redhat-.*"); + } +}