-
Notifications
You must be signed in to change notification settings - Fork 16
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 #550 from jonesbusy/feature/remove-relative-path-p…
…recondition Remove relativePath precondition
- Loading branch information
Showing
6 changed files
with
148 additions
and
58 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
58 changes: 58 additions & 0 deletions
58
...core/src/main/java/io/jenkins/tools/pluginmodernizer/core/recipes/EnsureRelativePath.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,58 @@ | ||
package io.jenkins.tools.pluginmodernizer.core.recipes; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.maven.MavenIsoVisitor; | ||
import org.openrewrite.xml.tree.Content; | ||
import org.openrewrite.xml.tree.Xml; | ||
|
||
/** | ||
* Ensure the parent pom has a relativePath set to disable local resolution. | ||
*/ | ||
public class EnsureRelativePath extends Recipe { | ||
@Override | ||
public String getDisplayName() { | ||
return "Ensure the parent pom has a relativePath set to disable local resolution"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Ensure the parent pom has a relativePath set to disable local resolution."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new MavenIsoVisitor<>() { | ||
@Override | ||
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { | ||
if (isParentTag()) { | ||
Xml.Tag relativePathTag = Xml.Tag.build("<relativePath />"); | ||
if (tag.getContent() == null) { | ||
return tag; | ||
} | ||
List<Content> contents = new ArrayList<>(tag.getContent()); | ||
// Skip if relativePath is already present | ||
if (contents.stream() | ||
.anyMatch(content -> content instanceof Xml.Tag | ||
&& ((Xml.Tag) content).getName().equals("relativePath"))) { | ||
return tag; | ||
} | ||
Optional<Xml.Tag> maybeChild = tag.getChild("artifactId"); | ||
if (maybeChild.isEmpty()) { | ||
return tag; | ||
} | ||
relativePathTag = | ||
relativePathTag.withPrefix(maybeChild.get().getPrefix()); | ||
|
||
contents.add(relativePathTag); | ||
return tag.withContent(contents); | ||
} | ||
return super.visitTag(tag, ctx); | ||
} | ||
}; | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
.../src/test/java/io/jenkins/tools/pluginmodernizer/core/recipes/EnsureRelativePathTest.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,85 @@ | ||
package io.jenkins.tools.pluginmodernizer.core.recipes; | ||
|
||
import static org.openrewrite.maven.Assertions.pomXml; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
public class EnsureRelativePathTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new EnsureRelativePath()); | ||
} | ||
|
||
@Test | ||
void addRelativePath() { | ||
rewriteRun( | ||
// language=xml | ||
pomXml( | ||
""" | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>plugin</artifactId> | ||
<version>4.88</version> | ||
</parent> | ||
<artifactId>empty</artifactId> | ||
<repositories> | ||
<repository> | ||
<id>repo.jenkins-ci.org</id> | ||
<url>https://repo.jenkins-ci.org/public/</url> | ||
</repository> | ||
</repositories> | ||
</project> | ||
""", | ||
""" | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>plugin</artifactId> | ||
<version>4.88</version> | ||
<relativePath /> | ||
</parent> | ||
<artifactId>empty</artifactId> | ||
<repositories> | ||
<repository> | ||
<id>repo.jenkins-ci.org</id> | ||
<url>https://repo.jenkins-ci.org/public/</url> | ||
</repository> | ||
</repositories> | ||
</project> | ||
""")); | ||
} | ||
|
||
@Test | ||
void keepRelativePath() { | ||
rewriteRun( | ||
// language=xml | ||
pomXml( | ||
""" | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>plugin</artifactId> | ||
<version>4.88</version> | ||
<relativePath/> | ||
</parent> | ||
<artifactId>empty</artifactId> | ||
<repositories> | ||
<repository> | ||
<id>repo.jenkins-ci.org</id> | ||
<url>https://repo.jenkins-ci.org/public/</url> | ||
</repository> | ||
</repositories> | ||
</project> | ||
""")); | ||
} | ||
} |
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