Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GradleDependency trait #4394

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* 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.gradle.trait;

import lombok.Getter;
import lombok.Value;
import org.openrewrite.Cursor;
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.util.DependencyStringNotationConverter;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.maven.tree.Dependency;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.trait.Trait;

import java.util.List;

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

@Value
public class GradleDependency implements Trait<J.MethodInvocation> {
Cursor cursor;

@Getter
ResolvedDependency resolvedDependency;

public static class Matcher extends GradleTraitMatcher<GradleDependency> {
@Nullable
protected String groupId;

@Nullable
protected String artifactId;

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

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

@Override
protected @Nullable GradleDependency test(Cursor cursor) {
Object object = cursor.getValue();
if (object instanceof J.MethodInvocation) {
J.MethodInvocation methodInvocation = (J.MethodInvocation) object;

GradleProject gradleProject = getGradleProject(cursor);
if (gradleProject == null) {
return null;
}

GradleDependencyConfiguration configuration = gradleProject.getConfiguration(methodInvocation.getSimpleName());
if (configuration == null) {
return null;
}

org.openrewrite.gradle.util.Dependency dependency = null;
Expression argument = methodInvocation.getArguments().get(0);
if (argument instanceof J.Literal || argument instanceof G.GString || argument instanceof G.MapEntry) {
dependency = parseDependency(methodInvocation.getArguments());
} else if (argument instanceof J.MethodInvocation &&
(((J.MethodInvocation) argument).getSimpleName().equals("platform") ||
((J.MethodInvocation) argument).getSimpleName().equals("enforcedPlatform"))) {
dependency = parseDependency(((J.MethodInvocation) argument).getArguments());
}

if (dependency == null) {
return null;
}

if (configuration.isCanBeResolved()) {
for (ResolvedDependency resolvedDependency : configuration.getResolved()) {
if ((groupId == null || matchesGlob(resolvedDependency.getGroupId(), groupId)) &&
(artifactId == null || matchesGlob(resolvedDependency.getArtifactId(), artifactId))) {
Dependency req = resolvedDependency.getRequested();
if ((req.getGroupId() == null || req.getGroupId().equals(dependency.getGroupId())) &&
req.getArtifactId().equals(dependency.getArtifactId())) {
return new GradleDependency(cursor, resolvedDependency);
}
}
}
} else {
for (GradleDependencyConfiguration transitiveConfiguration : gradleProject.configurationsExtendingFrom(configuration, true)) {
if (transitiveConfiguration.isCanBeResolved()) {
for (ResolvedDependency resolvedDependency : transitiveConfiguration.getResolved()) {
if ((groupId == null || matchesGlob(resolvedDependency.getGroupId(), groupId)) &&
(artifactId == null || matchesGlob(resolvedDependency.getArtifactId(), artifactId))) {
Dependency req = resolvedDependency.getRequested();
if ((req.getGroupId() == null || req.getGroupId().equals(dependency.getGroupId())) &&
req.getArtifactId().equals(dependency.getArtifactId())) {
return new GradleDependency(cursor, resolvedDependency);
}
}
}
Comment on lines +104 to +113
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callout: I wasn't quite sure what should happen here, so I opted to just return the first ResolvedDependency encountered.

}
}
}
}

return null;
}

private @Nullable org.openrewrite.gradle.util.Dependency parseDependency(List<Expression> arguments) {
Expression argument = arguments.get(0);
if (argument instanceof J.Literal) {
return DependencyStringNotationConverter.parse((String) ((J.Literal) argument).getValue());
} else if (argument instanceof G.GString) {
G.GString gstring = (G.GString) argument;
List<J> strings = gstring.getStrings();
if (strings.size() >= 2 && strings.get(0) instanceof J.Literal && ((J.Literal) strings.get(0)).getValue() != null) {
return DependencyStringNotationConverter.parse((String) ((J.Literal) strings.get(0)).getValue());
}
} else if (argument instanceof G.MapEntry) {
String group = null;
String artifact = null;

for (Expression e : arguments) {
if (!(e instanceof G.MapEntry)) {
continue;
}
G.MapEntry arg = (G.MapEntry) e;
if (!(arg.getKey() instanceof J.Literal) || !(arg.getValue() instanceof J.Literal)) {
continue;
}
J.Literal key = (J.Literal) arg.getKey();
J.Literal value = (J.Literal) arg.getValue();
if (!(key.getValue() instanceof String) || !(value.getValue() instanceof String)) {
continue;
}
String keyValue = (String) key.getValue();
if ("group".equals(keyValue)) {
group = (String) value.getValue();
} else if ("name".equals(keyValue)) {
artifact = (String) value.getValue();
}
}

if (group == null || artifact == null) {
return null;
}

return new org.openrewrite.gradle.util.Dependency(group, artifact, null, null, null);
}

return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.gradle.trait;

import org.openrewrite.Cursor;
import org.openrewrite.SourceFile;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.trait.SimpleTraitMatcher;
import org.openrewrite.trait.Trait;

import java.util.Optional;

public abstract class GradleTraitMatcher<U extends Trait<?>> extends SimpleTraitMatcher<U> {
protected @Nullable GradleProject getGradleProject(Cursor cursor) {
SourceFile sourceFile = cursor.firstEnclosing(SourceFile.class);
if (sourceFile == null) {
return null;
}

Optional<GradleProject> maybeGp = sourceFile.getMarkers().findFirst(GradleProject.class);
return maybeGp.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*/
@NonNullApi
@NonNullFields
package org.openrewrite.gradle.trait;

import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.internal.lang.NonNullFields;
Loading
Loading