Preference setting for white-listing recipes
This commit is contained in:
@@ -167,6 +167,10 @@ public class BootJavaConfig implements InitializingBean {
|
||||
public Set<String> getRecipeDirectories() {
|
||||
return settings.getStringSet("boot-java", "rewrite", "scan-directories");
|
||||
}
|
||||
|
||||
public Set<String> getRecipesFilters() {
|
||||
return settings.getStringSet("boot-java", "rewrite", "recipe-filters");
|
||||
}
|
||||
|
||||
public Set<String> getRecipeFiles() {
|
||||
return settings.getStringSet("boot-java", "rewrite", "scan-files");
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.openrewrite.config.Environment;
|
||||
@@ -23,10 +24,21 @@ import org.openrewrite.config.OptionDescriptor;
|
||||
import org.openrewrite.config.RecipeDescriptor;
|
||||
|
||||
public class RecipesDescriptionGenerator {
|
||||
|
||||
private static final Set<String> TOP_LEVEL_RECIPES = Set.of(
|
||||
"org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration",
|
||||
"org.openrewrite.java.spring.boot2.SpringBoot2BestPractices",
|
||||
"org.openrewrite.java.spring.boot2.SpringBoot1To2Migration",
|
||||
"org.openrewrite.java.testing.junit5.JUnit5BestPractices",
|
||||
"org.openrewrite.java.testing.junit5.JUnit4to5Migration",
|
||||
"org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_6",
|
||||
"org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0"
|
||||
);
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String s = Environment.builder().scanRuntimeClasspath().build().listRecipeDescriptors().stream()
|
||||
.filter(d -> RewriteRecipeRepository.TOP_LEVEL_RECIPES.contains(d.getName()))
|
||||
.filter(d -> TOP_LEVEL_RECIPES.contains(d.getName()))
|
||||
.map(d -> convertToMarkdown(d, 1))
|
||||
.collect(Collectors.joining("\n\n"));
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -107,17 +108,8 @@ public class RewriteRecipeRepository implements ApplicationContextAware {
|
||||
|
||||
private Set<String> scanFiles = Collections.emptySet();
|
||||
private Set<String> scanDirs = Collections.emptySet();
|
||||
|
||||
static final Set<String> TOP_LEVEL_RECIPES = Set.of(
|
||||
"org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration",
|
||||
"org.openrewrite.java.spring.boot2.SpringBoot2BestPractices",
|
||||
"org.openrewrite.java.spring.boot2.SpringBoot1To2Migration",
|
||||
"org.openrewrite.java.testing.junit5.JUnit5BestPractices",
|
||||
"org.openrewrite.java.testing.junit5.JUnit4to5Migration",
|
||||
"org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_6",
|
||||
"org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0"
|
||||
);
|
||||
|
||||
private Set<String> recipeFilters = Collections.emptySet();
|
||||
|
||||
private static Gson serializationGson = new GsonBuilder()
|
||||
.registerTypeAdapter(Duration.class, new DurationTypeConverter())
|
||||
.create();
|
||||
@@ -134,6 +126,7 @@ public class RewriteRecipeRepository implements ApplicationContextAware {
|
||||
server.doOnInitialized(() -> {
|
||||
this.scanDirs = config.getRecipeDirectories();
|
||||
this.scanFiles = config.getRecipeFiles();
|
||||
this.recipeFilters = config.getRecipesFilters();
|
||||
load().thenAccept(v -> registerCommands());
|
||||
config.addListener(l -> {
|
||||
if (!scanDirs.equals(config.getRecipeDirectories())
|
||||
@@ -144,6 +137,11 @@ public class RewriteRecipeRepository implements ApplicationContextAware {
|
||||
scanFiles = config.getRecipeFiles();
|
||||
load();
|
||||
}
|
||||
Set<String> recipeFilterFromConfig = config.getRecipesFilters();
|
||||
if (!recipeFilters.equals(recipeFilterFromConfig)) {
|
||||
recipeFilters = recipeFilterFromConfig;
|
||||
updateGlobalCommandRecipes();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -176,19 +174,9 @@ public class RewriteRecipeRepository implements ApplicationContextAware {
|
||||
}
|
||||
recipes.put(r.getName(), r);
|
||||
|
||||
if (TOP_LEVEL_RECIPES.contains(r.getName()) || r.getName().startsWith("rewrite.test.")
|
||||
|| r.getName().startsWith("org.rewrite.java.security")
|
||||
|| r.getName().startsWith("org.springframework.rewrite.test")) {
|
||||
Validated validation = Validated.invalid(null, null, null);
|
||||
try {
|
||||
validation = r.validate();
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
if (validation.isValid()) {
|
||||
globalCommandRecipes.add(r);
|
||||
}
|
||||
}
|
||||
if (isAcceptableGlobalCommandRecipe(r)) {
|
||||
globalCommandRecipes.add(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
javaProblemDescriptors.addAll(env.listProblemDescriptors());
|
||||
@@ -201,6 +189,45 @@ public class RewriteRecipeRepository implements ApplicationContextAware {
|
||||
}
|
||||
}
|
||||
|
||||
private void updateGlobalCommandRecipes() {
|
||||
globalCommandRecipes.clear();
|
||||
for (Recipe r : recipes.values()) {
|
||||
if (isAcceptableGlobalCommandRecipe(r)) {
|
||||
globalCommandRecipes.add(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAcceptableGlobalCommandRecipe(Recipe r) {
|
||||
for (String filter : recipeFilters) {
|
||||
if (!filter.isBlank()) {
|
||||
// Check if wild-card character present
|
||||
if (filter.indexOf('*') < 0) {
|
||||
// No wild-card - direct equality
|
||||
if (filter.equals(r.getName())) {
|
||||
return isRecipeValid(r);
|
||||
}
|
||||
} else {
|
||||
// Wild-card present - convert to regular expression
|
||||
if (Pattern.matches(filter.replaceAll("\\*", "\\.*"), r.getName())) {
|
||||
return isRecipeValid(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isRecipeValid(Recipe r) {
|
||||
Validated validation = Validated.invalid(null, null, null);
|
||||
try {
|
||||
validation = r.validate();
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
return validation.isValid();
|
||||
}
|
||||
|
||||
private StsEnvironment createRewriteEnvironment() {
|
||||
StsEnvironment.Builder builder = (StsEnvironment.Builder) StsEnvironment.builder().scanRuntimeClasspath();
|
||||
for (String p : scanFiles) {
|
||||
|
||||
Reference in New Issue
Block a user