-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #513 from jonesbusy/feature/add-property-comment-v…
…isitor Add visitor to add and remove a comment for properties and `jenkins.baseline` migration
- Loading branch information
Showing
12 changed files
with
1,006 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...c/main/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddBeforePropertyVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.jenkins.tools.pluginmodernizer.core.visitors; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.maven.MavenIsoVisitor; | ||
import org.openrewrite.xml.tree.Content; | ||
import org.openrewrite.xml.tree.Xml; | ||
|
||
/** | ||
* A visitor that add a maven property before another property. | ||
* If the property already exists, it will update its value. | ||
* If the previous property does not exist, the new property will not be added. | ||
*/ | ||
public class AddBeforePropertyVisitor extends MavenIsoVisitor<ExecutionContext> { | ||
|
||
/** | ||
* The previous property name to add the new property before. | ||
*/ | ||
private final String previousPropertyName; | ||
|
||
/** | ||
* The property name to add. | ||
*/ | ||
private final String propertyName; | ||
|
||
/** | ||
* The property value to add. | ||
*/ | ||
private final String propertyValue; | ||
|
||
/** | ||
* Constructor of the visitor. | ||
* @param previousPropertyName The previous property name to add the new property before. | ||
* @param propertyName The property name to add. | ||
* @param propertyValue The property value to add. | ||
*/ | ||
public AddBeforePropertyVisitor(String previousPropertyName, String propertyName, String propertyValue) { | ||
this.previousPropertyName = previousPropertyName; | ||
this.propertyName = propertyName; | ||
this.propertyValue = propertyValue; | ||
} | ||
|
||
@Override | ||
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext executionContext) { | ||
tag = super.visitTag(tag, executionContext); | ||
if (tag.getName().equals("properties")) { | ||
|
||
// Ensure previous | ||
Xml.Tag previousPropertyTag = tag.getChild(previousPropertyName).orElse(null); | ||
if (previousPropertyTag == null) { | ||
return tag; | ||
} | ||
|
||
// Ensure value if exists | ||
Xml.Tag existingPropertyTag = tag.getChild(propertyName).orElse(null); | ||
if (existingPropertyTag != null && existingPropertyTag.getValue().isPresent()) { | ||
// doAfterVisit(new ChangeTagValueVisitor<>(existingPropertyTag, propertyValue)); | ||
return tag; | ||
} | ||
|
||
if (tag.getContent() == null || tag.getContent().isEmpty()) { | ||
return tag; | ||
} | ||
|
||
List<Content> contents = new ArrayList<>(tag.getContent()); | ||
int propertyIndex = contents.indexOf(previousPropertyTag); | ||
|
||
// If there are comments leave them as they are | ||
while (propertyIndex > 0 && contents.get(propertyIndex - 1) instanceof Xml.Comment) { | ||
propertyIndex--; | ||
} | ||
|
||
// Place the tag just before the property or on first element of the sequence | ||
Xml.Tag propertyTag = Xml.Tag.build("<" + propertyName + ">" + propertyValue + "</" + propertyName + ">"); | ||
propertyTag = propertyTag.withPrefix(previousPropertyTag.getPrefix()); | ||
contents.add(propertyIndex, propertyTag); | ||
|
||
tag = tag.withContent(contents); | ||
} | ||
|
||
return tag; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
.../main/java/io/jenkins/tools/pluginmodernizer/core/visitors/AddPropertyCommentVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.jenkins.tools.pluginmodernizer.core.visitors; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Tree; | ||
import org.openrewrite.marker.Markers; | ||
import org.openrewrite.maven.MavenIsoVisitor; | ||
import org.openrewrite.xml.tree.Content; | ||
import org.openrewrite.xml.tree.Xml; | ||
|
||
/** | ||
* A visitor that add a comment before a maven property. | ||
* If a comment already exists, it's updated. | ||
*/ | ||
public class AddPropertyCommentVisitor extends MavenIsoVisitor<ExecutionContext> { | ||
|
||
/** | ||
* The property name to add the comment before. | ||
*/ | ||
private final String propertyName; | ||
|
||
/** | ||
* The comment to add. | ||
*/ | ||
private final String comment; | ||
|
||
/** | ||
* Constructor of the visitor. | ||
* @param propertyName The property name to add the comment before. | ||
* @param comment The comment to add. | ||
*/ | ||
public AddPropertyCommentVisitor(String propertyName, String comment) { | ||
this.propertyName = propertyName; | ||
this.comment = comment; | ||
} | ||
|
||
@Override | ||
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext executionContext) { | ||
tag = super.visitTag(tag, executionContext); | ||
if (tag.getName().equals("properties")) { | ||
|
||
// Ensure property exists | ||
Xml.Tag propertyTag = tag.getChild(propertyName).orElse(null); | ||
if (propertyTag == null) { | ||
return tag; | ||
} | ||
|
||
if (tag.getContent() == null) { | ||
return tag; | ||
} | ||
|
||
List<Content> contents = new ArrayList<>(tag.getContent()); | ||
int propertyIndex = contents.indexOf(propertyTag); | ||
|
||
// Add or update comment | ||
if (propertyTag.getContent() != null) { | ||
boolean containsComment = contents.stream() | ||
.anyMatch(c -> c instanceof Xml.Comment && comment.equals(((Xml.Comment) c).getText())); | ||
|
||
// Add comment if not exists | ||
if (!containsComment) { | ||
|
||
// If there is a comment just before, remove it | ||
if (propertyIndex > 0 && contents.get(propertyIndex - 1) instanceof Xml.Comment xmlComment) { | ||
propertyIndex--; | ||
contents.remove(propertyIndex); | ||
doAfterVisit(new RemovePropertyCommentVisitor(xmlComment.getText())); | ||
} | ||
|
||
Xml.Comment customComment = new Xml.Comment( | ||
Tree.randomId(), contents.get(propertyIndex).getPrefix(), Markers.EMPTY, comment); | ||
contents.add(propertyIndex, customComment); | ||
tag = tag.withContent(contents); | ||
} | ||
} | ||
} | ||
return tag; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...in/java/io/jenkins/tools/pluginmodernizer/core/visitors/RemovePropertyCommentVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.jenkins.tools.pluginmodernizer.core.visitors; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.maven.MavenIsoVisitor; | ||
import org.openrewrite.xml.tree.Content; | ||
import org.openrewrite.xml.tree.Xml; | ||
|
||
public class RemovePropertyCommentVisitor extends MavenIsoVisitor<ExecutionContext> { | ||
|
||
private String comment; | ||
|
||
public RemovePropertyCommentVisitor(String comment) { | ||
this.comment = comment; | ||
} | ||
|
||
@Override | ||
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext executionContext) { | ||
tag = super.visitTag(tag, executionContext); | ||
if (tag.getName().equals("properties")) { | ||
|
||
List<Content> contents = new ArrayList<>(tag.getContent()); | ||
|
||
// Remove the comment if needed | ||
boolean containsComment = contents.stream() | ||
.anyMatch(c -> c instanceof Xml.Comment && comment.equals(((Xml.Comment) c).getText())); | ||
|
||
// Add comment if not exists | ||
if (containsComment) { | ||
for (int i = 0; i < contents.size(); i++) { | ||
if (contents.get(i) instanceof Xml.Comment | ||
&& comment.equals(((Xml.Comment) contents.get(i)).getText())) { | ||
contents.remove(i); | ||
break; | ||
} | ||
} | ||
tag = tag.withContent(contents); | ||
} | ||
} | ||
return tag; | ||
} | ||
} |
Oops, something went wrong.