Skip to content

Commit

Permalink
Move DependencyList recipe to this module from rewrite-all
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Apr 7, 2023
1 parent f771488 commit 5789fbf
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 2 deletions.
7 changes: 5 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ description = "A rewrite module automating Java dependency management."

val rewriteVersion = rewriteRecipe.rewriteVersion.get()
dependencies {
implementation("org.openrewrite:rewrite-maven:$rewriteVersion")
implementation("org.openrewrite:rewrite-gradle:$rewriteVersion")
implementation(platform("org.openrewrite:rewrite-bom:$rewriteVersion"))
implementation("org.openrewrite:rewrite-maven")
implementation("org.openrewrite:rewrite-gradle")
implementation("org.openrewrite:rewrite-groovy")

implementation("org.openrewrite.gradle.tooling:model:latest.release")

Expand All @@ -17,6 +19,7 @@ dependencies {
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-csv")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

runtimeOnly("org.gradle:gradle-tooling-api:latest.release")
compileOnly("org.projectlombok:lombok:latest.release")
annotationProcessor("org.projectlombok:lombok:latest.release")
testRuntimeOnly("ch.qos.logback:logback-classic:1.2.+")
Expand Down
143 changes: 143 additions & 0 deletions src/main/java/org/openrewrite/java/dependencies/DependencyList.java
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);
}
}
}
}
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;
}
}
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>
""")
);
}
}

0 comments on commit 5789fbf

Please sign in to comment.