Skip to content

Commit

Permalink
Update for compatibility with Rewrite 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Aug 17, 2020
1 parent 8a91b21 commit 9003c91
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 52 deletions.
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ java {
val plugin: Configuration by configurations.creating

configurations.getByName("compileOnly").extendsFrom(plugin)
val rewriteVersion = "3.0.3"
val rewriteVersion = "4.0.0"

dependencies {
plugin("org.openrewrite:rewrite-java:$rewriteVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@
import org.openrewrite.config.YamlResourceLoader;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.style.ImportLayoutStyle;
import org.openrewrite.java.tree.J;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

Expand All @@ -53,8 +50,8 @@ public abstract class AbstractRewriteTask extends DefaultTask implements Rewrite
private String metricsUri;
private String metricsUsername;
private String metricsPassword;
private final List<GradleProfileConfiguration> profiles = new ArrayList<>();
private final SortedSet<String> activeProfiles = new TreeSet<>(Collections.singletonList("default"));
private final List<GradleRecipeConfiguration> recipes = new ArrayList<>();
private final SortedSet<String> activeRecipes = new TreeSet<>();
private final SortedSet<String> includes = new TreeSet<>();
private final SortedSet<String> excludes = new TreeSet<>();
private FileCollection sources = null;
Expand Down Expand Up @@ -87,13 +84,13 @@ public void setResources(FileCollection resources) {
* The dependencies required to compile the java source files in #sources
*/
@Input
public List<GradleProfileConfiguration> getProfiles() {
return profiles;
public List<GradleRecipeConfiguration> getRecipes() {
return recipes;
}

@Input
public SortedSet<String> getActiveProfiles() {
return activeProfiles;
public SortedSet<String> getActiveRecipes() {
return activeRecipes;
}

@Input
Expand Down Expand Up @@ -127,13 +124,13 @@ protected RefactorPlan plan() {
).scanResources()
.scanUserHome();

getProfiles().forEach(profile -> plan.loadProfile(profile.toProfileConfiguration()));
getRecipes().forEach(recipe -> plan.loadRecipe(recipe.toRecipeConfiguration()));

File rewriteConfig = getExtension().getConfigFile();
if(rewriteConfig != null && rewriteConfig.exists()) {
try (FileInputStream is = new FileInputStream(rewriteConfig)) {
YamlResourceLoader resourceLoader = new YamlResourceLoader(is);
plan.loadProfiles(resourceLoader);
plan.loadRecipes(resourceLoader);
plan.loadVisitors(resourceLoader);
} catch (IOException e) {
throw new RuntimeException("Unable to load rewrite configuration", e);
Expand All @@ -148,8 +145,8 @@ protected Collection<Change> listChanges() {
MeterRegistry meterRegistry = meterRegistryProvider.registry();

RefactorPlan plan = plan();
Set<String> profiles = getActiveProfiles();
Collection<RefactorVisitor<?>> visitors = plan.visitors(profiles);
Set<String> recipes = getActiveRecipes();
Collection<RefactorVisitor<?>> visitors = plan.visitors(recipes);

List<SourceFile> sourceFiles = new ArrayList<>();
List<Path> sourcePaths = getJavaSources().getFiles().stream()
Expand All @@ -162,7 +159,7 @@ protected Collection<Change> listChanges() {

Path projectDir = getProject().getProjectDir().toPath();

ImportLayoutStyle importLayoutStyle = plan.style(ImportLayoutStyle.class, profiles);
ImportLayoutStyle importLayoutStyle = plan.style(ImportLayoutStyle.class, recipes);
sourceFiles.addAll(JavaParser.fromJavaVersion()
.importStyle(importLayoutStyle)
.classpath(dependencyPaths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package org.openrewrite.gradle;

import org.openrewrite.config.ProfileConfiguration;
import org.openrewrite.config.RecipeConfiguration;

import java.util.*;

import static java.util.stream.Collectors.toMap;

public class GradleProfileConfiguration {
public class GradleRecipeConfiguration {

public String name;

Expand All @@ -31,15 +31,15 @@ public class GradleProfileConfiguration {

public final List<GradleProfileProperty> configure = new ArrayList<>();

public ProfileConfiguration toProfileConfiguration() {
ProfileConfiguration profile = new ProfileConfiguration();
public RecipeConfiguration toRecipeConfiguration() {
RecipeConfiguration recipe = new RecipeConfiguration();

profile.setName(name);
profile.setInclude(include);
profile.setExclude(exclude);
profile.setConfigure(configure.stream()
recipe.setName(name);
recipe.setInclude(include);
recipe.setExclude(exclude);
recipe.setConfigure(configure.stream()
.collect(toMap(prop -> prop.visitor + "." + prop.key, prop -> prop.value)));

return profile;
return recipe;
}
}
30 changes: 15 additions & 15 deletions plugin/src/main/java/org/openrewrite/gradle/RewriteExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class RewriteExtension extends CodeQualityExtension {
private static final String magicalMetricsLogString = "LOG";

private boolean showViolations = true;
private final List<String> activeProfiles = new ArrayList<>();
private final List<String> activeRecipes = new ArrayList<>();
private Project project;
private File configFile;
private final List<GradleProfileConfiguration> profiles = new ArrayList<>();
private final List<GradleRecipeConfiguration> recipes = new ArrayList<>();
private String metricsUri = magicalMetricsLogString;

@SuppressWarnings("unused")
Expand Down Expand Up @@ -72,27 +72,27 @@ public void setMetricsUri(String value) {
}


public void activeProfile(String... profiles) {
activeProfiles.addAll(List.of(profiles));
public void activeRecipe(String... profiles) {
activeRecipes.addAll(List.of(profiles));
}

public void clearActiveProfiles() {
activeProfiles.clear();
public void clearActiveRecipes() {
activeRecipes.clear();
}
public void setActiveProfile(List<String> activeProfiles) {
this.activeProfiles.clear();
this.activeProfiles.addAll(activeProfiles);
public void setActiveRecipes(List<String> activeProfiles) {
this.activeRecipes.clear();
this.activeRecipes.addAll(activeProfiles);
}
public List<String> getActiveProfiles() {
return List.copyOf(activeProfiles);
public List<String> getActiveRecipes() {
return activeRecipes;
}

public void profile(GradleProfileConfiguration ... profiles) {
this.profiles.addAll(List.of(profiles));
public void recipe(GradleRecipeConfiguration... profiles) {
this.recipes.addAll(List.of(profiles));
}

public List<GradleProfileConfiguration> getProfiles() {
return List.copyOf(profiles);
public List<GradleRecipeConfiguration> getRecipes() {
return List.copyOf(recipes);
}

}
14 changes: 9 additions & 5 deletions plugin/src/main/java/org/openrewrite/gradle/RewriteFixTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public void execute() {

if(!changes.isEmpty()) {
for(Change change : changes) {
getLog().warn("Changes have been made to " +
change.getOriginal().getSourcePath() + " by: ");
if (change.getOriginal() != null) {
getLog().warn("Changes have been made to " +
change.getOriginal().getSourcePath() + " by: ");
}
for(String rule : change.getRulesThatMadeChanges()) {
log.warn(" " + rule);
}
Expand All @@ -54,9 +56,11 @@ public void execute() {

try {
for(Change change : changes) {
try(BufferedWriter sourceFileWriter = Files.newBufferedWriter(
getProject().getProjectDir().toPath().resolve(change.getOriginal().getSourcePath()))) {
sourceFileWriter.write(change.getFixed().print());
if (change.getOriginal() != null) {
try(BufferedWriter sourceFileWriter = Files.newBufferedWriter(
getProject().getProjectDir().toPath().resolve(change.getOriginal().getSourcePath()))) {
sourceFileWriter.write(change.getFixed().print());
}
}
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void apply(Project project) {
rewriteFixTask.setJavaSources(sourceSet.getAllJava());
rewriteFixTask.setDependencies(sourceSet.getCompileClasspath());
rewriteFixTask.setResources(sourceSet.getResources().getSourceDirectories());
rewriteFixTask.getActiveProfiles().addAll(extension.getActiveProfiles());
rewriteFixTask.getProfiles().addAll(extension.getProfiles());
rewriteFixTask.getActiveRecipes().addAll(extension.getActiveRecipes());
rewriteFixTask.getRecipes().addAll(extension.getRecipes());
});

String compileTaskName = sourceSet.getCompileTaskName("java");
Expand All @@ -68,8 +68,8 @@ public void apply(Project project) {
rewriteWarnTask.setJavaSources(sourceSet.getAllJava());
rewriteWarnTask.setDependencies(sourceSet.getCompileClasspath());
rewriteWarnTask.setResources(sourceSet.getResources().getSourceDirectories());
rewriteWarnTask.getActiveProfiles().addAll(extension.getActiveProfiles());
rewriteWarnTask.getProfiles().addAll(extension.getProfiles());
rewriteWarnTask.getActiveRecipes().addAll(extension.getActiveRecipes());
rewriteWarnTask.getRecipes().addAll(extension.getRecipes());
});
TaskProvider<?> checkTaskProvider = tasks.named("check");
checkTaskProvider.configure(checkTask -> checkTask.dependsOn(rewriteWarnTaskProvider));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class RewritePluginTest extends RewriteTestBase {

String rewriteYamlText = """\
---
type: beta.openrewrite.org/v1/visitor
type: openrewrite.org/v1beta/visitor
name: org.openrewrite.gradle.SayHello
visitors:
- org.openrewrite.java.ChangeMethodName:
method: org.openrewrite.gradle.HelloWorld sayGoodbye()
name: sayHello
---
type: beta.openrewrite.org/v1/profile
name: testProfile
type: openrewrite.org/v1beta/recipe
name: org.openrewrite.testProfile
include:
- 'org.openrewrite.gradle.SayHello'
""".stripIndent()
Expand All @@ -41,7 +41,7 @@ class RewritePluginTest extends RewriteTestBase {
rewrite {
configFile = "rewrite-config.yml"
activeProfile("testProfile")
activeRecipe("org.openrewrite.testProfile")
}
""".stripIndent()
String HelloWorldJavaBeforeRefactor = """\
Expand Down

0 comments on commit 9003c91

Please sign in to comment.