356d4ab780
- 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
189 lines
6.0 KiB
Nu
189 lines
6.0 KiB
Nu
const pj_module_dir = (path self | path dirname)
|
|
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")
|
|
}
|
|
|
|
def _pj_required_completion_files [] {
|
|
$pj_completion_commands | each {|name|
|
|
{
|
|
command: $name
|
|
file: (_pj_completion_script_path $name)
|
|
}
|
|
}
|
|
}
|
|
|
|
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')
|
|
} else {
|
|
(pwd | path join 'nushell' 'cache')
|
|
}
|
|
let repo_dir = ($cache_dir | path join 'nu_scripts')
|
|
let completion_dir = ($repo_dir | path join 'custom-completions')
|
|
|
|
if (which git | is-empty) {
|
|
error make {
|
|
msg: "`git` command is required for pj completion"
|
|
help: "Install Git first, then run `pj completion` again."
|
|
}
|
|
}
|
|
|
|
mkdir $cache_dir
|
|
|
|
if $force {
|
|
if ($repo_dir | path exists) {
|
|
rm -rf $repo_dir
|
|
}
|
|
}
|
|
|
|
if not ($repo_dir | path exists) {
|
|
print "[pj completion] nu_scripts cache not found, cloning..."
|
|
^git clone $repo_url $repo_dir
|
|
} else {
|
|
if not (($repo_dir | path join '.git') | path exists) {
|
|
print "[pj completion] existing cache is not a git repo, re-cloning..."
|
|
rm -rf $repo_dir
|
|
^git clone $repo_url $repo_dir
|
|
} else if $update {
|
|
print "[pj completion] cache exists, updating..."
|
|
^git -C $repo_dir pull --ff-only
|
|
}
|
|
}
|
|
|
|
if not ($completion_dir | path exists) {
|
|
error make {
|
|
msg: "nu_scripts cloned, but custom-completions directory is missing"
|
|
help: $"Check repository content under ($repo_dir)."
|
|
}
|
|
}
|
|
|
|
let required = (_pj_required_completion_files)
|
|
let missing = ($required | where {|it| not ($it.file | path exists) })
|
|
if not ($missing | is-empty) {
|
|
print "[pj completion] warning: some required completion files are missing:"
|
|
$missing | each {|it| print $" - ($it.command): ($it.file)" }
|
|
}
|
|
|
|
print "[pj completion] ok"
|
|
{
|
|
cache_dir: $cache_dir
|
|
repo_dir: $repo_dir
|
|
completion_dir: $completion_dir
|
|
commands: $pj_completion_commands
|
|
missing_files: $missing
|
|
}
|
|
}
|
|
|
|
export-env {
|
|
$env.config = {
|
|
completions: {
|
|
quick: true
|
|
partial: true
|
|
algorithm: "fuzzy"
|
|
case_sensitive: false
|
|
external: {
|
|
enable: true
|
|
max_results: 100
|
|
}
|
|
}
|
|
}
|
|
|
|
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 completion externs from generated static registry.
|
|
export use $pj_completion_bootstrap_module *
|