Skip to content

Commit d5e34d4

Browse files
committed
WIP
1 parent c53e885 commit d5e34d4

File tree

13 files changed

+430
-77
lines changed

13 files changed

+430
-77
lines changed

cli/src/main/java/eu/maveniverse/maven/toolbox/cli/CommandSupport.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import eu.maveniverse.maven.mima.context.Runtime;
2626
import eu.maveniverse.maven.mima.context.Runtimes;
2727
import eu.maveniverse.maven.toolbox.shared.Output;
28+
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
2829
import java.io.PrintStream;
2930
import java.nio.file.Path;
3031
import java.util.ArrayDeque;
@@ -46,7 +47,7 @@
4647
/**
4748
* Support class.
4849
*/
49-
public abstract class CommandSupport implements Callable<Integer> {
50+
public abstract class CommandSupport implements Callable<Integer>, CommandLine.IVersionProvider {
5051
@CommandLine.Option(
5152
names = {"-v", "--verbose"},
5253
description = "Be verbose about things happening")
@@ -137,9 +138,21 @@ protected void push(String key, Object object) {
137138
deque.push(object);
138139
}
139140

141+
@Override
142+
public String[] getVersion() {
143+
return new String[] {
144+
"MIMA " + getRuntime().version(),
145+
"Toolbox " + ToolboxCommando.getOrCreate(getContext()).getVersion()
146+
};
147+
}
148+
140149
protected void mayDumpEnv(Runtime runtime, Context context, boolean verbose) {
141-
normal("MIMA (Runtime '{}' version {})", runtime.name(), runtime.version());
142-
normal("====");
150+
warn(
151+
"Toolbox {} (MIMA Runtime '{}' version {})",
152+
ToolboxCommando.getOrCreate(getContext()).getVersion(),
153+
runtime.name(),
154+
runtime.version());
155+
warn("=======");
143156
normal(" Maven version {}", runtime.mavenVersion());
144157
normal(" Managed {}", runtime.managedRepositorySystem());
145158
normal(" Basedir {}", context.basedir());

cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Deploy.java

+29-12
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
package eu.maveniverse.maven.toolbox.cli;
99

1010
import eu.maveniverse.maven.mima.context.Context;
11+
import eu.maveniverse.maven.toolbox.shared.Artifacts;
1112
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
1213
import java.nio.file.Path;
13-
import java.util.ArrayList;
14-
import org.eclipse.aether.artifact.Artifact;
15-
import org.eclipse.aether.artifact.DefaultArtifact;
1614
import org.eclipse.aether.deployment.DeploymentException;
17-
import org.eclipse.aether.util.artifact.SubArtifact;
1815
import picocli.CommandLine;
1916

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

26-
@CommandLine.Parameters(index = "0", description = "The GAV to deploy")
23+
@CommandLine.Parameters(index = "0", description = "The RemoteRepository spec (id::url)", arity = "1")
24+
private String remoteRepositorySpec;
25+
26+
@CommandLine.Parameters(index = "1", description = "The GAV to install", arity = "1")
2727
private String gav;
2828

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

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

35-
@CommandLine.Parameters(index = "3", description = "The RemoteRepository spec (id::url)")
36-
private String remoteRepositorySpec;
37+
@CommandLine.Option(
38+
names = {"--sources"},
39+
description = "The sources JAR to deploy")
40+
private Path sources;
41+
42+
@CommandLine.Option(
43+
names = {"--javadoc"},
44+
description = "The javadoc JAR to deploy")
45+
private Path javadoc;
3746

3847
@Override
3948
protected boolean doCall(Context context) throws DeploymentException {
40-
ArrayList<Artifact> artifacts = new ArrayList<>();
41-
artifacts.add(new DefaultArtifact(gav).setFile(jar.toFile()));
42-
artifacts.add(new SubArtifact(artifacts.get(0), "", "pom").setFile(pom.toFile()));
49+
Artifacts artifacts = new Artifacts(gav);
50+
artifacts.addMain(jar);
51+
if (pom != null) {
52+
artifacts.addPom(pom);
53+
}
54+
if (sources != null) {
55+
artifacts.addSources(sources);
56+
}
57+
if (javadoc != null) {
58+
artifacts.addJavadoc(javadoc);
59+
}
4360
return ToolboxCommando.getOrCreate(getContext()).deploy(remoteRepositorySpec, artifacts, output);
4461
}
4562
}

cli/src/main/java/eu/maveniverse/maven/toolbox/cli/DeployRecorded.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import eu.maveniverse.maven.mima.context.Context;
1111
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
12-
import java.util.HashSet;
1312
import org.eclipse.aether.deployment.DeploymentException;
1413
import picocli.CommandLine;
1514

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

2423
@Override
2524
protected boolean doCall(Context context) throws DeploymentException {
26-
normal("Deploying recorded");
27-
28-
ToolboxCommando toolboxCommando = ToolboxCommando.getOrCreate(getContext());
29-
toolboxCommando.artifactRecorder().setActive(false);
30-
return toolboxCommando.deploy(
31-
remoteRepositorySpec,
32-
new HashSet<>(toolboxCommando.artifactRecorder().getAllArtifacts()),
33-
output);
25+
return ToolboxCommando.getOrCreate(getContext()).deployAllRecorded(remoteRepositorySpec, true, output);
3426
}
3527
}

cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Install.java

+27-10
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
package eu.maveniverse.maven.toolbox.cli;
99

1010
import eu.maveniverse.maven.mima.context.Context;
11+
import eu.maveniverse.maven.toolbox.shared.Artifacts;
1112
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
1213
import java.nio.file.Path;
13-
import java.util.ArrayList;
14-
import org.eclipse.aether.artifact.Artifact;
15-
import org.eclipse.aether.artifact.DefaultArtifact;
1614
import org.eclipse.aether.installation.InstallationException;
17-
import org.eclipse.aether.util.artifact.SubArtifact;
1815
import picocli.CommandLine;
1916

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

26-
@CommandLine.Parameters(index = "0", description = "The GAV to install")
23+
@CommandLine.Parameters(index = "0", description = "The GAV to install", arity = "1")
2724
private String gav;
2825

29-
@CommandLine.Parameters(index = "1", description = "The artifact JAR file")
26+
@CommandLine.Parameters(index = "1", description = "The artifact JAR file", arity = "1")
3027
private Path jar;
3128

32-
@CommandLine.Parameters(index = "2", description = "The artifact POM file")
29+
@CommandLine.Option(
30+
names = {"--pom"},
31+
description = "The POM to install")
3332
private Path pom;
3433

34+
@CommandLine.Option(
35+
names = {"--sources"},
36+
description = "The sources JAR to install")
37+
private Path sources;
38+
39+
@CommandLine.Option(
40+
names = {"--javadoc"},
41+
description = "The javadoc JAR to install")
42+
private Path javadoc;
43+
3544
@Override
3645
protected boolean doCall(Context context) throws InstallationException {
37-
ArrayList<Artifact> artifacts = new ArrayList<>();
38-
artifacts.add(new DefaultArtifact(gav).setFile(jar.toFile()));
39-
artifacts.add(new SubArtifact(artifacts.get(0), "", "pom").setFile(pom.toFile()));
46+
Artifacts artifacts = new Artifacts(gav);
47+
artifacts.addMain(jar);
48+
if (pom != null) {
49+
artifacts.addPom(pom);
50+
}
51+
if (sources != null) {
52+
artifacts.addSources(sources);
53+
}
54+
if (javadoc != null) {
55+
artifacts.addJavadoc(javadoc);
56+
}
4057
return ToolboxCommando.getOrCreate(getContext()).install(artifacts, output);
4158
}
4259
}

