Skip to content

Commit

Permalink
Move sink into commando
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 22, 2024
1 parent 93c7958 commit a329b93
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 17 deletions.
9 changes: 3 additions & 6 deletions cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Copy.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
*/
package eu.maveniverse.maven.toolbox.cli;

import eu.maveniverse.maven.toolbox.shared.DirectorySink;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.nio.file.Path;
import java.util.stream.Collectors;
import org.eclipse.aether.artifact.DefaultArtifact;
import picocli.CommandLine;
Expand All @@ -19,8 +17,8 @@
*/
@CommandLine.Command(name = "copy", description = "Resolves Maven Artifact and copies it to target")
public final class Copy extends ResolverCommandSupport {
@CommandLine.Parameters(index = "0", description = "The target", arity = "1")
private Path target;
@CommandLine.Parameters(index = "0", description = "The target spec", arity = "1")
private String targetSpec;

@CommandLine.Parameters(index = "1..*", description = "The GAVs to resolve", arity = "1")
private java.util.List<String> gav;
Expand All @@ -34,10 +32,9 @@ public final class Copy extends ResolverCommandSupport {

@Override
protected boolean doExecute(ToolboxCommando toolboxCommando) throws Exception {
Path targetPath = target.toAbsolutePath();
return toolboxCommando.copy(
gav.stream().map(DefaultArtifact::new).collect(Collectors.toList()),
DirectorySink.flat(output, targetPath),
toolboxCommando.artifactSink(output, targetSpec),
output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
*/
package eu.maveniverse.maven.toolbox.cli;

import eu.maveniverse.maven.toolbox.shared.DirectorySink;
import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.nio.file.Path;
import picocli.CommandLine;

/**
Expand All @@ -20,8 +18,8 @@
name = "copyTransitive",
description = "Resolves Maven Artifact transitively and copies all of them to target")
public final class CopyTransitive extends ResolverCommandSupport {
@CommandLine.Parameters(index = "0", description = "The target", arity = "1")
private Path target;
@CommandLine.Parameters(index = "0", description = "The target spec", arity = "1")
private String targetSpec;

@CommandLine.Parameters(index = "1..*", description = "The GAVs to resolve", arity = "1")
private java.util.List<String> gav;
Expand All @@ -41,11 +39,10 @@ public final class CopyTransitive extends ResolverCommandSupport {

@Override
protected boolean doExecute(ToolboxCommando toolboxCommando) throws Exception {
Path targetPath = target.toAbsolutePath();
return toolboxCommando.copyTransitive(
ResolutionScope.parse(resolutionScope),
toolboxCommando.loadGavs(gav, boms),
DirectorySink.flat(output, targetPath),
toolboxCommando.artifactSink(output, targetSpec),
output);
}
}
15 changes: 15 additions & 0 deletions shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>eu.maveniverse.maven.mima.runtime</groupId>
<artifactId>standalone-static-uber</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ private DeployingSink(
this.deployRequest.setTrace(RequestTrace.newChild(null, this));
}

public RemoteRepository getRemoteRepository() {
return deployRequest.getRepository();
}

@Override
public void accept(Collection<Artifact> artifacts) {
requireNonNull(artifacts, "artifacts");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ private DirectorySink(
: new StandardCopyOption[] {StandardCopyOption.COPY_ATTRIBUTES};
}

public Path getDirectory() {
return directory;
}

@Override
public void accept(Artifact artifact) throws IOException {
requireNonNull(artifact, "artifact");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.installation.InstallRequest;
import org.eclipse.aether.installation.InstallationException;
import org.eclipse.aether.repository.LocalRepository;

/**
* Construction to accept collection of artifacts and install them into local repository.
Expand All @@ -41,6 +42,10 @@ private InstallingSink(Output output, RepositorySystem system, RepositorySystemS
this.installRequest.setTrace(RequestTrace.newChild(null, this));
}

public LocalRepository getLocalRepository() {
return session.getLocalRepository();
}

@Override
public void accept(Collection<Artifact> artifacts) {
requireNonNull(artifacts, "artifacts");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ static ToolboxCommando create(Runtime runtime, Context context) {

// Resolver related commands: they target current context contained RemoteRepository

/**
* Provides {@link ArtifactSink} according to spec.
*/
ArtifactSink artifactSink(Output output, String spec) throws IOException;

/**
* Shorthand method, creates {@link ResolutionRoot} out of passed in artifact.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import eu.maveniverse.maven.mima.context.Runtime;
import eu.maveniverse.maven.mima.context.internal.RuntimeSupport;
import eu.maveniverse.maven.toolbox.shared.ArtifactSink;
import eu.maveniverse.maven.toolbox.shared.DeployingSink;
import eu.maveniverse.maven.toolbox.shared.DirectorySink;
import eu.maveniverse.maven.toolbox.shared.Output;
import eu.maveniverse.maven.toolbox.shared.ResolutionRoot;
import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
Expand Down Expand Up @@ -102,11 +104,6 @@ public ToolboxCommandoImpl(Runtime runtime, Context context) {
new ToolboxResolverImpl(context.repositorySystem(), session, context.remoteRepositories());
}

@Override
public ResolutionRoot loadGav(String gav, Collection<String> boms) throws ArtifactDescriptorException {
return toolboxResolver.loadGav(gav, boms);
}

@Override
public String getVersion() {
return discoverArtifactVersion("eu.maveniverse.maven.toolbox", "shared", "unknown");
Expand Down Expand Up @@ -188,6 +185,33 @@ public boolean dump(boolean verbose, Output output) {
return true;
}

@Override
public ArtifactSink artifactSink(Output output, String spec) throws IOException {
String prefix = spec.contains(":") ? spec.substring(0, spec.indexOf(":")) : "flatImplied";
switch (prefix) {
case "flatImplied":
return DirectorySink.flat(output, context.basedir().resolve(spec));
case "flat":
return DirectorySink.flat(output, context.basedir().resolve(spec.substring("flat:".length())));
case "repository":
return DirectorySink.repository(
output, context.basedir().resolve(spec.substring("repository:".length())));
case "deploy":
return DeployingSink.deploying(
output,
context.repositorySystem(),
context.repositorySystemSession(),
toolboxResolver.parseDeploymentRemoteRepository(spec.substring("deploy:".length())));
default:
throw new IllegalArgumentException("unknown artifact sink spec");
}
}

@Override
public ResolutionRoot loadGav(String gav, Collection<String> boms) throws ArtifactDescriptorException {
return toolboxResolver.loadGav(gav, boms);
}

@Override
public boolean classpath(ResolutionScope resolutionScope, ResolutionRoot resolutionRoot, Output output)
throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.shared.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import eu.maveniverse.maven.mima.context.Context;
import eu.maveniverse.maven.mima.context.ContextOverrides;
import eu.maveniverse.maven.mima.context.Runtime;
import eu.maveniverse.maven.mima.context.Runtimes;
import eu.maveniverse.maven.toolbox.shared.ArtifactSink;
import eu.maveniverse.maven.toolbox.shared.DeployingSink;
import eu.maveniverse.maven.toolbox.shared.DirectorySink;
import eu.maveniverse.maven.toolbox.shared.NullOutput;
import java.io.IOException;
import java.nio.file.Path;
import org.eclipse.aether.repository.RemoteRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class ToolboxCommandoImplTest {
@Test
void artifactSinkSpec(@TempDir Path tempDir) throws IOException {
Runtime runtime = Runtimes.INSTANCE.getRuntime();
try (Context context = runtime.create(ContextOverrides.create().build())) {
ToolboxCommandoImpl commando = new ToolboxCommandoImpl(runtime, context);
ArtifactSink sink;

sink = commando.artifactSink(new NullOutput(), tempDir.toString());
assertInstanceOf(DirectorySink.class, sink);
assertEquals(((DirectorySink) sink).getDirectory(), tempDir);

sink = commando.artifactSink(new NullOutput(), "flat:" + tempDir);
assertInstanceOf(DirectorySink.class, sink);
assertEquals(((DirectorySink) sink).getDirectory(), tempDir);

sink = commando.artifactSink(new NullOutput(), "repository:" + tempDir);
assertInstanceOf(DirectorySink.class, sink);
assertEquals(((DirectorySink) sink).getDirectory(), tempDir);

sink = commando.artifactSink(new NullOutput(), "deploy:id::https://somewhere.com/");
assertInstanceOf(DeployingSink.class, sink);
assertEquals(
((DeployingSink) sink).getRemoteRepository(),
new RemoteRepository.Builder("id", "default", "https://somewhere.com/").build());
}
}
}

0 comments on commit a329b93

Please sign in to comment.