Skip to content

Commit

Permalink
Fix up sourcePath determine logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiemen Schut committed Nov 15, 2024
1 parent f8a3819 commit a5d18d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
30 changes: 28 additions & 2 deletions src/main/java/org/openrewrite/kotlin/KotlinParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import static org.jetbrains.kotlin.config.JVMConfigurationKeys.DO_NOT_CLEAR_BINDING_CONTEXT;
import static org.jetbrains.kotlin.config.JVMConfigurationKeys.LINK_VIA_SIGNATURES;
import static org.jetbrains.kotlin.incremental.IncrementalFirJvmCompilerRunnerKt.configureBaseRoots;
import static org.openrewrite.kotlin.KotlinParser.SourcePathFromSourceTextResolver.*;

@SuppressWarnings("CommentedOutCode")
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -207,7 +208,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
})
.limit(1))
.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"));
.filter(source -> !source.getSourcePath().getFileName().toString().startsWith("dependsOn-"));
}

@Override
Expand Down Expand Up @@ -312,7 +313,7 @@ public Builder addClasspathEntry(Path classpath) {

public Builder dependsOn(@Language("kotlin") String... inputsAsStrings) {
this.dependsOn = Arrays.stream(inputsAsStrings)
.map(input -> Input.fromString(Paths.get("dependsOn-" + UUID.randomUUID() + ".kt"), input))
.map(input -> Input.fromString(determinePath("dependsOn-", input), input))
.collect(toList());
return this;
}
Expand Down Expand Up @@ -623,4 +624,29 @@ private List<Integer> getCRLFLocations(String source) {

return cRLFIndices;
}

static class SourcePathFromSourceTextResolver {
static Pattern packagePattern = Pattern.compile("^package\\s+(.+)\\s");
static Pattern classPattern = Pattern.compile("(class|interface|enum class)\\s*(<[^>]*>)?\\s+(\\w+)");
static Pattern publicClassPattern = Pattern.compile("public\\s+" + classPattern.pattern());

private static Optional<String> matchClassPattern(Pattern pattern, String source) {
Matcher classMatcher = pattern.matcher(source);
if (classMatcher.find()) {
return Optional.of(classMatcher.group(3));
}
return Optional.empty();
}

static Path determinePath(String prefix, String sourceCode) {
String className = matchClassPattern(publicClassPattern, sourceCode)
.orElseGet(() -> matchClassPattern(classPattern, sourceCode).orElse(Long.toString(System.nanoTime())));

Matcher packageMatcher = packagePattern.matcher(sourceCode);
String pkg = packageMatcher.find() ? packageMatcher.group(1).replace('.', '/') + "/" : "";

return Paths.get(pkg, prefix + className + ".kt");
}

}
}
4 changes: 2 additions & 2 deletions src/test/java/org/openrewrite/kotlin/KotlinParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class KotlinParserTest implements RewriteTest {

@Language("kotlin")
private String myClassDefinition = """
package foo.bar
package foo.bar
class MyClass
class MyClass
""";

@Test
Expand Down

0 comments on commit a5d18d5

Please sign in to comment.