Skip to content

Commit

Permalink
Plugin invoker requires Maven plugin version (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabapp2 committed Jun 6, 2024
1 parent 1ff0cdf commit 7569c18
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class RewriteMavenPlugin

private boolean debug;

private String rewriteMavenPluginVersion;

@Override
public RewriteMavenPluginBuilder.FinalizingBuilder withDependencies(String... dependencies) {
this.dependencies = Arrays.asList(dependencies);
Expand Down Expand Up @@ -113,29 +115,36 @@ public PluginInvocationResult onDir(Path baseDir) {
builder.withMemory(minMemory, maxMemory);
}
MavenInvocationResult result = execute(baseDir, debug, debugConfig, builder.build(), goal, recipes,
dependencies);
dependencies, rewriteMavenPluginVersion);

return new PluginInvocationResult(result.getExitCode() != 0 ? false : true, result.getCapturedLines());
}

static MavenInvocationResult execute(Path baseDir, boolean debug, DebugConfig debugConfig, BuildConfig buildConfig,
Goal openRewriteGoal, List<String> recipes1, List<String> dependencies1) {
String openRewriteCommand = renderOpenRewriteCommand(recipes1, dependencies1, openRewriteGoal);
Goal openRewriteGoal, List<String> recipes1, List<String> dependencies1, String rewriteMavenPluginVersion) {
String openRewriteCommand = renderOpenRewriteCommand(recipes1, dependencies1, openRewriteGoal,
rewriteMavenPluginVersion);
List<String> goals = List.of("--fail-at-end", openRewriteCommand);
return MavenInvoker.runGoals(baseDir, debugConfig, debug, buildConfig, goals);
}

private static String renderOpenRewriteCommand(List<String> recipeNames, List<String> dependencies,
Goal rewritePluginGoal) {
Goal rewritePluginGoal, String rewriteMavenPluginVersion) {
StringBuilder sb = new StringBuilder();
sb.append("org.openrewrite.maven:rewrite-maven-plugin:").append(rewritePluginGoal.getMaven()).append(" ");
sb.append("org.openrewrite.maven:rewrite-maven-plugin:")
.append(rewriteMavenPluginVersion)
.append(":")
.append(rewritePluginGoal.getMaven())
.append(" ");
String recipesList = recipeNames.stream().collect(Collectors.joining(","));
sb.append("-Drewrite.activeRecipes=").append(recipesList);
if (!dependencies.isEmpty()) {
String dependenciesList = dependencies.stream().collect(Collectors.joining(","));
sb.append(" ").append("-Drewrite.recipeArtifactCoordinates=").append(dependenciesList);
}
return sb.toString();

String cmd = sb.toString();
return cmd;
}

@Override
Expand All @@ -145,6 +154,12 @@ public RewriteMavenPluginBuilder.FinalizingBuilder withMemory(String minMemory,
return this;
}

@Override
public RewriteMavenPluginBuilder.FinalizingBuilder withMavenPluginVersion(String mavenPluginVersion) {
this.rewriteMavenPluginVersion = mavenPluginVersion;
return this;
}

