Skip to content

Commit 4049878

Browse files
committed
Migrate relocate-to-subpackages script from Bash to Java using JBang for improved portability and integration. Update Maven configurations accordingly.
1 parent 92f1507 commit 4049878

6 files changed

Lines changed: 111 additions & 64 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@ build/
3838
### Mac OS ###
3939
.DS_Store
4040

41+
### JBang ###
42+
.jbang/
43+
4144
### Environment ###
4245
.env

langfuse-java-api/pom.xml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,24 @@
217217
</target>
218218
</configuration>
219219
</execution>
220+
</executions>
221+
</plugin>
222+
<plugin>
223+
<groupId>dev.jbang</groupId>
224+
<artifactId>jbang-maven-plugin</artifactId>
225+
<executions>
220226
<execution>
221227
<id>relocate-to-subpackages</id>
222228
<phase>generate-sources</phase>
223229
<goals>
224230
<goal>run</goal>
225231
</goals>
226232
<configuration>
227-
<target>
228-
<exec executable="bash" failonerror="true">
229-
<arg value="${project.basedir}/../scripts/relocate-to-subpackages.sh"/>
230-
<arg value="${project.build.directory}/generated-sources/openapi-apis"/>
231-
<arg value="${project.build.directory}/generated-sources/openapi-async-apis"/>
232-
</exec>
233-
</target>
233+
<script>${project.basedir}/../scripts/RelocateToSubpackages.java</script>
234+
<args>
235+
<arg>${project.build.directory}/generated-sources/openapi-apis</arg>
236+
<arg>${project.build.directory}/generated-sources/openapi-async-apis</arg>
237+
</args>
234238
</configuration>
235239
</execution>
236240
</executions>

langfuse-java-client/pom.xml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,26 @@
201201
</target>
202202
</configuration>
203203
</execution>
204+
</executions>
205+
</plugin>
206+
<plugin>
207+
<groupId>dev.jbang</groupId>
208+
<artifactId>jbang-maven-plugin</artifactId>
209+
<executions>
204210
<execution>
205211
<id>relocate-to-subpackages</id>
206212
<phase>generate-sources</phase>
207213
<goals>
208214
<goal>run</goal>
209215
</goals>
210216
<configuration>
211-
<target>
212-
<exec executable="bash" failonerror="true">
213-
<arg value="${project.basedir}/../scripts/relocate-to-subpackages.sh"/>
214-
<arg value="${project.build.directory}/generated-sources/openapi"/>
215-
<arg value="${project.build.directory}/generated-sources/openapi-async"/>
216-
<arg value="--"/>
217-
<arg value="*Api.java"/>
218-
</exec>
219-
</target>
217+
<script>${project.basedir}/../scripts/RelocateToSubpackages.java</script>
218+
<args>
219+
<arg>${project.build.directory}/generated-sources/openapi</arg>
220+
<arg>${project.build.directory}/generated-sources/openapi-async</arg>
221+
<arg>--</arg>
222+
<arg>*Api.java</arg>
223+
</args>
220224
</configuration>
221225
</execution>
222226
</executions>

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<version>0.2.1-SNAPSHOT</version>
77
<packaging>pom</packaging>
88

9-
<name>langfuse-java-parent</name>
9+
<name>langfuse-java</name>
1010
<description>Parent POM for the Langfuse Java SDK</description>
1111
<url>https://langfuse.com</url>
1212

@@ -211,6 +211,11 @@
211211
<artifactId>openapi-generator-maven-plugin</artifactId>
212212
<version>${openapi-generator.version}</version>
213213
</plugin>
214+
<plugin>
215+
<groupId>dev.jbang</groupId>
216+
<artifactId>jbang-maven-plugin</artifactId>
217+
<version>0.0.8</version>
218+
</plugin>
214219
</plugins>
215220
</pluginManagement>
216221
</build>

scripts/RelocateToSubpackages.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

scripts/relocate-to-subpackages.sh

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)