cli/src/main/java/eu/maveniverse/maven/toolbox/cli/ListAvailablePlugins.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import eu.maveniverse.maven.mima.context.Context;
1111
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
12-
import java.util.Collections;
1312
import picocli.CommandLine;
1413

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

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

2423
@Override
2524
protected boolean doCall(Context context) throws Exception {
26-
return ToolboxCommando.getOrCreate(context).listAvailablePlugins(Collections.singletonList(g), output);
25+
return ToolboxCommando.getOrCreate(context).listAvailablePlugins(groupIds, output);
2726
}
2827
}

cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Main.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Main.
1515
*/
1616
@CommandLine.Command(
17-
name = "mima",
17+
name = "toolbox",
1818
subcommands = {
1919
Classpath.class,
2020
Deploy.class,
@@ -35,14 +35,9 @@
3535
Verify.class
3636
},
3737
versionProvider = Main.class,
38-
description = "MIMA CLI",
38+
description = "Toolbox CLI",
3939
mixinStandardHelpOptions = true)
40-
public class Main extends CommandSupport implements CommandLine.IVersionProvider {
41-
@Override
42-
public String[] getVersion() {
43-
return new String[] {"MIMA " + getRuntime().version()};
44-
}
45-
40+
public class Main extends CommandSupport {
4641
@Override
4742
protected boolean doCall(Context context) {
4843
mayDumpEnv(getRuntime(), context, false);

cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Record.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@
1818
@CommandLine.Command(name = "record", description = "Records resolved Maven Artifacts")
1919
public final class Record extends ResolverCommandSupport {
2020

21+
@CommandLine.Option(
22+
names = {"--stop"},
23+
description = "Stop recording (otherwise it starts it)")
24+
private boolean stop;
25+
2126
@Override
2227
protected boolean doCall(Context context) throws DependencyResolutionException {
23-
return ToolboxCommando.getOrCreate(context).artifactRecorder().setActive(true);
28+
if (stop) {
29+
return ToolboxCommando.getOrCreate(context).recordStop(output);
30+
} else {
31+
return ToolboxCommando.getOrCreate(context).recordStart(output);
32+
}
2433
}
2534
}

cli/src/main/java/eu/maveniverse/maven/toolbox/cli/Repl.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class Repl extends CommandSupport {
3636
@Override
3737
public boolean doCall(Context context) {
3838
Class<?> tp = JansiTerminalProvider.class;
39-
push(Context.class.getName(), context);
39+
push(Context.class.getName(), context.customize(context.contextOverrides()));
4040

4141
// set up JLine built-in commands
4242
ConfigurationPath configPath = new ConfigurationPath(context.basedir(), context.basedir());
@@ -90,8 +90,6 @@ public boolean doCall(Context context) {
9090
} catch (Exception e) {
9191
error("REPL Failure: ", e);
9292
return false;
93-
} finally {
94-
context.close();
9593
}
9694
}
9795
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2023-2024 Maveniverse Org.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-v20.html
7+
*/
8+
package eu.maveniverse.maven.toolbox.shared;
9+
10+
import static java.util.Objects.requireNonNull;
11+
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.util.ArrayList;
15+
import java.util.Collection;
16+
import java.util.HashMap;
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.Objects;
20+
import java.util.function.Supplier;
21+
import org.eclipse.aether.artifact.Artifact;
22+
import org.eclipse.aether.artifact.DefaultArtifact;
23+
24+
/**
25+
* Construction to group artifacts, make them "like a project is".
26+
* <p>
27+
* Warning: this abstraction uses extension and not packaging, as this one does not create POM, it is caller obligation.
28+
*/
29+
public final class Artifacts implements Supplier<Collection<Artifact>> {
30+
private final String groupId;
31+
private final String artifactId;
32+
private final String version;
33+
private final String extension;
34+
private final Map<CE, Path> artifacts;
35+
36+
public Artifacts(String gav) {
37+
DefaultArtifact prototype = new DefaultArtifact(gav);
38+
this.groupId = prototype.getGroupId();
39+
this.artifactId = prototype.getArtifactId();
40+
this.version = prototype.getVersion();
41+
this.extension = prototype.getExtension();
42+
this.artifacts = new HashMap<>();
43+
}
44+
45+
public void addPom(Path artifact) {
46+
addArtifact(null, "pom", artifact);
47+
}
48+
49+
public void addMain(Path artifact) {
50+
addArtifact(null, extension, artifact);
51+
}
52+
53+
public void addSources(Path artifact) {
54+
addArtifact("sources", "jar", artifact);
55+
}
56+
57+
public void addJavadoc(Path artifact) {
58+
addArtifact("javadoc", "jar", artifact);
59+
}
60+
61+
public void addArtifact(String classifier, String extension, Path artifact) {
62+
requireNonNull(extension, "extension");
63+
requireNonNull(artifact, "artifact");
64+
if (!Files.exists(artifact) || Files.isDirectory(artifact)) {
65+
throw new IllegalArgumentException("artifact backing file must exist and cannot be a directory");
66+
}
67+
CE ce = new CE(classifier, extension);
68+
if (artifacts.containsKey(ce)) {
69+
throw new IllegalArgumentException("artifact already present");
70+
}
71+
artifacts.put(ce, artifact);
72+
}
73+
74+
@Override
75+
public List<Artifact> get() {
76+
ArrayList<Artifact> result = new ArrayList<>(artifacts.size());
77+
artifacts.forEach((ce, path) -> result.add(
78+
new DefaultArtifact(groupId, artifactId, ce.classifier, ce.extension, version).setFile(path.toFile())));
79+
return result;
80+
}
81+
82+
private static final class CE {
83+
private final String classifier;
84+
private final String extension;
85+
86+
private CE(String classifier, String extension) {
87+
this.classifier = classifier;
88+
this.extension = requireNonNull(extension, "extension");
89+
}
90+
91+
@Override
92+
public boolean equals(Object o) {
93+
if (this == o) {
94+
return true;
95+
}
96+
if (o == null || getClass() != o.getClass()) {
97+
return false;
98+
}
99+
CE ce = (CE) o;
100+
return Objects.equals(classifier, ce.classifier) && Objects.equals(extension, ce.extension);
101+
}
102+
103+
@Override
104+
public int hashCode() {
105+
return Objects.hash(classifier, extension);
106+
}
107+
108+
@Override
109+
public String toString() {
110+
return (classifier == null ? "" : classifier) + "." + extension;
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)