Skip to content

Commit

Permalink
Add find methods to discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
fabapp2 committed Dec 13, 2023
1 parent 6b01e4f commit 1968302
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.Recipe;
import org.openrewrite.Validated;
import org.openrewrite.config.ClasspathScanningLoader;
Expand All @@ -26,10 +27,8 @@
import org.springframework.rewrite.parsers.ParserProperties;
import org.springframework.rewrite.parsers.RecipeValidationErrorException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.util.*;
import java.util.function.Predicate;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -151,16 +150,47 @@ public RecipeDescriptor findRecipeDescriptor(String anotherDummyRecipe) {
return descriptor;
}

public List<Recipe> findRecipesByTags(String tag) {
ResourceLoader resourceLoader = new ClasspathScanningLoader(new Properties(), new String[] {});
Environment environment = Environment.builder().load(resourceLoader).build();
public List<Recipe> findRecipesByTag(String tag) {
return getFilteredRecipes(r -> r.getTags().contains(tag));
}

List<Recipe> recipes = environment.listRecipes().stream().filter(r -> r.getTags().contains(tag)).toList();
/**
* @param name of the recipe that should be returned.
* @return Optional recipe matching name or empty when no recipe matches.
* @throws IllegalArgumentException when more than one recipe was found.
*/
public Optional<Recipe> findRecipeByName(String name) {
List<Recipe> filteredRecipes = getFilteredRecipes(r -> r.getName().equals(name));
if(filteredRecipes.size() > 1) {
throw new IllegalStateException("Found more than one recipe with name '%s'".formatted(name));
} else if(filteredRecipes.isEmpty()) {
return Optional.empty();
} else {
return Optional.of(filteredRecipes.get(0));
}
}

public Recipe getRecipeByName(String name) {
List<Recipe> filteredRecipes = getFilteredRecipes(r -> r.getName().equals(name));
if(filteredRecipes.size() > 1) {
throw new IllegalArgumentException("Found more than one recipe with name '%s'".formatted(name));
} else if(filteredRecipes.isEmpty()) {
throw new IllegalArgumentException("No recipe found with name '%s'".formatted(name));
} else {
return filteredRecipes.get(0);
}
}


@NotNull
public static List<Recipe> getFilteredRecipes(Predicate<Recipe> filterPredicate) {
ResourceLoader resourceLoader = new ClasspathScanningLoader(new Properties(), new String[] {});
Environment environment = Environment.builder().load(resourceLoader).build();
List<Recipe> recipes = environment.listRecipes().stream().filter(filterPredicate).toList();
return recipes;
}

// class AbstractRewriteMojoHelper extends AbstractRewriteMojo {
// class AbstractRewriteMojoHelper extends AbstractRewriteMojo {
//
// public AbstractRewriteMojoHelper(MavenProject mavenProject) {
// super.project = mavenProject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void loadRecipeFromClasspath2() {
void shouldFindRecipesByTag() {
String tag = "Java";
RewriteRecipeDiscovery sut = buildRecipeDiscovery();
List<Recipe> recipes = sut.findRecipesByTags(tag);
List<Recipe> recipes = sut.findRecipesByTag(tag);

assertThat(recipes).hasSize(1);
assertThat(getRecipeByName(recipes, "io.example.recipes.AnotherDummyRecipe")).isNotNull();
Expand Down

0 comments on commit 1968302

Please sign in to comment.