forked from openrewrite/rewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38d9981
commit c24e992
Showing
6 changed files
with
267 additions
and
0 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
109 changes: 109 additions & 0 deletions
109
rewrite-maven/src/main/java/org/openrewrite/maven/trait/MavenDependency.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,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; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
rewrite-maven/src/main/java/org/openrewrite/maven/trait/MavenTraitMatcher.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,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(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
rewrite-maven/src/main/java/org/openrewrite/maven/trait/package-info.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,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; |
69 changes: 69 additions & 0 deletions
69
rewrite-maven/src/test/java/org/openrewrite/maven/trait/MavenDependencyTest.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,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> | ||
""" | ||
) | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
rewrite-maven/src/test/java/org/openrewrite/maven/trait/Traits.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,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(); | ||
} | ||
} |