Skip to content

Commit

Permalink
Add our recipe model and remove OpenRewrite from CLI dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesbusy committed Dec 12, 2024
1 parent 637caf6 commit 9647b0d
Show file tree
Hide file tree
Showing 25 changed files with 196 additions and 42 deletions.
14 changes: 4 additions & 10 deletions plugin-modernizer-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@
<groupId>org.openrewrite</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- We only use the Recipe class and Xml.Tag from the CLI -->
<!-- TODO : We should be able to break this dependencies by introducing our own interfaces -->
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-core</artifactId>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-xml</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import io.jenkins.tools.pluginmodernizer.core.config.Config;
import io.jenkins.tools.pluginmodernizer.core.impl.PluginModernizer;
import io.jenkins.tools.pluginmodernizer.core.model.ModernizerException;
import io.jenkins.tools.pluginmodernizer.core.model.Recipe;
import java.util.ArrayList;
import org.openrewrite.Recipe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import io.jenkins.tools.pluginmodernizer.core.config.Config;
import io.jenkins.tools.pluginmodernizer.core.impl.PluginModernizer;
import io.jenkins.tools.pluginmodernizer.core.model.ModernizerException;
import io.jenkins.tools.pluginmodernizer.core.model.Recipe;
import java.util.ArrayList;
import org.openrewrite.Recipe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.jenkins.tools.pluginmodernizer.cli.converter;

import io.jenkins.tools.pluginmodernizer.core.config.Settings;
import org.openrewrite.Recipe;
import io.jenkins.tools.pluginmodernizer.core.model.Recipe;
import picocli.CommandLine;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.jenkins.tools.pluginmodernizer.core.model.Plugin;
import io.jenkins.tools.pluginmodernizer.core.model.Recipe;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import org.openrewrite.Recipe;

