Extract JLine support into its own module (#1426)

This commit is contained in:
Guillaume Nodet 2024-02-29 00:31:59 +01:00 committed by GitHub
parent ce15193394
commit a9934f91fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 144 additions and 19 deletions

View File

@ -48,6 +48,10 @@ under the License.
<groupId>org.apache.maven</groupId> <groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId> <artifactId>maven-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-jline</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.apache.maven</groupId> <groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId> <artifactId>maven-plugin-api</artifactId>

View File

@ -28,7 +28,7 @@ import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option; import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options; import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException; import org.apache.commons.cli.ParseException;
import org.apache.maven.cli.jline.MessageUtils; import org.apache.maven.jline.MessageUtils;
/** /**
*/ */

View File

@ -25,7 +25,7 @@ import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.Properties; import java.util.Properties;
import org.apache.maven.cli.jline.MessageUtils; import org.apache.maven.jline.MessageUtils;
import org.apache.maven.utils.Os; import org.apache.maven.utils.Os;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -58,8 +58,6 @@ import org.apache.maven.cli.event.ExecutionEventLogger;
import org.apache.maven.cli.internal.BootstrapCoreExtensionManager; import org.apache.maven.cli.internal.BootstrapCoreExtensionManager;
import org.apache.maven.cli.internal.extension.io.CoreExtensionsStaxReader; import org.apache.maven.cli.internal.extension.io.CoreExtensionsStaxReader;
import org.apache.maven.cli.internal.extension.model.CoreExtension; import org.apache.maven.cli.internal.extension.model.CoreExtension;
import org.apache.maven.cli.jline.JLineMessageBuilderFactory;
import org.apache.maven.cli.jline.MessageUtils;
import org.apache.maven.cli.logging.Slf4jConfiguration; import org.apache.maven.cli.logging.Slf4jConfiguration;
import org.apache.maven.cli.logging.Slf4jConfigurationFactory; import org.apache.maven.cli.logging.Slf4jConfigurationFactory;
import org.apache.maven.cli.logging.Slf4jLoggerManager; import org.apache.maven.cli.logging.Slf4jLoggerManager;
@ -81,6 +79,8 @@ import org.apache.maven.execution.scope.internal.MojoExecutionScope;
import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule; import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule;
import org.apache.maven.extension.internal.CoreExports; import org.apache.maven.extension.internal.CoreExports;
import org.apache.maven.extension.internal.CoreExtensionEntry; import org.apache.maven.extension.internal.CoreExtensionEntry;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.apache.maven.jline.MessageUtils;
import org.apache.maven.lifecycle.LifecycleExecutionException; import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.logwrapper.LogLevelRecorder; import org.apache.maven.logwrapper.LogLevelRecorder;
import org.apache.maven.logwrapper.MavenSlf4jWrapperFactory; import org.apache.maven.logwrapper.MavenSlf4jWrapperFactory;

View File

@ -160,7 +160,7 @@ public class Ansi implements Appendable {
} }
public static boolean isEnabled() { public static boolean isEnabled() {
return org.apache.maven.cli.jline.MessageUtils.isColorEnabled() && org.jline.jansi.Ansi.isEnabled(); return org.apache.maven.jline.MessageUtils.isColorEnabled() && org.jline.jansi.Ansi.isEnabled();
} }
public static Ansi ansi() { public static Ansi ansi() {

View File

@ -35,7 +35,6 @@ import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options; import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException; import org.apache.commons.cli.ParseException;
import org.apache.maven.Maven; import org.apache.maven.Maven;
import org.apache.maven.cli.jline.MessageUtils;
import org.apache.maven.cli.transfer.ConsoleMavenTransferListener; import org.apache.maven.cli.transfer.ConsoleMavenTransferListener;
import org.apache.maven.cli.transfer.QuietMavenTransferListener; import org.apache.maven.cli.transfer.QuietMavenTransferListener;
import org.apache.maven.cli.transfer.SimplexTransferListener; import org.apache.maven.cli.transfer.SimplexTransferListener;
@ -44,6 +43,7 @@ import org.apache.maven.eventspy.internal.EventSpyDispatcher;
import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.ProfileActivation; import org.apache.maven.execution.ProfileActivation;
import org.apache.maven.execution.ProjectActivation; import org.apache.maven.execution.ProjectActivation;
import org.apache.maven.jline.MessageUtils;
import org.apache.maven.model.root.DefaultRootLocator; import org.apache.maven.model.root.DefaultRootLocator;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.building.ToolchainsBuildingRequest; import org.apache.maven.toolchain.building.ToolchainsBuildingRequest;

View File

@ -22,10 +22,10 @@ import java.io.File;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.maven.cli.jline.JLineMessageBuilderFactory;
import org.apache.maven.cli.jline.MessageUtils;
import org.apache.maven.execution.ExecutionEvent; import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.apache.maven.jline.MessageUtils;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;

View File

@ -26,7 +26,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.apache.maven.cli.jline.JLineMessageBuilderFactory; import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.transfer.TransferCancelledException; import org.eclipse.aether.transfer.TransferCancelledException;
import org.eclipse.aether.transfer.TransferEvent; import org.eclipse.aether.transfer.TransferEvent;

61
maven-jline/pom.xml Normal file
View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven</groupId>
<artifactId>maven</artifactId>
<version>4.0.0-alpha-13-SNAPSHOT</version>
</parent>
<artifactId>maven-jline</artifactId>
<name>Maven JLine integration</name>
<description>Provides the JLine integration in Maven</description>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-api-core</artifactId>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interactivity-api</artifactId>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.apache.maven.cli.jline; package org.apache.maven.jline;
import javax.annotation.Priority; import javax.annotation.Priority;
import javax.inject.Named; import javax.inject.Named;
@ -40,8 +40,6 @@ import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle; import org.jline.utils.AttributedStyle;
import org.jline.utils.StyleResolver; import org.jline.utils.StyleResolver;
import static org.jline.utils.AttributedStyle.DEFAULT;
@Experimental @Experimental
@Named @Named
@Singleton @Singleton
@ -223,7 +221,7 @@ public class JLineMessageBuilderFactory implements MessageBuilderFactory, Prompt
@Override @Override
public MessageBuilder resetStyle() { public MessageBuilder resetStyle() {
builder.style(DEFAULT); builder.style(AttributedStyle.DEFAULT);
return this; return this;
} }

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.apache.maven.cli.jline; package org.apache.maven.jline;
import java.io.IOError; import java.io.IOError;
import java.io.IOException; import java.io.IOException;

View File

@ -47,7 +47,7 @@ under the License.
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.maven</groupId> <groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId> <artifactId>maven-jline</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -19,11 +19,12 @@
package org.apache.maven.slf4j; package org.apache.maven.slf4j;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.function.Consumer;
import org.apache.maven.api.services.MessageBuilder; import org.apache.maven.api.services.MessageBuilder;
import org.slf4j.simple.SimpleLogger; import org.slf4j.simple.ExtSimpleLogger;
import static org.apache.maven.cli.jline.MessageUtils.builder; import static org.apache.maven.jline.MessageUtils.builder;
/** /**
* Logger for Maven, that support colorization of levels and stacktraces. This class implements 2 methods introduced in * Logger for Maven, that support colorization of levels and stacktraces. This class implements 2 methods introduced in
@ -31,7 +32,7 @@ import static org.apache.maven.cli.jline.MessageUtils.builder;
* *
* @since 3.5.0 * @since 3.5.0
*/ */
public class MavenSimpleLogger extends SimpleLogger { public class MavenSimpleLogger extends ExtSimpleLogger {
private final String traceRenderedLevel = builder().trace("TRACE").build(); private final String traceRenderedLevel = builder().trace("TRACE").build();
private final String debugRenderedLevel = builder().debug("DEBUG").build(); private final String debugRenderedLevel = builder().debug("DEBUG").build();
@ -39,6 +40,12 @@ public class MavenSimpleLogger extends SimpleLogger {
private final String warnRenderedLevel = builder().warning("WARNING").build(); private final String warnRenderedLevel = builder().warning("WARNING").build();
private final String errorRenderedLevel = builder().error("ERROR").build(); private final String errorRenderedLevel = builder().error("ERROR").build();
static Consumer<String> logSink;
public static void setLogSink(Consumer<String> logSink) {
MavenSimpleLogger.logSink = logSink;
}
MavenSimpleLogger(String name) { MavenSimpleLogger(String name) {
super(name); super(name);
} }
@ -60,6 +67,16 @@ public class MavenSimpleLogger extends SimpleLogger {
} }
} }
@Override
protected void doWrite(StringBuilder buf, Throwable t) {
Consumer<String> sink = logSink;
if (sink != null) {
sink.accept(buf.toString());
} else {
super.doWrite(buf, t);
}
}
@Override @Override
protected void writeThrowable(Throwable t, PrintStream stream) { protected void writeThrowable(Throwable t, PrintStream stream) {
if (t == null) { if (t == null) {

View File

@ -0,0 +1,39 @@
/*
* 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.slf4j.simple;
/**
* Class inheriting SimpleLogger to work around the fact that the {@link #write(StringBuilder, Throwable)}
* method is package private.
*/
public class ExtSimpleLogger extends SimpleLogger {
public ExtSimpleLogger(String name) {
super(name);
}
@Override
void write(StringBuilder buf, Throwable t) {
doWrite(buf, t);
}
protected void doWrite(StringBuilder buf, Throwable t) {
super.write(buf, t);
}
}

View File

@ -24,7 +24,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import org.apache.maven.cli.jline.MessageUtils; import org.apache.maven.jline.MessageUtils;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;

View File

@ -107,6 +107,7 @@ under the License.
<module>maven-model-builder</module> <module>maven-model-builder</module>
<module>maven-di</module> <module>maven-di</module>
<module>maven-xml-impl</module> <module>maven-xml-impl</module>
<module>maven-jline</module>
<module>maven-core</module> <module>maven-core</module>
<module>maven-settings</module> <module>maven-settings</module>
<module>maven-settings-builder</module> <module>maven-settings-builder</module>
@ -203,6 +204,11 @@ under the License.
<artifactId>maven-builder-support</artifactId> <artifactId>maven-builder-support</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-jline</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.maven</groupId> <groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId> <artifactId>maven-core</artifactId>