Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RecipeDescriptor instead of raw Jackson #71

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.jenkins.tools.pluginmodernizer.core.config.Config;
import io.jenkins.tools.pluginmodernizer.core.config.Settings;
import io.jenkins.tools.pluginmodernizer.core.model.RecipeDescriptor;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
Expand All @@ -32,7 +32,6 @@
public class MavenInvoker {

private static final Logger LOG = LoggerFactory.getLogger(MavenInvoker.class);
private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());

private final Config config;

Expand Down Expand Up @@ -84,11 +83,11 @@ private List<String> createGoalsList() throws IOException {
goals.add("org.openrewrite.maven:rewrite-maven-plugin:" + Settings.MAVEN_REWRITE_PLUGIN_VERSION + ":" + mode);

try (InputStream inputStream = getClass().getResourceAsStream("/" + Settings.RECIPE_DATA_YAML_PATH)) {
ArrayNode recipesNode = (ArrayNode) objectMapper.readTree(inputStream);
List<RecipeDescriptor> recipeDescriptors = new YAMLMapper().readValue(inputStream, new TypeReference<List<RecipeDescriptor>>() {});

List<String> recipes = config.getRecipes();
List<String> activeRecipes = getActiveRecipes(recipes, recipesNode);
List<String> recipeArtifactCoordinates = getRecipeArtifactCoordinates(recipes, recipesNode);
List<String> activeRecipes = getActiveRecipes(recipes, recipeDescriptors);
List<String> recipeArtifactCoordinates = getRecipeArtifactCoordinates(recipes, recipeDescriptors);

if (activeRecipes.isEmpty()) {
return null;
Expand All @@ -101,30 +100,20 @@ private List<String> createGoalsList() throws IOException {
return goals;
}

private List<String> getActiveRecipes(List<String> recipes, ArrayNode recipesNode) {
List<String> activeRecipes = new ArrayList<>();
for (String recipe : recipes) {
for (JsonNode recipeNode : recipesNode) {
if (recipeNode.get("name").asText().equals(recipe)) {
activeRecipes.add(recipeNode.get("fqcn").asText());
break;
}
}
}
return activeRecipes;
private List<String> getActiveRecipes(List<String> recipes, List<RecipeDescriptor> recipeDescriptors) {
return recipes.stream()
.flatMap(recipe -> recipeDescriptors.stream()
.filter(descriptor -> descriptor.name().equals(recipe))
.map(RecipeDescriptor::fqcn))
.collect(Collectors.toList());
}

private List<String> getRecipeArtifactCoordinates(List<String> recipes, ArrayNode recipesNode) {
List<String> recipeArtifactCoordinates = new ArrayList<>();
for (String recipe : recipes) {
for (JsonNode recipeNode : recipesNode) {
if (recipeNode.get("name").asText().equals(recipe)) {
recipeArtifactCoordinates.add(recipeNode.get("artifactCoordinates").asText());
break;
}
}
}
return recipeArtifactCoordinates;
private List<String> getRecipeArtifactCoordinates(List<String> recipes, List<RecipeDescriptor> recipeDescriptors) {
return recipes.stream()
.flatMap(recipe -> recipeDescriptors.stream()
.filter(descriptor -> descriptor.name().equals(recipe))
.map(RecipeDescriptor::artifactCoordinates))
.collect(Collectors.toList());
}

private void invokeGoals(String plugin, String pluginPath, List<String> goals) {
Expand Down
Loading