[MNG-6220] Add CLI options to control color output

Introduce -Dstyle.color=[always|never|auto]
This commit is contained in:
rfscholte 2017-08-15 21:48:57 +02:00
parent f1ed6592b1
commit 785bad693c
3 changed files with 107 additions and 12 deletions

View File

@ -138,6 +138,11 @@ under the License.
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -144,6 +144,8 @@ public class MavenCli
private static final String MVN_MAVEN_CONFIG = ".mvn/maven.config";
public static final String STYLE_COLOR_PROPERTY = "style.color";
private ClassWorld classWorld;
private LoggerManager plexusLoggerManager;
@ -472,8 +474,9 @@ public class MavenCli
/**
* configure logging
*/
private void logging( CliRequest cliRequest )
void logging( CliRequest cliRequest )
{
// LOG LEVEL
cliRequest.debug = cliRequest.commandLine.hasOption( CLIManager.DEBUG );
cliRequest.quiet = !cliRequest.debug && cliRequest.commandLine.hasOption( CLIManager.QUIET );
cliRequest.showErrors = cliRequest.debug || cliRequest.commandLine.hasOption( CLIManager.ERRORS );
@ -494,18 +497,33 @@ public class MavenCli
// else fall back to default log level specified in conf
// see https://issues.apache.org/jira/browse/MNG-2570
if ( cliRequest.commandLine.hasOption( CLIManager.BATCH_MODE ) )
// LOG COLOR
String styleColor = cliRequest.getUserProperties().getProperty( STYLE_COLOR_PROPERTY, "auto" );
if ( "always".equals( styleColor ) )
{
MessageUtils.setColorEnabled( true );
}
else if ( "never".equals( styleColor ) )
{
MessageUtils.setColorEnabled( false );
}
else if ( !"auto".equals( styleColor ) )
{
throw new IllegalArgumentException( "Invalid color configuration option [" + styleColor
+ "]. Supported values are (auto|always|never)." );
}
else if ( cliRequest.commandLine.hasOption( CLIManager.BATCH_MODE )
|| cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) )
{
MessageUtils.setColorEnabled( false );
}
// LOG STREAMS
if ( cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) )
{
File logFile = new File( cliRequest.commandLine.getOptionValue( CLIManager.LOG_FILE ) );
logFile = resolveFile( logFile, cliRequest.workingDirectory );
MessageUtils.setColorEnabled( false );
// redirect stdout and stderr to file
try
{

View File

@ -19,26 +19,35 @@ package org.apache.maven.cli;
* under the License.
*/
import junit.framework.TestCase;
import org.apache.commons.cli.ParseException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import java.io.File;
import org.apache.commons.cli.ParseException;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MavenCliTest
extends TestCase
{
private MavenCli cli;
private String origBasedir;
protected void setUp()
@Before
public void setUp()
{
cli = new MavenCli();
origBasedir = System.getProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY );
}
@Override
protected void tearDown()
@After
public void tearDown()
throws Exception
{
if ( origBasedir != null )
@ -49,9 +58,9 @@ public class MavenCliTest
{
System.getProperties().remove( MavenCli.MULTIMODULE_PROJECT_DIRECTORY );
}
super.tearDown();
}
@Test
public void testCalculateDegreeOfConcurrencyWithCoreMultiplier()
{
int cores = Runtime.getRuntime().availableProcessors();
@ -71,6 +80,7 @@ public class MavenCliTest
}
}
@Test
public void testMavenConfig()
throws Exception
{
@ -90,6 +100,7 @@ public class MavenCliTest
assertEquals( "foobar", request.commandLine.getOptionValue( "builder" ) );
}
@Test
public void testMavenConfigInvalid()
throws Exception
{
@ -120,6 +131,7 @@ public class MavenCliTest
*
* @throws Exception in case of failure.
*/
@Test
public void testMVNConfigurationThreadCanBeOverwrittenViaCommandLine()
throws Exception
{
@ -145,6 +157,7 @@ public class MavenCliTest
*
* @throws Exception
*/
@Test
public void testMVNConfigurationDefinedPropertiesCanBeOverwrittenViaCommandLine()
throws Exception
{
@ -172,6 +185,7 @@ public class MavenCliTest
*
* @throws Exception
*/
@Test
public void testMVNConfigurationCLIRepeatedPropertiesLastWins()
throws Exception
{
@ -199,6 +213,7 @@ public class MavenCliTest
*
* @throws Exception
*/
@Test
public void testMVNConfigurationFunkyArguments()
throws Exception
{
@ -221,4 +236,61 @@ public class MavenCliTest
assertEquals( "-Dpom.xml", request.getCommandLine().getOptionValue( CLIManager.ALTERNATE_POM_FILE ) );
}
@Test
public void testStyleColors()
throws Exception
{
assumeTrue( "ANSI not supported", MessageUtils.isColorEnabled() );
CliRequest request;
MessageUtils.setColorEnabled( true );
request = new CliRequest( new String[] { "-B" }, null );
cli.cli( request );
cli.properties( request );
cli.logging( request );
assertFalse( MessageUtils.isColorEnabled() );
MessageUtils.setColorEnabled( true );
request = new CliRequest( new String[] { "-l", "target/temp/mvn.log" }, null );
cli.cli( request );
cli.properties( request );
cli.logging( request );
assertFalse( MessageUtils.isColorEnabled() );
MessageUtils.setColorEnabled( false );
request = new CliRequest( new String[] { "-Dstyle.color=always" }, null );
cli.cli( request );
cli.properties( request );
cli.logging( request );
assertTrue( MessageUtils.isColorEnabled() );
MessageUtils.setColorEnabled( true );
request = new CliRequest( new String[] { "-Dstyle.color=never" }, null );
cli.cli( request );
cli.properties( request );
cli.logging( request );
assertFalse( MessageUtils.isColorEnabled() );
MessageUtils.setColorEnabled( false );
request = new CliRequest( new String[] { "-Dstyle.color=always", "-B", "-l", "target/temp/mvn.log" }, null );
cli.cli( request );
cli.properties( request );
cli.logging( request );
assertTrue( MessageUtils.isColorEnabled() );
try
{
MessageUtils.setColorEnabled( false );
request = new CliRequest( new String[] { "-Dstyle.color=maybe", "-B", "-l", "target/temp/mvn.log" }, null );
cli.cli( request );
cli.properties( request );
cli.logging( request );
fail( "maybe is not a valid option" );
}
catch ( IllegalArgumentException e )
{
// noop
}
}
}