feat(nushell): add auto-bootstrapped completion registry flow

- generate cache registry module from command list and load it at startup

- add pj completion sync (-s) with optional help and restart behavior

- keep missing-cache fallback via noop module and preserve cache ignore rules
This commit is contained in:
2026-05-30 23:12:21 +08:00
parent 10d133444d
commit 356d4ab780
4 changed files with 95 additions and 18 deletions
+2 -1
View File
@@ -1,2 +1,3 @@
*
!.gitignore
!.gitignore
!noop.nu
+1
View File
@@ -0,0 +1 @@
# Fallback completion module intentionally exports nothing.
+92 -15
View File
@@ -1,6 +1,14 @@
const pj_module_dir = (path self | path dirname)
const pj_completion_base_dir = ($pj_module_dir | path join '..' 'cache' 'nu_scripts' 'custom-completions')
const pj_completion_commands = [git ssh cargo uv rustup make pytest rg tar fzf vscode winget zig]
const pj_cache_dir = ($pj_module_dir | path join '..' 'cache')
const pj_completion_base_dir = ($pj_cache_dir | path join 'nu_scripts' 'custom-completions')
const pj_completion_commands = [git ssh cargo uv rustup make pytest rg tar vscode winget zig]
const pj_completion_noop_module = ($pj_cache_dir | path join 'noop.nu')
const pj_completion_registry_module = ($pj_cache_dir | path join 'pj_completions_registry.nu')
const pj_completion_bootstrap_module = if ($pj_completion_registry_module | path exists) {
$pj_completion_registry_module
} else {
$pj_completion_noop_module
}
def _pj_completion_script_path [name: string] {
($pj_completion_base_dir | path join $name $"($name)-completions.nu")
@@ -15,10 +23,78 @@ def _pj_required_completion_files [] {
}
}
def _pj_completion_registry_lines [] {
let header = [
"# Auto-generated by `pj completion sync`."
"# Edit pj_completion_commands in pj_completions.nu, then re-run sync."
"const pj_registry_dir = (path self | path dirname)"
"const pj_completion_base_dir = ($pj_registry_dir | path join 'nu_scripts' 'custom-completions')"
"const pj_completion_noop_module = ($pj_registry_dir | path join 'noop.nu')"
""
]
let lines = ($pj_completion_commands | each {|name|
[
(
"export use (if ((($pj_completion_base_dir | path join '"
+ $name
+ "' '"
+ $name
+ "-completions.nu') | path exists)) { ($pj_completion_base_dir | path join '"
+ $name
+ "' '"
+ $name
+ "-completions.nu') } else { $pj_completion_noop_module }) *"
)
""
]
} | flatten)
($header | append $lines)
}
def _pj_completion_help [] {
print "pj completion commands:"
print " pj completion Clone/update cache and check required completion files."
print " pj completion -u Update cached nu_scripts repository."
print " pj completion -f Force re-clone cached nu_scripts repository."
print " pj completion sync Regenerate cache registry and restart Nushell."
print " pj completion -s Same as `pj completion sync`."
print " pj completion -h Show this help message."
}
export def "pj completion sync" [
--help(-h)
] {
if $help {
_pj_completion_help
return
}
mkdir $pj_cache_dir
let content = ((_pj_completion_registry_lines) | str join "\n")
$content | save -f $pj_completion_registry_module
print $"[pj completion] synced: ($pj_completion_registry_module)"
print "[pj completion] restarting Nushell..."
exec nu
}
export def "pj completion" [
--update(-u) # Pull latest changes when repo already exists.
--force(-f) # Re-clone even when repo already exists.
--sync(-s) # Regenerate static registry and restart Nushell.
--help(-h) # Show help.
] {
if $help {
_pj_completion_help
return
}
if $sync {
pj completion sync
return
}
let repo_url = "https://github.com/nushell/nu_scripts.git"
let cache_dir = if ('NU_POLYJUICE_PATH' in $env) {
($env.NU_POLYJUICE_PATH | path join 'cache')
@@ -94,18 +170,19 @@ export-env {
}
}
}
if not ($pj_completion_registry_module | path exists) {
mkdir $pj_cache_dir
let content = ((_pj_completion_registry_lines) | str join "\n")
$content | save -f $pj_completion_registry_module
if (($env.PJ_COMPLETION_BOOTSTRAPPED? | default "0") != "1") {
load-env { PJ_COMPLETION_BOOTSTRAPPED: "1" }
print "[pj completion] generated cache registry, restarting Nushell..."
exec nu
}
}
}
# Re-export imported extern signatures so they are visible when this module is `use`d.
export use ($pj_completion_base_dir | path join 'git' 'git-completions.nu') *
export use ($pj_completion_base_dir | path join 'ssh' 'ssh-completions.nu') *
export use ($pj_completion_base_dir | path join 'cargo' 'cargo-completions.nu') *
export use ($pj_completion_base_dir | path join 'uv' 'uv-completions.nu') *
export use ($pj_completion_base_dir | path join 'rustup' 'rustup-completions.nu') *
export use ($pj_completion_base_dir | path join 'make' 'make-completions.nu') *
export use ($pj_completion_base_dir | path join 'pytest' 'pytest-completions.nu') *
export use ($pj_completion_base_dir | path join 'rg' 'rg-completions.nu') *
export use ($pj_completion_base_dir | path join 'tar' 'tar-completions.nu') *
export use ($pj_completion_base_dir | path join 'vscode' 'vscode-completions.nu') *
export use ($pj_completion_base_dir | path join 'winget' 'winget-completions.nu') *
export use ($pj_completion_base_dir | path join 'zig' 'zig-completions.nu') *
# Re-export completion externs from generated static registry.
export use $pj_completion_bootstrap_module *