#!/usr/bin/env nu # # gen-completions.nu — regenerate nu/completions.nu from the usage print # blocks in the source files. # # Run from the repo root: # nu nu/gen-completions.nu # # The single source of truth for each subcommand's name and description is # its line in the relevant usage block (main [], main vm [], main ctl []). # This script extracts those lines and rewrites completions.nu in place so # they never drift apart. # Parse the usage print lines from a source file. # # Looks for lines of the form: # print " [optional args] " # where name and description are separated by 2+ spaces. # # strip_prefix: if non-empty, strip " " from the front of each # line's content before splitting (used for hermenix.nu, where lines read # " hermenix setup desc" and we want just "setup"). # # Returns a list of {value, description} records, skipping any line whose # extracted name contains a quote or starts with "hermenix" (those are # passthrough-example lines, not real subcommands). def parse-usage [file: string, strip_prefix: string = ""] { open --raw $file | lines | where { $in =~ 'print " \S' } | each {|line| # Strip everything up to and including the opening `print "` and the # closing `"` (last one), leaving just the inner usage string. let inner = ($line | str replace -r '^[^"]*"' "" | str replace -r '"[^"]*$' "") # Optionally remove a leading " " so that # " hermenix setup desc" becomes " setup desc". let body = if ($strip_prefix != "") { $inner | str replace $" ($strip_prefix) " " " } else { $inner } # Split on 2+ spaces — left side is "name [optional args]", right is desc. let parts = $body | str trim | split row -r " +" | where { $in != "" } if ($parts | length) < 2 { null } else { let name = $parts | first | split row " " | first # Re-join all remaining parts — descriptions like "systemctl stop hermes-vm" # would otherwise get cut at the double-space inside the description. let desc = $parts | skip 1 | str join " " | str trim {value: $name, description: $desc} } } | where { $in != null } | where { $in.value != "" } | where { not ($in.value | str contains '"') } | where { not ($in.value | str starts-with "hermenix") } | uniq-by value } # Render one `nu-complete` function that returns a [value description] table. def render-complete [fn_name: string, entries: list] { let rows = ($entries | each {|e| $" [($e.value) \"($e.description)\"]" } | str join "\n") [ $"def \"nu-complete ($fn_name)\" [] {" " [" " [value description];" $rows " ]" "}" ] | str join "\n" } # Render the context-aware dispatcher that picks between the three tables. # Written as a single-quoted literal — no interpolation needed. def render-ctx [] { '# Context-aware completer: inspects already-typed words to decide # whether to offer top-level subcommands or a second-level list. def "nu-complete hermenix ctx" [context: string, offset: int] { let words = $context | str trim | split row " " | where { $in != "" } match ($words | length) { 1 => (nu-complete hermenix top) 2 => { match ($words | get 1) { "vm" => (nu-complete hermenix vm) "ctl" => (nu-complete hermenix ctl) _ => [] } } _ => [] } }' } def render-extern [] { 'extern "hermenix" [...args: string@"nu-complete hermenix ctx"]' } def render-header [] { [ "# Tab-completion definitions for hermenix." "#" "# !! GENERATED — do not edit by hand." "# !! Regenerate with: nu nu/gen-completions.nu" "#" "# Installed to $out/share/nushell/vendor/autoload/ so nushell on NixOS" "# picks them up automatically from environment.systemPackages. No manual" "# sourcing needed." ] | str join "\n" } # ── main ──────────────────────────────────────────────────────────────── # # Guarded so nu/tests/completions_test.nu can `source` this file to reach # the pure parse-usage/render-* functions above without regenerating # nu/completions.nu as a side effect of the test. if not ($env.HERMENIX_TEST_SOURCING? | default false) { let repo_root = $env.FILE_PWD | path dirname let out_file = $repo_root | path join "nu" "completions.nu" let top = parse-usage ($repo_root | path join "nu" "hermenix.nu") "hermenix" let vm = parse-usage ($repo_root | path join "nu" "vm" "vm.nu") let ctl = parse-usage ($repo_root | path join "nu" "ctl" "ctl.nu") if ($top | is-empty) { print -e "warn: no top-level completions found in hermenix.nu" } if ($vm | is-empty) { print -e "warn: no vm completions found in vm/vm.nu" } if ($ctl | is-empty) { print -e "warn: no ctl completions found in ctl/ctl.nu" } [ (render-header) "" (render-complete "hermenix top" $top) "" (render-complete "hermenix vm" $vm) "" (render-complete "hermenix ctl" $ctl) "" (render-ctx) "" (render-extern) "" ] | str join "\n" | save -f $out_file nufmt $out_file print $"wrote ($out_file) [top=($top | length) vm=($vm | length) ctl=($ctl | length)]" }