-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move DependencyList recipe to this module from rewrite-all
- Loading branch information
Showing
4 changed files
with
284 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/main/java/org/openrewrite/java/dependencies/DependencyList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.dependencies; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Option; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.SourceFile; | ||
import org.openrewrite.gradle.marker.GradleDependencyConfiguration; | ||
import org.openrewrite.gradle.marker.GradleProject; | ||
import org.openrewrite.java.dependencies.table.DependencyListReport; | ||
import org.openrewrite.marker.Markers; | ||
import org.openrewrite.maven.tree.MavenResolutionResult; | ||
import org.openrewrite.maven.tree.ResolvedDependency; | ||
|
||
import java.util.List; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = true) | ||
public class DependencyList extends Recipe { | ||
|
||
transient DependencyListReport report = new DependencyListReport(this); | ||
|
||
@Option(displayName = "Scope", | ||
description = "The scope of the dependencies to include in the report.", | ||
valid = {"Compile", "Runtime"}, | ||
example = "Compile") | ||
Scope scope; | ||
|
||
@Option(displayName = "Include transitive dependencies", | ||
description = "Whether or not to include transitive dependencies in the report. " + | ||
"Defaults to including only direct dependencies.", | ||
example = "true") | ||
boolean includeTransitive; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Dependency report"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Emits a data table detailing all Gradle and Maven dependencies." + | ||
"This recipe makes no changes to any source file."; | ||
} | ||
|
||
@Override | ||
protected List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx) { | ||
for(SourceFile s : before) { | ||
Markers m = s.getMarkers(); | ||
m.findFirst(GradleProject.class).ifPresent(gradle -> { | ||
GradleDependencyConfiguration conf = gradle.getConfiguration(scope.asGradleConfigurationName()); | ||
if(conf != null) { | ||
for (ResolvedDependency dep : conf.getResolved()) { | ||
insertDependency(ctx, gradle, dep); | ||
} | ||
} | ||
}); | ||
m.findFirst(MavenResolutionResult.class).ifPresent(maven -> { | ||
for (ResolvedDependency dep : maven.getDependencies().get(scope.asMavenScope())) { | ||
insertDependency(ctx, maven, dep); | ||
} | ||
}); | ||
} | ||
return before; | ||
} | ||
|
||
private void insertDependency(ExecutionContext ctx, GradleProject gradle, ResolvedDependency dep) { | ||
report.insertRow(ctx, new DependencyListReport.Row( | ||
"Gradle", | ||
"", | ||
gradle.getName(), | ||
"", | ||
dep.getGroupId(), | ||
dep.getArtifactId(), | ||
dep.getVersion() | ||
)); | ||
if(includeTransitive) { | ||
for (ResolvedDependency transitive : dep.getDependencies()) { | ||
insertDependency(ctx, gradle, transitive); | ||
} | ||
} | ||
} | ||
|
||
private void insertDependency(ExecutionContext ctx, MavenResolutionResult maven, ResolvedDependency dep) { | ||
report.insertRow(ctx, new DependencyListReport.Row( | ||
"Maven", | ||
maven.getPom().getGroupId(), | ||
maven.getPom().getArtifactId(), | ||
maven.getPom().getVersion(), | ||
dep.getGroupId(), | ||
dep.getArtifactId(), | ||
dep.getVersion() | ||
)); | ||
if(includeTransitive) { | ||
for (ResolvedDependency transitive : dep.getDependencies()) { | ||
insertDependency(ctx, maven, transitive); | ||
} | ||
} | ||
} | ||
|
||
public enum Scope { | ||
Compile, | ||
Runtime; | ||
|
||
public org.openrewrite.maven.tree.Scope asMavenScope() { | ||
switch (this) { | ||
case Compile: | ||
return org.openrewrite.maven.tree.Scope.Compile; | ||
case Runtime: | ||
return org.openrewrite.maven.tree.Scope.Runtime; | ||
default: | ||
throw new IllegalStateException("Unexpected value: " + this); | ||
} | ||
} | ||
|
||
public String asGradleConfigurationName() { | ||
switch (this) { | ||
case Compile: | ||
return "compileClasspath"; | ||
case Runtime: | ||
return "runtimeClasspath"; | ||
default: | ||
throw new IllegalStateException("Unexpected value: " + this); | ||
} | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/org/openrewrite/java/dependencies/table/DependencyListReport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.dependencies.table; | ||
|
||
import lombok.Value; | ||
import org.openrewrite.Column; | ||
import org.openrewrite.DataTable; | ||
import org.openrewrite.Recipe; | ||
|
||
public class DependencyListReport extends DataTable<DependencyListReport.Row> { | ||
|
||
public DependencyListReport(Recipe recipe) { | ||
super(recipe, | ||
"Dependency report", | ||
"Lists all Gradle and Maven dependencies"); | ||
} | ||
|
||
@Value | ||
public static class Row { | ||
|
||
@Column(displayName = "Build tool", | ||
description = "The build tool used to manage dependencies (Gradle or Maven).") | ||
String buildTool; | ||
|
||
@Column(displayName = "Group id", | ||
description = "The Group ID of the Gradle project or Maven module requesting the dependency.") | ||
String groupId; | ||
|
||
@Column(displayName = "Artifact id", | ||
description = "The Artifact ID of the Gradle project or Maven module requesting the dependency.") | ||
String artifactId; | ||
|
||
@Column(displayName = "Version", | ||
description = "The version of Gradle project or Maven module requesting the dependency.") | ||
String version; | ||
|
||
String dependencyGroupId; | ||
|
||
String dependencyArtifactId; | ||
|
||
String dependencyVersion; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/test/java/org/openrewrite/java/dependencies/DependencyListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.dependencies; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.java.dependencies.table.DependencyListReport; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
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.maven.Assertions.pomXml; | ||
|
||
public class DependencyListTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new DependencyList(DependencyList.Scope.Compile, false)); | ||
} | ||
|
||
@Test | ||
void basic() { | ||
rewriteRun( | ||
spec -> spec | ||
.beforeRecipe(withToolingApi()) | ||
.dataTable(DependencyListReport.Row.class, rows -> { | ||
assertThat(rows).isNotEmpty(); | ||
assertThat(rows) | ||
.filteredOn( it -> it.getBuildTool().equals("Maven") && it.getDependencyArtifactId().equals("rewrite-core")) | ||
.hasSize(1); | ||
assertThat(rows) | ||
.filteredOn( it -> it.getBuildTool().equals("Gradle") && it.getDependencyArtifactId().equals("rewrite-core")) | ||
.hasSize(1); | ||
}), | ||
//language=groovy | ||
buildGradle(""" | ||
plugins { | ||
id 'java' | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
implementation('org.openrewrite:rewrite-core:7.39.0') | ||
} | ||
"""), | ||
//language=xml | ||
pomXml(""" | ||
<project> | ||
<groupId>com.mycompany.app</groupId> | ||
<artifactId>my-app</artifactId> | ||
<version>1</version> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.openrewrite</groupId> | ||
<artifactId>rewrite-core</artifactId> | ||
<version>7.39.0</version> | ||
</dependency> | ||
</dependencies> | ||
</project> | ||
""") | ||
); | ||
} | ||
} |