-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
276 additions
and
18 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
cli/src/main/java/eu/maveniverse/maven/toolbox/cli/CopyAll.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,51 @@ | ||
/* | ||
* 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.cli; | ||
|
||
import eu.maveniverse.maven.mima.context.Context; | ||
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; | ||
|
||
/** | ||
* Resolves transitively a given GAV and copies resulting artifacts to target. | ||
*/ | ||
@CommandLine.Command(name = "copyAll", description = "Resolves Maven Artifact and copies them to target") | ||
public final class CopyAll extends ResolverCommandSupport { | ||
@CommandLine.Parameters(index = "0", description = "The GAV to resolve", arity = "1") | ||
private String gav; | ||
|
||
@CommandLine.Parameters(index = "1", description = "The target", arity = "1") | ||
private Path target; | ||
|
||
@CommandLine.Option( | ||
names = {"--resolutionScope"}, | ||
defaultValue = "runtime", | ||
description = "Resolution scope to resolve (default main-runtime)") | ||
private String resolutionScope; | ||
|
||
@CommandLine.Option( | ||
names = {"--boms"}, | ||
defaultValue = "", | ||
split = ",", | ||
description = "Comma separated list of BOMs to apply") | ||
private java.util.List<String> boms; | ||
|
||
@Override | ||
protected boolean doCall(Context context) throws Exception { | ||
Path targetPath = target.toAbsolutePath(); | ||
ToolboxCommando toolboxCommando = ToolboxCommando.getOrCreate(context); | ||
return toolboxCommando.copyAll( | ||
ResolutionScope.parse(resolutionScope), | ||
toolboxCommando.toolboxResolver().loadGav(gav, boms), | ||
DirectorySink.flat(output, targetPath), | ||
output); | ||
} | ||
} |
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
Binary file not shown.
Binary file not shown.
102 changes: 102 additions & 0 deletions
102
shared/src/main/java/eu/maveniverse/maven/toolbox/shared/DirectorySink.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,102 @@ | ||
/* | ||
* 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; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import eu.maveniverse.maven.toolbox.shared.internal.ArtifactMapper; | ||
import eu.maveniverse.maven.toolbox.shared.internal.ArtifactMatcher; | ||
import eu.maveniverse.maven.toolbox.shared.internal.ArtifactNameMapper; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.function.Consumer; | ||
import org.eclipse.aether.artifact.Artifact; | ||
|
||
/** | ||
* Construction to accept collection of artifacts, for example like a filesystem directory. | ||
*/ | ||
public final class DirectorySink implements Consumer<Collection<Artifact>> { | ||
|
||
public static DirectorySink flat(Output output, Path path) throws IOException { | ||
return new DirectorySink(output, path); | ||
} | ||
|
||
private final Output output; | ||
private final Path directory; | ||
private final ArtifactMatcher artifactMatcher; | ||
private final ArtifactMapper artifactMapper; | ||
private final ArtifactNameMapper artifactNameMapper; | ||
private final boolean allowOverwrite; | ||
private final HashSet<Path> writtenPaths; | ||
|
||
private DirectorySink(Output output, Path directory) throws IOException { | ||
this.output = requireNonNull(output, "output"); | ||
this.directory = requireNonNull(directory, "directory"); | ||
if (Files.exists(directory) && !Files.isDirectory(directory)) { | ||
throw new IllegalArgumentException("directory must not exists, or be a directory"); | ||
} | ||
if (!Files.exists(directory)) { | ||
Files.createDirectories(directory); | ||
} | ||
|
||
this.artifactMatcher = ArtifactMatcher.any(); | ||
this.artifactMapper = ArtifactMapper.identity(); | ||
this.artifactNameMapper = ArtifactNameMapper.ACVE(); | ||
this.allowOverwrite = false; | ||
this.writtenPaths = new HashSet<>(); | ||
} | ||
|
||
@Override | ||
public void accept(Collection<Artifact> artifacts) { | ||
requireNonNull(artifacts, "artifacts"); | ||
output.verbose("Copying {} artifacts to directory {}", artifacts.size(), directory); | ||
try { | ||
for (Artifact artifact : artifacts) { | ||
accept(artifact); | ||
} | ||
} catch (IOException e) { | ||
cleanup(artifacts, e); | ||
} | ||
} | ||
|
||
private void accept(Artifact artifact) throws IOException { | ||
output.verbose("Artifact {} processed", artifact); | ||
if (artifactMatcher.test(artifact)) { | ||
output.verbose(" matched"); | ||
String name = artifactNameMapper.map(artifactMapper.map(artifact)); | ||
output.verbose(" mapped to name {}", name); | ||
Path target = directory.resolve(name); | ||
if (!writtenPaths.add(target) && !allowOverwrite) { | ||
throw new IOException("Overwrite prevented: check mappings"); | ||
} | ||
output.verbose(" copied to file {}", target); | ||
Files.copy( | ||
artifact.getFile().toPath(), | ||
target, | ||
StandardCopyOption.REPLACE_EXISTING, | ||
StandardCopyOption.COPY_ATTRIBUTES); | ||
} | ||
} | ||
|
||
private void cleanup(Collection<Artifact> artifacts, IOException e) { | ||
output.error("IO error happened, cleaning up", e); | ||
writtenPaths.forEach(p -> { | ||
try { | ||
Files.deleteIfExists(p); | ||
} catch (IOException ex) { | ||
// ignore | ||
} | ||
}); | ||
throw new UncheckedIOException(e); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
shared/src/main/java/eu/maveniverse/maven/toolbox/shared/NullOutput.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,32 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* Null Output. | ||
*/ | ||
public final class NullOutput implements Output { | ||
public NullOutput() {} | ||
|
||
@Override | ||
public boolean isVerbose() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void verbose(String msg, Object... params) {} | ||
|
||
@Override | ||
public void normal(String msg, Object... params) {} | ||
|
||
@Override | ||
public void warn(String msg, Object... params) {} | ||
|
||
@Override | ||
public void error(String msg, Object... params) {} | ||
} |
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
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
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