Skip to content

Commit 403a6d6

Browse files
committed
Simplify filtering
1 parent e6e1a31 commit 403a6d6

File tree

11 files changed

+42
-132
lines changed

11 files changed

+42
-132
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ To use the Gradle Plugins DSL, add the following lines to your `build.gradle` sc
1212

1313
```gradle
1414
plugins {
15-
id "org.javacc.javacc" version "4.0.0"
15+
id "org.javacc.javacc" version "4.0.1"
1616
}
1717
```
1818

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
allprojects {
88
group = 'org.javacc.plugin'
9-
version = '4.0.0'
9+
version = '4.0.1'
1010
}
1111

1212
defaultTasks 'clean', ':javacc-gradle-plugin:build'

subprojects/plugin/src/main/java/org/javacc/plugin/gradle/javacc/AbstractJavaccTask.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.javacc.plugin.gradle.javacc;
22

33
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
46
import java.util.Map;
57

6-
import org.gradle.api.Task;
78
import org.gradle.api.artifacts.Configuration;
9+
import org.gradle.api.file.FileTree;
810
import org.gradle.api.tasks.InputDirectory;
911
import org.gradle.api.tasks.Internal;
1012
import org.gradle.api.tasks.Optional;
@@ -13,16 +15,12 @@
1315
import org.gradle.api.tasks.PathSensitivity;
1416
import org.gradle.api.tasks.SkipWhenEmpty;
1517
import org.gradle.api.tasks.SourceTask;
16-
import org.gradle.api.tasks.TaskCollection;
17-
import org.gradle.api.tasks.compile.JavaCompile;
1818
import org.gradle.process.ExecOperations;
1919

