Skip to content

Commit

Permalink
MavenPlugin trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jul 16, 2024
1 parent 4299aff commit 7eb376f
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.trait;

import lombok.Value;
import org.openrewrite.Cursor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.trait.Trait;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

import java.util.Optional;

@Value
public class MavenPlugin implements Trait<Xml.Tag> {
static final XPathMatcher PLUGIN_MATCHER = new XPathMatcher("//plugins/plugin");

Cursor cursor;
String groupId;

@Nullable
String artifactId;

public static class Matcher extends MavenTraitMatcher<MavenPlugin> {

@Override
protected @Nullable MavenPlugin test(Cursor cursor) {
Object value = cursor.getValue();
if (value instanceof Xml.Tag) {
Xml.Tag tag = (Xml.Tag) value;

// `XPathMatcher` is still a bit expensive
if (!"plugin".equals(tag.getName()) ||
(!PLUGIN_MATCHER.matches(cursor))) {
return null;
}

return new MavenPlugin(
cursor,
getProperty(cursor, "groupId").orElse("org.apache.maven.plugins"),
getProperty(cursor, "artifactId").orElse(null)
);
}
return null;
}

private Optional<String> getProperty(Cursor cursor, String property) {
Xml.Tag tag = cursor.getValue();
if (getResolutionResult(cursor).getPom().getProperties() != null) {
if (tag.getChildValue(property).isPresent() && tag.getChildValue(property).get().trim().startsWith("${")) {
String propertyKey = tag.getChildValue(property).get().trim();
return Optional.ofNullable(getResolutionResult(cursor).getPom().getValue(propertyKey));
}
}
return tag.getChildValue(property);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.trait;

import org.junit.jupiter.api.Test;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.maven.Assertions.pomXml;
import static org.openrewrite.maven.trait.Traits.mavenPlugin;

public class MavenPluginTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(RewriteTest.toRecipe(() -> mavenPlugin().asVisitor(plugin ->
SearchResult.found(plugin.getTree(), plugin.getGroupId() + ":" + plugin.getArtifactId()))));
}

@Test
void findMavenPlugin() {
rewriteRun(
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bootstrap-maven-plugin</artifactId>
<version>3.0.0.Beta1</version>
</plugin>
</plugins>
</build>
</project>
""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<build>
<plugins>
<!--~~(io.quarkus:quarkus-bootstrap-maven-plugin)~~>--><plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bootstrap-maven-plugin</artifactId>
<version>3.0.0.Beta1</version>
</plugin>
</plugins>
</build>
</project>
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ private Traits() {
public static MavenDependency.Matcher mavenDependency() {
return new MavenDependency.Matcher();
}

public static MavenPlugin.Matcher mavenPlugin() {
return new MavenPlugin.Matcher();
}
}

0 comments on commit 7eb376f

Please sign in to comment.