Skip to content

Commit

Permalink
MavenDependency trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jul 12, 2024
1 parent 38d9981 commit c24e992
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.openrewrite.java.MethodMatcher;

public class Traits {
private Traits() {
}

public static Literal.Matcher literal() {
return new Literal.Matcher();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.Getter;
import lombok.Value;
import org.openrewrite.Cursor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.maven.tree.Dependency;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.trait.Trait;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

import java.util.List;
import java.util.Map;

import static org.openrewrite.internal.StringUtils.matchesGlob;

@Value
public class MavenDependency implements Trait<Xml.Tag> {
private static final XPathMatcher DEPENDENCY_MATCHER = new XPathMatcher("/project/dependencies/dependency");
private static final XPathMatcher PROFILE_DEPENDENCY_MATCHER = new XPathMatcher("/project/profiles/profile/dependencies/dependency");

Cursor cursor;

@Getter
ResolvedDependency resolvedDependency;

public static class Matcher extends MavenTraitMatcher<MavenDependency> {
@Nullable
protected String groupId;

@Nullable
protected String artifactId;

public Matcher groupId(@Nullable String groupId) {
this.groupId = groupId;
return this;
}

public Matcher artifactId(@Nullable String artifactId) {
this.artifactId = artifactId;
return this;
}

@Override
protected @Nullable MavenDependency 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 (!"dependency".equals(tag.getName()) ||
(!DEPENDENCY_MATCHER.matches(cursor) &&
!PROFILE_DEPENDENCY_MATCHER.matches(cursor))) {
return null;
}

Map<Scope, List<ResolvedDependency>> dependencies = getResolutionResult(cursor).getDependencies();
for (Scope scope : Scope.values()) {
if (dependencies.containsKey(scope)) {
for (ResolvedDependency resolvedDependency : dependencies.get(scope)) {
if ((groupId == null || matchesGlob(resolvedDependency.getGroupId(), groupId)) &&
(artifactId == null || matchesGlob(resolvedDependency.getArtifactId(), artifactId))) {
String scopeName = tag.getChildValue("scope").orElse(null);
Scope tagScope = scopeName != null ? Scope.fromName(scopeName) : null;
if (tagScope == null && artifactId != null) {
tagScope = getResolutionResult(cursor).getPom().getManagedScope(
groupId,
artifactId,
tag.getChildValue("type").orElse(null),
tag.getChildValue("classifier").orElse(null)
);
}
if (tagScope == null) {
tagScope = Scope.Compile;
}
Dependency req = resolvedDependency.getRequested();
String reqGroup = req.getGroupId();
if ((reqGroup == null || reqGroup.equals(tag.getChildValue("groupId").orElse(null))) &&
req.getArtifactId().equals(tag.getChildValue("artifactId").orElse(null)) &&
scope == tagScope) {
return new MavenDependency(cursor, resolvedDependency);
}
}
}
}
}
}

return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.openrewrite.Cursor;
import org.openrewrite.SourceFile;
import org.openrewrite.maven.MavenVisitor;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.trait.SimpleTraitMatcher;
import org.openrewrite.trait.Trait;
import org.openrewrite.xml.tree.Xml;

import java.util.concurrent.atomic.AtomicReference;

public abstract class MavenTraitMatcher<U extends Trait<?>> extends SimpleTraitMatcher<U> {

protected MavenResolutionResult getResolutionResult(Cursor cursor) {
AtomicReference<MavenResolutionResult> mrr = new AtomicReference<>();
new MavenVisitor<Integer>() {
@Override
public Xml visitDocument(Xml.Document document, Integer integer) {
mrr.set(getResolutionResult());
return document;
}
}.visit(cursor.firstEnclosing(SourceFile.class), 0);
return mrr.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020 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.
*/
@NonNullApi
@NonNullFields
package org.openrewrite.maven.trait;

import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.internal.lang.NonNullFields;
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.mavenDependency;

public class MavenDependencyTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(RewriteTest.toRecipe(() -> mavenDependency().asVisitor(dep ->
SearchResult.found(dep.getTree(), dep.getResolvedDependency().getGav().toString()))));
}

@Test
void dependency() {
rewriteRun(
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
</dependency>
</dependencies>
</project>
""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<!--~~(com.google.guava:guava:28.2-jre)~~>--><dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
</dependency>
</dependencies>
</project>
"""
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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;

public class Traits {
private Traits() {
}

public static MavenDependency.Matcher mavenDependency() {
return new MavenDependency.Matcher();
}
}

0 comments on commit c24e992

Please sign in to comment.