diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PomParser.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/MetadataCollector.java similarity index 70% rename from plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PomParser.java rename to plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/MetadataCollector.java index ca6af1bb..5e1f8ee6 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PomParser.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/MetadataCollector.java @@ -1,5 +1,7 @@ package io.jenkins.tools.pluginmodernizer.core.extractor; +import static java.util.Objects.requireNonNull; + import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; @@ -7,9 +9,13 @@ import java.util.Optional; import com.google.gson.Gson; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.openrewrite.ExecutionContext; -import org.openrewrite.Recipe; +import org.openrewrite.ScanningRecipe; +import org.openrewrite.SourceFile; +import org.openrewrite.Tree; import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.lang.Nullable; import org.openrewrite.marker.Markers; import org.openrewrite.maven.MavenIsoVisitor; import org.openrewrite.maven.tree.MavenResolutionResult; @@ -19,24 +25,48 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class PomParser extends Recipe { +public class MetadataCollector extends ScanningRecipe { - private static final Logger LOG = LoggerFactory.getLogger(PomParser.class); + private static final Logger LOG = LoggerFactory.getLogger(MetadataCollector.class); PluginMetadata pluginMetadata = PluginMetadata.getInstance(); @Override public String getDisplayName() { - return "Pom Parser"; + return "Plugin metadata extractor"; } @Override public String getDescription() { - return "Extracts Metadata from pom file."; + return "Extracts metadata from plugin."; + } + + public static class Metadata { + boolean hasJenkinsfile = false; + } + + @Override + public Metadata getInitialValue(ExecutionContext ctx) { + return new Metadata(); + } + + @Override + public TreeVisitor getScanner(Metadata acc) { + return new TreeVisitor() { + @SuppressFBWarnings(value = "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE", justification = "false postive") + @Override + public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { + SourceFile sourceFile = (SourceFile) requireNonNull(tree); + if (sourceFile.getSourcePath().endsWith("Jenkinsfile")) { + acc.hasJenkinsfile = true; + } + return tree; + } + }; } @Override - public TreeVisitor getVisitor() { + public TreeVisitor getVisitor(Metadata acc) { return new MavenIsoVisitor<>() { @Override public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { @@ -61,6 +91,7 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { pluginMetadata.setHasDevelopersTag(tagExtractor.hasDevelopersTag()); pluginMetadata.setLicensed(!pom.getLicenses().isEmpty()); pluginMetadata.setUsesHttps(tagExtractor.usesHttps()); + pluginMetadata.setHasJenkinsfile(acc.hasJenkinsfile); try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("target/pluginMetadata.json"), StandardCharsets.UTF_8)) { Gson gson = new Gson(); diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PluginMetadata.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PluginMetadata.java index e1eaeea2..ce367946 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PluginMetadata.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/extractor/PluginMetadata.java @@ -12,6 +12,7 @@ public class PluginMetadata { private boolean hasDevelopersTag; private boolean hasJavaLevel; private boolean usesHttps; + private boolean hasJenkinsfile; private List dependencies; private String jenkinsVersion; private Parent pluginParent; @@ -72,6 +73,14 @@ public void setUsesHttps(boolean usesHttps) { this.usesHttps = usesHttps; } + public boolean hasJenkinsfile() { + return hasJenkinsfile; + } + + public void setHasJenkinsfile(boolean hasJenkinsfile) { + this.hasJenkinsfile = hasJenkinsfile; + } + public List getDependencies() { return dependencies; } diff --git a/plugin-modernizer-core/src/main/resources/recipe_data.yaml b/plugin-modernizer-core/src/main/resources/recipe_data.yaml index 6923e33c..7d83b679 100644 --- a/plugin-modernizer-core/src/main/resources/recipe_data.yaml +++ b/plugin-modernizer-core/src/main/resources/recipe_data.yaml @@ -1,6 +1,6 @@ recipes: - PomParser: - fqcn: "io.jenkins.tools.pluginmodernizer.core.extractor.PomParser" + FetchMetadata: + fqcn: "io.jenkins.tools.pluginmodernizer.core.extractor.MetadataCollector" artifactCoordinates: "io.jenkins.plugin-modernizer:plugin-modernizer-core:999999-SNAPSHOT" AddPluginsBom: fqcn: "org.openrewrite.jenkins.AddPluginsBom" diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/extractor/PomParserTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/extractor/MetadataCollectorTest.java similarity index 98% rename from plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/extractor/PomParserTest.java rename to plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/extractor/MetadataCollectorTest.java index 43addf0d..8af154eb 100644 --- a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/extractor/PomParserTest.java +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/extractor/MetadataCollectorTest.java @@ -16,10 +16,10 @@ -public class PomParserTest implements RewriteTest { +public class MetadataCollectorTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new PomParser()); + spec.recipe(new MetadataCollector()); } @Test