Skip to content

Commit

Permalink
Use provided configuration for optional language dependencies (#411)
Browse files Browse the repository at this point in the history
Set groovy/kotlin/csharp as provided. Dependencies listed as "provided" will show up in the published pom with that scope, but otherwise are treated just like any other dependency placed into `compileOnly` and `testImplementation`.

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Sam Snyder <[email protected]>
  • Loading branch information
3 people authored Jan 3, 2025
1 parent 7189cb1 commit 0a5c49b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
13 changes: 8 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@file:Suppress("UnstableApiUsage")
plugins {
id("org.openrewrite.build.recipe-library") version "latest.release"
id("org.openrewrite.build.moderne-source-available-license") version "latest.release"
Expand All @@ -6,6 +7,7 @@ plugins {
group = "org.openrewrite.recipe"
description = "The first Static Analysis and REMEDIATION tool"

val provided = configurations.named("provided")
val rewriteVersion = rewriteRecipe.rewriteVersion.get()
dependencies {
compileOnly("org.projectlombok:lombok:latest.release")
Expand All @@ -14,21 +16,22 @@ dependencies {

implementation(platform("org.openrewrite:rewrite-bom:${rewriteVersion}"))
implementation("org.openrewrite:rewrite-java")
implementation("org.openrewrite:rewrite-groovy:${rewriteVersion}")
implementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}")
implementation("org.openrewrite:rewrite-csharp:${rewriteVersion}")
implementation("org.openrewrite.meta:rewrite-analysis:${rewriteVersion}")
implementation("org.apache.commons:commons-text:latest.release")

// Limit transitive dependencies for downstream projects like rewrite-spring
provided("org.openrewrite:rewrite-groovy:${rewriteVersion}")
provided("org.openrewrite:rewrite-kotlin:${rewriteVersion}")
provided("org.openrewrite:rewrite-csharp:${rewriteVersion}")

annotationProcessor("org.openrewrite:rewrite-templating:${rewriteVersion}")
implementation("org.openrewrite:rewrite-templating:${rewriteVersion}")
compileOnly("com.google.errorprone:error_prone_core:2.+:with-dependencies") {
exclude("com.google.auto.service", "auto-service-annotations")
}

testImplementation("org.jetbrains:annotations:24.+")
testImplementation("org.openrewrite:rewrite-groovy")
testImplementation("org.openrewrite:rewrite-test")
testImplementation("org.jetbrains:annotations:24.+")
testImplementation("org.junit-pioneer:junit-pioneer:2.+")
testImplementation("junit:junit:4.13.2")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.csharp.tree.Cs;
import org.openrewrite.internal.ReflectionUtils;
import org.openrewrite.marker.SearchResult;

/**
* Add a search marker if vising a CSharp file
*/
public class CSharpFileChecker<P> extends TreeVisitor<Tree, P> {
private static final boolean IS_CSHARP_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.csharp.tree.Cs");

@Override
public @Nullable Tree visit(@Nullable Tree tree, P p) {
if (tree instanceof Cs.CompilationUnit) {
if (IS_CSHARP_AVAILABLE && tree instanceof Cs.CompilationUnit) {
return SearchResult.found(tree);
}
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ReflectionUtils;
import org.openrewrite.marker.SearchResult;

/**
* Add a search marker if vising a Groovy file
*/
public class GroovyFileChecker<P> extends TreeVisitor<Tree, P> {
private static final boolean IS_GROOVY_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.groovy.tree.G");

@Override
public @Nullable Tree visit(@Nullable Tree tree, P p) {
if (tree instanceof G.CompilationUnit) {
if (IS_GROOVY_AVAILABLE && tree instanceof G.CompilationUnit) {
return SearchResult.found(tree);
}
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ReflectionUtils;
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.marker.SearchResult;

/**
* Add a search marker if vising a Kotlin file
*/
public class KotlinFileChecker<P> extends TreeVisitor<Tree, P> {
private static final boolean IS_KOTLIN_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.kotlin.tree.K");

@Override
public @Nullable Tree visit(@Nullable Tree tree, P p) {
if (tree instanceof K.CompilationUnit) {
if (IS_KOTLIN_AVAILABLE && tree instanceof K.CompilationUnit) {
return SearchResult.found(tree);
}
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,9 @@ void foo() {
@Test
void recordCompactConstructor() {
rewriteRun(
version(java(
version(
//language=java
java(
"""
public record MyRecord(
boolean bar,
Expand All @@ -999,6 +1001,7 @@ public record MyRecord(
@Test
void removeKotlinUnusedLocalVariable() {
rewriteRun(
//language=kotlin
kotlin(
"""
class A (val b: String) {
Expand Down Expand Up @@ -1101,12 +1104,13 @@ class Kotlin {
@Test
void retainUnusedLocalVariableWithNewClass() {
rewriteRun(
//language=kotlin
kotlin(
"""
class A {}
class B {
fun foo() {
val a = A();
val a = A()
}
}
"""
Expand Down

0 comments on commit 0a5c49b

Please sign in to comment.