repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(pi): add java source scripts skill

1d576228b873503619a354a8679ed30ab670defa

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

+138 / −0 text lines

Diff layout

mise.toml

Modified

+3 / −0 text lines

1[tools]
2java = "temurin"
3
14[settings]
25unix_default_inline_shell_args = "nu -c"
36windows_default_inline_shell_args = "nu -c"

pi/agent/skills/java-source-scripts/SKILL.md

Added
new file mode 100644

+135 / −0 text lines

1---
2name: java-source-scripts
3description: Write or review Java source-file scripts run with `java path/to/script.java` (JEP 330/458). This is Java, not JavaScript; do not use for JS/TS/browser/node scripting.
4---
5
6# Java Source Scripts
7
8Use this for **Java source-file scripts**: small operational `.java` files invoked directly with `java script.java`.
9Do **not** use this for JavaScript.
11## Default stance
13- Script invocation is `java path/to/script.java [args...]`.
14- Shebang is allowed for Unix convenience, but never required; Windows does not support it.
15- Prefer standard library only. No Maven/Gradle/JBang/Picocli unless the task explicitly needs them.
16- Prefer one pragmatic file over a package/module/build setup.
17- Use modern Java features and APIs. Terse is good. Boilerplate is not.
19## Source-file launcher facts
21- JDK 11+ runs a single `.java` file directly (`java Tool.java`).
22- Arguments after the source file go to the program.
23- The first top-level class, or an instance `void main(String[] args)` / `void main()` on modern Java, is the entry point.
24- JDK 22+ can resolve referenced sibling `.java` files automatically for multi-file source programs.
25- Shebang launch only applies to single-file scripts; for portability still document `java script.java`.
27## Shape
29Prefer this structure:
31```java
32import java.nio.file.*;
33import java.time.*;
34import java.util.*;
36/// Does one operational thing.
37///
38/// Usage:
39/// `java scripts/example.java [--flag] <arg>`
40void main( String[] args ) throws Exception
41{
42 final var opts = parseArgs( args );
43 if ( opts.help() ) { help(); return; }
45 // linear flow
46}
48Opts parseArgs( String[] args )
49{
50 var help = false;
51 for ( var arg : args )
52 {
53 switch ( arg )
54 {
55 case "-h", "--help" -> help = true;
56 default -> throw new IllegalArgumentException( "unknown arg: " + arg );
57 }
58 }
59 return new Opts( help );
60}
62void help()
63{
64 IO.println( "Usage: java scripts/example.java [--help]" );
65}
67record Opts( boolean help ) {}
68```
70## Style
72- Prefer `void main(...)` over `public class Main { public static void main... }` when the target Java supports it.
73- Prefer `final var` for obvious local values.
74- Prefer `record` for small immutable option/result/config carriers.
75- Prefer early returns over nested control flow.
76- Prefer `Path`, `Files`, `ProcessBuilder`, `HttpClient`, `URI`, `Instant`, `Duration`, `Pattern`, `System.getenv` over shelling out.
77- Prefer loops when clearer; do not force streams.
78- Keep helpers small and named by behavior.
79- Keep constants in one place when repeated.
81## CLI parsing
83- For a few flags, write a simple `switch` over `args`.
84- For 1–2 values, do a linear scan.
85- Do not create generic parser frameworks.
86- `--help` must be short and direct.
88## Processes
90Centralize external commands. Do not scatter `new ProcessBuilder(...)`.
92Use helper names that state failure/output behavior:
94- `run(...)` — fail-fast, inherited IO.
95- `runIgnoreFail(...)` — non-zero exit is acceptable.
96- `runCapture(...)` — capture stdout/stderr and fail on non-zero.
97- `runCaptureIgnoreFail(...)` — capture result and exit code.
99Keep OS special cases local to the call site, e.g. `mvnw.cmd` on Windows.
101## Output and errors
103- Console output is the UI. `IO.println`/`IO.print` are fine.
104- Use semantic ANSI constants if coloring: `STEP`, `SUCCESS`, `HINT`, `ERROR`, `RESET_STYLE`.
105- Fail fast on required errors with direct messages.
106- If ignoring errors, make it explicit in helper names or comments.
107- Validate required env vars early. Defaults must be explicit.
109## Documentation
111Put a short header comment in the source:
113- purpose
114- usage
115- options
116- environment variables
117- important side effects
119Keep docs matched to implementation.
121## Avoid
123- JavaScript/Node assumptions.
124- Bash/POSIX-only logic when Java stdlib works.
125- Build tools for simple scripts.
126- `public static void main` boilerplate unless compatibility demands it.
127- Mutable tiny config classes; use records.
128- Broad abstractions for cleanliness alone.
129- Hidden global side effects.
131## References
133- JEP 330: single-file source-code programs.
134- JEP 458: multi-file source-code programs.
135- Inside Java “Scripting with Java”.