Skip to content

Commit

Permalink
Clean CLI api a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Sep 29, 2024
1 parent 18486ba commit 672ea36
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.maven.api.cli;

import java.io.InputStream;
import java.io.PrintStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -105,13 +105,13 @@ public interface InvokerRequest<O extends Options> {
* Optional: if running embedded.
*/
@Nonnull
Optional<PrintStream> out();
Optional<OutputStream> out();

/**
* Optional: if running embedded.
*/
@Nonnull
Optional<PrintStream> err();
Optional<OutputStream> err();

/**
* Optional: if core extensions were configured in {@code .mvn/extensions.xml} file.
Expand Down
4 changes: 2 additions & 2 deletions maven-cli/src/main/java/org/apache/maven/api/cli/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.maven.api.cli;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -87,5 +87,5 @@ public interface Options {
/**
* Displays help.
*/
void displayHelp(PrintStream printStream);
void displayHelp(PrintWriter printWriter);
}
7 changes: 5 additions & 2 deletions maven-cli/src/main/java/org/apache/maven/api/cli/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.io.IOException;

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.services.MessageBuilderFactory;
import org.slf4j.Logger;

public interface Parser<O extends Options, R extends InvokerRequest<O>> {
@Nonnull
default R parse(@Nonnull String[] args) throws ParserException, IOException {
return parse(ParserRequest.builder(args).build());
default R parse(@Nonnull String[] args, Logger logger, MessageBuilderFactory messageBuilderFactory)
throws ParserException, IOException {
return parse(ParserRequest.builder(args, logger, messageBuilderFactory).build());
}

@Nonnull
Expand Down
67 changes: 26 additions & 41 deletions maven-cli/src/main/java/org/apache/maven/api/cli/ParserRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,26 @@
package org.apache.maven.api.cli;

import java.io.InputStream;
import java.io.PrintStream;
import java.io.OutputStream;
import java.nio.file.Path;

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.services.MessageBuilderFactory;
import org.apache.maven.cling.invoker.ProtoLogger;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.slf4j.Logger;

import static java.util.Objects.requireNonNull;

public interface ParserRequest {

/**
* Mandatory: Logger to use at very early stages. Defaults to {@link ProtoLogger}.
* Mandatory: Logger to use at very early stages.
*/
@Nonnull
Logger logger();

/**
* Mandatory: MessageBuilderFactory to use. Defaults to {@link JLineMessageBuilderFactory}.
* Mandatory: MessageBuilderFactory to use.
*/
@Nonnull
MessageBuilderFactory messageBuilderFactory();
Expand Down Expand Up @@ -79,42 +77,35 @@ public interface ParserRequest {
* Optional: the STDOUT to use. If not given, {@link System#out} is used.
*/
@Nullable
PrintStream out();
OutputStream out();

/**
* Optional: the STDERR to use. If not given, {@link System#err} is used.
*/
@Nullable
PrintStream err();
OutputStream err();

@Nonnull
static Builder builder(@Nonnull String[] args) {
return new Builder(args);
static Builder builder(
@Nonnull String[] args, @Nonnull Logger logger, @Nonnull MessageBuilderFactory messageBuilderFactory) {
return new Builder(args, logger, messageBuilderFactory);
}

class Builder {
private final String[] args;
private Logger logger = new ProtoLogger();
private MessageBuilderFactory messageBuilderFactory = new JLineMessageBuilderFactory();
private Logger logger;
private MessageBuilderFactory messageBuilderFactory;
private Path cwd;
private Path mavenHome;
private Path userHome;
private InputStream in;
private PrintStream out;
private PrintStream err;
private OutputStream out;
private OutputStream err;

private Builder(String[] args) {
private Builder(String[] args, Logger logger, MessageBuilderFactory messageBuilderFactory) {
this.args = requireNonNull(args);
}

public Builder logger(Logger logger) {
this.logger = requireNonNull(logger);
return this;
}

public Builder messageBuilderFactory(MessageBuilderFactory messageBuilderFactory) {
this.messageBuilderFactory = requireNonNull(messageBuilderFactory);
return this;
}

public Builder cwd(Path cwd) {
Expand All @@ -137,24 +128,18 @@ public Builder in(InputStream in) {
return this;
}

public Builder out(PrintStream out) {
public Builder out(OutputStream out) {
this.out = out;
if (this.logger instanceof ProtoLogger) {
this.logger = new ProtoLogger(this.out, this.err);
}
return this;
}

public Builder err(PrintStream err) {
public Builder err(OutputStream err) {
this.err = err;
if (this.logger instanceof ProtoLogger) {
this.logger = new ProtoLogger(this.out, this.err);
}
return this;
}

public ParserRequest build() {
return new ParserRequestImpl(logger, messageBuilderFactory, args, cwd, mavenHome, userHome, in, out, err);
return new ParserRequestImpl(args, logger, messageBuilderFactory, cwd, mavenHome, userHome, in, out, err);
}

@SuppressWarnings("ParameterNumber")
Expand All @@ -166,22 +151,22 @@ private static class ParserRequestImpl implements ParserRequest {
private final Path mavenHome;
private final Path userHome;
private final InputStream in;
private final PrintStream out;
private final PrintStream err;
private final OutputStream out;
private final OutputStream err;

private ParserRequestImpl(
String[] args,
Logger logger,
MessageBuilderFactory messageBuilderFactory,
String[] args,
Path cwd,
Path mavenHome,
Path userHome,
InputStream in,
PrintStream out,
PrintStream err) {
this.logger = logger;
this.messageBuilderFactory = messageBuilderFactory;
this.args = args;
OutputStream out,
OutputStream err) {
this.args = requireNonNull(args);
this.logger = requireNonNull(logger);
this.messageBuilderFactory = requireNonNull(messageBuilderFactory);
this.cwd = cwd;
this.mavenHome = mavenHome;
this.userHome = userHome;
Expand Down Expand Up @@ -226,12 +211,12 @@ public InputStream in() {
}

@Override
public PrintStream out() {
public OutputStream out() {
return out;
}

@Override
public PrintStream err() {
public OutputStream err() {
return err;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

import org.apache.maven.api.cli.InvokerException;
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.cling.invoker.ProtoLogger;
import org.apache.maven.cling.invoker.mvn.local.LocalInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalParser;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.apache.maven.jline.MessageUtils;
import org.codehaus.plexus.classworlds.ClassWorld;

Expand Down Expand Up @@ -78,7 +80,8 @@ public int run(String[] args) throws IOException {
MessageUtils.systemInstall();
MessageUtils.registerShutdownHook();
try {
return new LocalInvoker(classWorld).invoke(new LocalParser().parse(args));
return new LocalInvoker(classWorld)
.invoke(new LocalParser().parse(args, new ProtoLogger(), new JLineMessageBuilderFactory()));
} catch (ParserException e) {
System.err.println(e.getMessage());
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

import org.apache.maven.api.cli.InvokerException;
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.api.cli.ParserRequest;
import org.apache.maven.cling.invoker.ProtoLogger;
import org.apache.maven.cling.invoker.mvn.local.LocalInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalParser;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.apache.maven.jline.MessageUtils;
import org.codehaus.plexus.classworlds.ClassWorld;

Expand All @@ -56,44 +57,30 @@ public Set<SourceVersion> getSourceVersions() {

@Override
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
PrintStream stderr = toPsOrDef(err, System.err);
ProtoLogger logger = new ProtoLogger(out, err);
try (ClassWorld classWorld = new ClassWorld(
MavenCling.CORE_CLASS_REALM_ID, Thread.currentThread().getContextClassLoader())) {
MessageUtils.systemInstall();
MessageUtils.registerShutdownHook();
try {
return new LocalInvoker(classWorld)
.invoke(new LocalParser()
.parse(ParserRequest.builder(arguments)
.parse(ParserRequest.builder(arguments, logger, new JLineMessageBuilderFactory())
.in(in)
.out(toPs(out))
.err(toPs(err))
.out(out)
.err(err)
.build()));
} catch (ParserException e) {
stderr.println(e.getMessage());
logger.error(e.getMessage(), e);
return 1;
} catch (InvokerException e) {
return 1;
} finally {
MessageUtils.systemUninstall();
}
} catch (IOException e) {
e.printStackTrace(stderr);
logger.error(e.getMessage(), e);
return 2;
}
}

private PrintStream toPs(OutputStream outputStream) {
return toPsOrDef(outputStream, null);
}

private PrintStream toPsOrDef(OutputStream outputStream, PrintStream def) {
if (outputStream == null) {
return def;
}
if (outputStream instanceof PrintStream ps) {
return ps;
}
return new PrintStream(outputStream);
}
}
29 changes: 8 additions & 21 deletions maven-cli/src/main/java/org/apache/maven/cling/MavenTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

import org.apache.maven.api.cli.InvokerException;
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.api.cli.ParserRequest;
import org.apache.maven.cling.invoker.ProtoLogger;
import org.apache.maven.cling.invoker.mvn.local.LocalInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalParser;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.apache.maven.jline.MessageUtils;
import org.codehaus.plexus.classworlds.ClassWorld;

Expand All @@ -54,44 +55,30 @@ public Set<SourceVersion> getSourceVersions() {

@Override
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
PrintStream stderr = toPsOrDef(err, System.err);
ProtoLogger logger = new ProtoLogger(out, err);
try (ClassWorld classWorld = new ClassWorld(
MavenCling.CORE_CLASS_REALM_ID, Thread.currentThread().getContextClassLoader())) {
MessageUtils.systemInstall();
MessageUtils.registerShutdownHook();
try {
return new LocalInvoker(classWorld)
.invoke(new LocalParser()
.parse(ParserRequest.builder(arguments)
.parse(ParserRequest.builder(arguments, logger, new JLineMessageBuilderFactory())
.in(in)
.out(toPs(out))
.err(toPs(err))
.out(out)
.err(err)
.build()));
} catch (ParserException e) {
stderr.println(e.getMessage());
logger.error(e.getMessage(), e);
return 1;
} catch (InvokerException e) {
return 1;
} finally {
MessageUtils.systemUninstall();
}
} catch (IOException e) {
e.printStackTrace(stderr);
logger.error(e.getMessage(), e);
return 2;
}
}

private PrintStream toPs(OutputStream outputStream) {
return toPsOrDef(outputStream, null);
}

private PrintStream toPsOrDef(OutputStream outputStream, PrintStream def) {
if (outputStream == null) {
return def;
}
if (outputStream instanceof PrintStream ps) {
return ps;
}
return new PrintStream(outputStream);
}
}
Loading

0 comments on commit 672ea36

Please sign in to comment.