From 5675dd665ce68d2f61b3359213a3a729786fc95a Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 3 Oct 2023 15:03:44 +0200 Subject: [PATCH] Merge NormalizeMavenVariables into PrefixlessExpressions (#3600) Due to overlap, and the latter replacing more instances. --- .../maven/NormalizeMavenVariables.java | 66 ------------------- .../openrewrite/maven/RenamePropertyKey.java | 10 +-- .../main/resources/META-INF/rewrite/maven.yml | 12 ++++ .../maven/NormalizeMavenVariablesTest.java | 63 ------------------ 4 files changed, 14 insertions(+), 137 deletions(-) delete mode 100644 rewrite-maven/src/main/java/org/openrewrite/maven/NormalizeMavenVariables.java delete mode 100644 rewrite-maven/src/test/java/org/openrewrite/maven/NormalizeMavenVariablesTest.java diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/NormalizeMavenVariables.java b/rewrite-maven/src/main/java/org/openrewrite/maven/NormalizeMavenVariables.java deleted file mode 100644 index a0ecfb20a00..00000000000 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/NormalizeMavenVariables.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2023 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.maven; - -import org.openrewrite.ExecutionContext; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; -import org.openrewrite.xml.tree.Xml; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; - -public class NormalizeMavenVariables extends Recipe { - - @Override - public String getDisplayName() { - return "Normalize Maven variables"; - } - - @Override - public String getDescription() { - return "Variables are all referenced by the prefix `project.`. You may also see references with `pom.` as the " + - "prefix, or the prefix omitted entirely - these forms are now deprecated and should not be used."; - } - - @Override - public TreeVisitor getVisitor() { - return new MavenIsoVisitor() { - private final List properties = Arrays.asList( - "basedir", - "groupId", - "artifactId", - "version", - "build.timestamp" - ); - - @Override - public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext executionContext) { - Xml.Tag t = super.visitTag(tag, executionContext); - Optional value = t.getValue(); - if (value.isPresent()) { - String newValue = value - .filter(v -> properties.stream().anyMatch(prop -> v.equals("${" + prop + "}"))) - .map(v -> "${project." + v.substring(2)) - .orElse(value.get()); - return value.get().equals(newValue) ? t : t.withValue(newValue); - } - return t; - } - }; - } -} diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/RenamePropertyKey.java b/rewrite-maven/src/main/java/org/openrewrite/maven/RenamePropertyKey.java index f924d94bdf2..c13065278a7 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/RenamePropertyKey.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/RenamePropertyKey.java @@ -50,13 +50,7 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new MavenVisitor() { - @Override - public Xml visitDocument(Xml.Document document, ExecutionContext executionContext) { - // Scanning every tag's value is not an efficient applicable test, so just accept all maven files - return SearchResult.found(document); - } - }, new MavenIsoVisitor() { + return new MavenIsoVisitor() { final String oldKeyAsProperty = "${" + oldKey + "}"; final String newKeyAsProperty = "${" + newKey + "}"; @@ -76,6 +70,6 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { } return t; } - }); + }; } } diff --git a/rewrite-maven/src/main/resources/META-INF/rewrite/maven.yml b/rewrite-maven/src/main/resources/META-INF/rewrite/maven.yml index 351033dad40..3021cda4ef5 100644 --- a/rewrite-maven/src/main/resources/META-INF/rewrite/maven.yml +++ b/rewrite-maven/src/main/resources/META-INF/rewrite/maven.yml @@ -28,6 +28,18 @@ name: org.openrewrite.maven.cleanup.PrefixlessExpressions displayName: Drop prefixless expressions in POM description: MNG-7404 drops support for prefixless in POMs. This recipe will add the `project.` prefix where missing. recipeList: + - org.openrewrite.maven.RenamePropertyKey: + oldKey: basedir + newKey: project.basedir + - org.openrewrite.maven.RenamePropertyKey: + oldKey: build.timestamp + newKey: project.build.timestamp + - org.openrewrite.maven.RenamePropertyKey: + oldKey: groupId + newKey: project.groupId + - org.openrewrite.maven.RenamePropertyKey: + oldKey: artifactId + newKey: project.artifactId - org.openrewrite.maven.RenamePropertyKey: oldKey: version newKey: project.version diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/NormalizeMavenVariablesTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/NormalizeMavenVariablesTest.java deleted file mode 100644 index 4f4eb973965..00000000000 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/NormalizeMavenVariablesTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2023 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.maven; - -import org.junit.jupiter.api.Test; -import org.openrewrite.DocumentExample; -import org.openrewrite.test.RecipeSpec; -import org.openrewrite.test.RewriteTest; - -import static org.openrewrite.maven.Assertions.pomXml; - -public class NormalizeMavenVariablesTest implements RewriteTest { - - @Override - public void defaults(RecipeSpec spec) { - spec.recipe(new NormalizeMavenVariables()); - } - - @DocumentExample - @Test - void prefixProject() { - rewriteRun( - //language=xml - pomXml( - """ - - 4.0.0 - com.mycompany.app - my-app - 1 - - ${artifactId} - - - """, - """ - - 4.0.0 - com.mycompany.app - my-app - 1 - - ${project.artifactId} - - - """ - ) - ); - } -}