public enum Goal {

RUN("run"), RUN_NO_FORK("runNoFork"), DRY_RUN("dryRun"), DRY_RUN_NO_FORK("dryRunNoFork"), DISCOVER("discover"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ public interface RewriteMavenPluginBuilder {

public interface Recipes {

FinalizingBuilder recipes(String... recipeNames);
MavenPLuginVersionBuilder recipes(String... recipeNames);

}

interface FinalizingBuilder {
interface MavenPLuginVersionBuilder {

FinalizingBuilder withMavenPluginVersion(String mavenPluginVersion);

}

interface FinalizingBuilder extends MavenPLuginVersionBuilder {

FinalizingBuilder withDependencies(String... dependencies);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class RewriteMavenPluginTest {
void simpleProjectSimpleConfig() {
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();

PluginInvocationResult result = RewriteMavenPlugin.run().recipes(RECIPES).onDir(baseDir);
PluginInvocationResult result = RewriteMavenPlugin.run()
.recipes(RECIPES)
.withMavenPluginVersion("5.32.1")
.onDir(baseDir);

String out = result.capturedOutput();
assertThat(result.success()).isTrue();
Expand All @@ -66,7 +69,10 @@ void simpleProjectSimpleConfig() {
@DisplayName("builder runNoFork")
void simpleProjectSimpleConfigRunNoFork() {
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
PluginInvocationResult result = RewriteMavenPlugin.runNoFork().recipes(RECIPES).onDir(baseDir);
PluginInvocationResult result = RewriteMavenPlugin.runNoFork()
.recipes(RECIPES)
.withMavenPluginVersion("5.32.1")
.onDir(baseDir);
String out = result.capturedOutput();
assertThat(result.success()).isTrue();
assertTasksExecuted(out, "runNoFork");
Expand All @@ -77,7 +83,10 @@ void simpleProjectSimpleConfigRunNoFork() {
@DisplayName("builder dryRun")
void simpleProjectSimpleConfigDryRun() {
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
PluginInvocationResult result = RewriteMavenPlugin.dryRun().recipes(RECIPES).onDir(baseDir);
PluginInvocationResult result = RewriteMavenPlugin.dryRun()
.recipes(RECIPES)
.withMavenPluginVersion("5.32.1")
.onDir(baseDir);
String out = result.capturedOutput();
assertThat(result.success()).isTrue();
assertTasksExecuted(out, tasksOf(DEFAULT_GOALS, "dryRun"));
Expand All @@ -88,7 +97,10 @@ void simpleProjectSimpleConfigDryRun() {
@DisplayName("builder dryRunNoFork")
void simpleProjectSimpleConfigDryRunNoFork() {
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
PluginInvocationResult result = RewriteMavenPlugin.dryRunNoFork().recipes(RECIPES).onDir(baseDir);
PluginInvocationResult result = RewriteMavenPlugin.dryRunNoFork()
.recipes(RECIPES)
.withMavenPluginVersion("5.32.1")
.onDir(baseDir);
String out = result.capturedOutput();
assertThat(result.success()).isTrue();
assertTasksExecuted(out, "dryRunNoFork");
Expand All @@ -99,7 +111,10 @@ void simpleProjectSimpleConfigDryRunNoFork() {
@DisplayName("builder discover")
void simpleProjectSimpleConfigDiscover() {
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
PluginInvocationResult result = RewriteMavenPlugin.discover().recipes(RECIPES).onDir(baseDir);
PluginInvocationResult result = RewriteMavenPlugin.discover()
.recipes(RECIPES)
.withMavenPluginVersion("5.32.1")
.onDir(baseDir);
String out = result.capturedOutput();
assertThat(result.success()).isTrue();
assertTasksExecuted(out, "discover");
Expand All @@ -110,7 +125,10 @@ void simpleProjectSimpleConfigDiscover() {
@DisplayName("builder cyclonedx")
void simpleProjectSimpleConfigCyclonedx() {
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
PluginInvocationResult result = RewriteMavenPlugin.cyclonedx().recipes(RECIPES).onDir(baseDir);
PluginInvocationResult result = RewriteMavenPlugin.cyclonedx()
.recipes(RECIPES)
.withMavenPluginVersion("5.32.1")
.onDir(baseDir);
String out = result.capturedOutput();
System.out.println(out);
assertThat(result.success()).isTrue();
Expand All @@ -125,6 +143,7 @@ void simpleProjectFullConfig() {
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
PluginInvocationResult result = RewriteMavenPlugin.run()
.recipes(EXTENDED_RECIPES)
.withMavenPluginVersion("5.32.1")
.withDependencies(EXTENDED_REFCIPES_DEPS)
.withDebugger(port, false)
.withDebug()
Expand All @@ -145,9 +164,10 @@ void executeDryRun() {

int port = TestSocketUtils.findAvailableTcpPort();
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
String rewriteMavenPluginVersion = "5.32.1";
MavenInvocationResult result = RewriteMavenPlugin.execute(baseDir, true, DebugConfig.from(port, false),
BuildConfig.builder().withMemory("1G", "4G").build(), RewriteMavenPlugin.Goal.DRY_RUN,
Arrays.asList(RECIPES), List.of());
Arrays.asList(RECIPES), List.of(), rewriteMavenPluginVersion);
String out = result.getCapturedLines();
assertDebugConfig(out, port, false);
assertMemory(out, "1G", "4G");
Expand Down Expand Up @@ -179,9 +199,10 @@ void executeRunNoFork() {
@DisplayName("execute discover")
void executeDiscover() {
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
String rewriteMavenPluginVersion = "5.32.1";
MavenInvocationResult result = RewriteMavenPlugin.execute(baseDir, false, DebugConfig.disabled(),
BuildConfig.fromDefault(), RewriteMavenPlugin.Goal.DISCOVER, Arrays.asList(EXTENDED_RECIPES),
List.of(EXTENDED_REFCIPES_DEPS));
List.of(EXTENDED_REFCIPES_DEPS), rewriteMavenPluginVersion);
String out = result.getCapturedLines();
assertThat(result.getExitCode()).isEqualTo(0);
assertTasksExecuted(out, "discover");
Expand All @@ -192,17 +213,20 @@ void executeDiscover() {
@DisplayName("execute cylonedx")
void executeCyclonedx() {
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
String rewriteMavenPluginVersion = "5.32.1";
MavenInvocationResult result = RewriteMavenPlugin.execute(baseDir, false, DebugConfig.disabled(),
BuildConfig.fromDefault(), RewriteMavenPlugin.Goal.CYCLONEDX, Arrays.asList(EXTENDED_RECIPES),
List.of(EXTENDED_REFCIPES_DEPS));
List.of(EXTENDED_REFCIPES_DEPS), rewriteMavenPluginVersion);
String out = result.getCapturedLines();
assertThat(result.getExitCode()).isEqualTo(0);
assertTasksExecuted(out, "cyclonedx");
}

private void verify(Path baseDir, RewriteMavenPlugin.Goal runNoFork, String expectedGoal) {
String rewriteMavenPluginVersion = "5.32.1";
MavenInvocationResult result = RewriteMavenPlugin.execute(baseDir, false, DebugConfig.disabled(),
BuildConfig.fromDefault(), runNoFork, Arrays.asList(EXTENDED_RECIPES), List.of(EXTENDED_REFCIPES_DEPS));
BuildConfig.fromDefault(), runNoFork, Arrays.asList(EXTENDED_RECIPES), List.of(EXTENDED_REFCIPES_DEPS),
rewriteMavenPluginVersion);
String out = result.getCapturedLines();
assertThat(result.getExitCode()).isEqualTo(0);
assertRecipesExecuted(out, EXTENDED_RECIPES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class RewritePlugin implements OpenRewritePluginBuilder.GradlePluginVersi

private String gradlePluginVersion;

private String mavenPluginVersion;

private List<String> recipes = new ArrayList<>();

private boolean debug;
Expand Down Expand Up @@ -80,6 +82,12 @@ public OpenRewritePluginBuilder.Recipes gradlePluginVersion(String pluginVersion
return this;
}

@Override
public OpenRewritePluginBuilder.GradlePluginVersion mavenPluginVersion(String mavenPluginVersion) {
this.mavenPluginVersion = mavenPluginVersion;
return this;
}

@Override
public OpenRewritePluginBuilder.FinalizingBuilder recipes(String... recipes) {
this.recipes = Arrays.asList(recipes);
Expand Down Expand Up @@ -151,7 +159,8 @@ private PluginInvocationResult executeMaven(Path baseDir) {
default -> builder = RewriteMavenPlugin.run();
}
RewriteMavenPluginBuilder.FinalizingBuilder finalizingBuilder = builder
.recipes(this.recipes.toArray(String[]::new));
.recipes(this.recipes.toArray(String[]::new))
.withMavenPluginVersion("5.32.1");
if (minMemory != null) {
finalizingBuilder = finalizingBuilder.withMemory(minMemory, maxMemory);
}
Expand Down Expand Up @@ -226,6 +235,8 @@ public interface GradlePluginVersion {

Recipes gradlePluginVersion(String pluginVersion);

GradlePluginVersion mavenPluginVersion(String s);

}

public interface Recipes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ void simpleMavenFullApi() {
int port = TestSocketUtils.findAvailableTcpPort();
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
PluginInvocationResult result = RewritePlugin.run()
.mavenPluginVersion("5.32.1")
.gradlePluginVersion("6.10.0")
.recipes(EXTENDED_RECIPES)
.dependencies(EXTENDED_RECIPES_DEPS)
Expand Down

0 comments on commit 7569c18

Please sign in to comment.