20-
import groovy.lang.Closure;
21-
2220
public abstract class AbstractJavaccTask extends SourceTask {
2321
protected Map<String, String> programArguments;
2422
protected final ExecOperations execOperations;
25-
protected TaskCollection<JavaCompile> javaCompileTasks;
23+
protected final List<FileTree> javaSources = new ArrayList<>();
2624

2725
private File inputDirectory;
2826
private File outputDirectory;
@@ -37,12 +35,6 @@ protected AbstractJavaccTask(String inputDirectory, String outputDirectory, Stri
3735
this.execOperations = execOperations;
3836
}
3937

40-
@Override
41-
public Task configure(Closure closure) {
42-
javaCompileTasks = this.getProject().getTasks().withType(JavaCompile.class);
43-
return super.configure(closure);
44-
}
45-
4638
@Internal
4739
public Map<String, String> getArguments() {
4840
return programArguments;
@@ -100,4 +92,8 @@ public AbstractJavaccTask setOutputDirectory(File outputDirectory) {
10092
protected Configuration getClasspath() {
10193
return classpath;
10294
}
95+
96+
public void addJavaSources(FileTree source) {
97+
javaSources.add(source);
98+
}
10399
}

subprojects/plugin/src/main/java/org/javacc/plugin/gradle/javacc/CompileJavaccTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public CompileJavaccTask(ExecOperations execOperations) {
3232
@TaskAction
3333
public void run() {
3434
CompilerInputOutputConfiguration inputOutputDirectories
35-
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaCompileTasks);
35+
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaSources);
3636
JavaccProgramInvoker javaccInvoker = new JavaccProgramInvoker(getClasspath(),
3737
inputOutputDirectories.getTempOutputDirectory(), execOperations);
3838
ProgramArguments arguments = new ProgramArguments();

subprojects/plugin/src/main/java/org/javacc/plugin/gradle/javacc/CompileJjdocTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public CompileJjdocTask(ExecOperations execOperations) {
3131
@TaskAction
3232
public void run() {
3333
CompilerInputOutputConfiguration inputOutputDirectories
34-
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaCompileTasks);
34+
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaSources);
3535
JjdocProgramInvoker jjdocInvoker = new JjdocProgramInvoker(getClasspath(),
3636
inputOutputDirectories.getTempOutputDirectory(), execOperations);
3737
ProgramArguments arguments = new ProgramArguments();

subprojects/plugin/src/main/java/org/javacc/plugin/gradle/javacc/CompileJjtreeTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public CompileJjtreeTask(ExecOperations execOperations) {
3131
@TaskAction
3232
public void run() {
3333
CompilerInputOutputConfiguration inputOutputDirectories
34-
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaCompileTasks);
34+
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaSources);
3535
JjtreeProgramInvoker jjtreeInvoker = new JjtreeProgramInvoker(getClasspath(),
3636
inputOutputDirectories.getTempOutputDirectory(), execOperations);
3737
ProgramArguments arguments = new ProgramArguments();

subprojects/plugin/src/main/java/org/javacc/plugin/gradle/javacc/JavaToJavaccDependencyAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ private void addJavaccDependencyToJavaCompileTask(Collection<JavaCompile> javaCo
5555
for (JavaCompile task : javaCompilationTasks) {
5656
task.dependsOn(compileJavaccTask);
5757
task.source(compileJavaccTask.getOutputDirectory());
58+
compileJavaccTask.addJavaSources(task.getSource());
5859
}
5960
}
6061

subprojects/plugin/src/main/java/org/javacc/plugin/gradle/javacc/compiler/CompilerInputOutputConfiguration.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,4 @@ public interface CompilerInputOutputConfiguration {
3434
*/
3535
FileTree getJavaSourceTree();
3636

37-
/**
38-
* @return a {@link FileTree} representation of all the source files, both Java and specific to the compiler
39-
* @see #getSource()
40-
* @see #getJavaSourceTree()
41-
*/
42-
FileTree getCompleteSourceTree();
4337
}

subprojects/plugin/src/main/java/org/javacc/plugin/gradle/javacc/compiler/JavaccCompilerInputOutputConfiguration.java

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
package org.javacc.plugin.gradle.javacc.compiler;
22

33
import java.io.File;
4-
import java.util.HashSet;
5-
import java.util.Set;
4+
import java.util.List;
65

76
import org.gradle.api.file.FileTree;
87
import org.gradle.api.specs.Spec;
9-
import org.gradle.api.tasks.TaskCollection;
10-
import org.gradle.api.tasks.compile.JavaCompile;
118

129
public class JavaccCompilerInputOutputConfiguration implements CompilerInputOutputConfiguration {
1310
private final File inputDirectory;
1411
private final File outputDirectory;
1512
private final FileTree source;
16-
private final Set<JavaCompile> javaCompileTasks;
13+
private final List<FileTree> javaSources;
1714

18-
public JavaccCompilerInputOutputConfiguration(File inputDirectory, File outputDirectory, FileTree source, TaskCollection<JavaCompile> javaCompileTasks) {
15+
public JavaccCompilerInputOutputConfiguration(File inputDirectory, File outputDirectory, FileTree source, List<FileTree> javaSources) {
1916
this.inputDirectory = inputDirectory;
2017
this.outputDirectory = outputDirectory;
2118
this.source = source;
22-
this.javaCompileTasks = new HashSet<>();
23-
24-
if (javaCompileTasks != null) {
25-
this.javaCompileTasks.addAll(javaCompileTasks);
26-
}
19+
this.javaSources = javaSources;
2720
}
2821

2922
@Override
@@ -46,30 +39,15 @@ public FileTree getSource() {
4639
return source;
4740
}
4841

49-
@Override
50-
public FileTree getCompleteSourceTree() {
51-
FileTree javaccTaskSourceTree = getSource();
52-
FileTree javaTasksSourceTree = getJavaSourceTree();
53-
FileTree completeSourceTree;
54-
55-
if (javaTasksSourceTree == null) {
56-
completeSourceTree = javaccTaskSourceTree;
57-
} else {
58-
completeSourceTree = javaccTaskSourceTree.plus(javaTasksSourceTree);
59-
}
60-
61-
return excludeOutputDirectory(completeSourceTree);
62-
}
63-
6442
@Override
6543
public FileTree getJavaSourceTree() {
6644
FileTree javaSourceTree = null;
6745

68-
for (JavaCompile task : javaCompileTasks) {
46+
for (FileTree excluded : javaSources) {
6947
if (javaSourceTree == null) {
70-
javaSourceTree = task.getSource();
48+
javaSourceTree = excluded;
7149
} else {
72-
javaSourceTree = javaSourceTree.plus(task.getSource());
50+
javaSourceTree = javaSourceTree.plus(excluded);
7351
}
7452
}
7553

subprojects/plugin/src/test/java/org/javacc/plugin/gradle/javacc/compiler/JavaccCompilerInputOutputConfigurationTest.java

Lines changed: 15 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010

1111
import java.io.File;
1212
import java.io.IOException;
13+
import java.util.ArrayList;
1314
import java.util.HashMap;
15+
import java.util.List;
1416
import java.util.Map;
17+
import java.util.stream.Collectors;
1518

1619
import org.gradle.api.Project;
1720
import org.gradle.api.Task;
1821
import org.gradle.api.file.FileTree;
1922
import org.gradle.api.tasks.SourceTask;
20-
import org.gradle.api.tasks.TaskCollection;
2123
import org.gradle.api.tasks.compile.JavaCompile;
2224
import org.gradle.testfixtures.ProjectBuilder;
2325
import org.junit.Before;
2426
import org.junit.Rule;
2527
import org.junit.Test;
2628
import org.junit.rules.TemporaryFolder;
2729

28-
import org.javacc.plugin.gradle.javacc.CompileJavaccTask;
29-
3030
public class JavaccCompilerInputOutputConfigurationTest {
3131

3232
@Rule
@@ -74,14 +74,19 @@ public void getJavaSourceTreeReturnsJavaCompileSourceWhenSingleJavaCompileTask()
7474
CompilerInputOutputConfiguration configuration = builder
7575
.withInputDirectory(inputDirectory)
7676
.withOutputDirectory(outputDirectory)
77-
.withJavaCompileTasks(project.getTasks().withType(JavaCompile.class))
77+
.withJavaSources(getJavaSources(project))
7878
.build();
7979

8080
FileTree javaSourceTree = configuration.getJavaSourceTree();
8181

8282
assertThat(javaSourceTree, contains(javaFile.getCanonicalFile()));
8383
}
8484

85+
private List<FileTree> getJavaSources(Project project) {
86+
return project.getTasks().withType(JavaCompile.class)
87+
.stream().map(JavaCompile::getSource).collect(Collectors.toList());
88+
}
89+
8590
private File addTaskWithSourceFile(Project project, String taskName, String sourceFileName, Class<? extends SourceTask> taskType) throws IOException {
8691
Map<String, Object> options = new HashMap<>();
8792
options.put(Task.TASK_TYPE, taskType);
@@ -102,7 +107,7 @@ public void getJavaSourceTreeReturnsAggregatedJavaCompileSourceWhenMultipleJavaC
102107
CompilerInputOutputConfiguration configuration = builder
103108
.withInputDirectory(inputDirectory)
104109
.withOutputDirectory(outputDirectory)
105-
.withJavaCompileTasks(project.getTasks().withType(JavaCompile.class))
110+
.withJavaSources(getJavaSources(project))
106111
.build();
107112

108113
FileTree javaSourceTree = configuration.getJavaSourceTree();
@@ -118,7 +123,7 @@ public void getJavaSourceTreeExcludesOutputFolder() throws IOException {
118123
CompilerInputOutputConfiguration configuration = builder
119124
.withInputDirectory(inputDirectory)
120125
.withOutputDirectory(outputDirectory)
121-
.withJavaCompileTasks(project.getTasks().withType(JavaCompile.class))
126+
.withJavaSources(getJavaSources(project))
122127
.build();
123128

124129
FileTree javaSourceTree = configuration.getJavaSourceTree();
@@ -127,61 +132,11 @@ public void getJavaSourceTreeExcludesOutputFolder() throws IOException {
127132
assertThat(javaSourceTree, not(contains(outputFile.getCanonicalFile())));
128133
}
129134

130-
@Test
131-
public void getCompleteSourceTreeReturnsSourceWhenNoJavaCompileTask() throws IOException {
132-
Project project = ProjectBuilder.builder().withProjectDir(testFolder.getRoot()).build();
133-
File javaccFile = addTaskWithSourceFile(project, "compileJavacc", "input/TestClass.jj", CompileJavaccTask.class);
134-
CompilerInputOutputConfiguration configuration = builder
135-
.withInputDirectory(inputDirectory)
136-
.withOutputDirectory(outputDirectory)
137-
.withSource(((SourceTask) project.getTasks().getByName("compileJavacc")).getSource())
138-
.build();
139-
140-
FileTree completeSourceTree = configuration.getCompleteSourceTree();
141-
142-
assertThat(completeSourceTree, contains(javaccFile.getCanonicalFile()));
143-
}
144-
145-
@Test
146-
public void getCompleteSourceTreeReturnsSourceAndJavaSourceWhenProjectHasJavaCompileTasks() throws IOException {
147-
Project project = ProjectBuilder.builder().withProjectDir(testFolder.getRoot()).build();
148-
File javaccFile = addTaskWithSourceFile(project, "compileJavacc", "input/TestClass.jj", CompileJavaccTask.class);
149-
testFolder.newFolder("inputJava");
150-
File javaFile = addTaskWithSourceFile(project, "compileJava", "inputJava/MyClass.java", JavaCompile.class);
151-
CompilerInputOutputConfiguration configuration = builder
152-
.withInputDirectory(inputDirectory)
153-
.withOutputDirectory(outputDirectory)
154-
.withSource(((SourceTask) project.getTasks().getByName("compileJavacc")).getSource())
155-
.withJavaCompileTasks(project.getTasks().withType(JavaCompile.class))
156-
.build();
157-
158-
FileTree completeSourceTree = configuration.getCompleteSourceTree();
159-
160-
assertThat(completeSourceTree, containsInAnyOrder(javaccFile.getCanonicalFile(), javaFile.getCanonicalFile()));
161-
}
162-
163-
@Test
164-
public void getCompleteSourceTreeExcludesOutputFolder() throws IOException {
165-
Project project = ProjectBuilder.builder().withProjectDir(testFolder.getRoot()).build();
166-
File javaccFile = addTaskWithSourceFile(project, "compileJavacc", "input/TestClass.jj", CompileJavaccTask.class);
167-
File outputFile = addTaskWithSourceFile(project, "compileJavaccGenerated", "output/Generated.java", JavaCompile.class);
168-
CompilerInputOutputConfiguration configuration = builder
169-
.withInputDirectory(inputDirectory)
170-
.withOutputDirectory(outputDirectory)
171-
.withSource(((SourceTask) project.getTasks().getByName("compileJavacc")).getSource())
172-
.withJavaCompileTasks(project.getTasks().withType(JavaCompile.class))
173-
.build();
174-
175-
FileTree completeSourceTree = configuration.getCompleteSourceTree();
176-
177-
assertThat(completeSourceTree, contains(javaccFile.getCanonicalFile()));
178-
assertThat(completeSourceTree, not(contains(outputFile.getCanonicalFile())));
179-
}
180135

181136
private static class JavaccConfigurationBuilder {
182137
private File inputDirectory;
183138
private File outputDirectory;
184-
private TaskCollection<JavaCompile> javaCompileTasks;
139+
private List<FileTree> javaSourceTrees = new ArrayList<>();
185140
private FileTree source;
186141

187142
private JavaccConfigurationBuilder() {
@@ -203,20 +158,14 @@ public JavaccConfigurationBuilder withOutputDirectory(File outputDirectory) {
203158
return this;
204159
}
205160

206-
public JavaccConfigurationBuilder withJavaCompileTasks(TaskCollection<JavaCompile> javaCompileTasks) {
207-
this.javaCompileTasks = javaCompileTasks;
208-
209-
return this;
210-
}
211-
212-
public JavaccConfigurationBuilder withSource(FileTree source) {
213-
this.source = source;
161+
public JavaccConfigurationBuilder withJavaSources(List<FileTree> javaSourceTrees) {
162+
this.javaSourceTrees.addAll(javaSourceTrees);
214163

215164
return this;
216165
}
217166

218167
public JavaccCompilerInputOutputConfiguration build() {
219-
return new JavaccCompilerInputOutputConfiguration(inputDirectory, outputDirectory, source, javaCompileTasks);
168+
return new JavaccCompilerInputOutputConfiguration(inputDirectory, outputDirectory, source, javaSourceTrees);
220169
}
221170
}
222171
}

subprojects/release/build.gradle

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,19 @@ ext.gitRepo = Grgit.open(rootProject.projectDir)
2222
def releaseVersionProperty = 'releaseVersion'
2323
def nextVersionProperty = 'nextVersion'
2424

25-
task verifyReleaseVersions {
25+
tasks.register('verifyReleaseVersions') {
2626
description "Verifies that the [${releaseVersionProperty}] and [${nextVersionProperty}] project properties are specified"
2727
group 'Release'
28-
}
29-
30-
verifyReleaseVersions {
3128
doLast {
3229
if (!project.hasProperty(releaseVersionProperty) || !project.hasProperty(nextVersionProperty)) {
3330
throw new RuntimeException("[${releaseVersionProperty}] and [${nextVersionProperty}] project properties are mandatory.")
3431
}
3532
}
3633
}
3734

38-
task verifySnapshotDependencies {
35+
tasks.register('verifySnapshotDependencies') {
3936
description 'Fails the build if there are SNAPSHOT dependencies.'
4037
group 'Release'
41-
}
42-
43-
verifySnapshotDependencies {
4438
doLast {
4539
configurations.each { configuration ->
4640
configuration.dependencies.each { dependency ->
@@ -67,12 +61,9 @@ def commitChangedFiles(String commitMessage) {
6761
}
6862
}
6963

70-
task tagRelease {
64+
tasks.register('tagRelease') {
7165
description 'Creates a tag in SCM for the release.'
7266
group 'Release'
73-
}
74-
75-
tagRelease {
7667
doLast {
7768
gitRepo.tag.add {
7869
name = "javacc-gradle-plugin-${releaseVersion}"
@@ -174,8 +165,9 @@ writeNextVersion.mustRunAfter tagRelease
174165

175166
copyNextBuildFile.mustRunAfter writeNextVersion
176167

177-
task release(dependsOn: [project(':javacc-gradle-plugin').clean, verifyReleaseVersions, verifySnapshotDependencies,
178-
project(':javacc-gradle-plugin').build, copyReleaseBuildFile, tagRelease, copyNextBuildFile]) {
168+
tasks.register('release') {
169+
dependsOn project(':javacc-gradle-plugin').clean, verifyReleaseVersions, verifySnapshotDependencies,
170+
project(':javacc-gradle-plugin').build, copyReleaseBuildFile, tagRelease, copyNextBuildFile
179171
description "Releases the plugin by verifying there are no snapshot dependencies, " +
180172
"writing the next version to build.gradle and committing the build file. " +
181173
"Versions must be specified by using the [${releaseVersionProperty}] and [${nextVersionProperty}] project properties."

0 commit comments

Comments
 (0)