Skip to content

Commit

Permalink
Extract a clean api (#1752)
Browse files Browse the repository at this point in the history
* Move api into a dedicated package
* Introduce a `MavenInvokerRequest`
* Rename `BaseOptions` to `Options`
* Make `InvokerRequest` parameterised as `InvokerRequest<T extends Options>`
  • Loading branch information
gnodet authored Sep 29, 2024
1 parent 7ba5e6f commit 4bf26a7
Show file tree
Hide file tree
Showing 23 changed files with 114 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker;
package org.apache.maven.api.cli;

import org.apache.maven.api.annotations.Nonnull;

/**
* Component responsible to invoke an application using information provided in invoker request.
Expand All @@ -27,5 +29,5 @@ public interface Invoker<R extends InvokerRequest> {
/**
* Invokes application and returns exit code.
*/
int invoke(R invokerRequest) throws InvokerException;
int invoke(@Nonnull R invokerRequest) throws InvokerException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker;
package org.apache.maven.api.cli;

public class InvokerException extends RuntimeException {
public InvokerException(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker;
package org.apache.maven.api.cli;

import java.io.InputStream;
import java.io.PrintStream;
Expand All @@ -34,7 +34,7 @@
/**
* Maven execution request.
*/
public interface InvokerRequest {
public interface InvokerRequest<T extends Options> {
/**
* Mandatory: the current working directory, usually the {@code "user.dir"} Java System Property.
*/
Expand Down Expand Up @@ -121,5 +121,5 @@ public interface InvokerRequest {
* The mandatory options.
*/
@Nonnull
BaseOptions options();
T options();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker;
package org.apache.maven.api.cli;

import java.io.PrintStream;
import java.util.Collection;
Expand All @@ -28,7 +28,7 @@
/**
* Base options, options supported by our tools.
*/
public interface BaseOptions {
public interface Options {
@Nonnull
Optional<Map<String, String>> userProperties();

Expand Down Expand Up @@ -78,11 +78,11 @@ public interface BaseOptions {
Optional<Boolean> help();

/**
* Returns new instance of {@link BaseOptions} that is result of interpolating this instance with given collection
* Returns new instance of {@link Options} that is result of interpolating this instance with given collection
* of properties.
*/
@Nonnull
BaseOptions interpolate(Collection<Map<String, String>> properties);
Options interpolate(Collection<Map<String, String>> properties);

/**
* Displays help.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker;
package org.apache.maven.api.cli;

import java.io.IOException;

import org.apache.maven.api.annotations.Nonnull;

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

@Nonnull
R parse(ParserRequest parserRequest) throws ParserException, IOException;
R parse(@Nonnull ParserRequest parserRequest) throws ParserException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker;
package org.apache.maven.api.cli;

/**
* Parser exception is a "terminal" exception, but usually denotes "user error", like bad argument lists, or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker;
package org.apache.maven.api.cli;

import java.io.InputStream;
import java.io.PrintStream;
Expand All @@ -25,6 +25,7 @@
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;

Expand Down Expand Up @@ -87,7 +88,7 @@ public interface ParserRequest {
PrintStream err();

@Nonnull
static Builder builder(String[] args) {
static Builder builder(@Nonnull String[] args) {
return new Builder(args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker.mvn;
package org.apache.maven.api.cli.mvn;

import org.apache.maven.cling.invoker.Invoker;
import org.apache.maven.cling.invoker.InvokerException;
import org.apache.maven.api.cli.Invoker;
import org.apache.maven.api.cli.InvokerException;

/**
* Component responsible to invoke Maven using information provided in invoker request.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.cli.mvn;

import org.apache.maven.api.cli.InvokerRequest;

public interface MavenInvokerRequest extends InvokerRequest<MavenOptions> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker.mvn;
package org.apache.maven.api.cli.mvn;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.cling.invoker.BaseOptions;
import org.apache.maven.api.cli.Options;

/**
* Maven options.
* <p>
* This is pretty much what Maven CLI surface offers as knobs and switches. Also, {@code maven.config} may contain
* subset of these (ie no goals).
*/
public interface MavenOptions extends BaseOptions {
public interface MavenOptions extends Options {
@Nonnull
Optional<String> alternatePomFile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cling.invoker.mvn;
package org.apache.maven.api.cli.mvn;

import java.io.IOException;

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.cling.invoker.Parser;
import org.apache.maven.cling.invoker.ParserException;
import org.apache.maven.cling.invoker.ParserRequest;
import org.apache.maven.api.cli.Parser;
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.api.cli.ParserRequest;

public interface MavenParser extends Parser<MavenInvokerRequest> {
@Nonnull
MavenInvokerRequest parse(ParserRequest parserRequest) throws ParserException, IOException;
MavenInvokerRequest parse(@Nonnull ParserRequest parserRequest) throws ParserException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import java.io.IOException;

import org.apache.maven.cling.invoker.InvokerException;
import org.apache.maven.cling.invoker.ParserException;
import org.apache.maven.api.cli.InvokerException;
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.cling.invoker.mvn.local.LocalInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalParser;
import org.apache.maven.jline.MessageUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import java.util.EnumSet;
import java.util.Set;

import org.apache.maven.cling.invoker.InvokerException;
import org.apache.maven.cling.invoker.ParserException;
import org.apache.maven.cling.invoker.ParserRequest;
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.mvn.local.LocalInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalParser;
import org.apache.maven.jline.MessageUtils;
Expand Down
6 changes: 3 additions & 3 deletions maven-cli/src/main/java/org/apache/maven/cling/MavenTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import java.util.EnumSet;
import java.util.Set;

import org.apache.maven.cling.invoker.InvokerException;
import org.apache.maven.cling.invoker.ParserException;
import org.apache.maven.cling.invoker.ParserRequest;
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.mvn.local.LocalInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalParser;
import org.apache.maven.jline.MessageUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.cli.InvokerRequest;
import org.apache.maven.api.services.MessageBuilderFactory;
import org.apache.maven.cli.internal.extension.model.CoreExtension;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.maven.api.cli.Options;
import org.apache.maven.cli.CLIManager;

import static java.util.Objects.requireNonNull;
import static org.apache.maven.cling.invoker.Utils.toMap;

public abstract class CommonsCliBaseOptions implements BaseOptions {
public abstract class CommonsCliOptions implements Options {
protected final CLIManager cliManager;
protected final CommandLine commandLine;

public CommonsCliBaseOptions(CLIManager cliManager, CommandLine commandLine) {
public CommonsCliOptions(CLIManager cliManager, CommandLine commandLine) {
this.cliManager = requireNonNull(cliManager);
this.commandLine = requireNonNull(commandLine);
}
Expand Down
Loading

0 comments on commit 4bf26a7

Please sign in to comment.