From 274a638e155c08cd0d6dbda1a0d4da02c3466c97 Mon Sep 17 00:00:00 2001 From: Fer Date: Mon, 12 Dec 2022 04:52:51 -0300 Subject: [PATCH] feat: support nushell (#1355) Closes https://github.com/asdf-vm/asdf/issues/745 --- asdf.nu | 251 ++++++++++++++++++++++++++++++++++ docs/guide/getting-started.md | 33 +++++ 2 files changed, 284 insertions(+) create mode 100644 asdf.nu diff --git a/asdf.nu b/asdf.nu new file mode 100644 index 00000000..1dd14ea3 --- /dev/null +++ b/asdf.nu @@ -0,0 +1,251 @@ +let possible_dirs = [ + ($env | get --ignore-errors ASDF_DIR | default '' | first) , + '/opt/asdf-vm/asdf.nu', + ($env.HOME | path join '.asdf/asdf.nu') + ] + +let is_brew_installed = (which 'brew' | get --ignore-errors path | default '' | str trim | str length | into bool | first ) + +let possible_dirs = ( $possible_dirs | append (if $is_brew_installed { (brew --prefix asdf | into string | path join 'libexec/asdf.nu') } else { '' }) ) + +let filtered_dirs = ( $possible_dirs | + where { |it| not ($it | is-empty) } ) + +let asdf_dir = ( $filtered_dirs | + where {|it| $it | path exists } | + first | + path dirname | + path join 'bin' ) + +let shims_dir = ( if ($asdf_dir | path join 'shims' | path exists) { $asdf_dir | path join 'shims' } else { $env.HOME | path join '.asdf/shims' } ) + +let-env PATH = ( $env.PATH | append $asdf_dir ) +let-env PATH = ( $env.PATH | append $shims_dir) + +## Completions + +module asdf { + + def "complete asdf sub-commands" [] { + [ + "plugin", + "list", + "install", + "uninstall", + "current", + "where", + "which", + "local", + "global", + "shell", + "latest", + "help", + "exec", + "env", + "info", + "reshim", + "shim-version", + "update" + ] + } + + def "complete asdf installed" [] { + ^asdf plugin list | lines | each { |line| $line | str trim } + } + + + def "complete asdf plugin sub-commands" [] { + [ + "list", + "list all", + "add", + "remove", + "update" + ] + } + + def "complete asdf installed plugins" [] { + ^asdf plugin list | lines | each { |line| + $line | str trim + } + } + + # ASDF version manager + export extern "asdf" [ + subcommand?: string@"complete asdf sub-commands" + ] + + # Manage plugins + export extern "asdf plugin" [ + subcommand?: string@"complete asdf plugin sub-commands" + ] + + # List installed plugins + export def "asdf plugin list" [ + --urls # Show urls + --refs # Show refs + ] { + + let params = [ + {name: 'urls', enabled: $urls, template: '\s+?(?Pgit@.+\.git)', flag: '--urls'} + {name: 'refs', enabled: $refs, template: '\s+?(?P\w+)\s+(?P\w+)', flag: '--refs'} + ] + + let template = '(?P.+)' + ( + $params | + where enabled | + get --ignore-errors template | + str join '' | + str trim + ) + + let parsed_urls_flag = ($params | where enabled and name == 'urls' | get --ignore-errors flag | default '' ) + let parsed_refs_flag = ($params | where enabled and name == 'refs' | get --ignore-errors flag | default '' ) + + ^asdf plugin list $parsed_urls_flag $parsed_refs_flag | lines | parse -r $template | str trim + } + + # list all available plugins + export def "asdf plugin list all" [] { + let template = '(?P.+)\s+?(?P[*]?)(?P(?:git|http).+\.git)' + let is_installed = { |it| $it.installed == '*' } + + ^asdf plugin list all | + lines | + parse -r $template | + str trim | + update installed $is_installed | + sort-by name + } + + # Add a plugin + export extern "asdf plugin add" [ + name: string # Name of the plugin + git_url?: string # Git url of the plugin + ] + + # Remove an installed plugin and their package versions + export extern "asdf plugin remove" [ + name: string@"complete asdf installed plugins" # Name of the plugin + ] + + # Update a plugin + export extern "asdf plugin update" [ + name: string@"complete asdf installed plugins" # Name of the plugin + git_ref?: string # Git ref to update the plugin + ] + + # Update all plugins to the latest commit + export extern "asdf plugin update --all" [] + + # install a package version + export extern "asdf install" [ + name?: string # Name of the package + version?: string # Version of the package or latest + ] + + + # Remove an installed package version + export extern "asdf uninstall" [ + name: string@"complete asdf installed" # Name of the package + version: string # Version of the package + ] + + # Display current version + export extern "asdf current" [ + name?: string@"complete asdf installed" # Name of installed version of a package + ] + + # Display path of an executable + export extern "asdf which" [ + command: string # Name of command + ] + + # Display install path for an installled package version + export extern "asdf where" [ + name: string@"complete asdf installed" # Name of installed package + version?: string # Version of installed package + ] + + # Set the package local version + export extern "asdf local" [ + name: string@"complete asdf installed" # Name of the package + version?: string # Version of the package or latest + ] + + # Set the package global version + export extern "asdf global" [ + name: string@"complete asdf installed" # Name of the package + version?: string # Version of the package or latest + ] + + # Set the package to version in the current shell + export extern "asdf shell" [ + name: string@"complete asdf installed" # Name of the package + version?: string # Version of the package or latest + ] + + # Show latest stable version of a package + export extern "asdf latest" [ + name: string # Name of the package + version?: string # Filter latest stable version from this version + ] + + # Show latest stable version for all installed packages + export extern "asdf latest --all" [] + + # List installed package versions + export extern "asdf list" [ + name?: string@"complete asdf installed" # Name of the package + version?: string # Filter the version + ] + + # List all available package versions + export def "asdf list all" [ + name: string@"complete asdf installed" # Name of the package + version?: string="" # Filter the version + ] { + ^asdf list all $name $version | lines | parse "{version}" | str trim + } + + # Show documentation for plugin + export extern "asdf help" [ + name: string@"complete asdf installed" # Name of the plugin + version?: string # Version of the plugin + ] + + # Execute a command shim for the current version + export extern "asdf exec" [ + command: string # Name of the command + ...args: any # Arguments to pass to the command + ] + + # Run util (default: env) inside the environment used for command shim execution + export extern "asdf env" [ + command?: string # Name of the command + util?: string = 'env' # Name of util to run + ] + + # Show information about OS, Shell and asdf Debug + export extern "asdf info" [] + + # Recreate shims for version package + export extern "asdf reshim" [ + name?: string@"complete asdf installed" # Name of the package + version?: string # Version of the package + ] + + # List the plugins and versions that provide a command + export extern "asdf shim-version" [ + command: string # Name of the command + ] + + # Update asdf to the latest version on the stable branch + export extern "asdf update" [] + + # Update asdf to the latest version on the main branch + export extern "asdf update --head" [] + +} + +use asdf * \ No newline at end of file diff --git a/docs/guide/getting-started.md b/docs/guide/getting-started.md index 9c523480..59406683 100644 --- a/docs/guide/getting-started.md +++ b/docs/guide/getting-started.md @@ -239,6 +239,39 @@ Add the following to `~/.zshrc`: Completions are placed in a ZSH friendly location, but [ZSH must be configured to use the autocompletions](https://wiki.archlinux.org/index.php/zsh#Command_completion). ::: +::: details Nushell & Git + +Add `asdf.nu` to your `~/.config/nushell/config.nu` with: + +```shell +"\nsource " + ($env.HOME | path join '.asdf/asdf.nu') | save --append $nu.config-path +``` + +Completions are automatically configured +::: + +::: details Nushell & Homebrew + +Add `asdf.nu` to your `~/.config/nushell/config.nu` with: + +```shell:no-line-numbers +"\nsource " + (brew --prefix asdf | into string | path join 'libexec/asdf.nu') | save --append $nu.config-path +``` + +Completions are automatically configured +::: + +::: details Nushell & Pacman + +Add `asdf.nu` to your `~/.config/nushell/config.nu` with: + +```shell +"\nsource /opt/asdf-vm/asdf.nu" | save --append $nu.config-path +``` + +Completions are automatically configured. +::: + `asdf` scripts need to be sourced **after** you have set your `$PATH` and **after** you have sourced your framework (oh-my-zsh etc). Restart your shell so that `PATH` changes take effect. Opening a new terminal tab will usually do it.