Skip to content

Commit

Permalink
Improve robustness of task configuration. Early realization of lazily…
Browse files Browse the repository at this point in the history
… evaluated tasks should no longer present correctness issues
  • Loading branch information
sambsnyd committed Jan 5, 2021
1 parent 4b6b545 commit 6420b11
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.SourceSet;
import org.openrewrite.Refactor;
import org.openrewrite.properties.PropertiesParser;
import org.openrewrite.xml.XmlParser;
Expand All @@ -46,83 +47,58 @@

public abstract class AbstractRewriteTask extends DefaultTask implements RewriteTask {

@Internal
protected abstract Logger getLog();

private String metricsUri;
private String metricsUsername;
private String metricsPassword;
private final List<GradleRecipeConfiguration> recipes = new ArrayList<>();
private final SortedSet<String> activeRecipes = new TreeSet<>();
private final SortedSet<String> activeStyles = new TreeSet<>();
private final SortedSet<String> includes = new TreeSet<>();
private final SortedSet<String> excludes = new TreeSet<>();
private FileCollection sources = null;
private FileCollection dependencies = null;
private FileCollection resources = null;
private MeterRegistry registry;

private final SourceSet sourceSet;
private final RewriteExtension extension;

public AbstractRewriteTask(SourceSet sourceSet, RewriteExtension extension) {
this.sourceSet = sourceSet;
this.extension = extension;
}

@Internal
protected abstract Logger getLog();

/**
* The Java source files that will be subject to rewriting
*/
@InputFiles
public FileCollection getJavaSources() {
return sources;
return sourceSet.getAllJava();
}

public void setJavaSources(FileCollection sources) {
this.sources = sources;
@Override
public void setMeterRegistry(MeterRegistry registry) {
this.registry = registry;
}


@InputFiles
public FileCollection getResources() {
return resources;
return sourceSet.getResources().getSourceDirectories();
}

public void setResources(FileCollection resources) {
this.resources = resources;
}

/**
* The dependencies required to compile the java source files in #sources
*/
@Input
public List<GradleRecipeConfiguration> getRecipes() {
return recipes;
return extension.getRecipes();
}

@Input
public SortedSet<String> getActiveRecipes() {
return activeRecipes;
return new TreeSet<>(extension.getActiveRecipes());
}

@Input
public SortedSet<String> getActiveStyles() {
return activeStyles;
}

@Input
public SortedSet<String> getIncludes() {
return includes;
}

@Input
public SortedSet<String> getExcludes() {
return excludes;
return new TreeSet<>(extension.getActiveStyles());
}

@InputFiles
public FileCollection getDependencies() {
return dependencies;
}

public void setDependencies(FileCollection dependencies) {
this.dependencies = dependencies;
}


private RewriteExtension getExtension() {
return getProject().getExtensions().findByType(RewriteExtension.class);
return sourceSet.getCompileClasspath();
}

protected Environment environment() {
Expand All @@ -137,7 +113,7 @@ protected Environment environment() {
)
.scanUserHome();

File rewriteConfig = getExtension().getConfigFile();
File rewriteConfig = extension.getConfigFile();
if (rewriteConfig != null && rewriteConfig.exists()) {
try (FileInputStream is = new FileInputStream(rewriteConfig)) {
Map<Object, Object> gradleProps = getProject().getProperties().entrySet().stream()
Expand Down Expand Up @@ -166,12 +142,12 @@ protected ChangesContainer listChanges() {

Environment env = environment();
Set<String> recipes = getActiveRecipes();
if (activeRecipes == null || activeRecipes.isEmpty()) {
if (recipes == null || recipes.isEmpty()) {
return new ChangesContainer(baseDir, emptyList());
}
Collection<RefactorVisitor<?>> visitors = env.visitors(recipes);
if(visitors.size() == 0) {
getLog().info("Could not find any Rewrite visitors matching active recipe(s): " + String.join(", ", activeRecipes) + ". " +
getLog().info("Could not find any Rewrite visitors matching active recipe(s): " + String.join(", ", recipes) + ". " +
"Double check that you have taken a dependency on the jar containing these recipes.");
return new ChangesContainer(baseDir, emptyList());
}
Expand All @@ -186,7 +162,7 @@ protected ChangesContainer listChanges() {
.collect(toList());

sourceFiles.addAll(JavaParser.fromJavaVersion()
.styles(env.styles(activeStyles))
.styles(env.styles(getActiveStyles()))
.classpath(dependencyPaths)
.logCompilationWarningsAndErrors(false)
.meterRegistry(meterRegistry)
Expand Down Expand Up @@ -229,13 +205,6 @@ protected ChangesContainer listChanges() {
}
}

private MeterRegistry registry;

@Override
public void setMeterRegistry(MeterRegistry registry) {
this.registry = registry;
}

public static class ChangesContainer {
final Path projectRoot;
final List<Change> generated = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
import org.openrewrite.Environment;
import org.openrewrite.Recipe;

import javax.inject.Inject;
import java.util.Map;
import java.util.Set;

Expand All @@ -31,6 +33,13 @@ protected Logger getLog() {
return log;
}

@Inject
public RewriteDiscoverTask(SourceSet sourceSet, RewriteExtension extension) {
super(sourceSet, extension);
setGroup("rewrite");
setDescription("Lists all available recipes and their visitors within the " + sourceSet.getName() + " SourceSet");
}

@TaskAction
public void execute() {
Environment env = environment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
public class RewriteExtension extends CodeQualityExtension {
private static final String magicalMetricsLogString = "LOG";

private boolean showViolations = true;
private final List<String> activeRecipes = new ArrayList<>();
private final List<String> activeStyles = new ArrayList<>();
private Project project;
Expand All @@ -41,14 +40,6 @@ public RewriteExtension(Project project) {
configFile = project.getRootProject().file("rewrite.yml");
}

public boolean getShowViolations() {
return showViolations;
}

public void setShowViolations(boolean showViolations) {
this.showViolations = showViolations;
}

public void setConfigFile(File configFile) {
this.configFile = configFile;
}
Expand Down
12 changes: 9 additions & 3 deletions plugin/src/main/java/org/openrewrite/gradle/RewriteFixTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@

import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.impldep.org.apache.maven.plugin.MojoExecutionException;
import org.openrewrite.Change;

import javax.inject.Inject;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;

public class RewriteFixTask extends AbstractRewriteTask {

private static final Logger log = Logging.getLogger(RewriteFixTask.class);

@Inject
public RewriteFixTask(SourceSet sourceSet, RewriteExtension extension) {
super(sourceSet, extension);
setGroup("rewrite");
setDescription("Apply the active refactoring recipes to sources within the " + sourceSet.getName() + " SourceSet");
}

@Override
protected Logger getLog() {
return log;
Expand Down
39 changes: 3 additions & 36 deletions plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.openrewrite.gradle;

import org.gradle.api.DefaultTask;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
Expand Down Expand Up @@ -62,54 +61,22 @@ public void apply(Project project) {
sourceSets.configureEach(sourceSet -> {

String rewriteFixTaskName = "rewriteFix" + sourceSet.getName().substring(0, 1).toUpperCase() + sourceSet.getName().substring(1);
TaskProvider<RewriteFixTask> rewriteFixProvider = tasks.register(rewriteFixTaskName, RewriteFixTask.class);

rewriteFixProvider.configure(rewriteFixTask -> {
rewriteFixTask.setGroup("rewrite");
rewriteFixTask.setDescription("Apply the active refactoring recipes to sources within the " + sourceSet.getName() + " SourceSet");
rewriteFixTask.setJavaSources(sourceSet.getAllJava());
rewriteFixTask.setDependencies(sourceSet.getCompileClasspath());
rewriteFixTask.setResources(sourceSet.getResources().getSourceDirectories());
rewriteFixTask.getActiveRecipes().addAll(extension.getActiveRecipes());
rewriteFixTask.getActiveStyles().addAll(extension.getActiveStyles());
rewriteFixTask.getRecipes().addAll(extension.getRecipes());
});
TaskProvider<RewriteFixTask> rewriteFixProvider = tasks.register(rewriteFixTaskName, RewriteFixTask.class, sourceSet, extension);
rewriteFixAllProvider.configure(it -> it.dependsOn(rewriteFixProvider));

String rewriteDiscoverTaskName = "rewriteDiscover" + sourceSet.getName().substring(0, 1).toUpperCase() + sourceSet.getName().substring(1);
TaskProvider<RewriteDiscoverTask> rewriteDiscoverProvider = tasks.register(rewriteDiscoverTaskName, RewriteDiscoverTask.class);
rewriteDiscoverProvider.configure(rewriteDiscoverTask -> {
rewriteDiscoverTask.setGroup("rewrite");
rewriteDiscoverTask.setDescription("Lists all available recipes and their visitors within the " + sourceSet.getName() + " SourceSet");
rewriteDiscoverTask.setJavaSources(sourceSet.getAllJava());
rewriteDiscoverTask.setDependencies(sourceSet.getCompileClasspath());
rewriteDiscoverTask.setResources(sourceSet.getResources().getSourceDirectories());
rewriteDiscoverTask.getActiveRecipes().addAll(extension.getActiveRecipes());
rewriteDiscoverTask.getActiveStyles().addAll(extension.getActiveStyles());
rewriteDiscoverTask.getRecipes().addAll(extension.getRecipes());
});
tasks.register(rewriteDiscoverTaskName, RewriteDiscoverTask.class, sourceSet, extension);

String compileTaskName = sourceSet.getCompileTaskName("java");
TaskProvider<?> compileTaskProvider = tasks.named(compileTaskName);
compileTaskProvider.configure(compileTask -> compileTask.mustRunAfter(rewriteFixProvider));

String rewriteWarnTaskName = "rewriteWarn" + sourceSet.getName().substring(0, 1).toUpperCase() + sourceSet.getName().substring(1);
TaskProvider<RewriteWarnTask> rewriteWarnProvider = tasks.register(rewriteWarnTaskName, RewriteWarnTask.class);
rewriteWarnProvider.configure(rewriteWarnTask -> {
rewriteWarnTask.setGroup("rewrite");
rewriteWarnTask.setDescription("Dry run the active refactoring recipes to sources within the " + sourceSet.getName() + "SourceSet. No changes will be made.");
rewriteWarnTask.setJavaSources(sourceSet.getAllJava());
rewriteWarnTask.setDependencies(sourceSet.getCompileClasspath());
rewriteWarnTask.setResources(sourceSet.getResources().getSourceDirectories());
rewriteWarnTask.getActiveRecipes().addAll(extension.getActiveRecipes());
rewriteWarnTask.getActiveStyles().addAll(extension.getActiveStyles());
rewriteWarnTask.getRecipes().addAll(extension.getRecipes());
});
TaskProvider<RewriteWarnTask> rewriteWarnProvider = tasks.register(rewriteWarnTaskName, RewriteWarnTask.class, sourceSet, extension);
rewriteWarnAllProvider.configure(it -> it.dependsOn(rewriteWarnProvider));
});
}


// void configureMetrics(RewriteTask task) {
// Project project = task.getProject();
//
Expand Down
14 changes: 9 additions & 5 deletions plugin/src/main/java/org/openrewrite/gradle/RewriteWarnTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@

import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
import org.openrewrite.Change;
import org.openrewrite.java.tree.J;

import java.util.Collection;
import java.util.List;
import javax.inject.Inject;

public class RewriteWarnTask extends AbstractRewriteTask{
private static final Logger log = Logging.getLogger(RewriteWarnTask.class);

// Visible for testing
public static String reviewAndCommitChanges = "Run rewriteFix to apply the fixes. Afterwards, review and commit the changes.";
@Inject
public RewriteWarnTask(SourceSet sourceSet, RewriteExtension extension) {
super(sourceSet, extension);
setGroup("rewrite");
setDescription("Dry run the active refactoring recipes to sources within the " + sourceSet.getName() + "SourceSet. No changes will be made.");
}

@Override
protected Logger getLog() {
return log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class RewritePluginTest extends RewriteTestBase {
}
""".stripIndent()


def "rewriteWarn task will run as part of a normal Java Build"() {
given:
projectDir.newFile("settings.gradle")
Expand Down

0 comments on commit 6420b11

Please sign in to comment.