From 253e3992cb3f9c7c018a59d67f8e1ade15461adf Mon Sep 17 00:00:00 2001 From: WonChul Heo Date: Mon, 24 Jun 2024 23:47:20 +0900 Subject: [PATCH] feat: support logging level (#13) --- .../java/dev/heowc/heo/gradle/HeoPlugin.java | 32 +++++++++++++++---- .../dev/heowc/heo/gradle/HeoPluginConfig.java | 11 +++++++ it/cycled-gradle-plugin/build.gradle | 1 + it/gradle-plugin/build.gradle | 7 ++-- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/heo-gradle-plugin/src/main/java/dev/heowc/heo/gradle/HeoPlugin.java b/heo-gradle-plugin/src/main/java/dev/heowc/heo/gradle/HeoPlugin.java index 11670f9..d138f4d 100644 --- a/heo-gradle-plugin/src/main/java/dev/heowc/heo/gradle/HeoPlugin.java +++ b/heo-gradle-plugin/src/main/java/dev/heowc/heo/gradle/HeoPlugin.java @@ -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 { @@ -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 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() @@ -66,4 +75,15 @@ private static String determineDestination(Project project, @Nullable String des : destination; } + private Stream logging(Project project, @Nullable List 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); + } + } diff --git a/heo-gradle-plugin/src/main/java/dev/heowc/heo/gradle/HeoPluginConfig.java b/heo-gradle-plugin/src/main/java/dev/heowc/heo/gradle/HeoPluginConfig.java index b53895b..851b2ad 100644 --- a/heo-gradle-plugin/src/main/java/dev/heowc/heo/gradle/HeoPluginConfig.java +++ b/heo-gradle-plugin/src/main/java/dev/heowc/heo/gradle/HeoPluginConfig.java @@ -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 logging; public String getDirectoryPath() { return directoryPath; @@ -38,4 +41,12 @@ public boolean isFailureOnCycles() { public void setFailureOnCycles(boolean failureOnCycles) { this.failureOnCycles = failureOnCycles; } + + public List getLogging() { + return logging; + } + + public void setLogging(List logging) { + this.logging = logging; + } } diff --git a/it/cycled-gradle-plugin/build.gradle b/it/cycled-gradle-plugin/build.gradle index 32098fd..38f9d77 100644 --- a/it/cycled-gradle-plugin/build.gradle +++ b/it/cycled-gradle-plugin/build.gradle @@ -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 { diff --git a/it/gradle-plugin/build.gradle b/it/gradle-plugin/build.gradle index 284251c..fea3fa9 100644 --- a/it/gradle-plugin/build.gradle +++ b/it/gradle-plugin/build.gradle @@ -16,6 +16,7 @@ if (project.hasProperty('localPlugin')) { heo { directoryPath = "${rootDir}/heo-core" prefixPackage = "dev.heowc.heo.core" + logging = [ 'root=DEBUG' ] } test { @@ -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) } } }