Skip to content

Commit

Permalink
wip: try to split cli and cli-common
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Sep 27, 2024
1 parent 386a5c0 commit ab5590e
Show file tree
Hide file tree
Showing 12 changed files with 616 additions and 3 deletions.
11 changes: 11 additions & 0 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6450,6 +6450,17 @@
<artifactId>quarkus-devtools-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-cli-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-cli-common</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-analytics-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
public interface QuarkusApplication {

int run(String... args) throws Exception;
}
}
106 changes: 106 additions & 0 deletions devtools/cli-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-build-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
<relativePath>../../build-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-cli-common</artifactId>
<name>Quarkus - Command Line Interface - Common</name>
<description>Quarkus command line common classes</description>

<dependencies>

<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-devtools-common</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-devtools-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config-crypto</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
<configuration>
<skipOriginalJarRename>true</skipOriginalJarRename>
<environmentVariables>
<MAVEN_REPO_LOCAL>${settings.localRepository}</MAVEN_REPO_LOCAL>
<GRADLE_OPTS>${env.MAVEN_OPTS}</GRADLE_OPTS>
</environmentVariables>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<JBANG_REPO>${settings.localRepository}</JBANG_REPO>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.cli.common;

public interface CommandWithOutput {

OutputOptionMixin getOutput();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package io.quarkus.cli.common;

import static io.quarkus.devtools.messagewriter.MessageIcons.ERROR_ICON;
import static io.quarkus.devtools.messagewriter.MessageIcons.WARN_ICON;

import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import io.quarkus.devtools.messagewriter.MessageWriter;
import picocli.CommandLine;
import picocli.CommandLine.Help.ColorScheme;
import picocli.CommandLine.Model.CommandSpec;

public class OutputOptionMixin implements MessageWriter {

static final boolean picocliDebugEnabled = "DEBUG".equalsIgnoreCase(System.getProperty("picocli.trace"));

boolean verbose = false;

@CommandLine.Option(names = { "-e", "--errors" }, description = "Print more context on errors and exceptions.")
boolean showErrors;

@CommandLine.Option(names = {
"--cli-test" }, hidden = true, description = "Manually set output streams for unit test purposes.")
boolean cliTestMode;

Path testProjectRoot;

@CommandLine.Option(names = { "--cli-test-dir" }, hidden = true)
void setTestProjectRoot(String path) {
// Allow the starting/project directory to be specified. Used during test.
testProjectRoot = Paths.get(path).toAbsolutePath();
}

@CommandLine.Spec(CommandLine.Spec.Target.MIXEE)
CommandSpec mixee;

ColorScheme scheme;
PrintWriter out;
PrintWriter err;

ColorScheme colorScheme() {
ColorScheme colors = scheme;
if (colors == null) {
colors = scheme = mixee.commandLine().getColorScheme();
}
return colors;
}

public PrintWriter out() {
PrintWriter o = out;
if (o == null) {
o = out = mixee.commandLine().getOut();
}
return o;
}

public PrintWriter err() {
PrintWriter e = err;
if (e == null) {
e = err = mixee.commandLine().getErr();
}
return e;
}

public boolean isShowErrors() {
return showErrors || picocliDebugEnabled;
}

private static OutputOptionMixin getOutput(CommandSpec commandSpec) {
return ((CommandWithOutput) commandSpec.root().userObject()).getOutput();
}

@CommandLine.Option(names = { "--verbose" }, description = "Verbose mode.")
public void setVerbose(boolean verbose) {
getOutput(mixee).verbose = verbose;
}

public boolean getVerbose() {
return getOutput(mixee).verbose;
}

public boolean isVerbose() {
return getVerbose() || picocliDebugEnabled;
}

public boolean isCliTest() {
return cliTestMode;
}

public boolean isAnsiEnabled() {
return CommandLine.Help.Ansi.AUTO.enabled();
}

public void printText(String... text) {
for (String line : text) {
out().println(colorScheme().ansi().new Text(line, colorScheme()));
}
}

public void printErrorText(String[] text) {
for (String line : text) {
err().println(colorScheme().errorText(line));
}
}

public void printStackTrace(Exception ex) {
if (isShowErrors()) {
err().println(colorScheme().stackTraceText(ex));
}
}

public Path getTestDirectory() {
if (isCliTest()) {
return testProjectRoot;
}
return null;
}

@Override
public void info(String msg) {
out().println(colorScheme().ansi().new Text(msg, colorScheme()));
}

@Override
public void error(String msg) {
out().println(colorScheme().errorText(ERROR_ICON + " " + msg));
}

@Override
public boolean isDebugEnabled() {
return isVerbose();
}

@Override
public void debug(String msg) {
if (isVerbose()) {
out().println(colorScheme().ansi().new Text("@|faint [DEBUG] " + msg + "|@", colorScheme()));
}
}

@Override
public void warn(String msg) {
out().println(colorScheme().ansi().new Text("@|yellow " + WARN_ICON + " " + msg + "|@", colorScheme()));
}

// CommandLine must be passed in (forwarded commands)
public void throwIfUnmatchedArguments(CommandLine cmd) {
List<String> unmatchedArguments = cmd.getUnmatchedArguments();
if (!unmatchedArguments.isEmpty()) {
throw new CommandLine.UnmatchedArgumentException(cmd, unmatchedArguments);
}
}

public int handleCommandException(Exception ex, String message) {
CommandLine cmd = mixee.commandLine();
printStackTrace(ex);
if (ex instanceof CommandLine.ParameterException) {
CommandLine.UnmatchedArgumentException.printSuggestions((CommandLine.ParameterException) ex, out());
}
error(message);
return cmd.getExitCodeExceptionMapper() != null ? cmd.getExitCodeExceptionMapper().getExitCode(ex)
: mixee.exitCodeOnInvalidInput();
}

@Override
public String toString() {
return "OutputOptions [testMode=" + cliTestMode
+ ", showErrors=" + showErrors
+ ", verbose=" + getVerbose() + "]";
}

}
Loading

0 comments on commit ab5590e

Please sign in to comment.