Skip to content

Commit

Permalink
feat: extension remains quiet when -q flag is passed
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Aug 3, 2023
1 parent 3964e81 commit 928e20d
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 19 deletions.
7 changes: 0 additions & 7 deletions src/it/gradle-7/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@
<artifactId>gradle-api-maven-plugin</artifactId>
<version>@project.version@</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>gradle-api</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
2 changes: 2 additions & 0 deletions src/it/gradle-7/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def buildLog = new File(basedir, 'build.log').text
assert buildLog.contains('Extracting Gradle 7.')
7 changes: 0 additions & 7 deletions src/it/gradle-8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@
<artifactId>gradle-api-maven-plugin</artifactId>
<version>@project.version@</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>gradle-api</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
2 changes: 2 additions & 0 deletions src/it/gradle-8/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def buildLog = new File(basedir, 'build.log').text
assert buildLog.contains('Extracting Gradle 8.')
2 changes: 2 additions & 0 deletions src/it/no-log/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals.1=verify
invoker.quiet=true
37 changes: 37 additions & 0 deletions src/it/no-log/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.marcnuri.plugins.it</groupId>
<artifactId>no-action</artifactId>
<version>0.1-SNAPSHOT</version>
<name>Maven Integration Test :: Gradle API :: No Action</name>

<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${version.groovy}</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-all</artifactId>
<version>${version.gradle.8}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.marcnuri.plugins</groupId>
<artifactId>gradle-api-maven-plugin</artifactId>
<version>@project.version@</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
2 changes: 2 additions & 0 deletions src/it/no-log/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def buildLog = new File(basedir, 'build.log').text
assert !buildLog.contains('Extracting Gradle')
4 changes: 2 additions & 2 deletions src/main/java/com/marcnuri/plugins/gradle/api/GradleApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public class GradleApi implements Callable<Collection<String>> {
static final String GRADLE_GROUP_ID = "org.gradle";
static final String GRADLE_ALL_ARTIFACT_ID = "gradle-all";
private static final String GRADLE_DISTRIBUTION_BASE_URL = "https://services.gradle.org/distributions/";
private final Log log;
private final GradleApiLog log;
private final boolean forceUpdate;
private final String gradleVersion;
private final Path repositoryBaseDir;
private final Path gradleBinZip;

public GradleApi(Log log, boolean forceUpdate, String gradleVersion, Path repositoryBaseDir) {
public GradleApi(GradleApiLog log, boolean forceUpdate, String gradleVersion, Path repositoryBaseDir) {
this.log = log;
this.gradleVersion = gradleVersion;
this.forceUpdate = forceUpdate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.apache.maven.project.MavenProject;

import javax.inject.Named;
Expand All @@ -24,7 +22,7 @@ public class GradleApiExtension extends AbstractMavenLifecycleParticipant {

@Override
public void afterProjectsRead(MavenSession session) {
final Log log = new SystemStreamLog();
final GradleApiLog log = new GradleApiLog(session.getRequest().getLoggingLevel());
final Map<MavenProject, Dependency> projects = new HashMap<>();
for (MavenProject project : session.getProjects()) {
project.getDependencies().stream()
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/marcnuri/plugins/gradle/api/GradleApiLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.marcnuri.plugins.gradle.api;

import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;

import java.util.function.Consumer;

public final class GradleApiLog {

private final int loggingLevel;
private final Log delegate;

GradleApiLog(int loggingLevel) {
this.loggingLevel = loggingLevel;
delegate = new SystemStreamLog();
}

public void debug(CharSequence content) {
log(MavenExecutionRequest.LOGGING_LEVEL_DEBUG, delegate::debug, content);
}

public void info(CharSequence content) {
log(MavenExecutionRequest.LOGGING_LEVEL_INFO, delegate::info, content);
}

public void warn(CharSequence content) {
log(MavenExecutionRequest.LOGGING_LEVEL_WARN, delegate::warn, content);
}

public void error(CharSequence content) {
log(MavenExecutionRequest.LOGGING_LEVEL_ERROR, delegate::error, content);
}

private void log(int level, Consumer<CharSequence> logFunc, CharSequence content) {
if (loggingLevel <= level) {
logFunc.accept(content);
}
}
}

0 comments on commit 928e20d

Please sign in to comment.