Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use provided configuration for optional language dependencies #411

9 changes: 6 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ 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}")
compileOnly("org.openrewrite:rewrite-groovy:${rewriteVersion}")
testImplementation("org.openrewrite:rewrite-groovy:${rewriteVersion}")
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
compileOnly("org.openrewrite:rewrite-kotlin:${rewriteVersion}")
testImplementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}")
compileOnly("org.openrewrite:rewrite-csharp:${rewriteVersion}")
testImplementation("org.openrewrite:rewrite-csharp:${rewriteVersion}")
implementation("org.openrewrite.meta:rewrite-analysis:${rewriteVersion}")
implementation("org.apache.commons:commons-text:latest.release")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.csharp.tree.Cs;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.staticanalysis.internal.ClassPathUtils;

/**
* Add a search marker if vising a CSharp file
*/
public class CSharpFileChecker<P> extends TreeVisitor<Tree, P> {
private static final boolean IS_CSHARP_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.csharp.tree.Cs$CompilationUnit");
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved

@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 @@ -20,14 +20,17 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.staticanalysis.internal.ClassPathUtils;

/**
* Add a search marker if vising a Groovy file
*/
public class GroovyFileChecker<P> extends TreeVisitor<Tree, P> {
private static final boolean IS_GROOVY_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.groovy.tree.G$CompilationUnit");
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved

@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
@@ -0,0 +1,30 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
* <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.staticanalysis.internal;

import lombok.experimental.UtilityClass;

@UtilityClass
public class ClassPathUtils {
public static boolean isAvailable(String className) {
try {
Class.forName(className);
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.staticanalysis.internal.ClassPathUtils;

/**
* Add a search marker if vising a Kotlin file
*/
public class KotlinFileChecker<P> extends TreeVisitor<Tree, P> {
private static final boolean IS_KOTLIN_AVAILABLE = ClassPathUtils.isAvailable("org.openrewrite.kotlin.tree.K$CompilationUnit");
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved

@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
Loading