Skip to content

Commit

Permalink
Added replaceExisting flag + example with plugin groupId
Browse files Browse the repository at this point in the history
  • Loading branch information
DidierLoiseau committed Sep 29, 2024
1 parent 1ab9aab commit 325e2b8
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.intellij.lang.annotations.Language;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.xml.tree.Xml;

Expand All @@ -37,6 +38,12 @@ public class AddOrUpdateChildTag extends Recipe {
@Language("xml")
String newChildTag;

@Option(displayName = "Replace existing child",
description = "Set to `false` to not replace the child tag if it already exists. Defaults to true.",
required = false)
@Nullable
Boolean replaceExisting;

@Override
public String getDisplayName() {
return "Add or update child tag";
Expand All @@ -45,7 +52,7 @@ public String getDisplayName() {
@Override
public String getDescription() {
return "Adds or updates a child element below the parent(s) matching the provided `parentXPath` expression. " +
"If a child with the same name exists, it will be replaced, otherwise a new child will be added. " +
"If a child with the same name already exists, it will be replaced by default. Otherwise, a new child will be added. " +
"This ensures idempotent behaviour.";
}

Expand All @@ -71,7 +78,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (xPathMatcher.matches(getCursor())) {
Xml.Tag newChild = Xml.Tag.build(newChildTag);
return AddOrUpdateChild.addOrUpdateChild(tag, newChild, getCursor().getParentOrThrow());
if (!Boolean.FALSE.equals(replaceExisting) || !tag.getChild(newChild.getName()).isPresent()) {
return AddOrUpdateChild.addOrUpdateChild(tag, newChild, getCursor().getParentOrThrow());
}
}
return super.visitTag(tag, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@
package org.openrewrite.xml;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.NullSource;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.xml.Assertions.xml;

class AddOrUpdateChildTagTest implements RewriteTest {
@Test
void addsTagEverywhereWhenAbsent() {
@ParameterizedTest
@NullSource
@CsvSource("true")
void addsTagEverywhereWhenAbsent(Boolean replaceExisting) {
rewriteRun(spec -> spec.recipe(new AddOrUpdateChildTag(
"/project//plugin[groupId='org.apache.maven.plugins' and artifactId='maven-resources-plugin']" +
"//configuration",
"<skip>true</skip>")),
"<skip>true</skip>",
replaceExisting)),
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -180,12 +186,15 @@ void addsTagEverywhereWhenAbsent() {
);
}

@Test
void updateTagEverywhere() {
@ParameterizedTest
@NullSource
@CsvSource("true")
void updateTagEverywhere(Boolean replaceExisting) {
rewriteRun(spec -> spec.recipe(new AddOrUpdateChildTag(
"/project//plugin[groupId='org.apache.maven.plugins' and artifactId='maven-resources-plugin']" +
"//configuration",
"<skip>true</skip>")),
"<skip>true</skip>",
replaceExisting)),
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -328,11 +337,94 @@ void updateTagEverywhere() {
}

@Test
void dontTouchAnythingElse() {
void dontUpdateIfReplaceIsDisabled() {
rewriteRun(spec -> spec.recipe(new AddOrUpdateChildTag(
"/project//plugin[groupId='org.apache.maven.plugins' and artifactId='maven-resources-plugin']" +
"//configuration",
"<skip>true</skip>")),
"<skip>true</skip>",
false)),
xml(
"""
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<encoding>UTF-8</encoding>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<encoding>UTF-8</encoding>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>profile1</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<encoding>UTF-8</encoding>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<encoding>UTF-8</encoding>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
"""
)
);
}

@ParameterizedTest
@NullSource
@CsvSource("true")
void dontTouchAnythingElse(Boolean replaceExisting) {
rewriteRun(spec -> spec.recipe(new AddOrUpdateChildTag(
"/project//plugin[groupId='org.apache.maven.plugins' and artifactId='maven-resources-plugin']" +
"//configuration",
"<skip>true</skip>",
replaceExisting)),
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -401,4 +493,147 @@ void dontTouchAnythingElse() {
)
);
}

@Test
void addMissingPluginGroupIdExample() {
rewriteRun(spec -> spec.recipe(new AddOrUpdateChildTag(
"plugin",
"<groupId>org.apache.maven.plugins</groupId>",
false)),
xml(
"""
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.41.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>profile1</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.41.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.41.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>profile1</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.41.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
"""
)
);
}
}

0 comments on commit 325e2b8

Please sign in to comment.