Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ class PruneGeneratedFactories implements Action<Task> {

@Override
void execute(Task task) {
File classesDirectory = destinationDirectory != null
? destinationDirectory.get().asFile
: ((AbstractCompile) task).destinationDirectory.get().asFile
Provider<Directory> targetDirectory = task instanceof AbstractCompile

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Construct the action with a destination that is not the destination of the AbstractCompile task that runs it. The action now uses the running task's directory. Stale registrations stay in the directory that was supplied, and the running task's file can be pruned instead. Use the supplied directory when it has a value.

? ((AbstractCompile) task).destinationDirectory
: destinationDirectory
File classesDirectory = targetDirectory?.getOrNull()?.asFile
if (classesDirectory == null) {
return
}
File factoriesFile = new File(classesDirectory, factoriesLocation)
if (!factoriesFile.isFile()) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,43 @@ class PruneGeneratedFactoriesSpec extends Specification {
load(generated) == ['grails.core.ArtefactHandler': 'example.KeepHandler']
}

void 'forwarded action prunes the running AbstractCompile task destination rather than the source task'() {
given: 'a source compileGroovy task with its own stale factories file'
String sourceText = 'grails.core.ArtefactHandler=example.SourceGoneHandler\n'
write(generated, sourceText)

and: 'a second GroovyCompile task with a different destination directory'
File otherClassesDirectory = new File(projectDir, 'build/classes/groovy/other')
GroovyCompile otherCompile = project.tasks.register('compileOtherGroovy', GroovyCompile) { GroovyCompile task ->
task.destinationDirectory.set(otherClassesDirectory)
}.get()
write(new File(otherClassesDirectory, 'example/OtherKeepHandler.class'), '')
File otherGenerated = new File(otherClassesDirectory, PruneGeneratedFactories.GRAILS_FACTORIES_LOCATION)
write(otherGenerated, 'grails.core.ArtefactHandler=example.OtherGoneHandler,example.OtherKeepHandler\n')

when: 'the action registered on compileGroovy is forwarded to the other GroovyCompile task'
PruneGeneratedFactories.register(project, compileGroovy, PruneGeneratedFactories.GRAILS_FACTORIES_LOCATION, [HAND_AUTHORED])
compileGroovy.actions.last().execute(otherCompile)

then: 'the running task destination is pruned and the source task destination is untouched'
load(otherGenerated) == ['grails.core.ArtefactHandler': 'example.OtherKeepHandler']
generated.exists()
generated.text == sourceText
}

void 'two-argument constructor ignores a non-AbstractCompile task without throwing'() {
given:
Task otherTask = project.tasks.register('plainTask').get()
Action<Task> action = new PruneGeneratedFactories(PruneGeneratedFactories.GRAILS_FACTORIES_LOCATION,
[new File(projectDir, HAND_AUTHORED)])

when:
action.execute(otherTask)

then:
noExceptionThrown()
}

private void prune() {
Action<Task> action = new PruneGeneratedFactories(PruneGeneratedFactories.GRAILS_FACTORIES_LOCATION,
[new File(projectDir, HAND_AUTHORED)])
Expand Down
Loading