public class Config {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package io.jenkins.tools.pluginmodernizer.core.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import io.jenkins.tools.pluginmodernizer.core.model.ModernizerException;
import io.jenkins.tools.pluginmodernizer.core.model.Plugin;
import io.jenkins.tools.pluginmodernizer.core.model.Recipe;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.Recipe;
import org.openrewrite.config.YamlResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -115,9 +117,16 @@ private Settings() {}

// Get recipes module
try (InputStream inputStream = Settings.class.getResourceAsStream("/" + Settings.RECIPE_DATA_YAML_PATH)) {
YamlResourceLoader yamlResourceLoader =
new YamlResourceLoader(inputStream, URI.create(Settings.RECIPE_DATA_YAML_PATH), new Properties());
AVAILABLE_RECIPES = yamlResourceLoader.listRecipes().stream().toList();
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
YAMLFactory yamlFactory = new YAMLFactory();
YAMLParser yamlParser = yamlFactory.createParser(inputStream);
List<Recipe> recipes = new ArrayList<>();
while (yamlParser.nextToken() != null) {
Recipe recipe = mapper.readValue(yamlParser, Recipe.class);
recipes.add(recipe);
}
AVAILABLE_RECIPES = recipes;

} catch (IOException e) {
LOG.error("Error reading recipes", e);
throw new ModernizerException("Error reading recipes", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,27 @@ private static class TagExtractor extends MavenIsoVisitor<ExecutionContext> {
*/
private final List<MetadataFlag> flags = new ArrayList<>();

/**
* Convert an OpenRewrite XML tag to a metadata XML tag.
* @param tag OpenRewrite XML tag
* @return metadata XML tag
*/
private MetadataXmlTag convertToMetadataXmlTag(Xml.Tag tag) {
MetadataXmlTag metadataXmlTag = new MetadataXmlTag();
metadataXmlTag.setName(tag.getName());
metadataXmlTag.setValue(tag.getValue());
metadataXmlTag.setChildren(tag.getChildren().stream()
.map(this::convertToMetadataXmlTag)
.collect(Collectors.toList()));
return metadataXmlTag;
}

@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = super.visitTag(tag, ctx);
MetadataXmlTag metadataXmlTag = convertToMetadataXmlTag(tag);
List<MetadataFlag> newFlags = Arrays.stream(MetadataFlag.values())
.filter(flag -> flag.isApplicable(tag))
.filter(flag -> flag.isApplicable(metadataXmlTag))
.toList();
flags.addAll(newFlags);
if (!newFlags.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Optional;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import org.openrewrite.xml.tree.Xml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -35,7 +34,6 @@ public enum MetadataFlag {
if ("repositories".equals(tag.getName())) {
return tag.getChildren().stream()
.filter(c -> "repository".equals(c.getName()))
.map(Xml.Tag.class::cast)
.map(r -> r.getChildValue("url").orElseThrow())
.allMatch(url -> url.startsWith("https"));
}
Expand All @@ -51,7 +49,6 @@ public enum MetadataFlag {
if ("licenses".equals(tag.getName())) {
return tag.getChildren().stream()
.filter(c -> "license".equals(c.getName()))
.map(Xml.Tag.class::cast)
.map(r -> r.getChildValue("name").orElseThrow())
.findAny()
.isPresent();
Expand All @@ -68,7 +65,6 @@ public enum MetadataFlag {
if ("developers".equals(tag.getName())) {
return tag.getChildren().stream()
.filter(c -> "developer".equals(c.getName()))
.map(Xml.Tag.class::cast)
.map(r -> r.getChildValue("id").orElseThrow())
.findAny()
.isPresent();
Expand Down Expand Up @@ -110,7 +106,7 @@ public enum MetadataFlag {
/**
* Function to check if the flag is applicable for the given XML tag
*/
private final Predicate<Xml.Tag> isApplicableTag;
private final Predicate<MetadataXmlTag> isApplicableTag;

/**
* Function to check if the flag is applicable for the given plugin
Expand All @@ -121,7 +117,7 @@ public enum MetadataFlag {
* Constructor
* @param isApplicableTag Predicate to check if the flag is applicable for the given XML tag
*/
MetadataFlag(Predicate<Xml.Tag> isApplicableTag, BiPredicate<Plugin, PluginService> isApplicablePlugin) {
MetadataFlag(Predicate<MetadataXmlTag> isApplicableTag, BiPredicate<Plugin, PluginService> isApplicablePlugin) {
this.isApplicableTag = isApplicableTag;
this.isApplicablePlugin = isApplicablePlugin;
}
Expand All @@ -131,7 +127,7 @@ public enum MetadataFlag {
* @param tag XML tag
* @return true if the flag is applicable
*/
public boolean isApplicable(Xml.Tag tag) {
public boolean isApplicable(MetadataXmlTag tag) {
if (isApplicableTag == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.jenkins.tools.pluginmodernizer.core.extractor;

import java.util.List;
import java.util.Optional;

/**
* To transport information form an OpenRewrite XML tag back to plugin modernizer
* This avoid create a dependency between OpenRewrite internals and Plugin Modernizer
*/
public final class MetadataXmlTag {

private String name;
private Optional<String> value;
private List<MetadataXmlTag> children = List.of();

public MetadataXmlTag() {}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Optional<String> getValue() {
return value;
}

public void setValue(Optional<String> value) {
this.value = value;
}

public List<MetadataXmlTag> getChildren() {
return children;
}

public void setChildren(List<MetadataXmlTag> children) {
this.children = children;
}

public Optional<MetadataXmlTag> getChild(String name) {
return children.stream().filter(c -> c.getName().equals(name)).findFirst();
}

public Optional<String> getChildValue(String name) {
return getChild(name).flatMap(MetadataXmlTag::getValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.jenkins.tools.pluginmodernizer.core.model.ModernizerException;
import io.jenkins.tools.pluginmodernizer.core.model.Plugin;
import io.jenkins.tools.pluginmodernizer.core.model.PluginProcessingException;
import io.jenkins.tools.pluginmodernizer.core.model.Recipe;
import io.jenkins.tools.pluginmodernizer.core.utils.JdkFetcher;
import jakarta.inject.Inject;
import java.io.IOException;
Expand All @@ -22,7 +23,6 @@
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.openrewrite.Recipe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.jenkins.tools.pluginmodernizer.core.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Set;

public class Recipe {

private String name;
private String displayName;
private String description;
private Set<String> tags;

@JsonIgnore
private Object type;

@JsonIgnore
private Object recipeList; // Use Object to avoid mapping complex nested structures.

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Set<String> getTags() {
return tags;
}

public void setTags(Set<String> tags) {
this.tags = tags;
}

public Object getType() {
return type;
}

public void setType(Object type) {
this.type = type;
}

public Object getRecipeList() {
return recipeList;
}

public void setRecipeList(Object recipeList) {
this.recipeList = recipeList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import io.jenkins.tools.pluginmodernizer.core.config.Settings;
import io.jenkins.tools.pluginmodernizer.core.model.ModernizerException;
import io.jenkins.tools.pluginmodernizer.core.model.Plugin;
import io.jenkins.tools.pluginmodernizer.core.model.Recipe;
import java.util.Map;
import org.openrewrite.Recipe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
2 changes: 1 addition & 1 deletion plugin-modernizer-core/src/main/jte/commit.jte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import org.openrewrite.Recipe
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@import static io.jenkins.tools.pluginmodernizer.core.config.Settings.RECIPE_FQDN_PREFIX
@param Plugin plugin
@param Recipe recipe
Expand Down
2 changes: 1 addition & 1 deletion plugin-modernizer-core/src/main/jte/pr-body.jte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import org.openrewrite.Recipe
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@param Plugin plugin
@param Recipe recipe
Hello `${plugin.getName()}` developers!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import org.openrewrite.Recipe
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@param Plugin plugin
@param Recipe recipe
Bump bom to ${plugin.getMetadata().getBomVersion()}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import org.openrewrite.Recipe
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@param Plugin plugin
@param Recipe recipe
Require ${plugin.getMetadata().getJenkinsVersion()} and Java 17
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import org.openrewrite.Recipe
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@param Plugin plugin
@param Recipe recipe
Bump parent pom to ${plugin.getMetadata().getParentVersion()}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import org.openrewrite.Recipe
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@param Plugin plugin
@param Recipe recipe
Require ${plugin.getMetadata().getJenkinsVersion()}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import org.openrewrite.Recipe
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@param Plugin plugin
@param Recipe recipe
Require ${plugin.getMetadata().getJenkinsVersion()}
Loading

0 comments on commit 9647b0d

Please sign in to comment.