|
| 1 | +///usr/bin/env jbang "$0" "$@" ; exit $? |
| 2 | + |
| 3 | +// |
| 4 | +// Relocates generated Java API files to match their declared package. |
| 5 | +// |
| 6 | +// The openapi-generator places all API files in a flat directory based on apiPackage, |
| 7 | +// but our custom templates declare tag-based sub-packages (e.g., com.langfuse.api.health). |
| 8 | +// This script reads each file's package declaration and moves it to the correct directory. |
| 9 | +// |
| 10 | +// Usage: RelocateToSubpackages <dir1> [dir2] ... [-- <glob-pattern>] |
| 11 | +// Example: RelocateToSubpackages target/generated-sources/openapi-apis -- '*.java' |
| 12 | +// |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.UncheckedIOException; |
| 16 | +import java.nio.file.FileSystems; |
| 17 | +import java.nio.file.Files; |
| 18 | +import java.nio.file.Path; |
| 19 | +import java.util.List; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +public class RelocateToSubpackages { |
| 23 | + |
| 24 | + private static final Pattern PACKAGE_PATTERN = Pattern.compile("^package\\s+([^;]+);"); |
| 25 | + |
| 26 | + public static void main(String[] args) throws IOException { |
| 27 | + var argList = List.of(args); |
| 28 | + var separatorIndex = argList.indexOf("--"); |
| 29 | + var dirs = (separatorIndex >= 0) ? argList.subList(0, separatorIndex) : argList; |
| 30 | + var pattern = (separatorIndex >= 0 && separatorIndex + 1 < argList.size()) |
| 31 | + ? argList.get(separatorIndex + 1) |
| 32 | + : "*.java"; |
| 33 | + |
| 34 | + var matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern); |
| 35 | + |
| 36 | + dirs.stream() |
| 37 | + .map(Path::of) |
| 38 | + .filter(Files::isDirectory) |
| 39 | + .flatMap(dir -> { |
| 40 | + try { |
| 41 | + return Files.walk(dir); |
| 42 | + } catch (IOException e) { |
| 43 | + throw new UncheckedIOException(e); |
| 44 | + } |
| 45 | + }) |
| 46 | + .filter(Files::isRegularFile) |
| 47 | + .filter(path -> matcher.matches(path.getFileName())) |
| 48 | + .forEach(RelocateToSubpackages::relocate); |
| 49 | + } |
| 50 | + |
| 51 | + private static void relocate(Path file) { |
| 52 | + try { |
| 53 | + Files.readAllLines(file).stream() |
| 54 | + .flatMap(line -> PACKAGE_PATTERN.matcher(line).results()) |
| 55 | + .map(match -> match.group(1)) |
| 56 | + .findFirst() |
| 57 | + .ifPresent(pkg -> moveToPackageDir(file, pkg)); |
| 58 | + } catch (IOException e) { |
| 59 | + throw new UncheckedIOException(e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static void moveToPackageDir(Path file, String pkg) { |
| 64 | + var filePath = file.toAbsolutePath().toString().replace('\\', '/'); |
| 65 | + var base = filePath.replaceFirst("(/src/main/java/).*", "$1"); |
| 66 | + var targetDir = Path.of(base, pkg.replace(".", "/")); |
| 67 | + var currentDir = file.getParent().toAbsolutePath().normalize(); |
| 68 | + |
| 69 | + if (!currentDir.equals(targetDir.toAbsolutePath().normalize())) { |
| 70 | + try { |
| 71 | + Files.createDirectories(targetDir); |
| 72 | + Files.move(file, targetDir.resolve(file.getFileName())); |
| 73 | + } catch (IOException e) { |
| 74 | + throw new UncheckedIOException(e); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments