Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 20, 2024
1 parent 48dd5cc commit e2aeec9
Show file tree
Hide file tree
Showing 33 changed files with 2,034 additions and 694 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Toolbox

The Maveniverse Toolbox started as manifold project. It aims to:
* replace `MIMA CLI` (basically continue along its goals)
* replace `maven-dependency-plugin`, providing similar (sub)set of Mojos
The Toolbox project started with manifold aims:
* replace `MIMA CLI`, provide drop-in replacement, but also continue improving it.
* provide replacement for `maven-dependency-plugin`, offering similar (sub)set of Mojos
* showcase how MIMA helps to write reusable Resolver code that runs in Maven (as Mojos) but also outside of Maven as well
* ~~and as a side effect, it codifies dependency scopes and resolution scopes (they are not only String labels anymore)~~ this was moved out from here as MRESOLVER-512

Structure of the project:
* Module "shared" is a library module, that depends on MIMA `Context` only, and implements all the logic. This module is then reused in modules below.
Expand All @@ -18,10 +17,13 @@ Project Goals:
* copy
* get
* list-repositories
* list-plugins
* properties
* repocopy
* tree
* unpack
* measure (dep count, size, repo count?)
* list-available-plugins (G given or settings one, A and latest V), groupIds from settings or explicitly given
* sources (P or GAV w/wo transitive deps)
* javadoc (P or GAV w/wo transitive deps)

plus what MIMA CLI already supports.
17 changes: 14 additions & 3 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<name>${project.groupId}:${project.artifactId}</name>

<properties>
<maven.compiler.release>11</maven.compiler.release>
<mainClass>eu.maveniverse.maven.toolbox.cli.Main</mainClass>
</properties>

Expand All @@ -36,6 +35,10 @@
<groupId>info.picocli</groupId>
<artifactId>picocli-shell-jline3</artifactId>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
Expand All @@ -61,7 +64,11 @@
<artifactId>search-backend-smo</artifactId>
</dependency>

<!-- MIMA Context -->
<!-- MIMA + Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>eu.maveniverse.maven.mima</groupId>
<artifactId>context</artifactId>
Expand All @@ -88,7 +95,6 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
<version>${version.maven}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -101,6 +107,11 @@
<artifactId>maven-resolver-util</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test -->
<dependency>
Expand Down
35 changes: 14 additions & 21 deletions cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Classpath.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
package eu.maveniverse.maven.toolbox.cli;

import eu.maveniverse.maven.mima.context.Context;
import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.Toolbox;
import eu.maveniverse.maven.toolbox.shared.internal.ToolboxImpl;
import java.io.File;
import java.util.stream.Collectors;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.resolution.DependencyResult;
import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator;
import picocli.CommandLine;

/**
Expand Down Expand Up @@ -48,18 +39,20 @@ public final class Classpath extends ResolverCommandSupport {

@Override
protected Integer doCall(Context context) throws Exception {
java.util.List<Dependency> managedDependencies = importBoms(context, boms);
Artifact gav = parseGav(this.gav, managedDependencies);

ResolutionScope resolutionScope = toResolutionScope(this.resolutionScope);
Toolbox toolbox = new ToolboxImpl(context);
DependencyResult dependencyResult = toolbox.resolve(
resolutionScope, new Dependency(gav, ""), null, managedDependencies, context.remoteRepositories());

PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
dependencyResult.getRoot().accept(nlg);
// TODO: Do not use PreorderNodeListGenerator#getClassPath() until MRESOLVER-483 is fixed/released
info("{}", nlg.getFiles().stream().map(File::getAbsolutePath).collect(Collectors.joining(File.pathSeparator)));
// java.util.List<Dependency> managedDependencies = importBoms(context, boms);
// Artifact gav = parseGav(this.gav, managedDependencies);
//
// ResolutionScope resolutionScope = toResolutionScope(this.resolutionScope);
// Toolbox toolbox = Toolbox.getOrCreate(context);
// DependencyResult dependencyResult = toolbox.resolve(
// resolutionScope, new Dependency(gav, ""), null, managedDependencies,
// context.remoteRepositories());
//
// PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
// dependencyResult.getRoot().accept(nlg);
// // TODO: Do not use PreorderNodeListGenerator#getClassPath() until MRESOLVER-483 is fixed/released
// info("{}",
// nlg.getFiles().stream().map(File::getAbsolutePath).collect(Collectors.joining(File.pathSeparator)));
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.eclipse.aether.version.VersionScheme;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;
import picocli.CommandLine;

Expand Down Expand Up @@ -72,6 +74,8 @@ public abstract class CommandSupport implements Callable<Integer> {
description = "Define a HTTP proxy (host:port)")
protected String proxy;

protected final Logger logger = LoggerFactory.getLogger(getClass());

private static final ConcurrentHashMap<String, ArrayDeque<Object>> EXECUTION_CONTEXT = new ConcurrentHashMap<>();

protected Object getOrCreate(String key, Supplier<?> supplier) {
Expand Down
81 changes: 12 additions & 69 deletions cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Exists.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@
*/
package eu.maveniverse.maven.toolbox.cli;

