Skip to content

Commit

Permalink
Simplify filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
zbynek committed Jan 29, 2025
1 parent e6e1a31 commit 403a6d6
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 132 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To use the Gradle Plugins DSL, add the following lines to your `build.gradle` sc

```gradle
plugins {
id "org.javacc.javacc" version "4.0.0"
id "org.javacc.javacc" version "4.0.1"
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

allprojects {
group = 'org.javacc.plugin'
version = '4.0.0'
version = '4.0.1'
}

defaultTasks 'clean', ':javacc-gradle-plugin:build'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.javacc.plugin.gradle.javacc;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
Expand All @@ -13,16 +15,12 @@
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.SourceTask;
import org.gradle.api.tasks.TaskCollection;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.process.ExecOperations;

import groovy.lang.Closure;

public abstract class AbstractJavaccTask extends SourceTask {
protected Map<String, String> programArguments;
protected final ExecOperations execOperations;
protected TaskCollection<JavaCompile> javaCompileTasks;
protected final List<FileTree> javaSources = new ArrayList<>();

private File inputDirectory;
private File outputDirectory;
Expand All @@ -37,12 +35,6 @@ protected AbstractJavaccTask(String inputDirectory, String outputDirectory, Stri
this.execOperations = execOperations;
}

@Override
public Task configure(Closure closure) {
javaCompileTasks = this.getProject().getTasks().withType(JavaCompile.class);
return super.configure(closure);
}

@Internal
public Map<String, String> getArguments() {
return programArguments;
Expand Down Expand Up @@ -100,4 +92,8 @@ public AbstractJavaccTask setOutputDirectory(File outputDirectory) {
protected Configuration getClasspath() {
return classpath;
}

public void addJavaSources(FileTree source) {
javaSources.add(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CompileJavaccTask(ExecOperations execOperations) {
@TaskAction
public void run() {
CompilerInputOutputConfiguration inputOutputDirectories
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaCompileTasks);
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaSources);
JavaccProgramInvoker javaccInvoker = new JavaccProgramInvoker(getClasspath(),
inputOutputDirectories.getTempOutputDirectory(), execOperations);
ProgramArguments arguments = new ProgramArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CompileJjdocTask(ExecOperations execOperations) {
@TaskAction
public void run() {
CompilerInputOutputConfiguration inputOutputDirectories
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaCompileTasks);
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaSources);
JjdocProgramInvoker jjdocInvoker = new JjdocProgramInvoker(getClasspath(),
inputOutputDirectories.getTempOutputDirectory(), execOperations);
ProgramArguments arguments = new ProgramArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CompileJjtreeTask(ExecOperations execOperations) {
@TaskAction
public void run() {
CompilerInputOutputConfiguration inputOutputDirectories
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaCompileTasks);
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaSources);
JjtreeProgramInvoker jjtreeInvoker = new JjtreeProgramInvoker(getClasspath(),
inputOutputDirectories.getTempOutputDirectory(), execOperations);
ProgramArguments arguments = new ProgramArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private void addJavaccDependencyToJavaCompileTask(Collection<JavaCompile> javaCo
for (JavaCompile task : javaCompilationTasks) {
task.dependsOn(compileJavaccTask);
task.source(compileJavaccTask.getOutputDirectory());
compileJavaccTask.addJavaSources(task.getSource());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,4 @@ public interface CompilerInputOutputConfiguration {
*/
FileTree getJavaSourceTree();

/**
* @return a {@link FileTree} representation of all the source files, both Java and specific to the compiler
* @see #getSource()
* @see #getJavaSourceTree()
*/
FileTree getCompleteSourceTree();
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
package org.javacc.plugin.gradle.javacc.compiler;

import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.List;

import org.gradle.api.file.FileTree;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.TaskCollection;
import org.gradle.api.tasks.compile.JavaCompile;

public class JavaccCompilerInputOutputConfiguration implements CompilerInputOutputConfiguration {
private final File inputDirectory;
private final File outputDirectory;
private final FileTree source;
private final Set<JavaCompile> javaCompileTasks;
private final List<FileTree> javaSources;

public JavaccCompilerInputOutputConfiguration(File inputDirectory, File outputDirectory, FileTree source, TaskCollection<JavaCompile> javaCompileTasks) {
public JavaccCompilerInputOutputConfiguration(File inputDirectory, File outputDirectory, FileTree source, List<FileTree> javaSources) {
this.inputDirectory = inputDirectory;
this.outputDirectory = outputDirectory;
this.source = source;
this.javaCompileTasks = new HashSet<>();

if (javaCompileTasks != null) {
this.javaCompileTasks.addAll(javaCompileTasks);
}
this.javaSources = javaSources;
}

@Override
Expand All @@ -46,30 +39,15 @@ public FileTree getSource() {
return source;
}

@Override
public FileTree getCompleteSourceTree() {
FileTree javaccTaskSourceTree = getSource();
FileTree javaTasksSourceTree = getJavaSourceTree();
FileTree completeSourceTree;

if (javaTasksSourceTree == null) {
completeSourceTree = javaccTaskSourceTree;
} else {
completeSourceTree = javaccTaskSourceTree.plus(javaTasksSourceTree);
}

return excludeOutputDirectory(completeSourceTree);
}

@Override
public FileTree getJavaSourceTree() {
FileTree javaSourceTree = null;

for (JavaCompile task : javaCompileTasks) {
for (FileTree excluded : javaSources) {
if (javaSourceTree == null) {
javaSourceTree = task.getSource();
javaSourceTree = excluded;
} else {
javaSourceTree = javaSourceTree.plus(task.getSource());
javaSourceTree = javaSourceTree.plus(excluded);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.SourceTask;
import org.gradle.api.tasks.TaskCollection;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import org.javacc.plugin.gradle.javacc.CompileJavaccTask;

public class JavaccCompilerInputOutputConfigurationTest {

@Rule
Expand Down Expand Up @@ -74,14 +74,19 @@ public void getJavaSourceTreeReturnsJavaCompileSourceWhenSingleJavaCompileTask()
CompilerInputOutputConfiguration configuration = builder
.withInputDirectory(inputDirectory)
.withOutputDirectory(outputDirectory)
.withJavaCompileTasks(project.getTasks().withType(JavaCompile.class))
.withJavaSources(getJavaSources(project))
.build();

FileTree javaSourceTree = configuration.getJavaSourceTree();

assertThat(javaSourceTree, contains(javaFile.getCanonicalFile()));
}

private List<FileTree> getJavaSources(Project project) {
return project.getTasks().withType(JavaCompile.class)
.stream().map(JavaCompile::getSource).collect(Collectors.toList());
}

private File addTaskWithSourceFile(Project project, String taskName, String sourceFileName, Class<? extends SourceTask> taskType) throws IOException {
Map<String, Object> options = new HashMap<>();
options.put(Task.TASK_TYPE, taskType);
Expand All @@ -102,7 +107,7 @@ public void getJavaSourceTreeReturnsAggregatedJavaCompileSourceWhenMultipleJavaC
CompilerInputOutputConfiguration configuration = builder
.withInputDirectory(inputDirectory)
.withOutputDirectory(outputDirectory)
.withJavaCompileTasks(project.getTasks().withType(JavaCompile.class))
.withJavaSources(getJavaSources(project))
.build();

FileTree javaSourceTree = configuration.getJavaSourceTree();
Expand All @@ -118,7 +123,7 @@ public void getJavaSourceTreeExcludesOutputFolder() throws IOException {
CompilerInputOutputConfiguration configuration = builder
.withInputDirectory(inputDirectory)
.withOutputDirectory(outputDirectory)
.withJavaCompileTasks(project.getTasks().withType(JavaCompile.class))
.withJavaSources(getJavaSources(project))
.build();

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

@Test
public void getCompleteSourceTreeReturnsSourceWhenNoJavaCompileTask() throws IOException {
Project project = ProjectBuilder.builder().withProjectDir(testFolder.getRoot()).build();
File javaccFile = addTaskWithSourceFile(project, "compileJavacc", "input/TestClass.jj", CompileJavaccTask.class);
CompilerInputOutputConfiguration configuration = builder
.withInputDirectory(inputDirectory)
.withOutputDirectory(outputDirectory)
.withSource(((SourceTask) project.getTasks().getByName("compileJavacc")).getSource())
.build();

FileTree completeSourceTree = configuration.getCompleteSourceTree();

assertThat(completeSourceTree, contains(javaccFile.getCanonicalFile()));
}

@Test
public void getCompleteSourceTreeReturnsSourceAndJavaSourceWhenProjectHasJavaCompileTasks() throws IOException {
Project project = ProjectBuilder.builder().withProjectDir(testFolder.getRoot()).build();
File javaccFile = addTaskWithSourceFile(project, "compileJavacc", "input/TestClass.jj", CompileJavaccTask.class);
testFolder.newFolder("inputJava");
File javaFile = addTaskWithSourceFile(project, "compileJava", "inputJava/MyClass.java", JavaCompile.class);
CompilerInputOutputConfiguration configuration = builder
.withInputDirectory(inputDirectory)
.withOutputDirectory(outputDirectory)
.withSource(((SourceTask) project.getTasks().getByName("compileJavacc")).getSource())
.withJavaCompileTasks(project.getTasks().withType(JavaCompile.class))
.build();

FileTree completeSourceTree = configuration.getCompleteSourceTree();

assertThat(completeSourceTree, containsInAnyOrder(javaccFile.getCanonicalFile(), javaFile.getCanonicalFile()));
}

@Test
public void getCompleteSourceTreeExcludesOutputFolder() throws IOException {
Project project = ProjectBuilder.builder().withProjectDir(testFolder.getRoot()).build();
File javaccFile = addTaskWithSourceFile(project, "compileJavacc", "input/TestClass.jj", CompileJavaccTask.class);
File outputFile = addTaskWithSourceFile(project, "compileJavaccGenerated", "output/Generated.java", JavaCompile.class);
CompilerInputOutputConfiguration configuration = builder
.withInputDirectory(inputDirectory)
.withOutputDirectory(outputDirectory)
.withSource(((SourceTask) project.getTasks().getByName("compileJavacc")).getSource())
.withJavaCompileTasks(project.getTasks().withType(JavaCompile.class))
.build();

FileTree completeSourceTree = configuration.getCompleteSourceTree();

assertThat(completeSourceTree, contains(javaccFile.getCanonicalFile()));
assertThat(completeSourceTree, not(contains(outputFile.getCanonicalFile())));
}

private static class JavaccConfigurationBuilder {
private File inputDirectory;
private File outputDirectory;
private TaskCollection<JavaCompile> javaCompileTasks;
private List<FileTree> javaSourceTrees = new ArrayList<>();
private FileTree source;

private JavaccConfigurationBuilder() {
Expand All @@ -203,20 +158,14 @@ public JavaccConfigurationBuilder withOutputDirectory(File outputDirectory) {
return this;
}

public JavaccConfigurationBuilder withJavaCompileTasks(TaskCollection<JavaCompile> javaCompileTasks) {
this.javaCompileTasks = javaCompileTasks;

return this;
}

public JavaccConfigurationBuilder withSource(FileTree source) {
this.source = source;
public JavaccConfigurationBuilder withJavaSources(List<FileTree> javaSourceTrees) {
this.javaSourceTrees.addAll(javaSourceTrees);

return this;
}

public JavaccCompilerInputOutputConfiguration build() {
return new JavaccCompilerInputOutputConfiguration(inputDirectory, outputDirectory, source, javaCompileTasks);
return new JavaccCompilerInputOutputConfiguration(inputDirectory, outputDirectory, source, javaSourceTrees);
}
}
}
20 changes: 6 additions & 14 deletions subprojects/release/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,19 @@ ext.gitRepo = Grgit.open(rootProject.projectDir)
def releaseVersionProperty = 'releaseVersion'
def nextVersionProperty = 'nextVersion'

task verifyReleaseVersions {
tasks.register('verifyReleaseVersions') {
description "Verifies that the [${releaseVersionProperty}] and [${nextVersionProperty}] project properties are specified"
group 'Release'
}

verifyReleaseVersions {
doLast {
if (!project.hasProperty(releaseVersionProperty) || !project.hasProperty(nextVersionProperty)) {
throw new RuntimeException("[${releaseVersionProperty}] and [${nextVersionProperty}] project properties are mandatory.")
}
}
}

task verifySnapshotDependencies {
tasks.register('verifySnapshotDependencies') {
description 'Fails the build if there are SNAPSHOT dependencies.'
group 'Release'
}

verifySnapshotDependencies {
doLast {
configurations.each { configuration ->
configuration.dependencies.each { dependency ->
Expand All @@ -67,12 +61,9 @@ def commitChangedFiles(String commitMessage) {
}
}

task tagRelease {
tasks.register('tagRelease') {
description 'Creates a tag in SCM for the release.'
group 'Release'
}

tagRelease {
doLast {
gitRepo.tag.add {
name = "javacc-gradle-plugin-${releaseVersion}"
Expand Down Expand Up @@ -174,8 +165,9 @@ writeNextVersion.mustRunAfter tagRelease

copyNextBuildFile.mustRunAfter writeNextVersion

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

0 comments on commit 403a6d6

Please sign in to comment.