Skip to content

Commit

Permalink
Extract JLine support into its own module (#1426)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Feb 28, 2024
1 parent ce15193 commit a9934f9
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 19 deletions.
4 changes: 4 additions & 0 deletions maven-embedder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ under the License.
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-jline</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.maven.cli.jline.MessageUtils;
import org.apache.maven.jline.MessageUtils;

/**
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Locale;
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.slf4j.Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
import org.apache.maven.cli.internal.BootstrapCoreExtensionManager;
import org.apache.maven.cli.internal.extension.io.CoreExtensionsStaxReader;
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.Slf4jConfigurationFactory;
import org.apache.maven.cli.logging.Slf4jLoggerManager;
Expand All @@ -81,6 +79,8 @@
import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule;
import org.apache.maven.extension.internal.CoreExports;
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.logwrapper.LogLevelRecorder;
import org.apache.maven.logwrapper.MavenSlf4jWrapperFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public interface Consumer {
}

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
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.QuietMavenTransferListener;
import org.apache.maven.cli.transfer.SimplexTransferListener;
Expand All @@ -44,6 +43,7 @@
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.ProfileActivation;
import org.apache.maven.execution.ProjectActivation;
import org.apache.maven.jline.MessageUtils;
import org.apache.maven.model.root.DefaultRootLocator;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.building.ToolchainsBuildingRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

import com.google.common.collect.ImmutableList;
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.MavenSession;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.apache.maven.jline.MessageUtils;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.concurrent.ExecutorService;
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.transfer.TransferCancelledException;
import org.eclipse.aether.transfer.TransferEvent;
Expand Down
61 changes: 61 additions & 0 deletions maven-jline/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
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.cli.jline;
package org.apache.maven.jline;

import javax.annotation.Priority;
import javax.inject.Named;
Expand All @@ -40,8 +40,6 @@
import org.jline.utils.AttributedStyle;
import org.jline.utils.StyleResolver;

import static org.jline.utils.AttributedStyle.DEFAULT;

@Experimental
@Named
@Singleton
Expand Down Expand Up @@ -223,7 +221,7 @@ public MessageBuilder style(String style) {

@Override
public MessageBuilder resetStyle() {
builder.style(DEFAULT);
builder.style(AttributedStyle.DEFAULT);
return this;
}

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.cli.jline;
package org.apache.maven.jline;

import java.io.IOError;
import java.io.IOException;
Expand Down
2 changes: 1 addition & 1 deletion maven-slf4j-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ under the License.
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<artifactId>maven-jline</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,33 @@
package org.apache.maven.slf4j;

import java.io.PrintStream;
import java.util.function.Consumer;

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
* slf4j-simple provider local copy.
*
* @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 debugRenderedLevel = builder().debug("DEBUG").build();
private final String infoRenderedLevel = builder().info("INFO").build();
private final String warnRenderedLevel = builder().warning("WARNING").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) {
super(name);
}
Expand All @@ -60,6 +67,16 @@ protected String renderLevel(int level) {
}
}

@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
protected void writeThrowable(Throwable t, PrintStream stream) {
if (t == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.List;
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.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ under the License.
<module>maven-model-builder</module>
<module>maven-di</module>
<module>maven-xml-impl</module>
<module>maven-jline</module>
<module>maven-core</module>
<module>maven-settings</module>
<module>maven-settings-builder</module>
Expand Down Expand Up @@ -203,6 +204,11 @@ under the License.
<artifactId>maven-builder-support</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-jline</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
Expand Down

0 comments on commit a9934f9

Please sign in to comment.