Skip to content

Commit

Permalink
Merge pull request #22 from xstefank/manifest-from-channel
Browse files Browse the repository at this point in the history
Add manifest-from command
  • Loading branch information
spyrkob authored Jun 7, 2024
2 parents 784b5bd + c2e1757 commit 825b3b2
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 29 deletions.
69 changes: 69 additions & 0 deletions src/main/java/org/wildfly/prospero/extras/ChannelOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.wildfly.prospero.extras;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.jboss.galleon.ProvisioningException;
import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestCoordinate;
import org.wildfly.channel.ChannelManifestMapper;
import org.wildfly.channel.ChannelMapper;
import org.wildfly.channel.MavenCoordinate;
import org.wildfly.prospero.extras.repository.create.MavenDownloader;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

public class ChannelOperations {

public static List<Channel> read(Path path) {
try {
return ChannelMapper.fromString(Files.readString(path));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static ChannelManifestDownload getChannelManifest(Channel channel) throws ProvisioningException, ArtifactResolutionException, MalformedURLException, VersionRangeResolutionException {
final List<RemoteRepository> repositories = channel.getRepositories().stream()
.map(r -> new RemoteRepository.Builder(r.getId(), "default", r.getUrl()).build())
.collect(Collectors.toList());

final MavenDownloader downloader = new MavenDownloader(repositories);
return getChannelManifest(channel, downloader);
}

public static ChannelManifestDownload getChannelManifest(Channel channel, MavenDownloader downloader) throws VersionRangeResolutionException, ArtifactResolutionException, MalformedURLException {
ChannelManifest manifest;

if (channel.getManifestCoordinate().getUrl() != null) {
manifest = ChannelManifestMapper.from(channel.getManifestCoordinate().getUrl());
return new ChannelManifestDownload(manifest, null);
} else {
final MavenCoordinate coord = channel.getManifestCoordinate().getMaven();
final Artifact manifestArtifact = downloader.downloadManifest(ChannelManifestCoordinate.create(null, coord));

System.out.println("Using manifest: " + manifestArtifact);

manifest = ChannelManifestMapper.from(manifestArtifact.getFile().toURI().toURL());

return new ChannelManifestDownload(manifest, manifestArtifact);
}
}

public static final class ChannelManifestDownload {
public ChannelManifest manifest;
public Artifact manifestArtifact;

public ChannelManifestDownload(ChannelManifest manifest, Artifact manifestDownload) {
this.manifest = manifest;
this.manifestArtifact = manifestDownload;
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/wildfly/prospero/extras/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.wildfly.prospero.extras.channel.query.QueryVersionCommand;
import org.wildfly.prospero.extras.manifest.diff.ManifestsDiffCommand;
import org.wildfly.prospero.extras.manifest.download.DownloadDiffCommand;
import org.wildfly.prospero.extras.manifest.from.ManifestFromCommand;
import org.wildfly.prospero.extras.manifest.merge.ManifestMergeCommand;
import org.wildfly.prospero.extras.manifest.subtract.ManifestSubtractCommand;
import org.wildfly.prospero.extras.repoository.RepositoryCommands;
Expand All @@ -46,6 +47,7 @@ private static CommandLine createCommandLine() {
commandLine.addSubcommand(new DownloadDiffCommand());
commandLine.addSubcommand(new ManifestMergeCommand());
commandLine.addSubcommand(new ManifestSubtractCommand());
commandLine.addSubcommand(new ManifestFromCommand());

commandLine.addSubcommand(new DownloadRepositoryCommand());
commandLine.addSubcommand(new DownloadArtifactListCommand());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelMapper;
import org.wildfly.channel.Repository;
import org.wildfly.prospero.extras.ChannelOperations;
import org.wildfly.prospero.extras.ReturnCodes;
import org.wildfly.prospero.extras.shared.CommandWithHelp;
import picocli.CommandLine;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
Expand All @@ -33,7 +32,7 @@ public class ChannelMergeCommand extends CommandWithHelp {
@Override
public Integer call() throws Exception {
final Stream<Channel> channels = inputChannelPaths.stream()
.map(ChannelMergeCommand::read)
.map(ChannelOperations::read)
.flatMap(List::stream);

final Channel channel = merge(channels, manifestUrl, name, description);
Expand Down Expand Up @@ -64,12 +63,4 @@ Channel merge(Stream<Channel> channels, URL url, String name, String description
return channelBuilder.build();

}

private static List<Channel> read(Path path) {
try {
return ChannelMapper.fromString(Files.readString(path));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.wildfly.prospero.extras.manifest.from;

import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestMapper;
import org.wildfly.prospero.extras.ChannelOperations;
import org.wildfly.prospero.extras.ReturnCodes;
import org.wildfly.prospero.extras.manifest.merge.ManifestMergeCommand;
import org.wildfly.prospero.extras.manifest.merge.VersionMergeStrategy;
import picocli.CommandLine;

import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

@CommandLine.Command(name = "manifest-from")
public class ManifestFromCommand implements Callable<Integer> {
@CommandLine.Option(names = "--channel", split = ",", required = true)
List<Path> inputChannelPaths;

@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true)
boolean help;

@Override
public Integer call() throws Exception {
List<Channel> channelList = inputChannelPaths.stream()
.map(ChannelOperations::read)
.flatMap(List::stream)
.collect(Collectors.toList());

final ChannelManifest channelManifest = manifestFrom(channelList);

System.out.println(ChannelManifestMapper.toYaml(channelManifest));

return ReturnCodes.SUCCESS;
}

public static ChannelManifest manifestFrom(List<Channel> channelPaths) {
return channelPaths.stream()
.map(channel -> {
try {
ChannelOperations.ChannelManifestDownload channelManifestDownload = ChannelOperations.getChannelManifest(channel);
return channelManifestDownload.manifest;
} catch (Exception e) {
throw new RuntimeException(e);
}
})
.reduce((m1, m2) -> ManifestMergeCommand.merge(m1, m2, VersionMergeStrategy.Strategies.LATEST, null, null))
.orElseThrow(() -> new RuntimeException("Couldn't extract manifest from the provided channels"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
import org.jboss.logging.Logger;
import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestCoordinate;
import org.wildfly.channel.ChannelManifestMapper;
import org.wildfly.channel.ChannelMapper;
import org.wildfly.channel.MavenCoordinate;
import org.wildfly.channel.Stream;
import org.wildfly.prospero.extras.ChannelOperations;
import org.wildfly.prospero.extras.ReturnCodes;
import org.wildfly.prospero.extras.shared.CommandWithHelp;
import picocli.CommandLine;
Expand All @@ -42,6 +40,8 @@
import java.util.TreeSet;
import java.util.stream.Collectors;

import static org.wildfly.prospero.extras.ChannelOperations.getChannelManifest;

@CommandLine.Command(name = "from-channel")
public class DownloadRepositoryCommand extends CommandWithHelp {

Expand Down Expand Up @@ -69,6 +69,8 @@ public class DownloadRepositoryCommand extends CommandWithHelp {
private boolean artifactList = false;
private final ChannelFeaturePackResolver channelFeaturePackResolver = new ChannelFeaturePackResolver();

private Set<Artifact> artifactSet;

enum FpMapperValues { ZIP, OFFLINER }

public DownloadRepositoryCommand() {
Expand All @@ -89,21 +91,13 @@ public Integer call() throws Exception {

final MavenDownloader downloader = new MavenDownloader(repositories);

final Set<Artifact> artifactSet = new HashSet<>();

ChannelManifest manifest;
if (channel.getManifestCoordinate().getUrl() != null) {
manifest = ChannelManifestMapper.from(channel.getManifestCoordinate().getUrl());
} else {
final MavenCoordinate coord = channel.getManifestCoordinate().getMaven();
final Artifact manifestArtifact = downloader.downloadManifest(ChannelManifestCoordinate.create(null, coord));

System.out.println("Using manifest: " + manifestArtifact);

manifest = ChannelManifestMapper.from(manifestArtifact.getFile().toURI().toURL());
artifactSet = new HashSet<>();

artifactSet.add(manifestArtifact);
ChannelOperations.ChannelManifestDownload manifestDownload = getChannelManifest(channel, downloader);
if (channel.getManifestCoordinate().getUrl() == null) {
artifactSet.add(manifestDownload.manifestArtifact);
}
ChannelManifest manifest = manifestDownload.manifest;

// get GAVs of feature-packs;
if (featurePacks.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class MavenDownloader {
private final DefaultRepositorySystemSession mvnSession;
private final List<RemoteRepository> repositories;

MavenDownloader(List<RemoteRepository> repositories) throws ProvisioningException {
public MavenDownloader(List<RemoteRepository> repositories) throws ProvisioningException {
this.repositories = repositories;

final MavenSessionManager msm = new MavenSessionManager(MavenOptions.DEFAULT_OPTIONS);
Expand All @@ -52,7 +52,7 @@ public void transferStarted(TransferEvent event) throws TransferCancelledExcepti
});
}

Artifact downloadManifest(ChannelManifestCoordinate coord) throws VersionRangeResolutionException, ArtifactResolutionException {
public Artifact downloadManifest(ChannelManifestCoordinate coord) throws VersionRangeResolutionException, ArtifactResolutionException {
// resolve version range
final VersionRangeRequest req = new VersionRangeRequest();
req.setRepositories(repositories);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.wildfly.prospero.extras.manifest.from;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestMapper;
import org.wildfly.channel.Stream;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;

class ManifestFromCommandTest {

private List<Path> tempFilesPaths;

@BeforeEach
public void setUp() {
tempFilesPaths = new ArrayList<>();
}

@AfterEach
public void cleanUp() {
tempFilesPaths.stream().filter(Objects::nonNull).forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

@Test
public void shouldGetManifestFromURLInSingleChannel() throws Exception {
final Channel channel = getChannel(getManifest(new Stream("org.acme", "acme", "1.2.3")));
final ChannelManifest res = callManifestFrom(List.of(channel));

assertThat(res.getStreams())
.containsOnly(new Stream("org.acme", "acme", "1.2.3"));
}

@Test
public void shouldGetManifestFromURLInMultipleChannels() throws Exception {
final Channel channel1 = getChannel(getManifest(new Stream("org.acme", "acme1", "1.2.3")));
final Channel channel2 = getChannel(getManifest(new Stream("org.acme", "acme2", "1.2.3")));

final ChannelManifest res = callManifestFrom(List.of(channel1, channel2));
assertThat(res.getStreams())
.containsOnly(new Stream("org.acme", "acme1", "1.2.3"),
new Stream("org.acme", "acme2", "1.2.3"));
}

@Test
public void shouldNotDuplicateArtifactsInTwoDifferentManifestsFromDifferentChannels() throws Exception {
final Channel channel1 = getChannel(getManifest(new Stream("org.acme", "acme", "1.2.3")));
final Channel channel2 = getChannel(getManifest(new Stream("org.acme", "acme", "1.2.3")));

final ChannelManifest res = callManifestFrom(List.of(channel1, channel2));
assertThat(res.getStreams())
.containsOnly(new Stream("org.acme", "acme", "1.2.3"));
}

private static ChannelManifest callManifestFrom(List<Channel> channels) {
return ManifestFromCommand.manifestFrom(channels);
}

private Channel getChannel(Path manifestPath) throws IOException {
return new Channel.Builder()
.setManifestUrl(manifestPath.toUri().toURL())
.build();
}

private Path getManifest(Stream... streams) throws IOException {
ChannelManifest channelManifest = new ChannelManifest("", "", "", List.of(streams));

Path tempFilePath = Files.createTempFile("manifest", "yaml");
Files.writeString(tempFilePath, ChannelManifestMapper.toYaml(channelManifest));
tempFilesPaths.add(tempFilePath);

return tempFilePath;
}
}

0 comments on commit 825b3b2

Please sign in to comment.