Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 21, 2024
1 parent c53e885 commit d5e34d4
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import eu.maveniverse.maven.mima.context.Runtime;
import eu.maveniverse.maven.mima.context.Runtimes;
import eu.maveniverse.maven.toolbox.shared.Output;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.io.PrintStream;
import java.nio.file.Path;
import java.util.ArrayDeque;
Expand All @@ -46,7 +47,7 @@
/**
* Support class.
*/
public abstract class CommandSupport implements Callable<Integer> {
public abstract class CommandSupport implements Callable<Integer>, CommandLine.IVersionProvider {
@CommandLine.Option(
names = {"-v", "--verbose"},
description = "Be verbose about things happening")
Expand Down Expand Up @@ -137,9 +138,21 @@ protected void push(String key, Object object) {
deque.push(object);
}

@Override
public String[] getVersion() {
return new String[] {
"MIMA " + getRuntime().version(),
"Toolbox " + ToolboxCommando.getOrCreate(getContext()).getVersion()
};
}

protected void mayDumpEnv(Runtime runtime, Context context, boolean verbose) {
normal("MIMA (Runtime '{}' version {})", runtime.name(), runtime.version());
normal("====");
warn(
"Toolbox {} (MIMA Runtime '{}' version {})",
ToolboxCommando.getOrCreate(getContext()).getVersion(),
runtime.name(),
runtime.version());
warn("=======");
normal(" Maven version {}", runtime.mavenVersion());
normal(" Managed {}", runtime.managedRepositorySystem());
normal(" Basedir {}", context.basedir());
Expand Down
41 changes: 29 additions & 12 deletions cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Deploy.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
package eu.maveniverse.maven.toolbox.cli;

import eu.maveniverse.maven.mima.context.Context;
import eu.maveniverse.maven.toolbox.shared.Artifacts;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.nio.file.Path;
import java.util.ArrayList;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.deployment.DeploymentException;
import org.eclipse.aether.util.artifact.SubArtifact;
import picocli.CommandLine;

/**
Expand All @@ -23,23 +20,43 @@
@CommandLine.Command(name = "deploy", description = "Deploys Maven Artifacts")
public final class Deploy extends ResolverCommandSupport {

@CommandLine.Parameters(index = "0", description = "The GAV to deploy")
@CommandLine.Parameters(index = "0", description = "The RemoteRepository spec (id::url)", arity = "1")
private String remoteRepositorySpec;

@CommandLine.Parameters(index = "1", description = "The GAV to install", arity = "1")
private String gav;

@CommandLine.Parameters(index = "1", description = "The artifact JAR file")
@CommandLine.Parameters(index = "2", description = "The artifact JAR file", arity = "1")
private Path jar;

@CommandLine.Parameters(index = "2", description = "The artifact POM file")
@CommandLine.Option(
names = {"--pom"},
description = "The POM to deploy")
private Path pom;

@CommandLine.Parameters(index = "3", description = "The RemoteRepository spec (id::url)")
private String remoteRepositorySpec;
@CommandLine.Option(
names = {"--sources"},
description = "The sources JAR to deploy")
private Path sources;

@CommandLine.Option(
names = {"--javadoc"},
description = "The javadoc JAR to deploy")
private Path javadoc;

@Override
protected boolean doCall(Context context) throws DeploymentException {
ArrayList<Artifact> artifacts = new ArrayList<>();
artifacts.add(new DefaultArtifact(gav).setFile(jar.toFile()));
artifacts.add(new SubArtifact(artifacts.get(0), "", "pom").setFile(pom.toFile()));
Artifacts artifacts = new Artifacts(gav);
artifacts.addMain(jar);
if (pom != null) {
artifacts.addPom(pom);
}
if (sources != null) {
artifacts.addSources(sources);
}
if (javadoc != null) {
artifacts.addJavadoc(javadoc);
}
return ToolboxCommando.getOrCreate(getContext()).deploy(remoteRepositorySpec, artifacts, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import eu.maveniverse.maven.mima.context.Context;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.util.HashSet;
import org.eclipse.aether.deployment.DeploymentException;
import picocli.CommandLine;

Expand All @@ -23,13 +22,6 @@ public final class DeployRecorded extends ResolverCommandSupport {

@Override
protected boolean doCall(Context context) throws DeploymentException {
normal("Deploying recorded");

ToolboxCommando toolboxCommando = ToolboxCommando.getOrCreate(getContext());
toolboxCommando.artifactRecorder().setActive(false);
return toolboxCommando.deploy(
remoteRepositorySpec,
new HashSet<>(toolboxCommando.artifactRecorder().getAllArtifacts()),
output);
return ToolboxCommando.getOrCreate(getContext()).deployAllRecorded(remoteRepositorySpec, true, output);
}
}
37 changes: 27 additions & 10 deletions cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Install.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
package eu.maveniverse.maven.toolbox.cli;

import eu.maveniverse.maven.mima.context.Context;
import eu.maveniverse.maven.toolbox.shared.Artifacts;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.nio.file.Path;
import java.util.ArrayList;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.installation.InstallationException;
import org.eclipse.aether.util.artifact.SubArtifact;
import picocli.CommandLine;

/**
Expand All @@ -23,20 +20,40 @@
@CommandLine.Command(name = "install", description = "Installs Maven Artifacts")
public final class Install extends ResolverCommandSupport {

@CommandLine.Parameters(index = "0", description = "The GAV to install")
@CommandLine.Parameters(index = "0", description = "The GAV to install", arity = "1")
private String gav;

@CommandLine.Parameters(index = "1", description = "The artifact JAR file")
@CommandLine.Parameters(index = "1", description = "The artifact JAR file", arity = "1")
private Path jar;

@CommandLine.Parameters(index = "2", description = "The artifact POM file")
@CommandLine.Option(
names = {"--pom"},
description = "The POM to install")
private Path pom;

@CommandLine.Option(
names = {"--sources"},
description = "The sources JAR to install")
private Path sources;

@CommandLine.Option(
names = {"--javadoc"},
description = "The javadoc JAR to install")
private Path javadoc;

@Override
protected boolean doCall(Context context) throws InstallationException {
ArrayList<Artifact> artifacts = new ArrayList<>();
artifacts.add(new DefaultArtifact(gav).setFile(jar.toFile()));
artifacts.add(new SubArtifact(artifacts.get(0), "", "pom").setFile(pom.toFile()));
Artifacts artifacts = new Artifacts(gav);
artifacts.addMain(jar);
if (pom != null) {
artifacts.addPom(pom);
}
if (sources != null) {
artifacts.addSources(sources);
}
if (javadoc != null) {
artifacts.addJavadoc(javadoc);
}
return ToolboxCommando.getOrCreate(getContext()).install(artifacts, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import eu.maveniverse.maven.mima.context.Context;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.util.Collections;
import picocli.CommandLine;

/**
Expand All @@ -18,11 +17,11 @@
@CommandLine.Command(name = "listAvailablePlugins", description = "List available plugins")
public final class ListAvailablePlugins extends ResolverCommandSupport {

@CommandLine.Parameters(index = "0", description = "The G to list")
private String g;
@CommandLine.Parameters(index = "*", description = "The G to list", arity = "1")
private java.util.List<String> groupIds;

@Override
protected boolean doCall(Context context) throws Exception {
return ToolboxCommando.getOrCreate(context).listAvailablePlugins(Collections.singletonList(g), output);
return ToolboxCommando.getOrCreate(context).listAvailablePlugins(groupIds, output);
}
}
11 changes: 3 additions & 8 deletions cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Main.
*/
@CommandLine.Command(
name = "mima",
name = "toolbox",
subcommands = {
Classpath.class,
Deploy.class,
Expand All @@ -35,14 +35,9 @@
Verify.class
},
versionProvider = Main.class,
description = "MIMA CLI",
description = "Toolbox CLI",
mixinStandardHelpOptions = true)
public class Main extends CommandSupport implements CommandLine.IVersionProvider {
@Override
public String[] getVersion() {
return new String[] {"MIMA " + getRuntime().version()};
}

public class Main extends CommandSupport {
@Override
protected boolean doCall(Context context) {
mayDumpEnv(getRuntime(), context, false);
Expand Down
11 changes: 10 additions & 1 deletion cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@
@CommandLine.Command(name = "record", description = "Records resolved Maven Artifacts")
public final class Record extends ResolverCommandSupport {

@CommandLine.Option(
names = {"--stop"},
description = "Stop recording (otherwise it starts it)")
private boolean stop;

@Override
protected boolean doCall(Context context) throws DependencyResolutionException {
return ToolboxCommando.getOrCreate(context).artifactRecorder().setActive(true);
if (stop) {
return ToolboxCommando.getOrCreate(context).recordStop(output);
} else {
return ToolboxCommando.getOrCreate(context).recordStart(output);
}
}
}
4 changes: 1 addition & 3 deletions cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class Repl extends CommandSupport {
@Override
public boolean doCall(Context context) {
Class<?> tp = JansiTerminalProvider.class;
push(Context.class.getName(), context);
push(Context.class.getName(), context.customize(context.contextOverrides()));

// set up JLine built-in commands
ConfigurationPath configPath = new ConfigurationPath(context.basedir(), context.basedir());
Expand Down Expand Up @@ -90,8 +90,6 @@ public boolean doCall(Context context) {
} catch (Exception e) {
error("REPL Failure: ", e);
return false;
} finally {
context.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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 java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;

/**
* Construction to group artifacts, make them "like a project is".
* <p>
* Warning: this abstraction uses extension and not packaging, as this one does not create POM, it is caller obligation.
*/
public final class Artifacts implements Supplier<Collection<Artifact>> {
private final String groupId;
private final String artifactId;
private final String version;
private final String extension;
private final Map<CE, Path> artifacts;

public Artifacts(String gav) {
DefaultArtifact prototype = new DefaultArtifact(gav);
this.groupId = prototype.getGroupId();
this.artifactId = prototype.getArtifactId();
this.version = prototype.getVersion();
this.extension = prototype.getExtension();
this.artifacts = new HashMap<>();
}

public void addPom(Path artifact) {
addArtifact(null, "pom", artifact);
}

public void addMain(Path artifact) {
addArtifact(null, extension, artifact);
}

public void addSources(Path artifact) {
addArtifact("sources", "jar", artifact);
}

public void addJavadoc(Path artifact) {
addArtifact("javadoc", "jar", artifact);
}

public void addArtifact(String classifier, String extension, Path artifact) {
requireNonNull(extension, "extension");
requireNonNull(artifact, "artifact");
if (!Files.exists(artifact) || Files.isDirectory(artifact)) {
throw new IllegalArgumentException("artifact backing file must exist and cannot be a directory");
}
CE ce = new CE(classifier, extension);
if (artifacts.containsKey(ce)) {
throw new IllegalArgumentException("artifact already present");
}
artifacts.put(ce, artifact);
}

@Override
public List<Artifact> get() {
ArrayList<Artifact> result = new ArrayList<>(artifacts.size());
artifacts.forEach((ce, path) -> result.add(
new DefaultArtifact(groupId, artifactId, ce.classifier, ce.extension, version).setFile(path.toFile())));
return result;
}

private static final class CE {
private final String classifier;
private final String extension;

private CE(String classifier, String extension) {
this.classifier = classifier;
this.extension = requireNonNull(extension, "extension");
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CE ce = (CE) o;
return Objects.equals(classifier, ce.classifier) && Objects.equals(extension, ce.extension);
}

@Override
public int hashCode() {
return Objects.hash(classifier, extension);
}

@Override
public String toString() {
return (classifier == null ? "" : classifier) + "." + extension;
}
}
}
Loading

0 comments on commit d5e34d4

Please sign in to comment.