From ce6e1f3ae5cb84fbddf6bf04c52f0ec1ef972c42 Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Mon, 19 Feb 2024 12:59:28 -0500 Subject: [PATCH 1/2] Extract DependencyVersionSelector --- .../gradle/AddDependencyVisitor.java | 44 +----- .../openrewrite/gradle/ChangeDependency.java | 63 ++++---- .../gradle/UpgradeDependencyVersion.java | 112 +++++++------- .../UpgradeTransitiveDependencyVersion.java | 107 +++++++++++++ .../dependency/DependencyVersionSelector.java | 141 ++++++++++++++++++ .../openrewrite/gradle/util/Dependency.java | 5 + 6 files changed, 337 insertions(+), 135 deletions(-) create mode 100644 rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java create mode 100644 rewrite-gradle/src/main/java/org/openrewrite/gradle/dependency/DependencyVersionSelector.java diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java index c3028e0a769..36db4837d78 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java @@ -20,6 +20,7 @@ import org.openrewrite.ExecutionContext; import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.SourceFile; +import org.openrewrite.gradle.dependency.DependencyVersionSelector; import org.openrewrite.gradle.internal.InsertDependencyComparator; import org.openrewrite.gradle.marker.GradleDependencyConfiguration; import org.openrewrite.gradle.marker.GradleProject; @@ -38,7 +39,6 @@ import org.openrewrite.maven.internal.MavenPomDownloader; import org.openrewrite.maven.table.MavenMetadataFailures; import org.openrewrite.maven.tree.*; -import org.openrewrite.semver.*; import org.openrewrite.tree.ParseError; import java.util.*; @@ -231,8 +231,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu resolvedVersion = version; } else { try { - resolvedVersion = resolveDependencyVersion(groupId, artifactId, "0", version, versionPattern, gp.getMavenRepositories(), metadataFailures, ctx) - .orElse(null); + resolvedVersion = new DependencyVersionSelector(metadataFailures, gp) + .select(new GroupArtifact(groupId, artifactId), configuration, version, versionPattern, ctx); } catch (MavenDownloadingException e) { return e.warn(m); } @@ -365,42 +365,4 @@ private DependencyStyle autodetectDependencyStyle(List statements) { return string >= map ? DependencyStyle.String : DependencyStyle.Map; } - - public static Optional resolveDependencyVersion(String groupId, String artifactId, String currentVersion, @Nullable String newVersion, @Nullable String versionPattern, - List repositories, @Nullable MavenMetadataFailures metadataFailures, ExecutionContext ctx) throws MavenDownloadingException { - VersionComparator versionComparator = StringUtils.isBlank(newVersion) ? - new LatestRelease(versionPattern) : - requireNonNull(Semver.validate(newVersion, versionPattern).getValue()); - - Optional version; - if (versionComparator instanceof ExactVersion) { - version = versionComparator.upgrade(currentVersion, singletonList(newVersion)); - } else if (versionComparator instanceof LatestPatch && !versionComparator.isValid(currentVersion, currentVersion)) { - // in the case of "latest.patch", a new version can only be derived if the - // current version is a semantic version - return Optional.empty(); - } else { - version = findNewerVersion(groupId, artifactId, currentVersion, versionComparator, repositories, metadataFailures, ctx); - } - return version; - } - - private static Optional findNewerVersion(String groupId, String artifactId, String version, VersionComparator versionComparator, - List repositories, @Nullable MavenMetadataFailures metadataFailures, ExecutionContext ctx) throws MavenDownloadingException { - try { - MavenMetadata mavenMetadata = metadataFailures == null ? - downloadMetadata(groupId, artifactId, repositories, ctx) : - metadataFailures.insertRows(ctx, () -> downloadMetadata(groupId, artifactId, repositories, ctx)); - return versionComparator.upgrade(version, mavenMetadata.getVersioning().getVersions()); - } catch (IllegalStateException e) { - // this can happen when we encounter exotic versions - return Optional.empty(); - } - } - - private static MavenMetadata downloadMetadata(String groupId, String artifactId, List repositories, ExecutionContext ctx) throws MavenDownloadingException { - return new MavenPomDownloader(ctx) - .downloadMetadata(new GroupArtifact(groupId, artifactId), null, - repositories); - } } diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java index f5e827010c4..c93581ec334 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java @@ -19,6 +19,7 @@ import lombok.EqualsAndHashCode; import lombok.Value; import org.openrewrite.*; +import org.openrewrite.gradle.dependency.DependencyVersionSelector; import org.openrewrite.gradle.marker.GradleDependencyConfiguration; import org.openrewrite.gradle.marker.GradleProject; import org.openrewrite.gradle.search.FindGradleProject; @@ -34,8 +35,8 @@ import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; import org.openrewrite.maven.MavenDownloadingException; +import org.openrewrite.maven.tree.GroupArtifact; import org.openrewrite.maven.tree.GroupArtifactVersion; -import org.openrewrite.maven.tree.MavenRepository; import org.openrewrite.maven.tree.ResolvedGroupArtifactVersion; import org.openrewrite.semver.DependencyMatcher; import org.openrewrite.semver.Semver; @@ -83,7 +84,7 @@ public class ChangeDependency extends Recipe { @Option(displayName = "Version pattern", description = "Allows version selection to be extended beyond the original Node Semver semantics. So for example," + - "Setting 'version' to \"25-29\" can be paired with a metadata pattern of \"-jre\" to select Guava 29.0-jre", + "Setting 'version' to \"25-29\" can be paired with a metadata pattern of \"-jre\" to select Guava 29.0-jre", example = "-jre", required = false) @Nullable @@ -91,14 +92,15 @@ public class ChangeDependency extends Recipe { @Option(displayName = "Override managed version", description = "If the old dependency has a managed version, this flag can be used to explicitly set the version on the new dependency. " + - "WARNING: No check is done on the NEW dependency to verify if it is managed, it relies on whether the OLD dependency had a managed version. " + - "The default for this flag is `false`.", + "WARNING: No check is done on the NEW dependency to verify if it is managed, it relies on whether the OLD dependency had a managed version. " + + "The default for this flag is `false`.", required = false) @Nullable Boolean overrideManagedVersion; /** * Keeping this constructor just for compatibility purposes + * * @deprecated Use {@link ChangeDependency#ChangeDependency(String, String, String, String, String, String, Boolean)} */ @Deprecated @@ -187,8 +189,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu if (depArgs.get(0) instanceof J.Literal || depArgs.get(0) instanceof G.GString || depArgs.get(0) instanceof G.MapEntry) { m = updateDependency(m, ctx); } else if (depArgs.get(0) instanceof J.MethodInvocation && - (((J.MethodInvocation) depArgs.get(0)).getSimpleName().equals("platform") || - ((J.MethodInvocation) depArgs.get(0)).getSimpleName().equals("enforcedPlatform"))) { + (((J.MethodInvocation) depArgs.get(0)).getSimpleName().equals("platform") || + ((J.MethodInvocation) depArgs.get(0)).getSimpleName().equals("enforcedPlatform"))) { m = m.withArguments(ListUtils.mapFirst(depArgs, platform -> updateDependency((J.MethodInvocation) platform, ctx))); } @@ -210,13 +212,10 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte updated = updated.withArtifactId(newArtifactId); } if (!StringUtils.isBlank(newVersion) && (!StringUtils.isBlank(original.getVersion()) || Boolean.TRUE.equals(overrideManagedVersion))) { - List repositories = "classpath".equals(m.getSimpleName()) ? - gradleProject.getMavenPluginRepositories() : - gradleProject.getMavenRepositories(); String resolvedVersion; try { - resolvedVersion = AddDependencyVisitor.resolveDependencyVersion(updated.getGroupId(), updated.getArtifactId(), "0", newVersion, versionPattern, repositories, null, ctx) - .orElse(null); + resolvedVersion = new DependencyVersionSelector(null, gradleProject) + .select(new GroupArtifact(updated.getGroupId(), updated.getArtifactId()), m.getSimpleName(), newVersion, versionPattern, ctx); } catch (MavenDownloadingException e) { return e.warn(m); } @@ -233,9 +232,11 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte } else if (m.getArguments().get(0) instanceof G.GString) { G.GString gstring = (G.GString) depArgs.get(0); List strings = gstring.getStrings(); - if (strings.size() >= 2 && strings.get(0) instanceof J.Literal) { + if (strings.size() >= 2 && strings.get(0) instanceof J.Literal && + ((J.Literal) strings.get(0)).getValue() != null) { + J.Literal literal = (J.Literal) strings.get(0); - Dependency original = DependencyStringNotationConverter.parse((String)literal.getValue()); + Dependency original = DependencyStringNotationConverter.parse((String) requireNonNull(literal.getValue())); if (depMatcher.matches(original.getGroupId(), original.getArtifactId())) { Dependency updated = original; if (!StringUtils.isBlank(newGroupId) && !updated.getGroupId().equals(newGroupId)) { @@ -245,13 +246,10 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte updated = updated.withArtifactId(newArtifactId); } if (!StringUtils.isBlank(newVersion)) { - List repositories = "classpath".equals(m.getSimpleName()) ? - gradleProject.getMavenPluginRepositories() : - gradleProject.getMavenRepositories(); String resolvedVersion; try { - resolvedVersion = AddDependencyVisitor.resolveDependencyVersion(updated.getGroupId(), updated.getArtifactId(), "0", newVersion, versionPattern, repositories, null, ctx) - .orElse(null); + resolvedVersion = new DependencyVersionSelector(null, gradleProject) + .select(new GroupArtifact(updated.getGroupId(), updated.getArtifactId()), m.getSimpleName(), newVersion, versionPattern, ctx); } catch (MavenDownloadingException e) { return e.warn(m); } @@ -290,15 +288,19 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte } String keyValue = (String) key.getValue(); String valueValue = (String) value.getValue(); - if ("group".equals(keyValue)) { - groupEntry = arg; - groupId = valueValue; - } else if ("name".equals(keyValue)) { - artifactEntry = arg; - artifactId = valueValue; - } else if ("version".equals(keyValue)) { - versionEntry = arg; - version = valueValue; + switch (keyValue) { + case "group": + groupEntry = arg; + groupId = valueValue; + break; + case "name": + artifactEntry = arg; + artifactId = valueValue; + break; + case "version": + versionEntry = arg; + version = valueValue; + break; } } if (groupId == null || artifactId == null) { @@ -317,13 +319,10 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte } String updatedVersion = version; if (!StringUtils.isBlank(newVersion) && (!StringUtils.isBlank(version) || Boolean.TRUE.equals(overrideManagedVersion))) { - List repositories = "classpath".equals(m.getSimpleName()) ? - gradleProject.getMavenPluginRepositories() : - gradleProject.getMavenRepositories(); String resolvedVersion; try { - resolvedVersion = AddDependencyVisitor.resolveDependencyVersion(updatedGroupId, updatedArtifactId, "0", newVersion, versionPattern, repositories, null, ctx) - .orElse(null); + resolvedVersion = new DependencyVersionSelector(null, gradleProject) + .select(new GroupArtifact(updatedGroupId, updatedArtifactId), m.getSimpleName(), newVersion, versionPattern, ctx); } catch (MavenDownloadingException e) { return e.warn(m); } diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java index 1eb10d8e6b8..75b545916b0 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java @@ -16,8 +16,10 @@ package org.openrewrite.gradle; import lombok.EqualsAndHashCode; +import lombok.RequiredArgsConstructor; import lombok.Value; import org.openrewrite.*; +import org.openrewrite.gradle.dependency.DependencyVersionSelector; import org.openrewrite.gradle.marker.GradleDependencyConfiguration; import org.openrewrite.gradle.marker.GradleProject; import org.openrewrite.gradle.search.FindGradleProject; @@ -123,11 +125,14 @@ public Validated validate() { private static final String UPDATE_VERSION_ERROR_KEY = "UPDATE_VERSION_ERROR_KEY"; - @Value public static class DependencyVersionState { Map versionPropNameToGA = new HashMap<>(); - // The value is either a String representing the resolved version or a MavenDownloadingException representing an error during resolution. + + /** + * The value is either a String representing the resolved version + * or a MavenDownloadingException representing an error during resolution. + */ Map gaToNewVersion = new HashMap<>(); } @@ -193,12 +198,16 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) continue; } String keyValue = (String) key.getValue(); - if ("group".equals(keyValue)) { - groupId = valueValue; - } else if ("name".equals(keyValue)) { - artifactId = valueValue; - } else if ("version".equals(keyValue)) { - version = valueValue; + switch (keyValue) { + case "group": + groupId = valueValue; + break; + case "name": + artifactId = valueValue; + break; + case "version": + version = valueValue; + break; } } if (groupId == null || artifactId == null) { @@ -214,10 +223,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) return m; } try { - String resolvedVersion = "classpath".equals(m.getSimpleName()) ? - findNewerPluginVersion(groupId, artifactId, "0", gradleProject, ctx) : - findNewerProjectDependencyVersion(groupId, artifactId, "0", gradleProject, ctx); - + String resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject) + .select(new GroupArtifact(groupId, artifactId), m.getSimpleName(), newVersion, versionPattern, ctx); acc.versionPropNameToGA.put(versionVariableName, ga); acc.gaToNewVersion.put(ga, resolvedVersion); } catch (MavenDownloadingException e) { @@ -247,10 +254,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) continue; } try { - String resolvedVersion = "classpath".equals(m.getSimpleName()) ? - findNewerPluginVersion(dep.getGroupId(), dep.getArtifactId(), "0", gradleProject, ctx) : - findNewerProjectDependencyVersion(dep.getGroupId(), dep.getArtifactId(), "0", gradleProject, ctx); - + String resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject) + .select(new GroupArtifact(dep.getGroupId(), dep.getArtifactId()), m.getSimpleName(), newVersion, versionPattern, ctx); acc.versionPropNameToGA.put(versionVariableName, ga); acc.gaToNewVersion.put(ga, resolvedVersion); } catch (MavenDownloadingException e) { @@ -391,21 +396,19 @@ private J.MethodInvocation updateDependency(J.MethodInvocation method, Execution return arg; } - String version = dep.getVersion(); try { - String newVersion = "classpath".equals(method.getSimpleName()) ? - findNewerPluginVersion(dep.getGroupId(), dep.getArtifactId(), version, gradleProject, ctx) : - findNewerProjectDependencyVersion(dep.getGroupId(), dep.getArtifactId(), version, gradleProject, ctx); - if (newVersion == null || version.equals(newVersion)) { + String selectedVersion = new DependencyVersionSelector(metadataFailures, gradleProject) + .select(dep.getGav(), method.getSimpleName(), newVersion, versionPattern, ctx); + if (selectedVersion == null || dep.getVersion().equals(selectedVersion)) { return arg; } getCursor().dropParentUntil(p -> p instanceof SourceFile) .computeMessageIfAbsent(NEW_VERSION_KEY, it -> new HashMap>()) - .computeIfAbsent(new GroupArtifactVersion(dep.getGroupId(), dep.getArtifactId(), newVersion), it -> new HashSet<>()) + .computeIfAbsent(new GroupArtifactVersion(dep.getGroupId(), dep.getArtifactId(), selectedVersion), it -> new HashSet<>()) .add(method.getSimpleName()); String newGav = dep - .withVersion(newVersion) + .withVersion(selectedVersion) .toStringNotation(); return literal .withValue(newGav) @@ -448,15 +451,15 @@ private J.MethodInvocation updateDependency(J.MethodInvocation method, Execution if (version.startsWith("$")) { return m; } - String newVersion; + String selectedVersion; try { - newVersion = "classpath".equals(m.getSimpleName()) ? - findNewerPluginVersion((String) groupLiteral.getValue(), (String) artifactLiteral.getValue(), version, gradleProject, ctx) : - findNewerProjectDependencyVersion((String) groupLiteral.getValue(), (String) artifactLiteral.getValue(), version, gradleProject, ctx); + GroupArtifactVersion gav = new GroupArtifactVersion((String) groupLiteral.getValue(), (String) artifactLiteral.getValue(), version); + selectedVersion = new DependencyVersionSelector(metadataFailures, gradleProject) + .select(gav, m.getSimpleName(), newVersion, versionPattern, ctx); } catch (MavenDownloadingException e) { return e.warn(m); } - if (newVersion == null || version.equals(newVersion)) { + if (selectedVersion == null || version.equals(selectedVersion)) { return m; } List newArgs = new ArrayList<>(3); @@ -464,8 +467,8 @@ private J.MethodInvocation updateDependency(J.MethodInvocation method, Execution newArgs.add(depArgs.get(1)); newArgs.add(versionEntry.withValue( versionLiteral - .withValueSource(requireNonNull(versionLiteral.getValueSource()).replace(version, newVersion)) - .withValue(newVersion))); + .withValueSource(requireNonNull(versionLiteral.getValueSource()).replace(version, selectedVersion)) + .withValue(selectedVersion))); newArgs.addAll(depArgs.subList(3, depArgs.size())); return m.withArguments(newArgs); @@ -486,11 +489,10 @@ private J.MethodInvocation updateDependency(J.MethodInvocation method, Execution ); } - @Value - @EqualsAndHashCode(callSuper = false) + @RequiredArgsConstructor private class UpdateVariable extends GroovyIsoVisitor { - Map>> versionVariableNames; - GradleProject gradleProject; + private final Map>> versionVariableNames; + private final GradleProject gradleProject; @Override public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) { @@ -522,19 +524,19 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations try { for (Map.Entry> gaEntry : gaToConfigurations.entrySet()) { GroupArtifact ga = gaEntry.getKey(); - String newVersion = findNewerProjectDependencyVersion(ga.getGroupId(), ga.getArtifactId(), version, gradleProject, ctx); - if (newVersion == null) { - newVersion = findNewerPluginVersion(ga.getGroupId(), ga.getArtifactId(), version, gradleProject, ctx); - } - if (newVersion == null) { + GroupArtifactVersion gav = new GroupArtifactVersion(ga.getGroupId(), ga.getArtifactId(), version); + DependencyVersionSelector selector = new DependencyVersionSelector(metadataFailures, gradleProject); + String selectedVersion = Optional.ofNullable(selector.select(gav, null, newVersion, versionPattern, ctx)) + .orElse(selector.select(gav, "classpath", newVersion, versionPattern, ctx)); + if (selectedVersion == null) { return v; } getCursor().dropParentUntil(p -> p instanceof SourceFile) .computeMessageIfAbsent(NEW_VERSION_KEY, m -> new HashMap>()) - .computeIfAbsent(new GroupArtifactVersion(ga.getGroupId(), ga.getArtifactId(), newVersion), it -> new HashSet<>()) + .computeIfAbsent(new GroupArtifactVersion(ga.getGroupId(), ga.getArtifactId(), selectedVersion), it -> new HashSet<>()) .addAll(gaEntry.getValue()); - J.Literal newVersionLiteral = ChangeStringLiteral.withStringValue(initializer, newVersion); + J.Literal newVersionLiteral = ChangeStringLiteral.withStringValue(initializer, selectedVersion); v = v.withInitializer(newVersionLiteral); } } catch (MavenDownloadingException e) { @@ -577,19 +579,19 @@ public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ct try { for (Map.Entry> gaEntry : gaToConfigurations.entrySet()) { GroupArtifact ga = gaEntry.getKey(); - String newVersion = findNewerProjectDependencyVersion(ga.getGroupId(), ga.getArtifactId(), version, gradleProject, ctx); - if (newVersion == null) { - newVersion = findNewerPluginVersion(ga.getGroupId(), ga.getArtifactId(), version, gradleProject, ctx); - } - if (newVersion == null) { + GroupArtifactVersion gav = new GroupArtifactVersion(ga.getGroupId(), ga.getArtifactId(), version); + DependencyVersionSelector selector = new DependencyVersionSelector(metadataFailures, gradleProject); + String selectedVersion = Optional.ofNullable(selector.select(gav, null, newVersion, versionPattern, ctx)) + .orElse(selector.select(gav, "classpath", newVersion, versionPattern, ctx)); + if (selectedVersion == null) { return a; } getCursor().dropParentUntil(p -> p instanceof SourceFile) .computeMessageIfAbsent(NEW_VERSION_KEY, m -> new HashMap>()) - .computeIfAbsent(new GroupArtifactVersion(ga.getGroupId(), ga.getArtifactId(), newVersion), it -> new HashSet<>()) + .computeIfAbsent(new GroupArtifactVersion(ga.getGroupId(), ga.getArtifactId(), selectedVersion), it -> new HashSet<>()) .addAll(gaEntry.getValue()); - J.Literal newVersionLiteral = ChangeStringLiteral.withStringValue(literal, newVersion); + J.Literal newVersionLiteral = ChangeStringLiteral.withStringValue(literal, selectedVersion); a = a.withAssignment(newVersionLiteral); } } catch (MavenDownloadingException e) { @@ -599,20 +601,6 @@ public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ct } } - @Nullable - private String findNewerPluginVersion(String groupId, String artifactId, String version, GradleProject gradleProject, - ExecutionContext ctx) throws MavenDownloadingException { - return AddDependencyVisitor.resolveDependencyVersion(groupId, artifactId, version, newVersion, versionPattern, gradleProject.getMavenPluginRepositories(), metadataFailures, ctx) - .orElse(null); - } - - @Nullable - private String findNewerProjectDependencyVersion(String groupId, String artifactId, String version, GradleProject gradleProject, - ExecutionContext ctx) throws MavenDownloadingException { - return AddDependencyVisitor.resolveDependencyVersion(groupId, artifactId, version, newVersion, versionPattern, gradleProject.getMavenRepositories(), metadataFailures, ctx) - .orElse(null); - } - static GradleProject replaceVersion(GradleProject gp, ExecutionContext ctx, GroupArtifactVersion gav, Set configurations) { try { if (gav.getGroupId() == null || gav.getArtifactId() == null) { diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java new file mode 100644 index 00000000000..ee2e3efe99c --- /dev/null +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java @@ -0,0 +1,107 @@ +/* + * Copyright 2024 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.gradle; + +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.openrewrite.*; +import org.openrewrite.gradle.marker.GradleProject; +import org.openrewrite.gradle.search.FindGradleProject; +import org.openrewrite.groovy.GroovyVisitor; +import org.openrewrite.groovy.tree.G; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.java.tree.J; +import org.openrewrite.semver.Semver; + +@Value +@EqualsAndHashCode(callSuper = false) +public class UpgradeTransitiveDependencyVersion extends Recipe { + + @Option(displayName = "Group", + description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression.", + example = "com.fasterxml.jackson*") + String groupId; + + @Option(displayName = "Artifact", + description = "The second part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression.", + example = "jackson-module*") + String artifactId; + + @Option(displayName = "Version", + description = "An exact version number or node-style semver selector used to select the version number. " + + "You can also use `latest.release` for the latest available version and `latest.patch` if " + + "the current version is a valid semantic version. For more details, you can look at the documentation " + + "page of [version selectors](https://docs.openrewrite.org/reference/dependency-version-selectors). " + + "Defaults to `latest.release`.", + example = "29.X", + required = false) + @Nullable + String version; + + @Option(displayName = "Version pattern", + description = "Allows version selection to be extended beyond the original Node Semver semantics. So for example," + + "Setting 'newVersion' to \"25-29\" can be paired with a metadata pattern of \"-jre\" to select Guava 29.0-jre", + example = "-jre", + required = false) + @Nullable + String versionPattern; + + @Option(displayName = "Strategy", + description = "The technique used to upgrade the dependency.", + valid = {"direct", "resolutionStrategy"}) + String strategy; + + @Override + public String getDisplayName() { + return "Upgrade transitive Gradle dependencies"; + } + + @Override + public String getDescription() { + return "Upgrades the version of a transitive dependency in a Gradle build file. " + + "There are many ways to do this in Gradle, so the mechanism for upgrading a " + + "transitive dependency must be considered carefully depending on your style " + + "of dependency management."; + } + + @Override + public Validated validate() { + Validated validated = super.validate(); + if (version != null) { + validated = validated.and(Semver.validate(version, versionPattern)); + } + return validated; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new FindGradleProject(FindGradleProject.SearchCriteria.Marker), new GroovyVisitor() { + GradleProject gradleProject; + + @Override + public J visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) { + gradleProject = cu.getMarkers().findFirst(GradleProject.class) + .orElseThrow(() -> new IllegalStateException("Unable to find GradleProject marker.")); + + if (strategy.equals("direct")) { + + } + + return super.visitCompilationUnit(cu, ctx); + } + }); + } +} diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/dependency/DependencyVersionSelector.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/dependency/DependencyVersionSelector.java new file mode 100644 index 00000000000..7488336954e --- /dev/null +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/dependency/DependencyVersionSelector.java @@ -0,0 +1,141 @@ +/* + * Copyright 2024 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.gradle.dependency; + +import lombok.Value; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Incubating; +import org.openrewrite.gradle.marker.GradleProject; +import org.openrewrite.internal.StringUtils; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.maven.MavenDownloadingException; +import org.openrewrite.maven.internal.MavenPomDownloader; +import org.openrewrite.maven.table.MavenMetadataFailures; +import org.openrewrite.maven.tree.*; +import org.openrewrite.semver.*; + +import java.util.List; +import java.util.Optional; + +import static java.util.Collections.singletonList; +import static java.util.Objects.requireNonNull; + +/** + * Selects versions for new or existing dependencies based on a node-semver selector + * by inspecting available versions in the Maven metadata from a set of Maven repositories. + */ +@Incubating(since = "8.17.0") +@Value +public class DependencyVersionSelector { + @Nullable + MavenMetadataFailures metadataFailures; + + GradleProject gradleProject; + + /** + * Used to select a version for a new dependency that has no prior version. + * + * @param ga The group and artifact of the new dependency. + * @param configuration The configuration to select the version for. The configuration influences + * which set of Maven repositories (either plugin repositories or regular repositories) + * are used to resolve Maven metadata from. + * @param version The version to select, in node-semver format. + * @param versionPattern The version pattern to select, if any. + * @param ctx The execution context, which can influence dependency resolution. + * @return The selected version, if any. + * @throws MavenDownloadingException If there is a problem downloading metadata for the dependency. + */ + @Nullable + public String select(GroupArtifact ga, + String configuration, + @Nullable String version, + @Nullable String versionPattern, + ExecutionContext ctx) throws MavenDownloadingException { + return select( + new GroupArtifactVersion(ga.getGroupId(), ga.getArtifactId(), "0"), + configuration, + // we don't want to select the latest patch in the 0.x line... + "latest.patch".equalsIgnoreCase(version) ? "latest.release" : version, + versionPattern, + ctx + ); + } + + /** + * Used to upgrade a version for a dependency that already has a version. + * + * @param gav The group, artifact, and version of the existing dependency. + * @param configuration The configuration to select the version for. The configuration influences + * which set of Maven repositories (either plugin repositories or regular repositories) + * are used to resolve Maven metadata from. * @param version The version to select, in node-semver format. + * @param versionPattern The version pattern to select, if any. + * @param ctx The execution context, which can influence dependency resolution. + * @return The selected version, if any. + * @throws MavenDownloadingException If there is a problem downloading metadata for the dependency. + */ + @Nullable + public String select(GroupArtifactVersion gav, + String configuration, + @Nullable String version, + @Nullable String versionPattern, + ExecutionContext ctx) throws MavenDownloadingException { + if (gav.getVersion() == null) { + throw new IllegalArgumentException("Version must be specified. Call the select method " + + "that accepts a GroupArtifact instead if there is no " + + "current version."); + } + + VersionComparator versionComparator = StringUtils.isBlank(version) ? + new LatestRelease(versionPattern) : + requireNonNull(Semver.validate(version, versionPattern).getValue()); + + if (versionComparator instanceof ExactVersion) { + return versionComparator.upgrade(gav.getVersion(), singletonList(version)).orElse(null); + } else if (versionComparator instanceof LatestPatch && + !versionComparator.isValid(gav.getVersion(), gav.getVersion())) { + // in the case of "latest.patch", a new version can only be derived if the + // current version is a semantic version + return null; + } else { + return findNewerVersion(gav, configuration, versionComparator, ctx).orElse(null); + } + } + + private Optional findNewerVersion(GroupArtifactVersion gav, + String configuration, + VersionComparator versionComparator, + ExecutionContext ctx) throws MavenDownloadingException { + try { + List repos = "classpath".equals(configuration) ? + gradleProject.getMavenPluginRepositories() : + gradleProject.getMavenRepositories(); + MavenMetadata mavenMetadata = metadataFailures == null ? + downloadMetadata(gav.getGroupId(), gav.getArtifactId(), repos, ctx) : + metadataFailures.insertRows(ctx, () -> downloadMetadata(gav.getGroupId(), gav.getArtifactId(), + repos, ctx)); + return versionComparator.upgrade(requireNonNull(gav.getVersion()), + mavenMetadata.getVersioning().getVersions()); + } catch (IllegalStateException e) { + // this can happen when we encounter exotic versions + return Optional.empty(); + } + } + + private MavenMetadata downloadMetadata(String groupId, String artifactId, List repositories, ExecutionContext ctx) throws MavenDownloadingException { + return new MavenPomDownloader(ctx).downloadMetadata( + new GroupArtifact(groupId, artifactId), null, repositories); + } +} diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/util/Dependency.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/util/Dependency.java index 820b8d50494..9e114832b32 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/util/Dependency.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/util/Dependency.java @@ -19,6 +19,7 @@ import lombok.Value; import lombok.With; import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.maven.tree.GroupArtifactVersion; @Value @With @@ -36,6 +37,10 @@ public class Dependency { @Nullable String ext; + public GroupArtifactVersion getGav() { + return new GroupArtifactVersion(groupId, artifactId, version); + } + public String toStringNotation() { StringBuilder builder = new StringBuilder(); builder.append(groupId).append(':').append(artifactId); From 546f8b13bfa84fc8a6595fe647f79d2b20510cff Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Mon, 19 Feb 2024 23:54:30 -0500 Subject: [PATCH 2/2] Move markers from gradle tooling model into rewrite-gradle, use org.openrewirte.gradle.toolingapi.Assertions#withToolingApi --- rewrite-gradle/build.gradle.kts | 4 +- .../gradle/AddDependencyVisitor.java | 5 +- ...DependencyToUpgradeTransitiveVersion.java} | 55 ++++- .../org/openrewrite/gradle/Assertions.java | 137 +++-------- .../openrewrite/gradle/ChangeDependency.java | 3 +- .../DependencyVersionSelector.java | 24 +- .../org/openrewrite/gradle/GradleParser.java | 1 - .../openrewrite/gradle/RemoveDependency.java | 2 +- .../gradle/UpgradeDependencyVersion.java | 5 +- .../gradle/marker/FeaturePreview.java | 36 +++ .../marker/GradleDependencyConfiguration.java | 140 ++++++++++++ .../gradle/marker/GradlePluginDescriptor.java | 36 +++ .../gradle/marker/GradleProject.java | 132 +++++++++++ .../gradle/marker/GradleSettings.java | 50 ++++ .../gradle/marker/package-info.java | 19 ++ .../openrewrite/gradle/AddDependencyTest.java | 216 +++++++++--------- ...endencyToUpgradeTransitiveVersionTest.java | 63 +++++ .../gradle/ChangeDependencyTest.java | 2 +- .../gradle/RemoveDependencyTest.java | 2 +- .../gradle/UpgradeDependencyVersionTest.java | 112 ++++----- .../gradle/plugins/AddBuildPluginTest.java | 8 +- .../AddDevelocityGradlePluginTest.java | 10 +- .../gradle/plugins/AddSettingsPluginTest.java | 48 ++-- .../gradle/plugins/ChangePluginTest.java | 2 +- .../plugins/UpgradePluginVersionTest.java | 6 +- .../gradle/search/DependencyInsightTest.java | 56 ++--- .../gradle/search/FindGradleProjectTest.java | 3 +- 27 files changed, 818 insertions(+), 359 deletions(-) rename rewrite-gradle/src/main/java/org/openrewrite/gradle/{UpgradeTransitiveDependencyVersion.java => AddDirectDependencyToUpgradeTransitiveVersion.java} (58%) rename rewrite-gradle/src/main/java/org/openrewrite/gradle/{dependency => }/DependencyVersionSelector.java (83%) create mode 100644 rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/FeaturePreview.java create mode 100644 rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleDependencyConfiguration.java create mode 100644 rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradlePluginDescriptor.java create mode 100644 rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleProject.java create mode 100644 rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleSettings.java create mode 100644 rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/package-info.java create mode 100644 rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDirectDependencyToUpgradeTransitiveVersionTest.java diff --git a/rewrite-gradle/build.gradle.kts b/rewrite-gradle/build.gradle.kts index e4a5613e6f3..bd3076b1add 100644 --- a/rewrite-gradle/build.gradle.kts +++ b/rewrite-gradle/build.gradle.kts @@ -35,7 +35,6 @@ dependencies { api("org.jetbrains:annotations:latest.release") compileOnly(project(":rewrite-test")) implementation(project(":rewrite-properties")) - implementation("org.openrewrite.gradle.tooling:model:$rewriteVersion") compileOnly("org.codehaus.groovy:groovy:latest.release") compileOnly(gradleApi()) @@ -59,6 +58,9 @@ dependencies { // because gradle-api fatjars this implementation already exclude("ch.qos.logback", "logback-classic") } + + testImplementation("org.openrewrite.gradle.tooling:model:latest.release") + testImplementation("com.squareup.okhttp3:mockwebserver:4.+") testRuntimeOnly("org.codehaus.groovy:groovy:latest.release") diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java index 36db4837d78..de8f6e1352a 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependencyVisitor.java @@ -20,7 +20,6 @@ import org.openrewrite.ExecutionContext; import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.SourceFile; -import org.openrewrite.gradle.dependency.DependencyVersionSelector; import org.openrewrite.gradle.internal.InsertDependencyComparator; import org.openrewrite.gradle.marker.GradleDependencyConfiguration; import org.openrewrite.gradle.marker.GradleProject; @@ -188,8 +187,8 @@ static G.CompilationUnit addDependency( }), newRequested)); if (newGdc.isCanBeResolved() && resolvedGav != null) { - newGdc = newGdc.withResolved(ListUtils.concat( - ListUtils.map(gdc.getResolved(), resolved -> { + newGdc = newGdc.withDirectResolved(ListUtils.concat( + ListUtils.map(gdc.getDirectResolved(), resolved -> { // Remove any existing dependency with the same group and artifact id if (Objects.equals(resolved.getGroupId(), resolvedGav.getGroupId()) && Objects.equals(resolved.getArtifactId(), resolvedGav.getArtifactId())) { return null; diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDirectDependencyToUpgradeTransitiveVersion.java similarity index 58% rename from rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java rename to rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDirectDependencyToUpgradeTransitiveVersion.java index ee2e3efe99c..97904d77199 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDirectDependencyToUpgradeTransitiveVersion.java @@ -18,17 +18,34 @@ import lombok.EqualsAndHashCode; import lombok.Value; import org.openrewrite.*; +import org.openrewrite.gradle.marker.GradleDependencyConfiguration; import org.openrewrite.gradle.marker.GradleProject; import org.openrewrite.gradle.search.FindGradleProject; import org.openrewrite.groovy.GroovyVisitor; import org.openrewrite.groovy.tree.G; +import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.tree.J; +import org.openrewrite.marker.Markup; +import org.openrewrite.maven.MavenDownloadingException; +import org.openrewrite.maven.table.MavenMetadataFailures; +import org.openrewrite.maven.tree.GroupArtifact; +import org.openrewrite.maven.tree.ResolvedDependency; +import org.openrewrite.semver.DependencyMatcher; import org.openrewrite.semver.Semver; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static java.util.Collections.singletonList; + @Value @EqualsAndHashCode(callSuper = false) -public class UpgradeTransitiveDependencyVersion extends Recipe { +public class AddDirectDependencyToUpgradeTransitiveVersion extends Recipe { + + @EqualsAndHashCode.Exclude + transient MavenMetadataFailures metadataFailures = new MavenMetadataFailures(this); @Option(displayName = "Group", description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression.", @@ -59,11 +76,6 @@ public class UpgradeTransitiveDependencyVersion extends Recipe { @Nullable String versionPattern; - @Option(displayName = "Strategy", - description = "The technique used to upgrade the dependency.", - valid = {"direct", "resolutionStrategy"}) - String strategy; - @Override public String getDisplayName() { return "Upgrade transitive Gradle dependencies"; @@ -88,6 +100,7 @@ public Validated validate() { @Override public TreeVisitor getVisitor() { + DependencyMatcher dependencyMatcher = new DependencyMatcher(groupId, artifactId, null); return Preconditions.check(new FindGradleProject(FindGradleProject.SearchCriteria.Marker), new GroovyVisitor() { GradleProject gradleProject; @@ -96,8 +109,36 @@ public J visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) { gradleProject = cu.getMarkers().findFirst(GradleProject.class) .orElseThrow(() -> new IllegalStateException("Unable to find GradleProject marker.")); - if (strategy.equals("direct")) { + Map> toUpdate = new HashMap<>(); + + DependencyVersionSelector versionSelector = new DependencyVersionSelector(metadataFailures, gradleProject); + for (GradleDependencyConfiguration configuration : gradleProject.getConfigurations()) { + for (ResolvedDependency resolvedDependency : configuration.getResolved()) { + if (resolvedDependency.getDepth() > 0 && + dependencyMatcher.matches(resolvedDependency.getGroupId(), resolvedDependency.getArtifactId(), resolvedDependency.getVersion())) { + try { + String selected = versionSelector.select(resolvedDependency.getGav(), configuration.getName(), + version, versionPattern, ctx); + if (!resolvedDependency.getVersion().equals(selected)) { + toUpdate.merge(new GroupArtifact(groupId, artifactId), singletonList(configuration), (existing, update) -> { + List all = ListUtils.concatAll(existing, update); + all.removeIf(c -> { + for (GradleDependencyConfiguration config : all) { + if (c.allExtendsFrom().contains(config)) { + return true; + } + } + return false; + }); + return all; + }); + } + } catch (MavenDownloadingException e) { + return Markup.warn(cu, e); + } + } + } } return super.visitCompilationUnit(cu, ctx); diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/Assertions.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/Assertions.java index 7086c13c84a..c5da707b506 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/Assertions.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/Assertions.java @@ -16,38 +16,19 @@ package org.openrewrite.gradle; import org.intellij.lang.annotations.Language; -import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.Parser; import org.openrewrite.SourceFile; -import org.openrewrite.gradle.marker.GradleProject; -import org.openrewrite.gradle.marker.GradleSettings; -import org.openrewrite.gradle.toolingapi.OpenRewriteModel; -import org.openrewrite.gradle.toolingapi.OpenRewriteModelBuilder; -import org.openrewrite.gradle.util.GradleWrapper; import org.openrewrite.groovy.GroovyParser; import org.openrewrite.groovy.tree.G; import org.openrewrite.internal.lang.Nullable; -import org.openrewrite.marker.OperatingSystemProvenance; -import org.openrewrite.properties.tree.Properties; import org.openrewrite.test.SourceSpec; import org.openrewrite.test.SourceSpecs; import org.openrewrite.test.UncheckedConsumer; -import java.io.File; -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; -import java.nio.file.attribute.PosixFilePermission; import java.util.List; -import java.util.Set; import java.util.function.Consumer; -import static java.util.Objects.requireNonNull; - public class Assertions { private Assertions() { @@ -56,90 +37,43 @@ private Assertions() { private static final Parser.Builder gradleParser = GradleParser.builder() .groovyParser(GroovyParser.builder().logCompilationWarningsAndErrors(true)); + /** + * @param version The Gradle version to use. + * @param distribution The Gradle distribution to use. + * @return An exception now that the method has moved. + * @deprecated This method has moved to org.openrewrite.gradle.toolingapi.Assertions. This + * has allowed us to remove the compile time dependency on the tooling model and thus save a lot + * of space in the distribution of rewrite-gradle recipes. + */ + @Deprecated public static UncheckedConsumer> withToolingApi(@Nullable String version, @Nullable String distribution) { - return sourceFiles -> { - try { - Path tempDirectory = Files.createTempDirectory("project"); - // Usage of Assertions.mavenProject() might result in gradle files inside a subdirectory - Path projectDir = tempDirectory; - try { - for (SourceFile sourceFile : sourceFiles) { - if (sourceFile instanceof G.CompilationUnit) { - G.CompilationUnit g = (G.CompilationUnit) sourceFile; - if (g.getSourcePath().toString().endsWith(".gradle")) { - Path groovyGradle = tempDirectory.resolve(g.getSourcePath()); - if (!tempDirectory.equals(groovyGradle.getParent()) && tempDirectory.equals(groovyGradle.getParent().getParent())) { - projectDir = groovyGradle.getParent(); - } - Files.createDirectories(groovyGradle.getParent()); - Files.write(groovyGradle, g.printAllAsBytes()); - } - } else if (sourceFile instanceof Properties.File) { - Properties.File f = (Properties.File) sourceFile; - if (f.getSourcePath().endsWith("gradle.properties")) { - Path gradleProperties = tempDirectory.resolve(f.getSourcePath()); - if (!tempDirectory.equals(gradleProperties.getParent()) && tempDirectory.equals(gradleProperties.getParent().getParent())) { - projectDir = gradleProperties.getParent(); - } - Files.createDirectories(gradleProperties.getParent()); - Files.write(gradleProperties, f.printAllAsBytes()); - } - } - } - - if (version != null) { - GradleWrapper gradleWrapper = GradleWrapper.create(distribution, version, null, new InMemoryExecutionContext()); - Files.createDirectories(projectDir.resolve("gradle/wrapper/")); - Files.write(projectDir.resolve(GradleWrapper.WRAPPER_PROPERTIES_LOCATION), ("distributionBase=GRADLE_USER_HOME\n" + - "distributionPath=wrapper/dists\n" + - "distributionUrl=" + gradleWrapper.getPropertiesFormattedUrl() + "\n" + - "distributionSha256Sum=" + gradleWrapper.getDistributionChecksum().getHexValue() + "\n" + - "zipStoreBase=GRADLE_USER_HOME\n" + - "zipStorePath=wrapper/dists").getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE_NEW); - Files.write(projectDir.resolve(GradleWrapper.WRAPPER_JAR_LOCATION), gradleWrapper.wrapperJar().printAllAsBytes(), StandardOpenOption.CREATE_NEW); - Path gradleSh = projectDir.resolve(GradleWrapper.WRAPPER_SCRIPT_LOCATION); - Files.copy(requireNonNull(UpdateGradleWrapper.class.getResourceAsStream("/gradlew")), gradleSh); - OperatingSystemProvenance current = OperatingSystemProvenance.current(); - if (current.isLinux() || current.isMacOsX()) { - Set permissions = Files.getPosixFilePermissions(gradleSh); - permissions.add(PosixFilePermission.OWNER_EXECUTE); - Files.setPosixFilePermissions(gradleSh, permissions); - } - Files.copy(requireNonNull(UpdateGradleWrapper.class.getResourceAsStream("/gradlew.bat")), projectDir.resolve(GradleWrapper.WRAPPER_BATCH_LOCATION)); - } - - for (int i = 0; i < sourceFiles.size(); i++) { - SourceFile sourceFile = sourceFiles.get(i); - if (sourceFile.getSourcePath().toString().endsWith(".gradle")) { - if (sourceFile.getSourcePath().endsWith("settings.gradle")) { - OpenRewriteModel model = OpenRewriteModelBuilder.forProjectDirectory(tempDirectory.resolve(sourceFile.getSourcePath()).getParent().toFile(), null); - org.openrewrite.gradle.toolingapi.GradleSettings rawSettings = model.gradleSettings(); - if (rawSettings != null) { - GradleSettings gradleSettings = GradleSettings.fromToolingModel(rawSettings); - sourceFiles.set(i, sourceFile.withMarkers(sourceFile.getMarkers().add(gradleSettings))); - } - } else { - OpenRewriteModel model = OpenRewriteModelBuilder.forProjectDirectory(projectDir.toFile(), tempDirectory.resolve(sourceFile.getSourcePath()).toFile()); - GradleProject gradleProject = GradleProject.fromToolingModel(model.gradleProject()); - sourceFiles.set(i, sourceFile.withMarkers(sourceFile.getMarkers().add(gradleProject))); - } - } - } - } finally { - deleteDirectory(tempDirectory.toFile()); - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - }; + throw new UnsupportedOperationException("This method has moved to org.openrewrite.gradle.toolingapi.Assertions. " + + "Add a dependency on org.openrewrite.gradle.tooling:model to continue using it."); } + /** + * @param version The Gradle version to use. + * @return An exception now that the method has moved. + * @deprecated This method has moved to org.openrewrite.gradle.toolingapi.Assertions. This + * has allowed us to remove the compile time dependency on the tooling model and thus save a lot + * of space in the distribution of rewrite-gradle recipes. + */ + @Deprecated public static UncheckedConsumer> withToolingApi(String version) { - return withToolingApi(version, "bin"); + throw new UnsupportedOperationException("This method has moved to org.openrewrite.gradle.toolingapi.Assertions. " + + "Add a dependency on org.openrewrite.gradle.tooling:model to continue using it."); } + /** + * @return An exception now that the method has moved. + * @deprecated This method has moved to org.openrewrite.gradle.toolingapi.Assertions. This + * has allowed us to remove the compile time dependency on the tooling model and thus save a lot + * of space in the distribution of rewrite-gradle recipes. + */ + @Deprecated public static UncheckedConsumer> withToolingApi() { - return withToolingApi(null, null); + throw new UnsupportedOperationException("This method has moved to org.openrewrite.gradle.toolingapi.Assertions. " + + "Add a dependency on org.openrewrite.gradle.tooling:model to continue using it."); } public static SourceSpecs buildGradle(@Language("groovy") @Nullable String before) { @@ -191,15 +125,4 @@ public static SourceSpecs settingsGradle(@Language("groovy") @Nullable String be spec.accept(gradle); return gradle; } - - private static void deleteDirectory(File directoryToBeDeleted) { - File[] allContents = directoryToBeDeleted.listFiles(); - if (allContents != null) { - for (File file : allContents) { - deleteDirectory(file); - } - } - //noinspection ResultOfMethodCallIgnored - directoryToBeDeleted.delete(); - } } diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java index c93581ec334..18aa0efa891 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java @@ -19,7 +19,6 @@ import lombok.EqualsAndHashCode; import lombok.Value; import org.openrewrite.*; -import org.openrewrite.gradle.dependency.DependencyVersionSelector; import org.openrewrite.gradle.marker.GradleDependencyConfiguration; import org.openrewrite.gradle.marker.GradleProject; import org.openrewrite.gradle.search.FindGradleProject; @@ -377,7 +376,7 @@ private GradleProject updateGradleModel(GradleProject gp) { } return requested; })); - newGdc = newGdc.withResolved(ListUtils.map(gdc.getResolved(), resolved -> { + newGdc = newGdc.withDirectResolved(ListUtils.map(gdc.getDirectResolved(), resolved -> { if (depMatcher.matches(resolved.getGroupId(), resolved.getArtifactId())) { ResolvedGroupArtifactVersion gav = resolved.getGav(); if (newGroupId != null) { diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/dependency/DependencyVersionSelector.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/DependencyVersionSelector.java similarity index 83% rename from rewrite-gradle/src/main/java/org/openrewrite/gradle/dependency/DependencyVersionSelector.java rename to rewrite-gradle/src/main/java/org/openrewrite/gradle/DependencyVersionSelector.java index 7488336954e..ea1c423eb34 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/dependency/DependencyVersionSelector.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/DependencyVersionSelector.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.openrewrite.gradle.dependency; +package org.openrewrite.gradle; import lombok.Value; import org.openrewrite.ExecutionContext; @@ -74,6 +74,28 @@ public String select(GroupArtifact ga, ); } + /** + * Used to upgrade a version for a dependency that already has a version. + * + * @param gav The group, artifact, and version of the existing dependency. + * @param configuration The configuration to select the version for. The configuration influences + * which set of Maven repositories (either plugin repositories or regular repositories) + * are used to resolve Maven metadata from. * @param version The version to select, in node-semver format. + * @param versionPattern The version pattern to select, if any. + * @param ctx The execution context, which can influence dependency resolution. + * @return The selected version, if any. + * @throws MavenDownloadingException If there is a problem downloading metadata for the dependency. + */ + @Nullable + public String select(ResolvedGroupArtifactVersion gav, + String configuration, + @Nullable String version, + @Nullable String versionPattern, + ExecutionContext ctx) throws MavenDownloadingException { + return select(new GroupArtifactVersion(gav.getGroupId(), gav.getArtifactId(), gav.getVersion()), + configuration, version, versionPattern, ctx); + } + /** * Used to upgrade a version for a dependency that already has a version. * diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/GradleParser.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/GradleParser.java index b2e95e92f65..970443b2ae3 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/GradleParser.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/GradleParser.java @@ -155,7 +155,6 @@ public GradleParser build() { public String getDslName() { return "gradle"; } - } private static List loadDefaultClasspath() { diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveDependency.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveDependency.java index dc235b87652..c622c1aa6d4 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveDependency.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveDependency.java @@ -102,7 +102,7 @@ public J visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) { } return requested; })); - newGdc = newGdc.withResolved(ListUtils.map(newGdc.getResolved(), resolved -> { + newGdc = newGdc.withDirectResolved(ListUtils.map(newGdc.getDirectResolved(), resolved -> { if (dependencyMatcher.matches(resolved.getGroupId(), resolved.getArtifactId())) { return null; } diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java index 75b545916b0..f11274ee64d 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeDependencyVersion.java @@ -19,7 +19,6 @@ import lombok.RequiredArgsConstructor; import lombok.Value; import org.openrewrite.*; -import org.openrewrite.gradle.dependency.DependencyVersionSelector; import org.openrewrite.gradle.marker.GradleDependencyConfiguration; import org.openrewrite.gradle.marker.GradleProject; import org.openrewrite.gradle.search.FindGradleProject; @@ -63,7 +62,7 @@ public class UpgradeDependencyVersion extends ScanningRecipe { + newGdc = newGdc.withDirectResolved(ListUtils.map(gdc.getDirectResolved(), resolved -> { if (!Objects.equals(resolved.getGroupId(), resolvedGav.getGroupId()) || !Objects.equals(resolved.getArtifactId(), resolvedGav.getArtifactId())) { return resolved; } diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/FeaturePreview.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/FeaturePreview.java new file mode 100644 index 00000000000..ad83d85f1c3 --- /dev/null +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/FeaturePreview.java @@ -0,0 +1,36 @@ +/* + * 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.gradle.marker; + +import lombok.Value; +import lombok.With; +import org.openrewrite.internal.lang.Nullable; + +import java.io.Serializable; + +@Value +@With +public class FeaturePreview implements Serializable { + /** + * The name of the feature preview + */ + String name; + + boolean active; + + @Nullable + Boolean enabled; +} diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleDependencyConfiguration.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleDependencyConfiguration.java new file mode 100644 index 00000000000..ae0bef91d6c --- /dev/null +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleDependencyConfiguration.java @@ -0,0 +1,140 @@ +/* + * Copyright 2022 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.gradle.marker; + +import lombok.Value; +import lombok.With; +import lombok.experimental.NonFinal; +import org.openrewrite.internal.StringUtils; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.maven.tree.Dependency; +import org.openrewrite.maven.tree.ResolvedDependency; + +import java.io.Serializable; +import java.util.*; + +@SuppressWarnings("unused") +@Value +@With +public class GradleDependencyConfiguration implements Serializable { + /** + * The name of the dependency configuration. Unique within a given project. + */ + String name; + + @Nullable + String description; + + boolean isTransitive; + + boolean isCanBeResolved; + + boolean isCanBeConsumed; + + /** + * The list of zero or more configurations this configuration extends from. + * The extended configuration's dependencies are all requested as part of this configuration, but different versions + * may be resolved. + */ + @NonFinal + List extendsFrom; + + List requested; + + /** + * The list of direct dependencies resolved for this configuration. + */ + List directResolved; + + /** + * The list of all dependencies resolved for this configuration, including transitive dependencies. + */ + public List getResolved() { + List resolved = new ArrayList<>(directResolved); + Set alreadyResolved = new HashSet<>(); + return resolveTransitiveDependencies(resolved, alreadyResolved); + } + + /** + * The type of exception thrown when attempting to resolve this configuration. null if no exception was thrown. + */ + @Nullable + String exceptionType; + + /** + * The message of the exception thrown when attempting to resolve this configuration. null if no exception was thrown. + */ + @Nullable + String message; + + /** + * List the configurations which are extended by the given configuration. + * Assuming a hierarchy like: + *

+     *     implementation
+     *     |> compileClasspath
+     *     |> runtimeClasspath
+     *     |> testImplementation
+     *        |> testCompileClasspath
+     *        |> testRuntimeClasspath
+     * 
+ *

+ * When querying "testCompileClasspath" this function will return [testImplementation, implementation]. + */ + public List allExtendsFrom() { + Set result = new LinkedHashSet<>(); + for (GradleDependencyConfiguration parentConfiguration : getExtendsFrom()) { + result.add(parentConfiguration); + result.addAll(parentConfiguration.allExtendsFrom()); + } + return new ArrayList<>(result); + } + + @Nullable + public Dependency findRequestedDependency(String groupId, String artifactId) { + for (Dependency d : requested) { + if (StringUtils.matchesGlob(d.getGav().getGroupId(), groupId) && + StringUtils.matchesGlob(d.getGav().getArtifactId(), artifactId)) { + return d; + } + } + return null; + } + + @Nullable + public ResolvedDependency findResolvedDependency(String groupId, String artifactId) { + for (ResolvedDependency d : directResolved) { + ResolvedDependency dependency = d.findDependency(groupId, artifactId); + if (dependency != null) { + return dependency; + } + } + return null; + } + + public void unsafeSetExtendsFrom(List extendsFrom) { + this.extendsFrom = extendsFrom; + } + + private static List resolveTransitiveDependencies(List resolved, Set alreadyResolved) { + for (ResolvedDependency dependency : resolved) { + if (alreadyResolved.add(dependency)) { + alreadyResolved.addAll(resolveTransitiveDependencies(dependency.getDependencies(), alreadyResolved)); + } + } + return new ArrayList<>(alreadyResolved); + } +} diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradlePluginDescriptor.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradlePluginDescriptor.java new file mode 100644 index 00000000000..934c971630c --- /dev/null +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradlePluginDescriptor.java @@ -0,0 +1,36 @@ +/* + * Copyright 2022 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.gradle.marker; + +import lombok.Value; +import org.openrewrite.internal.lang.Nullable; + +import java.io.Serializable; + +@Value +public class GradlePluginDescriptor implements Serializable { + /** + * The fully qualified name of the class which implements the plugin. + */ + String fullyQualifiedClassName; + + /** + * The ID by which a plugin can be applied in the plugins{} block. Not all Gradle plugins have an ID, including + * script plugins, or plugins which are implementation details of other plugins. + */ + @Nullable + String id; +} diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleProject.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleProject.java new file mode 100644 index 00000000000..3fdff204b06 --- /dev/null +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleProject.java @@ -0,0 +1,132 @@ +/* + * Copyright 2022 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.gradle.marker; + +import lombok.Value; +import lombok.With; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.marker.Marker; +import org.openrewrite.maven.tree.MavenRepository; + +import java.io.Serializable; +import java.util.*; + + +/** + * Contains metadata about a Gradle Project. Queried from Gradle itself when the OpenRewrite build plugin runs. + * Not automatically available on LSTs that aren't parsed through a Gradle plugin, so tests won't automatically have + * access to this metadata. + */ +@SuppressWarnings("unused") +@Value +public class GradleProject implements Marker, Serializable { + @With + UUID id; + + @With + String name; + + @With + String path; + + @With + List plugins; + + @With + List mavenRepositories; + + @With + List mavenPluginRepositories; + + Map nameToConfiguration; + + public List getMavenPluginRepositories() { + return mavenPluginRepositories == null ? Collections.emptyList() : mavenPluginRepositories; + } + + @Nullable + public GradleDependencyConfiguration getConfiguration(String name) { + return nameToConfiguration.get(name); + } + + public List getConfigurations() { + return new ArrayList<>(nameToConfiguration.values()); + } + + /** + * List the configurations which extend from the given configuration. + * Assuming a hierarchy like: + *

+     *     implementation
+     *     |> compileClasspath
+     *     |> runtimeClasspath
+     *     |> testImplementation
+     *        |> testCompileClasspath
+     *        |> testRuntimeClasspath
+     * 
+ *

+ * When querying "implementation" with transitive is false this function will return [compileClasspath, runtimeClasspath, testImplementation]. + * When transitive is true this function will also return [testCompileClasspath, testRuntimeClasspath]. + */ + public List configurationsExtendingFrom( + GradleDependencyConfiguration parentConfiguration, + boolean transitive + ) { + List result = new ArrayList<>(); + for (GradleDependencyConfiguration configuration : nameToConfiguration.values()) { + if (configuration == parentConfiguration) { + continue; + } + for (GradleDependencyConfiguration extendsFrom : configuration.getExtendsFrom()) { + if (extendsFrom == parentConfiguration) { + result.add(configuration); + if (transitive) { + result.addAll(configurationsExtendingFrom(configuration, true)); + } + } + } + } + return result; + } + + public GradleProject withNameToConfiguration(Map nameToConfiguration) { + Map configurations = new HashMap<>(nameToConfiguration); + for (GradleDependencyConfiguration gdc : configurations.values()) { + List extendsFromList = new ArrayList<>(gdc.getExtendsFrom()); + boolean changed = false; + for (int i = 0; i < extendsFromList.size(); i++) { + GradleDependencyConfiguration extendsFrom = extendsFromList.get(i); + if (configurations.get(extendsFrom.getName()) != extendsFrom) { + extendsFromList.set(i, configurations.get(extendsFrom.getName())); + changed = true; + } + } + if (changed) { + configurations.put(gdc.getName(), gdc.withExtendsFrom(extendsFromList)); + } + } + + return new GradleProject( + id, + name, + path, + plugins, + mavenRepositories, + mavenPluginRepositories, + configurations + ); + } +} diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleSettings.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleSettings.java new file mode 100644 index 00000000000..b6296cf41e0 --- /dev/null +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/GradleSettings.java @@ -0,0 +1,50 @@ +/* + * 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.gradle.marker; + +import lombok.Value; +import lombok.With; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.marker.Marker; +import org.openrewrite.maven.tree.MavenRepository; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.stream.Collectors; + +@Value +@With +public class GradleSettings implements Marker, Serializable { + UUID id; + List pluginRepositories; + List plugins; + Map featurePreviews; + + @Nullable + public Boolean isFeatureEnabled(String name) { + // Unclear how enabled status can be determined in latest gradle APIs + return null; + } + + public Set getActiveFeatures() { + return featurePreviews.values().stream() + .filter(FeaturePreview::isActive) + .collect(Collectors.toSet()); + } +} diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/package-info.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/package-info.java new file mode 100644 index 00000000000..96f6e264ba3 --- /dev/null +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/marker/package-info.java @@ -0,0 +1,19 @@ +/* + * Copyright 2021 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. + */ +@NonNullApi +package org.openrewrite.gradle.marker; + +import org.openrewrite.internal.lang.NonNullApi; diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java index 220812dd1a8..4ac17f74465 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java @@ -33,7 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.gradle.Assertions.buildGradle; import static org.openrewrite.gradle.Assertions.settingsGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; import static org.openrewrite.groovy.Assertions.groovy; import static org.openrewrite.groovy.Assertions.srcMainGroovy; import static org.openrewrite.java.Assertions.java; @@ -76,7 +76,7 @@ void onlyIfUsingTestScope(String onlyIfUsing) { plugins { id "java-library" } - + repositories { mavenCentral() } @@ -85,11 +85,11 @@ void onlyIfUsingTestScope(String onlyIfUsing) { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { testImplementation "com.google.guava:guava:29.0-jre" } @@ -115,11 +115,11 @@ void onlyIfUsingSmokeTestScope(String onlyIfUsing) { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" @@ -131,17 +131,17 @@ void onlyIfUsingSmokeTestScope(String onlyIfUsing) { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" } } - + dependencies { smokeTestImplementation "com.google.guava:guava:29.0-jre" } @@ -165,7 +165,7 @@ void onlyIfUsingCompileScope(String onlyIfUsing) { plugins { id 'java-library' } - + repositories { mavenCentral() } @@ -174,11 +174,11 @@ void onlyIfUsingCompileScope(String onlyIfUsing) { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:29.0-jre" } @@ -211,7 +211,7 @@ void onlyIfUsingMultipleScopes(String onlyIfUsing) { plugins { id "java-library" } - + repositories { mavenCentral() } @@ -220,11 +220,11 @@ void onlyIfUsingMultipleScopes(String onlyIfUsing) { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:29.0-jre" } @@ -257,11 +257,11 @@ void usedInMultipleSourceSetsUsingExplicitSourceSet(String onlyIfUsing) { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" @@ -273,17 +273,17 @@ void usedInMultipleSourceSetsUsingExplicitSourceSet(String onlyIfUsing) { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" } } - + dependencies { implementation "com.google.guava:guava:29.0-jre" } @@ -312,11 +312,11 @@ void usedInTransitiveSourceSet() { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" @@ -328,17 +328,17 @@ void usedInTransitiveSourceSet() { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "test" } } - + dependencies { testImplementation "com.google.guava:guava:29.0-jre" } @@ -367,11 +367,11 @@ void addDependencyIfNotUsedInATransitive() { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "main" @@ -383,20 +383,20 @@ void addDependencyIfNotUsedInATransitive() { id "java-library" id "com.netflix.nebula.facet" version "10.1.3" } - + repositories { mavenCentral() } - + facets { smokeTest { parentSourceSet = "main" } } - + dependencies { smokeTestImplementation "com.google.guava:guava:29.0-jre" - + testImplementation "com.google.guava:guava:29.0-jre" } """ @@ -419,7 +419,7 @@ void addDependencyWithClassifier() { plugins { id "java-library" } - + repositories { mavenCentral() } @@ -428,11 +428,11 @@ void addDependencyWithClassifier() { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { implementation "io.netty:netty-tcnative-boringssl-static:2.0.54.Final:linux-x86_64" } @@ -456,7 +456,7 @@ void addDependencyWithoutVersion() { plugins { id "java-library" } - + repositories { mavenCentral() } @@ -465,11 +465,11 @@ void addDependencyWithoutVersion() { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { implementation "io.netty:netty-tcnative-boringssl-static" } @@ -494,7 +494,7 @@ void addDependencyWithoutVersionWithClassifier() { plugins { id "java-library" } - + repositories { mavenCentral() } @@ -503,11 +503,11 @@ void addDependencyWithoutVersionWithClassifier() { plugins { id "java-library" } - + repositories { mavenCentral() } - + dependencies { implementation "io.netty:netty-tcnative-boringssl-static" } @@ -546,11 +546,11 @@ void addInOrder() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "commons-lang:commons-lang:1.0" } @@ -559,11 +559,11 @@ void addInOrder() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:29.0-jre" implementation "commons-lang:commons-lang:1.0" @@ -587,11 +587,11 @@ void addTestDependenciesAfterCompile() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "commons-lang:commons-lang:1.0" } @@ -600,11 +600,11 @@ void addTestDependenciesAfterCompile() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "commons-lang:commons-lang:1.0" @@ -629,11 +629,11 @@ void addDependenciesKeepFormatting() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "org.openrewrite:rewrite-core:7.40.8" testImplementation "junit:junit:4.12" @@ -643,11 +643,11 @@ void addDependenciesKeepFormatting() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "org.openrewrite:rewrite-core:7.40.8" implementation "org.slf4j:slf4j-api:2.0.7" @@ -680,11 +680,11 @@ class A { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "commons-lang:commons-lang:2.6" @@ -695,16 +695,16 @@ class A { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { annotationProcessor "org.projectlombok:lombok:1.18.26" - + implementation "commons-lang:commons-lang:2.6" - + testImplementation "junit:junit:4.13" } """ @@ -726,11 +726,11 @@ void addDependenciesToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" @@ -742,14 +742,14 @@ void addDependenciesToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + testImplementation group: "com.google.guava", name: "guava", version: "29.0-jre" def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion @@ -773,14 +773,14 @@ void addDependenciesWithoutVersionToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion } @@ -789,14 +789,14 @@ void addDependenciesWithoutVersionToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + testImplementation group: "com.google.guava", name: "guava" def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion @@ -820,14 +820,14 @@ void addDependenciesWithClassifierToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion } @@ -836,14 +836,14 @@ void addDependenciesWithClassifierToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + testImplementation group: "com.google.guava", name: "guava", version: "29.0-jre", classifier: "test" def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion @@ -868,14 +868,14 @@ void addDependenciesWithoutVersionWithClassifierToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion } @@ -884,14 +884,14 @@ void addDependenciesWithoutVersionWithClassifierToExistingGrouping() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" - + testImplementation group: "io.netty", name: "netty-tcnative-boringssl-static", classifier: "linux-x86_64" def junitVersion = "4.12" testImplementation group: "junit", name: "junit", version: junitVersion @@ -915,11 +915,11 @@ void matchesDependencyDeclarationStyle() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" } @@ -928,11 +928,11 @@ void matchesDependencyDeclarationStyle() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation group: "commons-lang", name: "commons-lang", version: "1.0" @@ -957,11 +957,11 @@ void addDependencyDoesntAddWhenExistingDependency() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:28.0-jre" } @@ -991,7 +991,7 @@ public class A { plugins { id 'java-library' } - + repositories { mavenCentral() } @@ -1000,11 +1000,11 @@ public class A { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { %s "com.fasterxml.jackson.core:jackson-core:2.12.0" } @@ -1038,7 +1038,7 @@ void addDependencyToProjectWithOtherSourceTypes() { plugins { id 'java-library' } - + repositories { mavenCentral() } @@ -1047,11 +1047,11 @@ void addDependencyToProjectWithOtherSourceTypes() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:29.0-jre" } @@ -1091,7 +1091,7 @@ void addDependencyToProjectsThatNeedIt() { plugins { id 'java-library' } - + repositories { mavenCentral() } @@ -1100,11 +1100,11 @@ void addDependencyToProjectsThatNeedIt() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation "com.google.guava:guava:29.0-jre" } @@ -1131,7 +1131,7 @@ void addDynamicVersionDependency() { groovy( """ import java.util.* - + class MyClass { static void main(String[] args) { Date date = new Date() @@ -1146,7 +1146,7 @@ static void main(String[] args) { plugins { id 'java' } - + repositories { mavenCentral() } @@ -1155,11 +1155,11 @@ static void main(String[] args) { plugins { id 'java' } - + repositories { mavenCentral() } - + dependencies { implementation "org.openrewrite:rewrite-core:7.39.1" } @@ -1196,24 +1196,24 @@ void addDependencyWithVariable() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + def guavaVersion = "29.0-jre" """, """ plugins { id 'java-library' } - + repositories { mavenCentral() } - + def guavaVersion = "29.0-jre" - + dependencies { implementation "com.google.guava:guava:${guavaVersion}" } @@ -1238,7 +1238,7 @@ void defaultConfigurationEscaped() { plugins { id 'java' } - + repositories { mavenCentral() } @@ -1247,11 +1247,11 @@ void defaultConfigurationEscaped() { plugins { id 'java' } - + repositories { mavenCentral() } - + dependencies { 'default' "com.google.guava:guava:29.0-jre" } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDirectDependencyToUpgradeTransitiveVersionTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDirectDependencyToUpgradeTransitiveVersionTest.java new file mode 100644 index 00000000000..98406e59545 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDirectDependencyToUpgradeTransitiveVersionTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2024 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.gradle; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; + +public class AddDirectDependencyToUpgradeTransitiveVersionTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .beforeRecipe(withToolingApi()) + .recipe(new AddDirectDependencyToUpgradeTransitiveVersion( + "com.fasterxml*", "jackson*", "latest.patch", null)); + } + + @Disabled + @Test + void addDirectDependency() { + rewriteRun( + buildGradle( + """ + plugins { id 'java' } + repositories { mavenCentral() } + + dependencies { + implementation 'org.openrewrite:rewrite-java:7.0.0' + } + """ + ), + buildGradle( + """ + plugins { id 'java' } + repositories { mavenCentral() } + + dependencies { + implementation 'org.openrewrite:rewrite-java:7.0.0' + implementation 'com.fasterxml.jackson:jackson-core:2.12.3' + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyTest.java index 22adbb388a8..cb9d2575c0c 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyTest.java @@ -21,7 +21,7 @@ import org.openrewrite.test.RewriteTest; import static org.openrewrite.gradle.Assertions.buildGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; class ChangeDependencyTest implements RewriteTest { @Override diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveDependencyTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveDependencyTest.java index f9581eed063..0f82ace29ce 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveDependencyTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveDependencyTest.java @@ -26,7 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.gradle.Assertions.buildGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; class RemoveDependencyTest implements RewriteTest { diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java index e3d35f96328..1a7bb3bab4e 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradeDependencyVersionTest.java @@ -29,7 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.gradle.Assertions.buildGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; import static org.openrewrite.properties.Assertions.properties; class UpgradeDependencyVersionTest implements RewriteTest { @@ -49,11 +49,11 @@ void guava() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { compileOnly 'com.google.guava:guava:29.0-jre' runtimeOnly ('com.google.guava:guava:29.0-jre') @@ -63,11 +63,11 @@ void guava() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { compileOnly 'com.google.guava:guava:30.1.1-jre' runtimeOnly ('com.google.guava:guava:30.1.1-jre') @@ -104,7 +104,7 @@ void noRepos() { plugins { id 'java-library' } - + dependencies { compileOnly 'com.google.guava:guava:29.0-jre' } @@ -113,7 +113,7 @@ void noRepos() { plugins { id 'java-library' } - + dependencies { /*~~(com.google.guava:guava failed. Unable to download metadata.)~~>*/compileOnly 'com.google.guava:guava:29.0-jre' } @@ -127,8 +127,8 @@ void noReposProperties() { rewriteRun( properties( """ - guavaVersion=29.0-jre - """, + guavaVersion=29.0-jre + """, spec -> spec.path("gradle.properties") ), buildGradle( @@ -136,7 +136,7 @@ void noReposProperties() { plugins { id 'java-library' } - + dependencies { compileOnly "com.google.guava:guava:${guavaVersion}" } @@ -145,7 +145,7 @@ void noReposProperties() { plugins { id 'java-library' } - + dependencies { /*~~(com.google.guava:guava failed. Unable to download metadata.)~~>*/compileOnly "com.google.guava:guava:${guavaVersion}" } @@ -162,13 +162,13 @@ void updateVersionInVariable() { plugins { id 'java-library' } - + def guavaVersion = '29.0-jre' def otherVersion = "latest.release" repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") implementation "com.fasterxml.jackson.core:jackson-databind:$otherVersion" @@ -178,13 +178,13 @@ void updateVersionInVariable() { plugins { id 'java-library' } - + def guavaVersion = '30.1.1-jre' def otherVersion = "latest.release" repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") implementation "com.fasterxml.jackson.core:jackson-databind:$otherVersion" @@ -202,11 +202,11 @@ void varargsDependency() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation( 'com.google.guava:guava-gwt:29.0-jre', @@ -217,11 +217,11 @@ void varargsDependency() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation( 'com.google.guava:guava-gwt:29.0-jre', @@ -239,12 +239,12 @@ void mapNotationVariable() { plugins { id 'java-library' } - + def guavaVersion = '29.0-jre' repositories { mavenCentral() } - + dependencies { implementation group: "com.google.guava", name: "guava", version: guavaVersion } @@ -253,12 +253,12 @@ void mapNotationVariable() { plugins { id 'java-library' } - + def guavaVersion = '30.1.1-jre' repositories { mavenCentral() } - + dependencies { implementation group: "com.google.guava", name: "guava", version: guavaVersion } @@ -275,11 +275,11 @@ void mapNotationLiteral() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation (group: "com.google.guava", name: "guava", version: '29.0-jre') } @@ -288,11 +288,11 @@ void mapNotationLiteral() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation (group: "com.google.guava", name: "guava", version: '30.1.1-jre') } @@ -310,11 +310,11 @@ void worksWithPlatform() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation platform("com.google.guava:guava:29.0-jre") } @@ -323,11 +323,11 @@ implementation platform("com.google.guava:guava:29.0-jre") plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation platform("com.google.guava:guava:30.1.1-jre") } @@ -411,15 +411,15 @@ void matchesGlobs() { plugins { id "java" } - + repositories { mavenCentral() } - + ext { guavaVersion = "29.0-jre" } - + def guavaVersion2 = "29.0-jre" dependencies { implementation("com.google.guava:guava:29.0-jre") @@ -432,15 +432,15 @@ void matchesGlobs() { plugins { id "java" } - + repositories { mavenCentral() } - + ext { guavaVersion = "30.1.1-jre" } - + def guavaVersion2 = "30.1.1-jre" dependencies { implementation("com.google.guava:guava:30.1.1-jre") @@ -462,11 +462,11 @@ void defaultsToLatestRelease() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation 'com.google.guava:guava:29.0-jre' } @@ -481,11 +481,11 @@ void defaultsToLatestRelease() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation 'com.google.guava:guava:%s' } @@ -512,11 +512,11 @@ void versionInPropertiesFile() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") } @@ -539,11 +539,11 @@ void versionInPropertiesFileNotUpdatedIfNoDependencyVariable() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:30.1.1-jre") } @@ -578,11 +578,11 @@ void versionInParentAndMultiModulePropertiesFiles() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") } @@ -594,11 +594,11 @@ void versionInParentAndMultiModulePropertiesFiles() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") } @@ -626,7 +626,7 @@ void versionInParentSubprojectDefinitionWithPropertiesFiles() { plugins { id 'java-library' } - + repositories { mavenCentral() } @@ -671,11 +671,11 @@ void versionOnlyInMultiModuleChildPropertiesFiles() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation ("com.google.guava:guava:$guavaVersion") } @@ -706,7 +706,7 @@ void mapNotationVariableInPropertiesFile() { repositories { mavenCentral() } - + dependencies { implementation group: "com.google.guava", name: "guava", version: guavaVersion } @@ -736,7 +736,7 @@ void mapNotationGStringVariableInPropertiesFile() { repositories { mavenCentral() } - + dependencies { implementation group: "com.google.guava", name: "guava", version: "${guavaVersion}" } @@ -818,11 +818,11 @@ void dontDowngradeWhenExactVersion() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation 'com.google.guava:guava:29.0-jre' } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddBuildPluginTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddBuildPluginTest.java index 6d5cb9bd7b4..dd31f64ad57 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddBuildPluginTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddBuildPluginTest.java @@ -23,7 +23,7 @@ import static org.openrewrite.Tree.randomId; import static org.openrewrite.gradle.Assertions.buildGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; class AddBuildPluginTest implements RewriteTest { @Override @@ -129,16 +129,16 @@ void addPluginAfterBuildscriptBlock() { buildGradle( """ import java.util.List - + buildscript { } """, """ import java.util.List - + buildscript { } - + plugins { id 'com.jfrog.bintray' version '1.0' } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePluginTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePluginTest.java index da6cbb35379..ff9bc6e20cf 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePluginTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddDevelocityGradlePluginTest.java @@ -35,7 +35,7 @@ import static org.openrewrite.Tree.randomId; import static org.openrewrite.gradle.Assertions.buildGradle; import static org.openrewrite.gradle.Assertions.settingsGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; import static org.openrewrite.test.SourceSpecs.dir; class AddDevelocityGradlePluginTest implements RewriteTest { @@ -43,7 +43,7 @@ class AddDevelocityGradlePluginTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.beforeRecipe(withToolingApi()) - .recipe(new AddDevelocityGradlePlugin("3.x", null, null, null, null, null)); + .recipe(new AddDevelocityGradlePlugin("3.x", null, null, null, null, null)); } private static Consumer> interpolateResolvedVersion(@Language("groovy") String after) { @@ -140,7 +140,7 @@ void addNewSettingsPluginsBlock() { plugins { id 'com.gradle.enterprise' version '%s' } - + rootProject.name = 'my-project' """ ) @@ -159,14 +159,14 @@ void addExistingSettingsPluginsBlock() { """ plugins { } - + rootProject.name = 'my-project' """, interpolateResolvedVersion(""" plugins { id 'com.gradle.enterprise' version '%s' } - + rootProject.name = 'my-project' """ ) diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddSettingsPluginTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddSettingsPluginTest.java index 1625f27db1a..e469d1d574f 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddSettingsPluginTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddSettingsPluginTest.java @@ -28,13 +28,13 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.gradle.Assertions.settingsGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; class AddSettingsPluginTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.beforeRecipe(withToolingApi()) - .recipe(new AddSettingsPlugin("com.gradle.enterprise", "3.11.x", null)); + .recipe(new AddSettingsPlugin("com.gradle.enterprise", "3.11.x", null)); } @Test @@ -62,12 +62,12 @@ void addPluginToNewBlock() { """, interpolateResolvedVersion( """ - plugins { - id 'com.gradle.enterprise' version '%s' - } + plugins { + id 'com.gradle.enterprise' version '%s' + } - rootProject.name = 'my-project' - """ + rootProject.name = 'my-project' + """ ) ) ); @@ -80,17 +80,17 @@ void addPluginToExistingBlock() { """ plugins { } - + rootProject.name = 'my-project' """, interpolateResolvedVersion( """ - plugins { - id 'com.gradle.enterprise' version '%s' - } - - rootProject.name = 'my-project' - """ + plugins { + id 'com.gradle.enterprise' version '%s' + } + + rootProject.name = 'my-project' + """ ) ) ); @@ -111,18 +111,18 @@ void addPluginWithPluginManagementBlock() { """, interpolateResolvedVersion( """ - pluginManagement { - repositories { - gradlePluginPortal() - } - } + pluginManagement { + repositories { + gradlePluginPortal() + } + } - plugins { - id 'com.gradle.enterprise' version '%s' - } + plugins { + id 'com.gradle.enterprise' version '%s' + } - rootProject.name = 'my-project' - """ + rootProject.name = 'my-project' + """ ) ) ); diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/ChangePluginTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/ChangePluginTest.java index 13f768ce15d..5c8b1c8644c 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/ChangePluginTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/ChangePluginTest.java @@ -31,7 +31,7 @@ import static java.util.Objects.requireNonNull; import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.gradle.Assertions.buildGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; class ChangePluginTest implements RewriteTest { @Override diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/UpgradePluginVersionTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/UpgradePluginVersionTest.java index 72f199c97ed..f40eb40c563 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/UpgradePluginVersionTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/UpgradePluginVersionTest.java @@ -29,7 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.gradle.Assertions.buildGradle; import static org.openrewrite.gradle.Assertions.settingsGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; import static org.openrewrite.properties.Assertions.properties; class UpgradePluginVersionTest implements RewriteTest { @@ -172,8 +172,8 @@ void dontDowngradeWhenExactVersionProperties() { ), properties( """ - springDependencyManagementVersion=1.1.0 - """, + springDependencyManagementVersion=1.1.0 + """, spec -> spec.path("gradle.properties") ) ); diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/DependencyInsightTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/DependencyInsightTest.java index 74c9c585a80..c5a0e1b990c 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/DependencyInsightTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/DependencyInsightTest.java @@ -23,14 +23,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.gradle.Assertions.buildGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; class DependencyInsightTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.beforeRecipe(withToolingApi()) - .recipe(new DependencyInsight("com.google.guava", "failureaccess", null,null)); + .recipe(new DependencyInsight("com.google.guava", "failureaccess", null, null)); } @DocumentExample @@ -41,11 +41,11 @@ void findTransitiveDependency() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation 'com.google.guava:guava:31.1-jre' } @@ -54,11 +54,11 @@ void findTransitiveDependency() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { /*~~(com.google.guava:failureaccess:1.0.1)~~>*/implementation 'com.google.guava:guava:31.1-jre' } @@ -72,18 +72,18 @@ void recursive() { rewriteRun( spec -> spec.recipe(new DependencyInsight("doesnotexist", "doesnotexist", null, null)), buildGradle(""" - plugins { - id 'java-library' - } - - repositories { - mavenCentral() - } - - dependencies { - implementation 'io.grpc:grpc-services:1.59.0' - } - """ + plugins { + id 'java-library' + } + + repositories { + mavenCentral() + } + + dependencies { + implementation 'io.grpc:grpc-services:1.59.0' + } + """ ) ); } @@ -91,7 +91,7 @@ void recursive() { @Test void pattern() { rewriteRun( - spec -> spec.recipe(new DependencyInsight("*", "jackson-core", null, null)) + spec -> spec.recipe(new DependencyInsight("*", "jackson-core", null, null)) .dataTable(DependenciesInUse.Row.class, rows -> { assertThat(rows).isNotEmpty(); DependenciesInUse.Row row = rows.get(0); @@ -103,11 +103,11 @@ void pattern() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation 'org.openrewrite:rewrite-core:7.39.1' } @@ -116,11 +116,11 @@ void pattern() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { /*~~(com.fasterxml.jackson.core:jackson-core:2.13.4)~~>*/implementation 'org.openrewrite:rewrite-core:7.39.1' } @@ -132,16 +132,16 @@ void pattern() { @Test void versionSearch() { rewriteRun( - spec -> spec.recipe(new DependencyInsight("org.openrewrite", "*", "7.0.0", null)), + spec -> spec.recipe(new DependencyInsight("org.openrewrite", "*", "7.0.0", null)), buildGradle(""" plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { implementation 'org.openrewrite:rewrite-yaml:7.0.0' implementation 'org.openrewrite:rewrite-java:8.0.0' @@ -151,11 +151,11 @@ void versionSearch() { plugins { id 'java-library' } - + repositories { mavenCentral() } - + dependencies { /*~~(org.openrewrite:rewrite-yaml:7.0.0)~~>*/implementation 'org.openrewrite:rewrite-yaml:7.0.0' implementation 'org.openrewrite:rewrite-java:8.0.0' diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindGradleProjectTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindGradleProjectTest.java index 7d7ec03f24f..77aac1afae2 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindGradleProjectTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindGradleProjectTest.java @@ -23,7 +23,7 @@ import org.openrewrite.test.RewriteTest; import static org.openrewrite.gradle.Assertions.buildGradle; -import static org.openrewrite.gradle.Assertions.withToolingApi; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; import static org.openrewrite.maven.Assertions.pomXml; import static org.openrewrite.test.SourceSpecs.text; @@ -60,7 +60,6 @@ void isGradleGroovyProject(FindGradleProject.SearchCriteria criteria) { void isGradleKotlinProject() { rewriteRun( text( - //language=kotlin """ plugins { id("java")