import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.search.api.SearchBackend;
import org.apache.maven.search.api.SearchRequest;
import org.apache.maven.search.api.SearchResponse;
import org.apache.maven.search.api.request.Query;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.util.artifact.SubArtifact;
import picocli.CommandLine;

/**
Expand All @@ -25,8 +17,8 @@
@CommandLine.Command(name = "exists", description = "Checks Maven Artifact existence")
public final class Exists extends SearchCommandSupport {

@CommandLine.Parameters(description = "The GAVs to check")
private List<String> gavs;
@CommandLine.Parameters(description = "The GAV to check")
private String gav;

@CommandLine.Option(
names = {"--pom"},
Expand All @@ -43,6 +35,11 @@ public final class Exists extends SearchCommandSupport {
description = "Check javadoc JARs as well (derive coordinates from GAV)")
private boolean javadoc;

@CommandLine.Option(
names = {"--signature"},
description = "Check PGP signature presence as well (derive coordinates from GAV)")
private boolean signature;

@CommandLine.Option(
names = {"--all-required"},
description =
Expand All @@ -51,63 +48,9 @@ public final class Exists extends SearchCommandSupport {

@Override
protected Integer doCall() throws IOException {
ArrayList<Artifact> missingOnes = new ArrayList<>();
ArrayList<Artifact> existingOnes = new ArrayList<>();
try (SearchBackend backend = getRemoteRepositoryBackend(repositoryId, repositoryBaseUri, repositoryVendor)) {
for (String gav : gavs) {
Artifact artifact = new DefaultArtifact(gav);
boolean exists = exists(backend, artifact);
if (!exists) {
missingOnes.add(artifact);
} else {
existingOnes.add(artifact);
}
info("Artifact {} {}", artifact, exists ? "EXISTS" : "NOT EXISTS");
if (pom && !"pom".equals(artifact.getExtension())) {
Artifact pom = new SubArtifact(artifact, null, "pom");
exists = exists(backend, pom);
if (!exists && allRequired) {
missingOnes.add(pom);
} else if (allRequired) {
existingOnes.add(pom);
}
info(" {} {}", pom, exists ? "EXISTS" : "NOT EXISTS");
}
if (sources) {
Artifact sources = new SubArtifact(artifact, "sources", "jar");
exists = exists(backend, sources);
if (!exists && allRequired) {
missingOnes.add(sources);
} else if (allRequired) {
existingOnes.add(sources);
}
info(" {} {}", sources, exists ? "EXISTS" : "NOT EXISTS");
}
if (javadoc) {
Artifact javadoc = new SubArtifact(artifact, "javadoc", "jar");
exists = exists(backend, javadoc);
if (!exists && allRequired) {
missingOnes.add(javadoc);
} else if (allRequired) {
existingOnes.add(javadoc);
}
info(" {} {}", javadoc, exists ? "EXISTS" : "NOT EXISTS");
}
}
}
info("");
info(
"Checked TOTAL of {} (existing: {} not existing: {})",
existingOnes.size() + missingOnes.size(),
existingOnes.size(),
missingOnes.size());
return missingOnes.isEmpty() ? 0 : 1;
}

private boolean exists(SearchBackend backend, Artifact artifact) throws IOException {
Query query = toRrQuery(artifact);
SearchRequest searchRequest = new SearchRequest(query);
SearchResponse searchResponse = backend.search(searchRequest);
return searchResponse.getTotalHits() == 1;
return ToolboxCommando.getOrCreate(getContext())
.exists(getRemoteRepository(), gav, pom, sources, javadoc, signature, allRequired, logger)
? 0
: 1;
}
}
48 changes: 2 additions & 46 deletions cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Identify.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,8 @@
*/
package eu.maveniverse.maven.toolbox.cli;

