repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

chore: harden java mise shell

8e94747bf029ee8d730bbe20daa0a998f58d4d39

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

pi-ext

Modified
1 -Subproject commit 902e78f5dc75dc19294d2edde54504c971c54ee9
1 +Subproject commit 66a87ef7d12bdfe7c5b4e1eb92c21753ad1d0c45

scripts/dotfiles_integration_test.java

Modified
69 69 var miseTools = run(root, "mise", "ls", "--current").toLowerCase(Locale.ROOT);
70 70 if (miseTools.contains("nushell")) fail("root-mise-nushell-tool");
71 71 System.out.println(run(root, "java", "-version").lines().findFirst().orElse("java"));
72 + miseShellSmoke();
73 74 run(root, "mise", "run", "hook");
74 75 if (!Files.isExecutable(root.resolve(".git/hooks/pre-commit"))) fail("hook-executable");
95 96 System.out.println("RESULT:ok");
99 + static void miseShellSmoke() throws Exception {
100 + var smoke = "mise-shell-smoke";
101 + var snippet = """
102 + mkdir(\"%1$s/tree\");
103 + write(\"%1$s/a.txt\", \"alpha\");
104 + IO.println(read(\"%1$s/a.txt\"));
105 + IO.println(out(\"java\", \"-version\").contains(\"openjdk\"));
106 + rm(\"%1$s/a.txt\");
107 + write(\"%1$s/tree/b.txt\", \"beta\");
108 + rmTree(\"%1$s/tree\");
109 + rmdir(\"%1$s\");
110 + """.formatted(smoke);
111 + var out = run(root, "java", "scripts/mise_shell.java", snippet);
112 + if (!out.contains("alpha") || !out.contains("true")) fail("mise-shell-helpers");
113 + if (!runExpectFail(root, Map.of(), "java", "scripts/mise_shell.java", "bad();").contains("cannot find symbol")) fail("mise-shell-errors");
114 + ok("mise-shell");
98 117 static List<String> allHosts() throws IOException {
99 118 var hosts = new ArrayList<String>();
100 119 try (var s = Files.list(root)) {

scripts/mise_shell.java

Modified
1 1 import jdk.jshell.*;
2 2 import java.util.*;
4 +final int USAGE_EXIT = 64;
5 +final int FAILURE_EXIT = 1;
6 +final String LOCAL_ENGINE = "local";
4 8 /// Tiny Java shell for mise inline tasks.
6 10 /// Mise passes each task command as one argv item. This evaluates that item as
10 14 if (args.length != 1)
12 16 System.err.println("expected one mise task snippet arg, got " + args.length);
13 - System.exit(64);
17 + System.exit(USAGE_EXIT);
16 - var failed = false;
17 - try (var jshell = JShell.builder().executionEngine("local").out(System.out).err(System.err).build())
20 + try (var jshell = JShell.builder().executionEngine(LOCAL_ENGINE).out(System.out).err(System.err).build())
19 - failed |= !evalAll(jshell, startup(), true);
20 - if (!failed)
21 - failed |= !evalAll(jshell, args[0], false);
22 + if (!evalAll(jshell, startup(), true) || !evalAll(jshell, args[0], false))
23 + System.exit(FAILURE_EXIT);
23 - if (failed)
24 - System.exit(1);
27 27 /// Evaluate a possibly multi-snippet Java task.
38 38 /// entries.
39 39 boolean evalAll(JShell jshell, String input, boolean startup)
41 - var ok = true;
42 - var rest = input;
43 - while (!rest.isBlank())
41 + for (var rest = input; !rest.isBlank(); )
45 43 var info = jshell.sourceCodeAnalysis().analyzeCompletion(rest);
46 44 if (!info.completeness().isComplete())
48 - System.err.println((startup ? "startup: " : "") + "incomplete snippet: " + info.completeness());
46 + System.err.println(prefix(startup) + "incomplete snippet: " + info.completeness());
49 47 return false;
51 49 if (!evalOne(jshell, info.source(), startup))
52 - {
53 - ok = false;
54 - break;
55 - }
50 + return false;
56 51 rest = info.remaining();
58 - return ok;
53 + return true;
61 56 /// Evaluate one complete JShell snippet and translate JShell status into exit status.
78 73 System.err.println(message == null ? event.exception().toString() : message);
79 74 ok = false;
81 - var status = event.status();
82 - if (status == Snippet.Status.REJECTED || status == Snippet.Status.RECOVERABLE_NOT_DEFINED)
76 + if (rejected(event.status()))
84 - jshell.diagnostics(event.snippet()).forEach(d -> System.err.println((startup ? "startup: " : "") + d.getMessage(Locale.ROOT)));
78 + jshell.diagnostics(event.snippet()).forEach(d -> System.err.println(prefix(startup) + d.getMessage(Locale.ROOT)));
85 79 ok = false;
88 82 return ok;
85 +boolean rejected(Snippet.Status status)
87 + return status == Snippet.Status.REJECTED || status == Snippet.Status.RECOVERABLE_NOT_DEFINED;
90 +String prefix(boolean startup)
92 + return startup ? "startup: " : "";
91 95 /// Built-in imports and shell-like helpers exposed to every mise task.
93 97 /// Reason: root dorkfiles tasks should stay Java, terse, and cross-platform
99 103 /// Pitfall: this is startup source code for JShell. Syntax errors here break
100 104 /// every task before the task body runs.
101 105 String startup()
107 + return String.join("\n",
108 + imports(),
109 + stateHelpers(),
110 + resultHelpers(),
111 + pathHelpers(),
112 + envHelpers(),
113 + processHelpers(),
114 + fileHelpers(),
115 + deleteHelpers(),
116 + globHelpers(),
117 + whichHelpers()
118 + );
121 +String imports()
103 123 return """
104 124 import java.io.*;
107 127 import java.util.*;
108 128 import java.util.regex.*;
109 129 import java.util.stream.*;
130 +""";
133 +String stateHelpers()
135 + return """
111 136 Path CWD = Path.of("").toAbsolutePath().normalize();
112 137 Map<String, String> ENV = new HashMap<>(System.getenv());
138 +""";
141 +String resultHelpers()
143 + return """
114 144 /// Captured process result.
116 146 /// Reason: `run(...)` is fail-fast and streams output. Some tasks need captured
151 181 /// Pitfall: uses Java `String.lines()`, so line terminators are removed.
152 182 List<String> lines() { return out.lines().toList(); }
184 +""";
187 +String pathHelpers()
189 + return """
155 190 /// Return the task working directory.
157 192 /// Reason: `cd(...)` is local state inside this Java shell, not OS process state.
196 231 throw new IllegalArgumentException("not a directory: " + next);
197 232 CWD = next;
234 +""";
237 +String envHelpers()
239 + return """
200 240 /// Read a task environment variable from this shell's environment map.
202 242 /// Reason: tasks need access to mise `env = { ... }` values without shell syntax.
223 263 ENV.put(name, value);
265 +""";
268 +String processHelpers()
270 + return """
226 271 /// Build a configured `ProcessBuilder`.
228 273 /// Reason: centralizes `CWD` and `ENV` application for process helpers.
298 343 return result(cmd).code();
345 +""";
348 +String fileHelpers()
350 + return """
301 351 /// Read a UTF-8-ish text file using Java defaults.
303 353 /// Reason: common task file reads should not shell out to `cat`.
340 390 Files.createDirectories(path(dir));
392 +""";
395 +String deleteHelpers()
397 + return """
343 398 /// Guard destructive helpers against catastrophic targets.
345 400 /// Reason: `rm(...)` convenience is useful, but deleting HOME, filesystem roots,
412 467 Files.deleteIfExists(x);
470 +""";
473 +String globHelpers()
475 + return """
416 476 /// List direct children matching a glob in `pwd()`.
418 478 /// Reason: simple file selection should not need shell glob expansion.
431 491 return paths;
494 +""";
497 +String whichHelpers()
499 + return """
435 500 /// Find an executable on `PATH`.
437 502 /// Reason: tasks sometimes need an explicit dependency probe without shelling out