Skip to content

Commit

Permalink
Start implementing unlessUsing property
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Nov 13, 2024
1 parent 49462b2 commit eaf467a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,37 @@
import java.util.Arrays;
import java.util.List;

import static java.util.Collections.emptyList;

@Value
@EqualsAndHashCode(callSuper = false)
public class RemoveDependency extends Recipe {
@Option(displayName = "Group ID",
description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression.",
example = "com.fasterxml.jackson*")
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 ID",
description = "The second part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression.",
example = "jackson-module*")
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 = "Unless using", description = "If a dependency is used in the code, do not remove it.")
@Nullable
Boolean unlessUsing;

// Gradle only parameter
@Option(displayName = "The dependency configuration", description = "The dependency configuration to remove from.", example = "api", required = false)
@Nullable
String configuration;

// Maven only parameter
@Option(displayName = "Scope",
description = "Only remove dependencies if they are in this scope. If 'runtime', this will" +
"also remove dependencies in the 'compile' scope because 'compile' dependencies are part of the runtime dependency set",
valid = {"compile", "test", "runtime", "provided"},
example = "compile",
required = false)
description = "Only remove dependencies if they are in this scope. If 'runtime', this will" +
"also remove dependencies in the 'compile' scope because 'compile' dependencies are part of the runtime dependency set",
valid = {"compile", "test", "runtime", "provided"},
example = "compile",
required = false)
@Nullable
String scope;

Expand All @@ -61,20 +66,22 @@ public String getDisplayName() {
@Override
public String getDescription() {
return "For Gradle project, removes a single dependency from the dependencies section of the `build.gradle`.\n" +
"For Maven project, removes a single dependency from the <dependencies> section of the pom.xml.";
"For Maven project, removes a single dependency from the <dependencies> section of the pom.xml.";
}

org.openrewrite.gradle.@Nullable RemoveDependency removeGradleDependency;

org.openrewrite.maven.@Nullable RemoveDependency removeMavenDependency;

public RemoveDependency(
String groupId,
String artifactId,
@Nullable String configuration,
@Nullable String scope) {
String groupId,
String artifactId,
@Nullable Boolean unlessUsing,
@Nullable String configuration,
@Nullable String scope) {
this.groupId = groupId;
this.artifactId = artifactId;
this.unlessUsing = unlessUsing;
this.configuration = configuration;
this.scope = scope;
removeGradleDependency = new org.openrewrite.gradle.RemoveDependency(groupId, artifactId, configuration);
Expand All @@ -83,6 +90,10 @@ public RemoveDependency(

@Override
public List<Recipe> getRecipeList() {
if (Boolean.TRUE.equals(unlessUsing)) {
// logic to check if code uses the lib, if so ->
return emptyList();
}
return Arrays.asList(removeGradleDependency, removeMavenDependency);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ class RemoveDependencyTest implements RewriteTest {
void removeGradleDependencyUsingStringNotationWithExclusion() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi())
.recipe(new RemoveDependency("org.springframework.boot", "spring-boot*", null, null)),
.recipe(new RemoveDependency("org.springframework.boot", "spring-boot*", false, null, null)),
//language=groovy
buildGradle(
"""
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:2.7.0") {
exclude group: "junit"
Expand All @@ -53,11 +53,11 @@ void removeGradleDependencyUsingStringNotationWithExclusion() {
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation "org.junit.vintage:junit-vintage-engine:5.6.2"
}
Expand All @@ -70,17 +70,17 @@ void removeGradleDependencyUsingStringNotationWithExclusion() {
@Test
void removeMavenDependency() {
rewriteRun(
spec -> spec.recipe(new RemoveDependency("junit", "junit", null, null)),
spec -> spec.recipe(new RemoveDependency("junit", "junit", false, null, null)),
//language=xml
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
Expand All @@ -99,11 +99,11 @@ void removeMavenDependency() {
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
Expand All @@ -116,4 +116,31 @@ void removeMavenDependency() {
)
);
}

@Test
void doNotRemoveDependencyIfItIsUsedAndUnlessUsingIsTrue() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi())
.recipe(new RemoveDependency("org.springframework.boot", "spring-boot*", true, null, null)),
//language=groovy
buildGradle(
"""
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:2.7.0") {
exclude group: "junit"
}
testImplementation "org.junit.vintage:junit-vintage-engine:5.6.2"
}
"""
)
);
}
}

0 comments on commit eaf467a

Please sign in to comment.