import static org.apache.maven.search.api.request.FieldQuery.fieldQuery;

import java.io.FileInputStream;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.maven.search.api.MAVEN;
import org.apache.maven.search.api.SearchBackend;
import org.apache.maven.search.api.SearchRequest;
import org.apache.maven.search.api.SearchResponse;
import org.eclipse.aether.util.ChecksumUtils;
import picocli.CommandLine;

/**
Expand All @@ -33,39 +22,6 @@ public final class Identify extends SearchCommandSupport {

@Override
protected Integer doCall() throws IOException {
String sha1;
if (Files.exists(Paths.get(target))) {
try {
verbose("Calculating SHA1 of file {}", target);
MessageDigest sha1md = MessageDigest.getInstance("SHA-1");
byte[] buf = new byte[8192];
int read;
try (FileInputStream fis = new FileInputStream(target)) {
read = fis.read(buf);
while (read != -1) {
sha1md.update(buf, 0, read);
read = fis.read(buf);
}
}
sha1 = ChecksumUtils.toHexString(sha1md.digest());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA1 MessageDigest unavailable", e);
}
} else {
sha1 = target;
}
verbose("Identifying artifact with SHA1={}", sha1);
try (SearchBackend backend = getSmoBackend(repositoryId)) {
SearchRequest searchRequest = new SearchRequest(fieldQuery(MAVEN.SHA1, sha1));
SearchResponse searchResponse = backend.search(searchRequest);

renderPage(searchResponse.getPage(), null).forEach(this::info);
while (searchResponse.getCurrentHits() > 0) {
searchResponse =
backend.search(searchResponse.getSearchRequest().nextPage());
renderPage(searchResponse.getPage(), null).forEach(this::info);
}
}
return 0;
return ToolboxCommando.getOrCreate(getContext()).identify(getRemoteRepository(), target, logger) ? 0 : 1;
}
}
52 changes: 2 additions & 50 deletions cli/src/main/java/eu/maveniverse/maven/toolbox/cli/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,8 @@
*/
package eu.maveniverse.maven.toolbox.cli;

import static org.apache.maven.search.api.request.BooleanQuery.and;
import static org.apache.maven.search.api.request.FieldQuery.fieldQuery;

import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.io.IOException;
import java.util.function.Predicate;
import org.apache.maven.search.api.MAVEN;
import org.apache.maven.search.api.SearchBackend;
import org.apache.maven.search.api.SearchRequest;
import org.apache.maven.search.api.SearchResponse;
import org.apache.maven.search.api.request.Query;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.aether.version.VersionConstraint;
import org.eclipse.aether.version.VersionScheme;
import picocli.CommandLine;

/**
Expand All @@ -33,43 +22,6 @@ public final class List extends SearchCommandSupport {

@Override
protected Integer doCall() throws IOException {
try (SearchBackend backend = getRemoteRepositoryBackend(repositoryId, repositoryBaseUri, repositoryVendor)) {
String[] elements = gavoid.split(":");
if (elements.length < 1 || elements.length > 3) {
throw new IllegalArgumentException("Invalid gavoid");
}

Query query = fieldQuery(MAVEN.GROUP_ID, elements[0]);
if (elements.length > 1) {
query = and(query, fieldQuery(MAVEN.ARTIFACT_ID, elements[1]));
}

VersionScheme versionScheme = getVersionScheme();
Predicate<String> versionPredicate = null;
if (elements.length > 2) {
try {
VersionConstraint versionConstraint = versionScheme.parseVersionConstraint(elements[2]);
if (versionConstraint.getRange() != null) {
versionPredicate = s -> {
try {
return versionConstraint.containsVersion(versionScheme.parseVersion(s));
} catch (InvalidVersionSpecificationException e) {
return false;
}
};
}
} catch (InvalidVersionSpecificationException e) {
// ignore and continue as before
}
if (versionPredicate == null) {
query = and(query, fieldQuery(MAVEN.VERSION, elements[2]));
}
}
SearchRequest searchRequest = new SearchRequest(query);
SearchResponse searchResponse = backend.search(searchRequest);

renderPage(searchResponse.getPage(), versionPredicate).forEach(this::info);
}
return 0;
return ToolboxCommando.getOrCreate(getContext()).list(getRemoteRepository(), gavoid, logger) ? 0 : 1;
}
}
Loading

0 comments on commit e2aeec9

Please sign in to comment.