33import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
44import io .jenkins .tools .pluginmodernizer .core .model .JDK ;
55import io .jenkins .tools .pluginmodernizer .core .utils .JsonUtils ;
6- import java .util .ArrayList ;
76import java .util .Arrays ;
87import java .util .HashMap ;
9- import java .util .HashSet ;
8+ import java .util .LinkedHashSet ;
9+ import java .util .LinkedList ;
1010import java .util .List ;
1111import java .util .Map ;
1212import java .util .Optional ;
1313import java .util .Set ;
1414import java .util .stream .Collectors ;
1515import java .util .stream .Stream ;
16- import org .openrewrite .ExecutionContext ;
17- import org .openrewrite .FindSourceFiles ;
18- import org .openrewrite .Preconditions ;
19- import org .openrewrite .ScanningRecipe ;
20- import org .openrewrite .SourceFile ;
21- import org .openrewrite .Tree ;
22- import org .openrewrite .TreeVisitor ;
16+ import org .openrewrite .*;
2317import org .openrewrite .groovy .GroovyIsoVisitor ;
2418import org .openrewrite .groovy .tree .G ;
2519import org .openrewrite .java .tree .Expression ;
2620import org .openrewrite .java .tree .J ;
2721import org .openrewrite .marker .Markers ;
22+ import org .openrewrite .marker .SearchResult ;
2823import org .openrewrite .maven .MavenIsoVisitor ;
2924import org .openrewrite .maven .tree .MavenResolutionResult ;
3025import org .openrewrite .maven .tree .Parent ;
@@ -44,6 +39,27 @@ public class MetadataCollector extends ScanningRecipe<MetadataCollector.Metadata
4439 */
4540 private static final Logger LOG = LoggerFactory .getLogger (MetadataCollector .class );
4641
42+ public MetadataCollector () {}
43+
44+ public MetadataCollector (boolean force , boolean search ) {
45+ this .force = force ;
46+ this .search = search ;
47+ }
48+
49+ @ Option (
50+ displayName = "Force collection of metadata. Default to false" ,
51+ required = false ,
52+ description = "Force the collection of metadata. If false will reuse the existing metadata if any." ,
53+ example = "true" )
54+ boolean force = false ;
55+
56+ @ Option (
57+ displayName = "Search for metadata Default to false" ,
58+ required = false ,
59+ description = "Search for metadata. If true will search for metadata." ,
60+ example = "true" )
61+ boolean search = false ;
62+
4763 @ Override
4864 public String getDisplayName () {
4965 return "Plugin metadata extractor" ;
@@ -58,12 +74,12 @@ public String getDescription() {
5874 * Accumulator to store metadata.
5975 */
6076 public static class MetadataAccumulator {
61- private final List <ArchetypeCommonFile > commonFiles = new ArrayList <>();
62- private final Set <MetadataFlag > flags = new HashSet <>();
63- private final Set <JDK > jdkVersions = new HashSet <>();
77+ private final List <ArchetypeCommonFile > commonFiles = new LinkedList <>();
78+ private final Set <MetadataFlag > flags = new LinkedHashSet <>();
79+ private final Set <JDK > jdkVersions = new LinkedHashSet <>();
6480
6581 public List <ArchetypeCommonFile > getCommonFiles () {
66- return commonFiles ;
82+ return commonFiles . stream (). sorted (). collect ( Collectors . toList ()) ;
6783 }
6884
6985 public Set <JDK > getJdkVersions () {
@@ -82,7 +98,7 @@ public Set<MetadataFlag> getFlags() {
8298 return flags ;
8399 }
84100
85- public void addFlags (List <MetadataFlag > flags ) {
101+ public void addFlags (Set <MetadataFlag > flags ) {
86102 this .flags .addAll (flags );
87103 }
88104 }
@@ -211,6 +227,21 @@ public TreeVisitor<?, ExecutionContext> getVisitor(MetadataAccumulator acc) {
211227 @ Override
212228 public Xml .Document visitDocument (Xml .Document document , ExecutionContext ctx ) {
213229
230+ final PluginMetadata pluginMetadata = new PluginMetadata ();
231+
232+ if (!force ) {
233+ // Check if metadata already exists
234+ if (pluginMetadata .exists ()) {
235+ PluginMetadata refreshedMetadata = pluginMetadata .refresh ();
236+ LOG .info ("Metadata already exists, skipping metadata extraction" );
237+ LOG .info ("Metadata: {}" , JsonUtils .toJson (refreshedMetadata ));
238+ if (search ) {
239+ return SearchResult .found (document , JsonUtils .toJson (refreshedMetadata ));
240+ }
241+ return document .withMarkers (document .getMarkers ().add (refreshedMetadata ));
242+ }
243+ }
244+
214245 // Ensure maven resolution result is present
215246 Markers markers = document .getMarkers ();
216247 Optional <MavenResolutionResult > mavenResolutionResult = markers .findFirst (MavenResolutionResult .class );
@@ -236,7 +267,6 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
236267 properties .remove ("basedir" );
237268
238269 // Construct the plugin metadata
239- PluginMetadata pluginMetadata = new PluginMetadata ();
240270 pluginMetadata .setPluginName (pom .getName ());
241271 Parent parent = pom .getParent ();
242272 if (parent != null ) {
@@ -259,8 +289,10 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
259289 pluginMetadata .save ();
260290 LOG .debug ("Plugin metadata written to {}" , pluginMetadata .getRelativePath ());
261291 LOG .debug (JsonUtils .toJson (pluginMetadata ));
262-
263- return document ;
292+ if (search ) {
293+ return SearchResult .found (document , JsonUtils .toJson (pluginMetadata ));
294+ }
295+ return document .withMarkers (document .getMarkers ().add (pluginMetadata ));
264296 }
265297 };
266298 }
@@ -273,7 +305,7 @@ private static class TagExtractor extends MavenIsoVisitor<ExecutionContext> {
273305 /**
274306 * Detected flag
275307 */
276- private final List <MetadataFlag > flags = new ArrayList <>();
308+ private final Set <MetadataFlag > flags = new LinkedHashSet <>();
277309
278310 /**
279311 * Convert an OpenRewrite XML tag to a metadata XML tag.
@@ -311,7 +343,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
311343 * Get the flags for this visitor.
312344 * @return flags for this visitor
313345 */
314- public List <MetadataFlag > getFlags () {
346+ public Set <MetadataFlag > getFlags () {
315347 return flags ;
316348 }
317349 }
0 commit comments