|
| 1 | +#!/usr/bin/env rad |
| 2 | +--- |
| 3 | +Formats Rad scripts. |
| 4 | + |
| 5 | +Rewrites each script in place into Rad's canonical style. Use --stdout to |
| 6 | +preview without writing, or --check to verify formatting (e.g. in CI). |
| 7 | +--- |
| 8 | +args: |
| 9 | + *paths str # Paths of Rad scripts to format. |
| 10 | + stdout bool # Print formatted output to stdout instead of writing files. |
| 11 | + check bool # Don't write; exit non-zero if any file isn't formatted. |
| 12 | + |
| 13 | + stdout excludes check |
| 14 | + |
| 15 | +// No paths + piped stdin: format stdin to stdout (editor / pipe friendly). |
| 16 | +if not paths and has_stdin(): |
| 17 | + result = _rad_fmt(read_stdin()) |
| 18 | + if not result.ok: |
| 19 | + print_err("stdin: could not parse.") |
| 20 | + exit(1) |
| 21 | + if check: |
| 22 | + // In check mode, don't emit the formatted text; just signal via exit code. |
| 23 | + exit(result.changed) |
| 24 | + print(result.formatted, end="") |
| 25 | + exit() |
| 26 | + |
| 27 | +if not paths: |
| 28 | + print_err("Error: provide at least one path to format (or pipe a script via stdin).") |
| 29 | + exit(1) |
| 30 | + |
| 31 | +any_unformatted = false |
| 32 | +any_error = false |
| 33 | + |
| 34 | +for path in paths: |
| 35 | + file = read_file(path) catch: |
| 36 | + pass |
| 37 | + if type_of(file) == "error": |
| 38 | + print_err("{path}: {file}") |
| 39 | + any_error = true |
| 40 | + continue |
| 41 | + |
| 42 | + result = _rad_fmt(file.content) |
| 43 | + if not result.ok: |
| 44 | + print_err("{path}: could not parse, skipping.") |
| 45 | + any_error = true |
| 46 | + continue |
| 47 | + |
| 48 | + if stdout: |
| 49 | + print(result.formatted, end="") |
| 50 | + else if check: |
| 51 | + if result.changed: |
| 52 | + print(path) |
| 53 | + any_unformatted = true |
| 54 | + else if result.changed: |
| 55 | + write = write_file(path, result.formatted) catch: |
| 56 | + pass |
| 57 | + if type_of(write) == "error": |
| 58 | + print_err("{path}: {write}") |
| 59 | + any_error = true |
| 60 | + continue |
| 61 | + print("Formatted {path}") |
| 62 | + |
| 63 | +if any_error: |
| 64 | + exit(1) |
| 65 | +if check and any_unformatted: |
| 66 | + exit(1) |
0 commit comments