Skip to content

Commit

Permalink
feat: support logging level (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
heowc authored Jun 24, 2024
1 parent 19ee368 commit 253e399
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import javax.annotation.Nullable;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.tasks.JavaExec;

public class HeoPlugin implements Plugin<Project> {
Expand All @@ -37,17 +42,21 @@ public void apply(Project project) {

task.setMain("-jar");
task.args(tempJar.getAbsolutePath());
task.args(List.of(
"-d", determineDirectory(project, config.getDirectoryPath()),
"-p", determinePrefixPackage(project, config.getPrefixPackage()),
"-o", determineDestination(project, config.getDestination()),
"--failure-on-cycles", config.isFailureOnCycles()
));
task.args(arguments(project, config));

tempJar.deleteOnExit();
});
}

private List<? extends Serializable> arguments(Project project, HeoPluginConfig config) {
return Stream.concat(Stream.of("-d", determineDirectory(project, config.getDirectoryPath()),
"-p", determinePrefixPackage(project, config.getPrefixPackage()),
"-o", determineDestination(project, config.getDestination()),
"--failure-on-cycles", String.valueOf(config.isFailureOnCycles())),
logging(project, config.getLogging()))
.toList();
}

private static String determineDirectory(Project project, @Nullable String directoryPath) {
return StringUtils.isBlank(directoryPath)
? project.getProjectDir().getAbsolutePath()
Expand All @@ -66,4 +75,15 @@ private static String determineDestination(Project project, @Nullable String des
: destination;
}

private Stream<String> logging(Project project, @Nullable List<String> logging) {
if (project.getGradle().getStartParameter().getLogLevel() == LogLevel.DEBUG) {
return Stream.of("--logging.level.root=DEBUG");
}
return Stream.ofNullable(logging)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.filter(Objects::nonNull)
.map(it -> "--logging.level." + it);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dev.heowc.heo.gradle;

import java.util.List;

public class HeoPluginConfig {

private String directoryPath;
private String prefixPackage;
private String destination;
private boolean failureOnCycles;
private List<String> logging;

public String getDirectoryPath() {
return directoryPath;
Expand Down Expand Up @@ -38,4 +41,12 @@ public boolean isFailureOnCycles() {
public void setFailureOnCycles(boolean failureOnCycles) {
this.failureOnCycles = failureOnCycles;
}

public List<String> getLogging() {
return logging;
}

public void setLogging(List<String> logging) {
this.logging = logging;
}
}
1 change: 1 addition & 0 deletions it/cycled-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if (project.hasProperty('localPlugin')) {
directoryPath = "${rootDir}/it/cycled-gradle-plugin"
prefixPackage = "dev.heowc.heo.it.cycled"
failureOnCycles = true
logging = [ 'root=DEBUG' ]
}

test {
Expand Down
7 changes: 2 additions & 5 deletions it/gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if (project.hasProperty('localPlugin')) {
heo {
directoryPath = "${rootDir}/heo-core"
prefixPackage = "dev.heowc.heo.core"
logging = [ 'root=DEBUG' ]
}

test {
Expand All @@ -24,14 +25,10 @@ if (project.hasProperty('localPlugin')) {

tasks.register('pluginTest') {
doLast {
var failure = false
try {
tasks.heoReport.exec()
} catch (Exception e) {
failure = true
}
if (failure) {
throw new IllegalStateException("The task did not succeed")
throw new IllegalStateException("The task did not succeed", e)
}
}
}
Expand Down

0 comments on commit 253e399

Please sign in to comment.