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

Add .dependsOn method for KotlinParser #617

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/main/java/org/openrewrite/kotlin/KotlinParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public class KotlinParser implements Parser {
@Nullable
private final Collection<Path> classpath;

@Nullable
private final Collection<Input> dependsOn;

private final List<NamedStyles> styles;
private final boolean logCompilationWarningsAndErrors;
private final JavaTypeCache typeCache;
Expand All @@ -139,7 +142,7 @@ public Stream<SourceFile> parse(@Language("kotlin") String... sources) {
String pkg = packageMatcher.find() ? packageMatcher.group(1).replace('.', '/') + "/" : "";

String className = Optional.ofNullable(simpleName.apply(sourceFile))
.orElse(Long.toString(System.nanoTime())) + ".kt";
.orElse(Long.toString(System.nanoTime())) + ".kt";

Path path = Paths.get(pkg + className);
return new Input(
Expand All @@ -162,10 +165,12 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
// TODO: FIR and disposable may not be necessary using the IR.
Disposable disposable = Disposer.newDisposable();
CompiledSource compilerCus;
List<Input> acceptedInputs = dependsOn == null ? new ArrayList<>() : new ArrayList<>(dependsOn);
acceptedInputs.addAll(acceptedInputs(sources).collect(Collectors.toList()));
try {
compilerCus = parse(acceptedInputs(sources).collect(Collectors.toList()), disposable, pctx);
compilerCus = parse(acceptedInputs, disposable, pctx);
} catch (Exception e) {
return acceptedInputs(sources).map(input -> ParseError.build(this, input, relativeTo, ctx, e));
return acceptedInputs.stream().map(input -> ParseError.build(this, input, relativeTo, ctx, e));
}

FirSession firSession = compilerCus.getFirSession();
Expand Down Expand Up @@ -201,7 +206,8 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
return (SourceFile) null;
})
.limit(1))
.filter(Objects::nonNull);
.filter(Objects::nonNull)
.filter(source -> !source.getSourcePath().getFileName().toString().matches("dependsOn-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\\.kt"));
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down Expand Up @@ -252,6 +258,7 @@ public static class Builder extends Parser.Builder {
@Nullable
private Collection<Path> classpath = emptyList();

private Collection<Input> dependsOn = emptyList();
private JavaTypeCache typeCache = new JavaTypeCache();
private boolean logCompilationWarningsAndErrors;
private final List<NamedStyles> styles = new ArrayList<>();
Expand Down Expand Up @@ -303,6 +310,13 @@ public Builder addClasspathEntry(Path classpath) {
return this;
}

public Builder dependsOn(@Language("kotlin") String... inputsAsStrings) {
this.dependsOn = Arrays.stream(inputsAsStrings)
.map(input -> Input.fromString(Paths.get("dependsOn-" + UUID.randomUUID() + ".kt"), input))
.collect(toList());
return this;
}

public Builder typeCache(JavaTypeCache typeCache) {
this.typeCache = typeCache;
return this;
Expand Down Expand Up @@ -335,7 +349,7 @@ public Builder languageLevel(KotlinLanguageLevel languageLevel) {

@Override
public KotlinParser build() {
return new KotlinParser(resolvedClasspath(), styles, logCompilationWarningsAndErrors, typeCache, moduleName, languageLevel, isKotlinScript);
return new KotlinParser(resolvedClasspath(), dependsOn, styles, logCompilationWarningsAndErrors, typeCache, moduleName, languageLevel, isKotlinScript);
}

@Override
Expand Down