Skip to content

Commit

Permalink
PlanJavaMigration
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Sep 12, 2024
1 parent 0f9298e commit c91c113
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.search;

import org.openrewrite.*;
import org.openrewrite.java.marker.JavaVersion;
import org.openrewrite.java.migrate.table.JavaVersionMigrationPlan;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.marker.BuildTool;
import org.openrewrite.marker.Markers;

import java.io.File;
import java.util.Collection;
import java.util.Collections;

public class PlanJavaMigration extends ScanningRecipe<JavaVersionMigrationPlan.Row.Builder> {
transient JavaVersionMigrationPlan plan = new JavaVersionMigrationPlan(this);

@Override
public String getDisplayName() {
return "Plan a Java version migration";
}

@Override
public String getDescription() {
return "Study the set of Java versions and associated tools in " +
"use across many repositories.";
}

@Override
public JavaVersionMigrationPlan.Row.Builder getInitialValue(ExecutionContext ctx) {
return JavaVersionMigrationPlan.Row.builder();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(JavaVersionMigrationPlan.Row.Builder acc) {
return new TreeVisitor<Tree, ExecutionContext>() {

@Override
public Tree preVisit(Tree tree, ExecutionContext ctx) {
if (tree instanceof SourceFile) {
File sourceFile = ((SourceFile) tree).getSourcePath().toFile();
if (sourceFile.getName().contains("build.gradle")) {
acc.hasGradleBuild(true);
} else if (sourceFile.getName().contains("pom.xml")) {
acc.hasMavenPom(true);
}
}

if (tree instanceof JavaSourceFile) {
acc.hasJava(true);
Markers markers = tree.getMarkers();
markers.findFirst(JavaVersion.class).ifPresent(javaVersion -> {
acc.sourceCompatibility(javaVersion.getSourceCompatibility());
acc.majorVersionSourceCompatibility(javaVersion.getMajorVersion());
acc.targetCompatibility(javaVersion.getTargetCompatibility());
});
markers.findFirst(BuildTool.class).ifPresent(buildTool -> {
switch (buildTool.getType()) {
case Gradle:
acc.gradleVersion(buildTool.getVersion());
break;
case Maven:
acc.mavenVersion(buildTool.getVersion());
break;
}
});
}
return tree;
}
};
}

@Override
public Collection<? extends SourceFile> generate(JavaVersionMigrationPlan.Row.Builder acc, ExecutionContext ctx) {
plan.insertRow(ctx, acc.build());
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.table;

import lombok.Builder;
import lombok.Value;
import org.openrewrite.Column;
import org.openrewrite.DataTable;
import org.openrewrite.Recipe;

public class JavaVersionMigrationPlan extends DataTable<JavaVersionMigrationPlan.Row> {

public JavaVersionMigrationPlan(Recipe recipe) {
super(
recipe,
"Java version migration plan",
"A per-repository view of the current state of Java versions and associated build tools"
);
}

@Builder(builderClassName = "Builder")
@Value
public static class Row {
@Column(displayName = "Has Java",
description = "Whether this is a Java repository at all.")
boolean hasJava;

@Column(displayName = "Source compatibility",
description = "The source compatibility of the source file.")
String sourceCompatibility;

@Column(displayName = "Major version source compatibility",
description = "The major version.")
Integer majorVersionSourceCompatibility;

@Column(displayName = "Target compatibility",
description = "The target compatibility or `--release` version of the source file.")
String targetCompatibility;

@Column(displayName = "Gradle version",
description = "The version of Gradle in use, if any.")
String gradleVersion;

@Column(displayName = "Has Gradle build",
description = "Whether a build.gradle file exists in the repository.")
Boolean hasGradleBuild;

@Column(displayName = "Maven version",
description = "The version of Maven in use, if any.")
String mavenVersion;

@Column(displayName = "Has Maven pom",
description = "Whether a pom.xml file exists in the repository.")
Boolean hasMavenPom;
}
}

0 comments on commit c91c113

Please sign in to comment.