From 12357c9bbdfc461663269ee43cebb12d83eb76da Mon Sep 17 00:00:00 2001 From: Kevin Brightwell Date: Sat, 22 Apr 2023 11:40:01 -0400 Subject: [PATCH] [Simplify] Remove unused methods, add `ListProperty<>` values for `Templates` This is extracted from #31 This removes unused methods that were knock ons from #30. This fixes issues with serialization of the `Templates` value when computing if the tasks should run. --- .../AssertJGeneratorGradlePlugin.groovy | 27 +-- .../DefaultAssertJGeneratorSourceSet.groovy | 15 +- .../DefaultAssertJGeneratorOptions.groovy | 9 +- .../gradle/tasks/AssertJGenerationTask.groovy | 15 +- .../gradle/tasks/config/Templates.groovy | 161 +++++++----------- 5 files changed, 107 insertions(+), 120 deletions(-) diff --git a/src/main/groovy/org/assertj/generator/gradle/AssertJGeneratorGradlePlugin.groovy b/src/main/groovy/org/assertj/generator/gradle/AssertJGeneratorGradlePlugin.groovy index 2614d06..11c08db 100644 --- a/src/main/groovy/org/assertj/generator/gradle/AssertJGeneratorGradlePlugin.groovy +++ b/src/main/groovy/org/assertj/generator/gradle/AssertJGeneratorGradlePlugin.groovy @@ -13,15 +13,14 @@ package org.assertj.generator.gradle import org.assertj.generator.gradle.internal.tasks.DefaultAssertJGeneratorSourceSet - import org.assertj.generator.gradle.tasks.AssertJGenerationTask import org.assertj.generator.gradle.tasks.AssertJGeneratorSourceSet import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.Configuration -import org.gradle.api.internal.file.SourceDirectorySetFactory import org.gradle.api.internal.plugins.DslObject import org.gradle.api.logging.Logging +import org.gradle.api.model.ObjectFactory import org.gradle.api.plugins.Convention import org.gradle.api.plugins.JavaPlugin import org.gradle.api.plugins.JavaPluginConvention @@ -37,13 +36,13 @@ class AssertJGeneratorGradlePlugin implements Plugin { static final ASSERTJ_GEN_CONFIGURATION_NAME = "assertJ" - private final SourceDirectorySetFactory sourceDirectorySetFactory + private final ObjectFactory objects private static final logger = Logging.getLogger(AssertJGeneratorGradlePlugin) @Inject - AssertJGeneratorGradlePlugin(SourceDirectorySetFactory sourceDirectorySetFactory) { - this.sourceDirectorySetFactory = sourceDirectorySetFactory + AssertJGeneratorGradlePlugin(ObjectFactory objects) { + this.objects = objects } @Override @@ -79,8 +78,11 @@ class AssertJGeneratorGradlePlugin implements Plugin { Convention sourceSetConvention = new DslObject(sourceSet).convention // Create the assertJ closure within the source set, e.g. main { assertJ { } } - DefaultAssertJGeneratorSourceSet assertJSourceSet = new DefaultAssertJGeneratorSourceSet( - sourceSet, sourceDirectorySetFactory) + def assertJSourceSet = objects.newInstance( + DefaultAssertJGeneratorSourceSet, + objects, + sourceSet, + ) sourceSetConvention.plugins[AssertJGeneratorSourceSet.NAME] = assertJSourceSet sourceSet.allSource.source(assertJSourceSet.assertJ) @@ -104,17 +106,18 @@ class AssertJGeneratorGradlePlugin implements Plugin { if (!generationTask) { generationTask = project.tasks.create(generateTaskName, AssertJGenerationTask) { description = "Generates AssertJ assertions for the ${sourceSet} sources." - generationClasspath = sourceSet.runtimeClasspath // Get the classes used when creating the ClassLoader for - // Generation + generationClasspath = sourceSet.runtimeClasspath + // Get the classes used when creating the ClassLoader for + // Generation - source = assertJSS.assertJ // Set up the conventional sources - assertJOptions = assertJSS // Set the config options, too + source = assertJSS.assertJ // Set up the conventional sources + assertJOptions = assertJSS // Set the config options, too } final def compileJavaTask = project.tasks.findByName(sourceSet.compileJavaTaskName) generationTask.dependsOn compileJavaTask } - + project.afterEvaluate { generationTask.configure { outputDir = assertJSS.getOutputDir(sourceSet) diff --git a/src/main/groovy/org/assertj/generator/gradle/internal/tasks/DefaultAssertJGeneratorSourceSet.groovy b/src/main/groovy/org/assertj/generator/gradle/internal/tasks/DefaultAssertJGeneratorSourceSet.groovy index 51191b2..58fab67 100644 --- a/src/main/groovy/org/assertj/generator/gradle/internal/tasks/DefaultAssertJGeneratorSourceSet.groovy +++ b/src/main/groovy/org/assertj/generator/gradle/internal/tasks/DefaultAssertJGeneratorSourceSet.groovy @@ -20,11 +20,12 @@ import org.assertj.generator.gradle.tasks.config.EntryPointGeneratorOptions import org.assertj.generator.gradle.tasks.config.Templates import org.gradle.api.Action import org.gradle.api.file.SourceDirectorySet -import org.gradle.api.internal.file.SourceDirectorySetFactory -import org.gradle.api.internal.tasks.DefaultSourceSet +import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.SourceSet import org.gradle.util.ConfigureUtil +import javax.inject.Inject + /** * Simple, default implementation of {@link AssertJGeneratorSourceSet} */ @@ -38,10 +39,14 @@ class DefaultAssertJGeneratorSourceSet extends DefaultAssertJGeneratorOptions im private final SourceDirectorySet assertJDirectorySet - DefaultAssertJGeneratorSourceSet(SourceSet sourceSet, SourceDirectorySetFactory sourceDirectorySetFactory) { - super() + @Inject + DefaultAssertJGeneratorSourceSet(ObjectFactory objectFactory, SourceSet sourceSet) { + super(objectFactory) this.name = sourceSet.name - this.assertJDirectorySet = sourceDirectorySetFactory.create("${((DefaultSourceSet) sourceSet).displayName} AssertJ Sources") + this.assertJDirectorySet = objectFactory.sourceDirectorySet( + "$sourceSet AssertJ Sources", + sourceSet.name, + ) // We default to the java directory assertJ.setSrcDirs(["src/${this.name}/java"]) diff --git a/src/main/groovy/org/assertj/generator/gradle/internal/tasks/config/DefaultAssertJGeneratorOptions.groovy b/src/main/groovy/org/assertj/generator/gradle/internal/tasks/config/DefaultAssertJGeneratorOptions.groovy index c69323b..9b8448a 100644 --- a/src/main/groovy/org/assertj/generator/gradle/internal/tasks/config/DefaultAssertJGeneratorOptions.groovy +++ b/src/main/groovy/org/assertj/generator/gradle/internal/tasks/config/DefaultAssertJGeneratorOptions.groovy @@ -19,9 +19,11 @@ import org.assertj.assertions.generator.AssertionsEntryPointType import org.assertj.generator.gradle.tasks.config.AssertJGeneratorOptions import org.assertj.generator.gradle.tasks.config.EntryPointGeneratorOptions import org.assertj.generator.gradle.tasks.config.Templates +import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.SourceSet import org.gradle.util.ConfigureUtil +import javax.inject.Inject import java.nio.file.Path import java.nio.file.Paths @@ -39,15 +41,16 @@ class DefaultAssertJGeneratorOptions implements AssertJGeneratorOptions, Seriali protected String outputDir - DefaultAssertJGeneratorOptions() { + @Inject + DefaultAssertJGeneratorOptions(ObjectFactory objects) { this.outputDir = "generated-src/${SOURCE_SET_NAME_TAG}-test/java" skip = true hierarchical = null - templates = new Templates() + templates = objects.newInstance(Templates) // default entry points - this._entryPoints = new EntryPointGeneratorOptions() + this._entryPoints = objects.newInstance(EntryPointGeneratorOptions) this._entryPoints.only(AssertionsEntryPointType.STANDARD) } diff --git a/src/main/groovy/org/assertj/generator/gradle/tasks/AssertJGenerationTask.groovy b/src/main/groovy/org/assertj/generator/gradle/tasks/AssertJGenerationTask.groovy index 39d42aa..51cea9a 100644 --- a/src/main/groovy/org/assertj/generator/gradle/tasks/AssertJGenerationTask.groovy +++ b/src/main/groovy/org/assertj/generator/gradle/tasks/AssertJGenerationTask.groovy @@ -23,6 +23,7 @@ import org.assertj.generator.gradle.internal.tasks.AssertionsGeneratorReport import org.assertj.generator.gradle.tasks.config.AssertJGeneratorOptions import org.gradle.api.file.* import org.gradle.api.logging.Logging +import org.gradle.api.provider.ListProperty import org.gradle.api.tasks.* import org.gradle.api.tasks.incremental.IncrementalTaskInputs @@ -41,12 +42,18 @@ class AssertJGenerationTask extends SourceTask { @Classpath FileCollection generationClasspath - @Input + @Internal AssertJGeneratorOptions assertJOptions @InputFiles - List getTemplateFiles() { - assertJOptions.templates.files + @Classpath + ListProperty getTemplateFiles() { + assertJOptions.templates.templateFiles + } + + @Input + ListProperty getTemplateStrings() { + assertJOptions.templates.templateStrings } @OutputDirectory @@ -81,7 +88,7 @@ class AssertJGenerationTask extends SourceTask { } else if (sourceFiles.contains(change.file)) { // source file changed classesToGenerate += change.file - } else if (templateFiles.contains(change.file)) { + } else if (templateFiles.get().contains(change.file)) { fullRegenRequired = true } } diff --git a/src/main/groovy/org/assertj/generator/gradle/tasks/config/Templates.groovy b/src/main/groovy/org/assertj/generator/gradle/tasks/config/Templates.groovy index 33d76b1..16fbcf3 100644 --- a/src/main/groovy/org/assertj/generator/gradle/tasks/config/Templates.groovy +++ b/src/main/groovy/org/assertj/generator/gradle/tasks/config/Templates.groovy @@ -19,6 +19,11 @@ import org.assertj.core.util.Files import org.assertj.generator.gradle.internal.tasks.AssertionsGeneratorReport import org.assertj.generator.gradle.util.Either import org.gradle.api.Action +import org.gradle.api.Project +import org.gradle.api.model.ObjectFactory +import org.gradle.api.provider.ListProperty + +import javax.inject.Inject import static org.assertj.assertions.generator.Template.Type.* @@ -35,11 +40,11 @@ class Templates implements Serializable { /** * Class-level templates. - * + * * @see ClassTemplates */ ClassTemplates classes = new ClassTemplates() - + def classes(Action action) { action.execute(classes) this @@ -59,7 +64,7 @@ class Templates implements Serializable { * @see MethodTemplates */ MethodTemplates methods = new MethodTemplates() - + def methods(Action action) { action.execute(methods) this @@ -79,81 +84,60 @@ class Templates implements Serializable { * @see EntryPointTemplates */ EntryPointTemplates entryPoints = new EntryPointTemplates() - + def entryPoints(Action action) { action.execute(entryPoints) this } def entryPoints(Closure closure) { - + // DO NOT USE ConfigureUtil.configure() it will not allow us to override the `setProperty()` method via the // mixin. closure.delegate = this.entryPoints closure.run() this } - - private final List handlers + + private final List handlers = [classes, methods, entryPoints] /** - * Gets all files associated with templates. This is used for building up dependencies. - * @return Possibly empty List of files + * All files associated with templates. This is used for building up dependencies. */ - def getFiles() { - List files = handlers.collect { it.files }.flatten() as List - - if (dir) { - // resolve all of them if there is a directory - def dirPath = dir.toPath() - files = files.collect { - dirPath.resolve(it.toPath()).toFile() - } - } - - files - } - - Templates() { - handlers = new ArrayList<>(3) - handlers.add(classes) - handlers.add(methods) - handlers.add(entryPoints) - } + ListProperty templateFiles /** - * Does a shallow copy of the fields from {@code other} into {@code this} - * @param other + * All template strings that have been set by a user. */ - void copyFrom(Templates other) { - this.dir = other.dir - - // Directly copy all the property values for the handlers - handlers.each { handler -> - handler.metaClass.properties.each { prop -> - def fromOther = prop.getProperty(other) - if (fromOther) { - prop.setProperty(this, fromOther) + ListProperty templateStrings + + @Inject + Templates(ObjectFactory objects, Project project) { + templateStrings = objects.listProperty(String) + + def handlers = this.handlers + templateStrings.set( + project.provider { + handlers.collectMany { it.strings } } - } - } - } + ) + + templateFiles = objects.listProperty(File) + templateFiles.set( + project.provider { + List files = handlers.collectMany { it.files } + + if (dir) { + // resolve all of them if there is a directory + def dirPath = dir.toPath() + files = files.collect { + dirPath.resolve(it.toPath()).toFile() + } + } - /** - * Defaults all the values in {@code defaults} into {@code this} - * @param defaults Default values - */ - void defaults(Templates defaults) { - if (!this.dir) { - this.dir = defaults.dir - } - // Directly override if not set - this.metaClass.properties.findAll { v -> TemplateHandler.class.isAssignableFrom(v.type) } - .each { prop -> - TemplateHandler handler = prop.getProperty(this) as TemplateHandler - TemplateHandler defHandler = prop.getProperty(defaults) as TemplateHandler - handler.defaultFrom(defHandler) - } + files + } + ) } /** @@ -168,19 +152,19 @@ class Templates implements Serializable { // load any templates overridden by the user List