From cd3660257594fd1bdc0d7b625cf9b469004c5683 Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Fri, 17 Jun 2022 17:26:51 +0200 Subject: [PATCH 01/10] Add PlantUml Plugin --- examples/plantuml/build.gradle | 4 ++ examples/plantuml/src/plantuml/test.puml | 7 +++ examples/settings.gradle | 1 + plantuml-plugin/build.gradle | 24 ++++++++ .../plugins/plantuml/PlantumlAction.java | 32 +++++++++++ .../plugins/plantuml/PlantumlParameters.java | 20 +++++++ .../plugins/plantuml/PlantumlPlugin.java | 30 ++++++++++ .../gradle/plugins/plantuml/PlantumlTask.java | 56 +++++++++++++++++++ .../plugins/plantuml/PlantumlPluginTest.java | 20 +++++++ settings.gradle | 1 + 10 files changed, 195 insertions(+) create mode 100644 examples/plantuml/build.gradle create mode 100644 examples/plantuml/src/plantuml/test.puml create mode 100644 plantuml-plugin/build.gradle create mode 100644 plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlAction.java create mode 100644 plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlParameters.java create mode 100644 plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlPlugin.java create mode 100644 plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java create mode 100644 plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java diff --git a/examples/plantuml/build.gradle b/examples/plantuml/build.gradle new file mode 100644 index 00000000..ef4ffd12 --- /dev/null +++ b/examples/plantuml/build.gradle @@ -0,0 +1,4 @@ +plugins { + id "io.freefair.plantuml" +} + diff --git a/examples/plantuml/src/plantuml/test.puml b/examples/plantuml/src/plantuml/test.puml new file mode 100644 index 00000000..216fce3b --- /dev/null +++ b/examples/plantuml/src/plantuml/test.puml @@ -0,0 +1,7 @@ +@startuml +Alice -> Bob: Authentication Request +Bob --> Alice: Authentication Response + +Alice -> Bob: Another authentication Request +Alice <-- Bob: another authentication Response +@enduml diff --git a/examples/settings.gradle b/examples/settings.gradle index 7c122ab3..75b0497e 100644 --- a/examples/settings.gradle +++ b/examples/settings.gradle @@ -42,3 +42,4 @@ include ":code-generator:generator" include ":test-maven-plugin" include 'quicktype' +include 'plantuml' diff --git a/plantuml-plugin/build.gradle b/plantuml-plugin/build.gradle new file mode 100644 index 00000000..b64d8f04 --- /dev/null +++ b/plantuml-plugin/build.gradle @@ -0,0 +1,24 @@ +apply plugin: "maven-publish" +apply plugin: "java-gradle-plugin" +apply plugin: "com.gradle.plugin-publish" + +dependencies { + //noinspection GradlePackageUpdate + compileOnly 'net.sourceforge.plantuml:plantuml:1.2022.5' + +} + +gradlePlugin { + plugins { + okhttp { + id = "io.freefair.plantuml" + implementationClass = "io.freefair.gradle.plugins.plantuml.PlantumlPlugin" + displayName = "PlantUML Plugin" + description = "PlantUML Plugin" + } + } +} + +pluginBundle { + tags = ["plantuml"] +} diff --git a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlAction.java b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlAction.java new file mode 100644 index 00000000..d853f683 --- /dev/null +++ b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlAction.java @@ -0,0 +1,32 @@ +package io.freefair.gradle.plugins.plantuml; + +import lombok.SneakyThrows; +import net.sourceforge.plantuml.FileFormat; +import net.sourceforge.plantuml.FileFormatOption; +import net.sourceforge.plantuml.SourceFileReader; +import org.gradle.workers.WorkAction; + +/** + * @author Lars Grefer + */ +public abstract class PlantumlAction implements WorkAction { + + @SneakyThrows + @Override + public void execute() { + + FileFormat fileFormat = getParameters().getFileFormat() + .map(String::toUpperCase) + .map(FileFormat::valueOf) + .getOrElse(FileFormat.PNG); + + FileFormatOption fileFormatOption = new FileFormatOption(fileFormat, getParameters().getWithMetadata().get()); + SourceFileReader sourceFileReader = new SourceFileReader( + getParameters().getInputFile().getAsFile().get(), + getParameters().getOutputDirectory().getAsFile().get(), + fileFormatOption + ); + + sourceFileReader.getGeneratedImages(); + } +} diff --git a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlParameters.java b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlParameters.java new file mode 100644 index 00000000..126f341e --- /dev/null +++ b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlParameters.java @@ -0,0 +1,20 @@ +package io.freefair.gradle.plugins.plantuml; + +import org.gradle.api.file.DirectoryProperty; +import org.gradle.api.file.RegularFileProperty; +import org.gradle.api.provider.Property; +import org.gradle.workers.WorkParameters; + +/** + * @author Lars Grefer + */ +public interface PlantumlParameters extends WorkParameters { + + RegularFileProperty getInputFile(); + + DirectoryProperty getOutputDirectory(); + + Property getFileFormat(); + + Property getWithMetadata(); +} diff --git a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlPlugin.java b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlPlugin.java new file mode 100644 index 00000000..759a295e --- /dev/null +++ b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlPlugin.java @@ -0,0 +1,30 @@ +package io.freefair.gradle.plugins.plantuml; + + +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.artifacts.Configuration; + +/** + * @author Lars Grefer + */ +public class PlantumlPlugin implements Plugin { + + @Override + public void apply(Project project) { + Configuration plantuml = project.getConfigurations().create("plantuml"); + + plantuml.defaultDependencies(s -> { + s.add(project.getDependencies().create("net.sourceforge.plantuml:plantuml:1.2022.5")); + }); + + project.getTasks().withType(PlantumlTask.class).configureEach(plantumlTask -> { + plantumlTask.getPlantumlClasspath().from(plantuml); + plantumlTask.getOutputDirectory().convention(project.getLayout().getBuildDirectory().dir("plantuml")); + }); + + project.getTasks().register("plantUml", PlantumlTask.class, plantumlTask -> { + plantumlTask.source("src/plantuml"); + }); + } +} diff --git a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java new file mode 100644 index 00000000..d72e9296 --- /dev/null +++ b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java @@ -0,0 +1,56 @@ +package io.freefair.gradle.plugins.plantuml; + +import lombok.Getter; +import org.gradle.api.file.ConfigurableFileCollection; +import org.gradle.api.file.DirectoryProperty; +import org.gradle.api.provider.Property; +import org.gradle.api.tasks.*; +import org.gradle.workers.WorkerExecutor; + +import javax.inject.Inject; +import java.io.File; + +/** + * @author Lars Grefer + */ +public class PlantumlTask extends SourceTask { + + private final WorkerExecutor workerExecutor; + + @Getter + @Classpath + private final ConfigurableFileCollection plantumlClasspath = getProject().files(); + + @Getter + @OutputDirectory + private final DirectoryProperty outputDirectory = getProject().getObjects().directoryProperty(); + + @Getter + @Input + private final Property fileFormat = getProject().getObjects().property(String.class).convention("SVG"); + + @Getter + @Input + private final Property withMetadata = getProject().getObjects().property(Boolean.class).convention(true); + + @Inject + public PlantumlTask(WorkerExecutor workerExecutor) { + this.workerExecutor = workerExecutor; + } + + @TaskAction + public void execute() { + + getProject().delete(outputDirectory); + + for (File file : getSource()) { + workerExecutor.classLoaderIsolation(iso -> iso.getClasspath().from(plantumlClasspath)) + .submit(PlantumlAction.class, params -> { + params.getInputFile().set(file); + params.getOutputDirectory().set(outputDirectory); + params.getFileFormat().set(fileFormat); + params.getWithMetadata().set(withMetadata); + }); + } + } +} diff --git a/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java b/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java new file mode 100644 index 00000000..2def7940 --- /dev/null +++ b/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java @@ -0,0 +1,20 @@ +package io.freefair.gradle.plugins.plantuml; + +import org.gradle.api.Project; +import org.gradle.testfixtures.ProjectBuilder; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class PlantumlPluginTest { + + @Test + void apply() { + Project project = ProjectBuilder.builder().build(); + + project.getPlugins().apply(PlantumlPlugin.class); + + assertThat(project.getTasks().getNames()).contains("plantUml"); + } +} diff --git a/settings.gradle b/settings.gradle index cd15cef2..358576a6 100644 --- a/settings.gradle +++ b/settings.gradle @@ -20,3 +20,4 @@ include "okhttp-plugin" include "git-plugin" include "mkdocs-plugin" include "quicktype-plugin" +include "plantuml-plugin" From 7bc8ed1d72c98da233938426d109c0474a2c4b11 Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Fri, 17 Jun 2022 19:10:54 +0200 Subject: [PATCH 02/10] Set Description --- plantuml-plugin/build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plantuml-plugin/build.gradle b/plantuml-plugin/build.gradle index b64d8f04..cfeadd31 100644 --- a/plantuml-plugin/build.gradle +++ b/plantuml-plugin/build.gradle @@ -2,6 +2,8 @@ apply plugin: "maven-publish" apply plugin: "java-gradle-plugin" apply plugin: "com.gradle.plugin-publish" +description = "Gradle Plugin for PlantUML" + dependencies { //noinspection GradlePackageUpdate compileOnly 'net.sourceforge.plantuml:plantuml:1.2022.5' From 4edb338c0528f47df12551e374d08460931f7e03 Mon Sep 17 00:00:00 2001 From: Dennis Fricke Date: Fri, 17 Jun 2022 19:11:46 +0200 Subject: [PATCH 03/10] some improvements --- .gitignore | 3 +++ examples/plantuml/.gitignore | 2 ++ examples/plantuml/build.gradle | 13 +++++++++++++ examples/plantuml/src/plantuml/include/test-i.iuml | 3 +++ examples/plantuml/src/plantuml/test.puml | 2 ++ examples/plantuml/src/plantuml2/test2.tuml | 7 +++++++ plantuml-plugin/README.md | 0 .../gradle/plugins/plantuml/PlantumlTask.java | 7 ++++++- 8 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 examples/plantuml/.gitignore create mode 100644 examples/plantuml/src/plantuml/include/test-i.iuml create mode 100644 examples/plantuml/src/plantuml2/test2.tuml create mode 100644 plantuml-plugin/README.md diff --git a/.gitignore b/.gitignore index 47e1517b..1187f335 100644 --- a/.gitignore +++ b/.gitignore @@ -209,3 +209,6 @@ gradle-app.setting **/build/ # End of https://www.gitignore.io/api/IntelliJ,macOS,osx,linux,windows,Gradle,java,gradle,lombok + +lombok.config +**/gen/ \ No newline at end of file diff --git a/examples/plantuml/.gitignore b/examples/plantuml/.gitignore new file mode 100644 index 00000000..930e968d --- /dev/null +++ b/examples/plantuml/.gitignore @@ -0,0 +1,2 @@ +dist +dist2 \ No newline at end of file diff --git a/examples/plantuml/build.gradle b/examples/plantuml/build.gradle index ef4ffd12..3bc92547 100644 --- a/examples/plantuml/build.gradle +++ b/examples/plantuml/build.gradle @@ -1,4 +1,17 @@ +import io.freefair.gradle.plugins.plantuml.PlantumlTask + plugins { id "io.freefair.plantuml" } +plantUml { + fileFormat = "PNG" + outputDirectory = file("dist") +} + +tasks.register("plantUml2", PlantumlTask) { + it.source("src/plantuml2") + it.includePattern = "**/*.tuml" + it.fileFormat = "SVG" + it.outputDirectory = file("dist2") +} \ No newline at end of file diff --git a/examples/plantuml/src/plantuml/include/test-i.iuml b/examples/plantuml/src/plantuml/include/test-i.iuml new file mode 100644 index 00000000..5972f809 --- /dev/null +++ b/examples/plantuml/src/plantuml/include/test-i.iuml @@ -0,0 +1,3 @@ +@startuml + +@enduml \ No newline at end of file diff --git a/examples/plantuml/src/plantuml/test.puml b/examples/plantuml/src/plantuml/test.puml index 216fce3b..df3a9839 100644 --- a/examples/plantuml/src/plantuml/test.puml +++ b/examples/plantuml/src/plantuml/test.puml @@ -1,4 +1,6 @@ @startuml +!include include/test-i.iuml + Alice -> Bob: Authentication Request Bob --> Alice: Authentication Response diff --git a/examples/plantuml/src/plantuml2/test2.tuml b/examples/plantuml/src/plantuml2/test2.tuml new file mode 100644 index 00000000..d0f7d798 --- /dev/null +++ b/examples/plantuml/src/plantuml2/test2.tuml @@ -0,0 +1,7 @@ +@startuml +Alice -> Bob: Authentication Request +Bob --> Alice: Authentication Response + +Alice -> Bob: Another authentication Request +Alice <-- Bob: another authentication Response +@enduml \ No newline at end of file diff --git a/plantuml-plugin/README.md b/plantuml-plugin/README.md new file mode 100644 index 00000000..e69de29b diff --git a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java index d72e9296..106f8f01 100644 --- a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java +++ b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java @@ -33,8 +33,13 @@ public class PlantumlTask extends SourceTask { @Input private final Property withMetadata = getProject().getObjects().property(Boolean.class).convention(true); + @Getter + @Input + private final Property includePattern = getProject().getObjects().property(String.class).convention("**/*.puml"); + @Inject public PlantumlTask(WorkerExecutor workerExecutor) { + this.setGroup("plantuml"); this.workerExecutor = workerExecutor; } @@ -43,7 +48,7 @@ public void execute() { getProject().delete(outputDirectory); - for (File file : getSource()) { + for (File file : getSource().matching(p -> p.include(includePattern.get()))) { workerExecutor.classLoaderIsolation(iso -> iso.getClasspath().from(plantumlClasspath)) .submit(PlantumlAction.class, params -> { params.getInputFile().set(file); From 6d9766768aaf7516811dc7fed02a09a8717b6f55 Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Fri, 17 Jun 2022 19:15:50 +0200 Subject: [PATCH 04/10] remove default format --- .../java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java index 106f8f01..bff91de9 100644 --- a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java +++ b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java @@ -27,7 +27,8 @@ public class PlantumlTask extends SourceTask { @Getter @Input - private final Property fileFormat = getProject().getObjects().property(String.class).convention("SVG"); + @Optional + private final Property fileFormat = getProject().getObjects().property(String.class); @Getter @Input From 4953f73d3eabe3b037f1dbeb146fa9969274894a Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Fri, 17 Jun 2022 19:21:19 +0200 Subject: [PATCH 05/10] Run plantuml in awt.headless mode --- .../io/freefair/gradle/plugins/plantuml/PlantumlTask.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java index bff91de9..59727a13 100644 --- a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java +++ b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java @@ -50,7 +50,11 @@ public void execute() { getProject().delete(outputDirectory); for (File file : getSource().matching(p -> p.include(includePattern.get()))) { - workerExecutor.classLoaderIsolation(iso -> iso.getClasspath().from(plantumlClasspath)) + workerExecutor + .processIsolation(iso -> { + iso.getClasspath().from(plantumlClasspath); + iso.getForkOptions().systemProperty("java.awt.headless", true); + }) .submit(PlantumlAction.class, params -> { params.getInputFile().set(file); params.getOutputDirectory().set(outputDirectory); From 063e9c99ac2757a74131203cb70bb35df0cd7baa Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Fri, 17 Jun 2022 22:53:26 +0200 Subject: [PATCH 06/10] cleanup plantuml example --- examples/plantuml/.gitignore | 2 -- examples/plantuml/build.gradle | 12 ++++++------ 2 files changed, 6 insertions(+), 8 deletions(-) delete mode 100644 examples/plantuml/.gitignore diff --git a/examples/plantuml/.gitignore b/examples/plantuml/.gitignore deleted file mode 100644 index 930e968d..00000000 --- a/examples/plantuml/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -dist -dist2 \ No newline at end of file diff --git a/examples/plantuml/build.gradle b/examples/plantuml/build.gradle index 3bc92547..8be004d4 100644 --- a/examples/plantuml/build.gradle +++ b/examples/plantuml/build.gradle @@ -6,12 +6,12 @@ plugins { plantUml { fileFormat = "PNG" - outputDirectory = file("dist") + outputDirectory = layout.buildDirectory.dir("dist") } tasks.register("plantUml2", PlantumlTask) { - it.source("src/plantuml2") - it.includePattern = "**/*.tuml" - it.fileFormat = "SVG" - it.outputDirectory = file("dist2") -} \ No newline at end of file + source("src/plantuml2") + includePattern = "**/*.tuml" + fileFormat = "SVG" + outputDirectory = layout.buildDirectory.dir("dist2") +} From 4cf574886347e60b5dc09f31494e298c5fca91f5 Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Fri, 17 Jun 2022 22:54:51 +0200 Subject: [PATCH 07/10] cleanup --- plantuml-plugin/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plantuml-plugin/build.gradle b/plantuml-plugin/build.gradle index cfeadd31..c799a912 100644 --- a/plantuml-plugin/build.gradle +++ b/plantuml-plugin/build.gradle @@ -12,7 +12,7 @@ dependencies { gradlePlugin { plugins { - okhttp { + plantuml { id = "io.freefair.plantuml" implementationClass = "io.freefair.gradle.plugins.plantuml.PlantumlPlugin" displayName = "PlantUML Plugin" From e91c91f6d335996e79061c3c8ad30e0a927c3e88 Mon Sep 17 00:00:00 2001 From: Dennis Fricke Date: Fri, 17 Jun 2022 23:03:20 +0200 Subject: [PATCH 08/10] added some new unit tests --- build.gradle | 1 + plantuml-plugin/build.gradle | 1 + .../plugins/plantuml/PlantumlPluginTest.java | 62 ++++++++++++++++++- .../src/test/resources/puml-files/test.puml | 6 ++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 plantuml-plugin/src/test/resources/puml-files/test.puml diff --git a/build.gradle b/build.gradle index 05d4d289..e7b66bad 100644 --- a/build.gradle +++ b/build.gradle @@ -60,6 +60,7 @@ allprojects { testImplementation 'org.assertj:assertj-core:3.23.1' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2' + testImplementation 'org.mockito:mockito-core:4.6.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' } } diff --git a/plantuml-plugin/build.gradle b/plantuml-plugin/build.gradle index c799a912..8f6a126f 100644 --- a/plantuml-plugin/build.gradle +++ b/plantuml-plugin/build.gradle @@ -8,6 +8,7 @@ dependencies { //noinspection GradlePackageUpdate compileOnly 'net.sourceforge.plantuml:plantuml:1.2022.5' + testImplementation 'net.sourceforge.plantuml:plantuml:1.2022.5' } gradlePlugin { diff --git a/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java b/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java index 2def7940..9f5bcc68 100644 --- a/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java +++ b/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java @@ -1,11 +1,16 @@ package io.freefair.gradle.plugins.plantuml; import org.gradle.api.Project; +import org.gradle.api.file.DirectoryProperty; +import org.gradle.api.file.RegularFileProperty; +import org.gradle.api.provider.Property; import org.gradle.testfixtures.ProjectBuilder; import org.junit.jupiter.api.Test; +import java.io.File; + import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; class PlantumlPluginTest { @@ -17,4 +22,59 @@ void apply() { assertThat(project.getTasks().getNames()).contains("plantUml"); } + + @Test + void execute() { + Project project = ProjectBuilder.builder().build(); + + project.getPlugins().apply(PlantumlPlugin.class); + + PlantumlTask task = project.getTasks().withType(PlantumlTask.class).getByName("plantUml"); + + task.source(getClass().getClassLoader().getResource("puml-files").getPath()); + + assertThrows(UnsupportedOperationException.class, task::execute); + } + + @Test + void taskExecution() { + Project project = ProjectBuilder.builder().build(); + + PlantumlAction action = new PlantumlAction() { + @Override + public PlantumlParameters getParameters() { + return new PlantumlParameters() { + @Override + public RegularFileProperty getInputFile() { + RegularFileProperty result = project.getObjects().fileProperty(); + result.set(new File(getClass().getClassLoader().getResource("puml-files/test.puml").getPath())); + return result; + } + + @Override + public DirectoryProperty getOutputDirectory() { + DirectoryProperty result = project.getObjects().directoryProperty(); + result.set(new File(getClass().getClassLoader().getResource("puml-files").getPath())); + return result; + } + + @Override + public Property getFileFormat() { + Property property = project.getObjects().property(String.class); + property.set("PNG"); + return property; + } + + @Override + public Property getWithMetadata() { + Property property = project.getObjects().property(Boolean.class); + property.set(false); + return property; + } + }; + } + }; + + action.execute(); + } } diff --git a/plantuml-plugin/src/test/resources/puml-files/test.puml b/plantuml-plugin/src/test/resources/puml-files/test.puml new file mode 100644 index 00000000..f9740ee8 --- /dev/null +++ b/plantuml-plugin/src/test/resources/puml-files/test.puml @@ -0,0 +1,6 @@ +@startuml + +Bob -> Alice: Test +Alice --> Bob: return Test + +@enduml \ No newline at end of file From 6f180df58bd9cc60f107ee225bd06c4da5735dee Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Fri, 17 Jun 2022 23:03:49 +0200 Subject: [PATCH 09/10] cleanup --- .../plugins/plantuml/PlantumlAction.java | 4 +++ .../plugins/plantuml/PlantumlParameters.java | 3 ++ .../gradle/plugins/plantuml/PlantumlTask.java | 33 +++++++++++-------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlAction.java b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlAction.java index d853f683..7368c02a 100644 --- a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlAction.java +++ b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlAction.java @@ -7,7 +7,10 @@ import org.gradle.workers.WorkAction; /** + * Gradle {@link WorkAction} for PlantUML {@link SourceFileReader}. + * * @author Lars Grefer + * @see SourceFileReader */ public abstract class PlantumlAction implements WorkAction { @@ -21,6 +24,7 @@ public void execute() { .getOrElse(FileFormat.PNG); FileFormatOption fileFormatOption = new FileFormatOption(fileFormat, getParameters().getWithMetadata().get()); + SourceFileReader sourceFileReader = new SourceFileReader( getParameters().getInputFile().getAsFile().get(), getParameters().getOutputDirectory().getAsFile().get(), diff --git a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlParameters.java b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlParameters.java index 126f341e..7247ee73 100644 --- a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlParameters.java +++ b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlParameters.java @@ -6,7 +6,10 @@ import org.gradle.workers.WorkParameters; /** + * {@link WorkParameters} for {@link PlantumlAction}. + * * @author Lars Grefer + * @see PlantumlPlugin */ public interface PlantumlParameters extends WorkParameters { diff --git a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java index 59727a13..b3841cf5 100644 --- a/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java +++ b/plantuml-plugin/src/main/java/io/freefair/gradle/plugins/plantuml/PlantumlTask.java @@ -3,8 +3,10 @@ import lombok.Getter; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.DirectoryProperty; +import org.gradle.api.file.FileSystemOperations; import org.gradle.api.provider.Property; import org.gradle.api.tasks.*; +import org.gradle.workers.WorkQueue; import org.gradle.workers.WorkerExecutor; import javax.inject.Inject; @@ -17,6 +19,8 @@ public class PlantumlTask extends SourceTask { private final WorkerExecutor workerExecutor; + private final FileSystemOperations fileSystemOperations; + @Getter @Classpath private final ConfigurableFileCollection plantumlClasspath = getProject().files(); @@ -39,28 +43,29 @@ public class PlantumlTask extends SourceTask { private final Property includePattern = getProject().getObjects().property(String.class).convention("**/*.puml"); @Inject - public PlantumlTask(WorkerExecutor workerExecutor) { - this.setGroup("plantuml"); + public PlantumlTask(WorkerExecutor workerExecutor, FileSystemOperations fileSystemOperations) { + this.fileSystemOperations = fileSystemOperations; this.workerExecutor = workerExecutor; + this.setGroup("plantuml"); } @TaskAction public void execute() { - getProject().delete(outputDirectory); + fileSystemOperations.delete(deleteSpec -> deleteSpec.delete(outputDirectory)); + + WorkQueue workQueue = workerExecutor.processIsolation(process -> { + process.getClasspath().from(plantumlClasspath); + process.getForkOptions().systemProperty("java.awt.headless", true); + }); for (File file : getSource().matching(p -> p.include(includePattern.get()))) { - workerExecutor - .processIsolation(iso -> { - iso.getClasspath().from(plantumlClasspath); - iso.getForkOptions().systemProperty("java.awt.headless", true); - }) - .submit(PlantumlAction.class, params -> { - params.getInputFile().set(file); - params.getOutputDirectory().set(outputDirectory); - params.getFileFormat().set(fileFormat); - params.getWithMetadata().set(withMetadata); - }); + workQueue.submit(PlantumlAction.class, params -> { + params.getInputFile().set(file); + params.getOutputDirectory().set(outputDirectory); + params.getFileFormat().set(fileFormat); + params.getWithMetadata().set(withMetadata); + }); } } } From 4a5ac9d7e405bb278d6d816e884269d1ebf1f2e1 Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Fri, 17 Jun 2022 23:10:11 +0200 Subject: [PATCH 10/10] cleanup test --- plantuml-plugin/build.gradle | 2 +- .../plugins/plantuml/PlantumlPluginTest.java | 53 +++++++------------ 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/plantuml-plugin/build.gradle b/plantuml-plugin/build.gradle index 8f6a126f..52a16c6b 100644 --- a/plantuml-plugin/build.gradle +++ b/plantuml-plugin/build.gradle @@ -8,7 +8,7 @@ dependencies { //noinspection GradlePackageUpdate compileOnly 'net.sourceforge.plantuml:plantuml:1.2022.5' - testImplementation 'net.sourceforge.plantuml:plantuml:1.2022.5' + testRuntimeOnly 'net.sourceforge.plantuml:plantuml:1.2022.5' } gradlePlugin { diff --git a/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java b/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java index 9f5bcc68..3d2e2a1b 100644 --- a/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java +++ b/plantuml-plugin/src/test/java/io/freefair/gradle/plugins/plantuml/PlantumlPluginTest.java @@ -4,8 +4,11 @@ import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.provider.Property; +import org.gradle.internal.impldep.org.junit.Before; import org.gradle.testfixtures.ProjectBuilder; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import java.io.File; @@ -14,10 +17,15 @@ class PlantumlPluginTest { + private Project project; + + @BeforeEach + void init() { + project = ProjectBuilder.builder().build(); + } + @Test void apply() { - Project project = ProjectBuilder.builder().build(); - project.getPlugins().apply(PlantumlPlugin.class); assertThat(project.getTasks().getNames()).contains("plantUml"); @@ -25,8 +33,6 @@ void apply() { @Test void execute() { - Project project = ProjectBuilder.builder().build(); - project.getPlugins().apply(PlantumlPlugin.class); PlantumlTask task = project.getTasks().withType(PlantumlTask.class).getByName("plantUml"); @@ -37,41 +43,18 @@ void execute() { } @Test - void taskExecution() { - Project project = ProjectBuilder.builder().build(); + void taskExecution(@TempDir File tempDir) { + PlantumlParameters parameters = project.getObjects().newInstance(PlantumlParameters.class); + + parameters.getInputFile().set(new File(getClass().getClassLoader().getResource("puml-files/test.puml").getPath())); + parameters.getOutputDirectory().set(tempDir); + parameters.getFileFormat().set("PNG"); + parameters.getWithMetadata().set(false); PlantumlAction action = new PlantumlAction() { @Override public PlantumlParameters getParameters() { - return new PlantumlParameters() { - @Override - public RegularFileProperty getInputFile() { - RegularFileProperty result = project.getObjects().fileProperty(); - result.set(new File(getClass().getClassLoader().getResource("puml-files/test.puml").getPath())); - return result; - } - - @Override - public DirectoryProperty getOutputDirectory() { - DirectoryProperty result = project.getObjects().directoryProperty(); - result.set(new File(getClass().getClassLoader().getResource("puml-files").getPath())); - return result; - } - - @Override - public Property getFileFormat() { - Property property = project.getObjects().property(String.class); - property.set("PNG"); - return property; - } - - @Override - public Property getWithMetadata() { - Property property = project.getObjects().property(Boolean.class); - property.set(false); - return property; - } - }; + return parameters; } };