Skip to content

Commit 8204cd3

Browse files
authored
[Simplify] Remove Either custom type in Templates infavour of simpler type (#38)
This removes a *lot* of groovy magic to make dynamic types work. Instead, we use a simple type constructed of two values. This type is then mapped to either a template string or a file and used as task input. Extracted from #29. Depends on #37.
1 parent 50f8567 commit 8204cd3

File tree

6 files changed

+355
-508
lines changed

6 files changed

+355
-508
lines changed

README.md

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ sourceSets {
175175
assertJ {
176176
templates {
177177
methods {
178-
wholeNumberPrimitive = 'public get${Property}() { }'
178+
wholeNumberPrimitive.template('public get${Property}() { }')
179179
}
180180
}
181181
}
@@ -192,32 +192,27 @@ sourceSets {
192192
templates {
193193
// Set the template to file content: ./wholeNumberOverride.txt
194194
methods {
195-
wholeNumberPrimitive = file('wholeNumberOverride.txt')
195+
wholeNumberPrimitive.file('wholeNumberOverride.txt')
196196
}
197197
}
198198
}
199199
}
200200
}
201201
```
202202

203-
The `templateDir` is not a scope-override property. If set, it only applies to the block it is defined within. This was
204-
intentionally done to remove ambiguity. Additionally, this example can be applied to local scopes.
203+
We can root all files under a directory by using a file from the scoped `project`:
205204

206205
```gradle
207-
assertJ {
208-
209-
}
210-
211206
sourceSets {
212207
main {
213208
assertJ {
214209
templates {
215210
// Set all templates in this block to be relative to the folder specified
216-
dir = "${projectDir}/gradle/other-templates"
211+
def rootDirectory = projectDir.dir("gradle/other-templates")
217212
218213
// Change the file content to:
219214
// ${projectDir}/gradle/other-templates/wholeNumberOverride.txt
220-
wholeNumberAssertion = file('wholeNumberOverride.txt')
215+
wholeNumberAssertion.file(rootDirectory.file('wholeNumberOverride.txt'))
221216
}
222217
}
223218
}
@@ -244,11 +239,6 @@ By default, only the `standard` style is turned on. To adjust this, simply set t
244239
`entryPoints` closure.
245240

246241
```gradle
247-
// For all source sets:
248-
assertJ {
249-
250-
}
251-
252242
sourceSets {
253243
main {
254244
assertJ {

src/main/groovy/org/assertj/generator/gradle/tasks/AssertJGenerationTask.groovy

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ import org.assertj.assertions.generator.description.converter.ClassToClassDescri
2121
import org.assertj.assertions.generator.util.ClassUtil
2222
import org.assertj.generator.gradle.internal.tasks.AssertionsGeneratorReport
2323
import org.assertj.generator.gradle.tasks.config.AssertJGeneratorExtension
24-
import org.gradle.api.file.ConfigurableFileCollection
25-
import org.gradle.api.file.DirectoryProperty
26-
import org.gradle.api.file.FileTree
27-
import org.gradle.api.file.FileVisitDetails
24+
import org.assertj.generator.gradle.tasks.config.SerializedTemplate
25+
import org.gradle.api.file.*
2826
import org.gradle.api.logging.Logging
2927
import org.gradle.api.model.ObjectFactory
3028
import org.gradle.api.provider.ListProperty
@@ -49,10 +47,10 @@ class AssertJGenerationTask extends SourceTask {
4947

5048
@InputFiles
5149
@Classpath
52-
final ListProperty<File> templateFiles
50+
final FileCollection templateFiles
5351

5452
@Input
55-
final ListProperty<String> templateStrings
53+
final ListProperty<SerializedTemplate> templates
5654

5755
@OutputDirectory
5856
final DirectoryProperty outputDir
@@ -72,7 +70,7 @@ class AssertJGenerationTask extends SourceTask {
7270

7371
this.outputDir = assertJOptions.outputDir
7472
this.templateFiles = assertJOptions.templates.templateFiles
75-
this.templateStrings = assertJOptions.templates.templateStrings
73+
this.templates = assertJOptions.templates.templates
7674
}
7775

7876
@TaskAction
@@ -92,7 +90,7 @@ class AssertJGenerationTask extends SourceTask {
9290
} else if (sourceFiles.contains(change.file)) {
9391
// source file changed
9492
classesToGenerate += change.file
95-
} else if (templateFiles.get().contains(change.file)) {
93+
} else if (templateFiles.contains(change.file)) {
9694
fullRegenRequired = true
9795
}
9896
}
@@ -139,8 +137,10 @@ class AssertJGenerationTask extends SourceTask {
139137

140138
Path absOutputDir = project.rootDir.toPath().resolve(this.outputDir.getAsFile().get().toPath())
141139
report.setDirectoryPathWhereAssertionFilesAreGenerated(absOutputDir.toFile())
142-
assertJOptions.templates.getTemplates(report).each {
143-
generator.register(it)
140+
141+
def templates = assertJOptions.templates.templates.get().collect { it.maybeLoadTemplate() }.findAll()
142+
for (template in templates) {
143+
generator.register(template)
144144
}
145145

146146
try {

0 commit comments

Comments
 (0)