-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from xstefank/manifest-from-channel
Add manifest-from command
- Loading branch information
Showing
7 changed files
with
226 additions
and
29 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/org/wildfly/prospero/extras/ChannelOperations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/org/wildfly/prospero/extras/manifest/from/ManifestFromCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/test/java/org/wildfly/prospero/extras/manifest/from/ManifestFromCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |