From 1503419f31f49b918629231280af9f985ae41441 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sun, 22 Dec 2024 19:20:36 -0500 Subject: [PATCH] chore(golang-rewrite): remove old Bash implementation code --- asdf.elv | 243 ----- asdf.fish | 37 - asdf.nu | 280 ------ asdf.ps1 | 31 - asdf.sh | 144 --- bin/asdf | 125 --- bin/private/asdf-exec | 15 - lib/commands/command-current.bash | 83 -- lib/commands/command-env.bash | 24 - lib/commands/command-exec.bash | 34 - .../command-export-shell-version.bash | 71 -- lib/commands/command-global.bash | 5 - lib/commands/command-help.bash | 116 --- lib/commands/command-info.bash | 18 - lib/commands/command-install.bash | 9 - lib/commands/command-latest.bash | 5 - lib/commands/command-list-all.bash | 5 - lib/commands/command-list.bash | 59 -- lib/commands/command-local.bash | 6 - lib/commands/command-plugin-add.bash | 5 - lib/commands/command-plugin-list-all.bash | 29 - lib/commands/command-plugin-list.bash | 5 - lib/commands/command-plugin-push.bash | 19 - lib/commands/command-plugin-remove.bash | 36 - lib/commands/command-plugin-test.bash | 187 ---- lib/commands/command-plugin-update.bash | 5 - lib/commands/command-reshim.bash | 6 - lib/commands/command-shim-versions.bash | 8 - lib/commands/command-uninstall.bash | 56 -- lib/commands/command-update.bash | 9 - lib/commands/command-version.bash | 2 - lib/commands/command-where.bash | 50 - lib/commands/command-which.bash | 29 - lib/commands/reshim.bash | 156 --- lib/commands/version_commands.bash | 3 - lib/functions/installs.bash | 256 ----- lib/functions/plugins.bash | 169 ---- lib/functions/versions.bash | 240 ----- lib/utils.bash | 898 ------------------ test/test_helpers.bash | 2 +- test/utils.bash | 33 + 41 files changed, 34 insertions(+), 3479 deletions(-) delete mode 100644 asdf.elv delete mode 100644 asdf.fish delete mode 100644 asdf.nu delete mode 100644 asdf.ps1 delete mode 100644 asdf.sh delete mode 100755 bin/asdf delete mode 100755 bin/private/asdf-exec delete mode 100644 lib/commands/command-current.bash delete mode 100644 lib/commands/command-env.bash delete mode 100644 lib/commands/command-exec.bash delete mode 100644 lib/commands/command-export-shell-version.bash delete mode 100644 lib/commands/command-global.bash delete mode 100644 lib/commands/command-help.bash delete mode 100644 lib/commands/command-info.bash delete mode 100644 lib/commands/command-install.bash delete mode 100644 lib/commands/command-latest.bash delete mode 100644 lib/commands/command-list-all.bash delete mode 100644 lib/commands/command-list.bash delete mode 100644 lib/commands/command-local.bash delete mode 100644 lib/commands/command-plugin-add.bash delete mode 100644 lib/commands/command-plugin-list-all.bash delete mode 100644 lib/commands/command-plugin-list.bash delete mode 100644 lib/commands/command-plugin-push.bash delete mode 100644 lib/commands/command-plugin-remove.bash delete mode 100644 lib/commands/command-plugin-test.bash delete mode 100644 lib/commands/command-plugin-update.bash delete mode 100644 lib/commands/command-reshim.bash delete mode 100644 lib/commands/command-shim-versions.bash delete mode 100644 lib/commands/command-uninstall.bash delete mode 100644 lib/commands/command-update.bash delete mode 100644 lib/commands/command-version.bash delete mode 100644 lib/commands/command-where.bash delete mode 100644 lib/commands/command-which.bash delete mode 100644 lib/commands/reshim.bash delete mode 100644 lib/commands/version_commands.bash delete mode 100644 lib/functions/installs.bash delete mode 100644 lib/functions/plugins.bash delete mode 100644 lib/functions/versions.bash delete mode 100644 lib/utils.bash create mode 100644 test/utils.bash diff --git a/asdf.elv b/asdf.elv deleted file mode 100644 index a2e016aa7..000000000 --- a/asdf.elv +++ /dev/null @@ -1,243 +0,0 @@ -use path -use re -use str - -var asdf_dir = ~'/.asdf' -if (and (has-env ASDF_DIR) (!=s $E:ASDF_DIR '')) { - set asdf_dir = $E:ASDF_DIR -} else { - set-env ASDF_DIR $asdf_dir -} - -var asdf_data_dir = $asdf_dir -if (and (has-env ASDF_DATA_DIR) (!=s $E:ASDF_DATA_DIR '')) { - set asdf_data_dir = $E:ASDF_DATA_DIR -} - -# Add function wrapper so we can export variables -fn asdf {|command @args| - if (==s $command 'shell') { - # set environment variables - var parts = [($asdf_dir'/bin/asdf' export-shell-version elvish $@args)] - if (==s $parts[0] 'set-env') { - set-env $parts[1] $parts[2] - } elif (==s $parts[0] 'unset-env') { - unset-env $parts[1] - } - } else { - # forward other commands to asdf script - $asdf_dir'/bin/asdf' $command $@args - } -} - -fn match {|argz @pats| - var matched = $true - if (!= (count $argz) (count $pats)) { - set matched = $false - } else { - for i [(range (count $pats))] { - var pat = '^'$pats[$i]'$' - var arg = $argz[$i] - if (not (re:match $pat $arg)) { - set matched = $false - break - } - } - } - put $matched -} - -fn ls-shims { - ls $asdf_data_dir'/shims' -} - -fn ls-executables { - # Print all executable files and links in path - try { - find $@paths '(' -type f -o -type l ')' -print 2>/dev/null | each {|p| - try { - if (test -x $p) { - path:base $p - } - } catch { - # don't fail if permission denied - } - } - } catch { - # silence default non-zero exit status - } -} - -fn ls-installed-versions {|plugin_name| - asdf list $plugin_name | each {|version| - put (re:replace '\s*(.*)\s*' '${1}' $version) - } -} - -fn ls-all-versions {|plugin_name| - asdf list-all $plugin_name | each {|version| - put (re:replace '\s*(.*)\s*' '${1}' $version) - } -} - -# Append ~/.asdf/bin and ~/.asdf/shims to PATH -for path [ - $asdf_dir'/bin' - $asdf_data_dir'/shims' -] { - if (not (has-value $paths $path)) { - set paths = [ - $path - $@paths - ] - } -} - -# Setup argument completions -fn arg-completer {|@argz| - set argz = $argz[1..-1] # strip 'asdf' and trailing empty string - var num = (count $argz) - if (== $num 0) { - # list all subcommands - find $asdf_dir'/lib/commands' -name 'command-*' | each {|cmd| - put (re:replace '.*/command-(.*)\.bash' '${1}' $cmd) - } - put 'plugin' - } else { - if (match $argz 'current') { - # asdf current - asdf plugin-list - } elif (match $argz 'env') { - # asdf env - ls-shims - } elif (match $argz 'env' '.*') { - # asdf env [util] - ls-executables - } elif (match $argz 'exec') { - # asdf exec - ls-shims - } elif (match $argz 'global') { - # asdf global - asdf plugin-list - } elif (match $argz 'global' '.*') { - # asdf global - ls-installed-versions $argz[-1] - } elif (match $argz 'install') { - # asdf install - asdf plugin-list - } elif (match $argz 'install' '.*') { - # asdf install - ls-all-versions $argz[-1] - } elif (match $argz 'install' '.*' '.*') { - # asdf install [--keep-download] - put '--keep-download' - } elif (match $argz 'latest') { - # asdf latest - asdf plugin-list - } elif (match $argz 'latest' '.*') { - # asdf latest [] - ls-all-versions $argz[-1] - } elif (match $argz 'list-all') { - # asdf list all - asdf plugin-list - } elif (match $argz 'list-all' '.*') { - # asdf list all [] - ls-all-versions $argz[-1] - } elif (match $argz 'list') { - # asdf list - asdf plugin-list - } elif (match $argz 'list' '.*') { - # asdf list [] - ls-installed-versions $argz[-1] - } elif (match $argz 'local') { - # asdf local [-p|--parent] - asdf plugin-list - put '-p' - put '--parent' - } elif (match $argz 'local' '(-p|(--parent))') { - # asdf local [-p|--parent] - asdf plugin-list - } elif (match $argz 'local' '.*') { - # asdf local [-p|--parent] - # asdf local - ls-installed-versions $argz[-1] - put '-p' - put '--parent' - } elif (match $argz 'local' '(-p|(--parent))' '.*') { - # asdf local [-p|--parent] - ls-installed-versions $argz[-1] - } elif (match $argz 'local' '.*' '(-p|(--parent))') { - # asdf local [-p|--parent] - ls-installed-versions $argz[-2] - } elif (match $argz 'local' '.*' '.*') { - # asdf local [-p|--parent] - put '-p' - put '--parent' - } elif (or (match $argz 'plugin-add') (match $argz 'plugin' 'add')) { - # asdf plugin add - asdf plugin-list-all | each {|line| - put (re:replace '([^\s]+)\s+.*' '${1}' $line) - } - } elif (or (match $argz 'plugin-list') (match $argz 'plugin' 'list')) { - # asdf plugin list - put '--urls' - put '--refs' - put 'all' - } elif (or (match $argz 'plugin-push') (match $argz 'plugin' 'push')) { - # asdf plugin push - asdf plugin-list - } elif (or (match $argz 'plugin-remove') (match $argz 'plugin' 'remove')) { - # asdf plugin remove - asdf plugin-list - } elif (and (>= (count $argz) 3) (match $argz[..3] 'plugin-test' '.*' '.*')) { - # asdf plugin-test [--asdf-tool-version ] [--asdf-plugin-gitref ] [test-command*] - put '--asdf-plugin-gitref' - put '--asdf-tool-version' - ls-executables - ls-shims - } elif (and (>= (count $argz) 4) (match $argz[..4] 'plugin' 'test' '.*' '.*')) { - # asdf plugin test [--asdf-tool-version ] [--asdf-plugin-gitref ] [test-command*] - put '--asdf-plugin-gitref' - put '--asdf-tool-version' - ls-executables - ls-shims - } elif (or (match $argz 'plugin-update') (match $argz 'plugin' 'update')) { - # asdf plugin update - asdf plugin-list - put '--all' - } elif (match $argz 'plugin') { - # list plugin-* subcommands - find $asdf_dir'/lib/commands' -name 'command-plugin-*' | each {|cmd| - put (re:replace '.*/command-plugin-(.*)\.bash' '${1}' $cmd) - } - } elif (match $argz 'reshim') { - # asdf reshim - asdf plugin-list - } elif (match $argz 'reshim' '.*') { - # asdf reshim - ls-installed-versions $argz[-1] - } elif (match $argz 'shim-versions') { - # asdf shim-versions - ls-shims - } elif (match $argz 'uninstall') { - # asdf uninstall - asdf plugin-list - } elif (match $argz 'uninstall' '.*') { - # asdf uninstall - ls-installed-versions $argz[-1] - } elif (match $argz 'update') { - if (== $num 1) { - # asdf update - put '--head' - } - } elif (match $argz 'where') { - # asdf where - asdf plugin-list - } elif (match $argz 'where' '.*') { - # asdf where [] - ls-installed-versions $argz[-1] - } elif (match $argz 'which') { - ls-shims - } - } -} diff --git a/asdf.fish b/asdf.fish deleted file mode 100644 index 20024efff..000000000 --- a/asdf.fish +++ /dev/null @@ -1,37 +0,0 @@ -if test -z $ASDF_DIR - set ASDF_DIR (builtin realpath --no-symlinks (dirname (status filename))) -end -set --export ASDF_DIR $ASDF_DIR - -set -l _asdf_bin "$ASDF_DIR/bin" -if test -z $ASDF_DATA_DIR - set _asdf_shims "$HOME/.asdf/shims" -else - set _asdf_shims "$ASDF_DATA_DIR/shims" -end - -# Do not use fish_add_path (added in Fish 3.2) because it -# potentially changes the order of items in PATH -if not contains $_asdf_bin $PATH - set -gx --prepend PATH $_asdf_bin -end -if not contains $_asdf_shims $PATH - set -gx --prepend PATH $_asdf_shims -end -set --erase _asdf_bin -set --erase _asdf_shims - -# The asdf function is a wrapper so we can export variables -function asdf - set command $argv[1] - set -e argv[1] - - switch "$command" - case shell - # Source commands that need to export variables. - command asdf export-shell-version fish $argv | source # asdf_allow: source - case '*' - # Forward other commands to asdf script. - command asdf "$command" $argv - end -end diff --git a/asdf.nu b/asdf.nu deleted file mode 100644 index 751033265..000000000 --- a/asdf.nu +++ /dev/null @@ -1,280 +0,0 @@ -def --env configure-asdf [] { - $env.ASDF_DIR = ( - if ($env | get --ignore-errors ASDF_NU_DIR | is-empty) == false { - $env.ASDF_NU_DIR - } - else if ($env | get --ignore-errors ASDF_DIR | is-empty) == false { - $env.ASDF_DIR - } else { - print --stderr "asdf: Either ASDF_NU_DIR or ASDF_DIR must not be empty" - return - } - ) - - let shims_dir = ( - if ( $env | get --ignore-errors ASDF_DATA_DIR | is-empty ) { - $env.HOME | path join '.asdf' - } else { - $env.ASDF_DATA_DIR - } | path join 'shims' - ) - let asdf_bin_dir = ( $env.ASDF_DIR | path join 'bin' ) - - $env.PATH = ( $env.PATH | split row (char esep) | where { |p| $p != $shims_dir } | prepend $shims_dir ) - $env.PATH = ( $env.PATH | split row (char esep) | where { |p| $p != $asdf_bin_dir } | prepend $asdf_bin_dir ) -} - -configure-asdf - -## 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 - } - } - - def "complete asdf plugin versions all" [context: string] { - let plugin = $context | str trim | split words | last - ^asdf list all $plugin - | lines - | each { |line| $line | str trim } - | prepend "latest" - } - - def "complete asdf plugin versions installed" [context: string] { - let plugin = $context | str trim | split words | last - let versions = ^asdf list $plugin - | lines - | each { |line| $line | str trim } - | each { |version| if ($version | str starts-with "*") {{value: ($version | str substring 1..), description: "current version"}} else {{value: $version, description: ""}} } - - let latest = ^asdf latest $plugin | str trim - - if ($versions | get value | any {|el| $el == $latest}) { - $versions | prepend {value: "latest", description: $"alias to ($latest)"} - } else { - $versions - } - } - - # ASDF version manager - export extern main [ - 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, flag: '--urls', - template: '\s+?(?P(?:http[s]?|git).+\.git|/.+)'} - {name: 'refs', enabled: $refs, flag: '--refs', - template: '\s+?(?P\w+)\s+(?P\w+)'} - ] - - let template = '(?P.+)' + ( - $params | - where enabled | - get --ignore-errors template | - str join '' | - str trim - ) - - let flags = ($params | where enabled | get --ignore-errors flag | default '' ) - - ^asdf plugin list ...$flags | lines | parse -r $template | str trim - } - - # list all available plugins - export def "asdf plugin list all" [] { - let template = '(?P.+)\s+?(?P[*]?)(?P(?:git|http|https).+)' - 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@"complete asdf installed plugins" # Name of the package - version?: string@"complete asdf plugin versions all" # 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@"complete asdf plugin versions installed" # 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@"complete asdf plugin versions installed" # Version of installed package - ] - - # Set the package local version - export extern "asdf local" [ - name: string@"complete asdf installed" # Name of the package - version?: string@"complete asdf plugin versions installed" # 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@"complete asdf plugin versions installed" # 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@"complete asdf plugin versions installed" # Version of the package or latest - ] - - # Show latest stable version of a package - export extern "asdf latest" [ - name: string@"complete asdf installed" # Name of the package - version?: string@"complete asdf plugin versions installed" # 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@"complete asdf plugin versions installed" # Filter the version - ] - - # List all available package versions - export def "asdf list all" [ - name: string@"complete asdf installed" # Name of the package - version?: string@"complete asdf plugin versions installed"="" # 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@"complete asdf plugin versions installed" # 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@"complete asdf plugin versions installed" # 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 * diff --git a/asdf.ps1 b/asdf.ps1 deleted file mode 100644 index a4b405ee2..000000000 --- a/asdf.ps1 +++ /dev/null @@ -1,31 +0,0 @@ -$Env:ASDF_DIR = $PSScriptRoot - -$_asdf_bin = "$Env:ASDF_DIR/bin" -if ($null -eq $ASDF_DATA_DIR -or $ASDF_DATA_DIR -eq '') { - $_asdf_shims = "${env:HOME}/.asdf/shims" -} -else { - $_asdf_shims = "$ASDF_DATA_DIR/shims" -} - -$env:PATH = "${_asdf_bin}:${_asdf_shims}:${env:PATH}" - -if ($env:PATH -cnotlike "*${_asdf_bin}*") { - $env:PATH = "_asdf_bin:${env:PATH}" -} -if ($env:PATH -cnotlike "*${_asdf_shims}*") { - $env:PATH = "_asdf_shims:${env:PATH}" -} - -Remove-Variable -Force _asdf_bin, _asdf_shims - -function asdf { - $asdf = $(Get-Command -CommandType Application asdf).Source - - if ($args.Count -gt 0 -and $args[0] -eq 'shell') { - Invoke-Expression $(& $asdf 'export-shell-version' pwsh $args[1..($args.Count + -1)]) - } - else { - & $asdf $args - } -} diff --git a/asdf.sh b/asdf.sh deleted file mode 100644 index 0fd679935..000000000 --- a/asdf.sh +++ /dev/null @@ -1,144 +0,0 @@ -# shellcheck shell=sh -# shellcheck disable=SC1007 - -# This file is the entrypoint for all POSIX-compatible shells. If `ASDF_DIR` is -# not already set, this script is able to calculate it, but only if the shell is -# either Bash, Zsh, and Ksh. For other shells, `ASDF_DIR` must be manually set. - -export ASDF_DIR="${ASDF_DIR:-}" - -if [ -z "$ASDF_DIR" ]; then - if [ -n "${BASH_VERSION:-}" ]; then - # Use BASH_SOURCE[0] to obtain the relative path to this source'd file. Since it's - # a relative path, 'cd' to its dirname and use '$PWD' to obtain the fullpath. - # Use 'builtin cd' to ensure user-defined 'cd()' functions aren't called. - # Use variable '_asdf_old_dir' to avoid using subshells. - - _asdf_old_dir=$PWD - # shellcheck disable=SC3028,SC3054 - if ! CDPATH= builtin cd -- "${BASH_SOURCE[0]%/*}"; then - printf '%s\n' 'asdf: Error: Failed to cd' >&2 - unset -v _asdf_old_dir - return 1 - fi - ASDF_DIR=$PWD - if ! CDPATH= builtin cd -- "$_asdf_old_dir"; then - printf '%s\n' 'asdf: Error: Failed to cd' >&2 - unset -v _asdf_old_dir - return 1 - fi - unset -v _asdf_old_dir - elif [ -n "${ZSH_VERSION:-}" ]; then - # Use '%x' to expand to path of current file. It must be prefixed - # with '(%):-', so it expands in non-prompt-string contexts. - - # shellcheck disable=SC2296 - ASDF_DIR=${(%):-%x} - ASDF_DIR=${ASDF_DIR%/*} - elif [ -n "${KSH_VERSION:-}" ] && [ -z "$PATHSEP" ]; then - # Only the original KornShell (kornshell.com) has a '.sh.file' variable with the path - # of the current file. To prevent errors with other variations, such as the MirBSD - # Korn shell (mksh), test for 'PATHSEP' which is _not_ set on the original Korn Shell. - - # shellcheck disable=SC2296 - ASDF_DIR=${.sh.file} - ASDF_DIR=${ASDF_DIR%/*} - fi -fi - -if [ -z "$ASDF_DIR" ]; then - printf "%s\n" "asdf: Error: Source directory could not be calculated. Please set \$ASDF_DIR manually before sourcing this file." >&2 - return 1 -fi - -if [ ! -d "$ASDF_DIR" ]; then - printf "%s\n" "asdf: Error: Variable '\$ASDF_DIR' is not a directory: $ASDF_DIR" >&2 - return 1 -fi - -_asdf_bin="$ASDF_DIR/bin" -_asdf_shims="${ASDF_DATA_DIR:-$HOME/.asdf}/shims" - -_asdf_should_prepend=no -if [ -n "${ASDF_FORCE_PREPEND+x}" ]; then - _asdf_should_prepend=$ASDF_FORCE_PREPEND -else - # If ASDF_FORCE_PREPEND is not set, then prepend by default on macOS - # to workaround `path_helper`. - if [ -n "${BASH_VERSION:-}" ] || [ -n "${ZSH_VERSION:-}" ]; then - # shellcheck disable=SC3028 - case $OSTYPE in - darwin*) _asdf_should_prepend=yes ;; - esac - else - if ! _asdf_output=$(uname); then - printf "%s\n" "asdf: Error: Failed to execute 'uname'" >&2 - return 1 - fi - if [ "$_asdf_output" = 'Darwin' ]; then - _asdf_should_prepend=yes - fi - unset -v _asdf_output - fi -fi - -# If prepending is enabled, remove any existing instances of asdf from PATH so -# the prepending done after is always at the frontmost part of the PATH. -if [ "$_asdf_should_prepend" = 'yes' ]; then - if [ -n "${BASH_VERSION:-}" ] || [ -n "${ZSH_VERSION:-}" ]; then - # shellcheck disable=SC3060 - case ":$PATH:" in - *":${_asdf_bin}:"*) PATH=${PATH//$_asdf_bin:/} ;; - esac - # shellcheck disable=SC3060 - case ":$PATH:" in - *":${_asdf_shims}:"*) PATH=${PATH//$_asdf_shims:/} ;; - esac - else - _path=${PATH}: - _new_path= - while [ -n "$_path" ]; do - _part=${_path%%:*} - _path=${_path#*:} - - if [ "$_part" = "$_asdf_bin" ] || [ "$_part" = "$_asdf_shims" ]; then - continue - fi - - _new_path="$_new_path${_new_path:+:}$_part" - done - PATH=$_new_path - unset -v _path _new_path _part - fi -fi -unset -v _asdf_should_prepend - -case ":$PATH:" in - *":$_asdf_bin:"*) : ;; - *) PATH="$_asdf_bin:$PATH" ;; -esac -case ":$PATH:" in - *":$_asdf_shims:"*) : ;; - *) PATH="$_asdf_shims:$PATH" ;; -esac - -unset -v _asdf_bin _asdf_shims - -# The asdf function is a wrapper so we can export variables -asdf() { - case $1 in - "shell") - if ! shift; then - printf '%s\n' 'asdf: Error: Failed to shift' >&2 - return 1 - fi - - # Invoke command that needs to export variables. - eval "$(asdf export-shell-version sh "$@")" # asdf_allow: eval - ;; - *) - # Forward other commands to asdf script. - command asdf "$@" # asdf_allow: ' asdf ' - ;; - esac -} diff --git a/bin/asdf b/bin/asdf deleted file mode 100755 index 439fd1814..000000000 --- a/bin/asdf +++ /dev/null @@ -1,125 +0,0 @@ -#!/usr/bin/env bash - -set -o pipefail -if [[ "${ASDF_DEBUG}" == "1" ]]; then - set -x -fi - -# shellcheck source=lib/utils.bash -. "$(dirname "$(dirname "$0")")/lib/utils.bash" - -find_cmd() { - local cmd_dir="$1" - shift - - local cmd_name - local args_offset="$#" - cmd_name="command-$(tr ' ' '-' <<<"${@:1:${args_offset}}").bash" - while [ ! -f "$cmd_dir/$cmd_name" ] && [ "$args_offset" -gt 0 ]; do - args_offset=$((args_offset - 1)) - cmd_name="command-$(tr ' ' '-' <<<"${@:1:${args_offset}}").bash" - done - - if [ -f "$cmd_dir/$cmd_name" ]; then - printf "%s %s\n" "$cmd_dir/$cmd_name" "$((args_offset + 1))" - elif [ -f "$cmd_dir/command.bash" ]; then - printf "%s %s\n" "$cmd_dir/command.bash" 1 - fi -} - -find_asdf_cmd() { - local asdf_cmd_dir - asdf_cmd_dir="$(asdf_dir)/lib/commands" - case "$1" in - 'exec' | 'current' | 'env' | 'global' | 'install' | 'latest' | 'local' | \ - 'reshim' | 'uninstall' | 'update' | 'where' | 'which' | \ - 'export-shell-version') - printf "%s %s\n" "$asdf_cmd_dir/command-$1.bash" 2 - ;; - - '' | '--help' | '-h' | 'help') - printf "%s %s\n" "$asdf_cmd_dir/command-help.bash" 2 - ;; - - '--version' | 'version') - printf "%s %s\n" "$asdf_cmd_dir/command-version.bash" 2 - ;; - - *) - find_cmd "$asdf_cmd_dir" "$@" - ;; - esac -} - -find_plugin_cmd() { - local ASDF_CMD_FILE args_offset - local result= - result="$(find_cmd "$(get_plugin_path "$1")/lib/commands" "${@:2}")" - ASDF_CMD_FILE=${result% *} - args_offset=${result##* } - if [ -n "$ASDF_CMD_FILE" ]; then - args_offset=$((args_offset + 1)) # since the first argument is the plugin name - printf "%s %s\n" "$ASDF_CMD_FILE" "$args_offset" - fi -} - -asdf_cmd() { - local ASDF_CMD_FILE args_offset - - if [ "shell" = "$1" ]; then - printf "Shell integration is not enabled. Please ensure you source asdf in your shell setup." >&2 - exit 1 - fi - - # Internal Variables - ASDF_DEFAULT_TOOL_VERSIONS_FILENAME=$(asdf_tool_versions_filename) - export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME - - ASDF_CONFIG_FILE=$(asdf_config_file) - export ASDF_CONFIG_FILE - - ASDF_DATA_DIR=$(asdf_data_dir) - export ASDF_DATA_DIR - - ASDF_DIR=$(asdf_dir) - export ASDF_DIR - - local result= - result="$(find_asdf_cmd "$@")" - ASDF_CMD_FILE=${result% *} - args_offset=${result##* } - if [ -z "$ASDF_CMD_FILE" ]; then - result="$(find_plugin_cmd "$@")" - ASDF_CMD_FILE=${result% *} - args_offset=${result##* } - fi - - if [ -x "$ASDF_CMD_FILE" ]; then - # When '$ASDF_CMD_FILE' is an executable, we are executing a command directly from a plugin. - # Example: https://github.com/asdf-community/asdf-nim/blob/397c14a7f04ad5b91963814afc2e9cc92366e1c5/lib/commands/command-install-deps.bash - # In those cases, the path to that command is always an absolute path. However, this codepath can also be activated if a user accidentally - # marks files in ./lib/commands/* as executable. This code detects when that happens and prints a useful warning message. - if [[ "$ASDF_CMD_FILE" == ./lib/commands/* ]]; then - printf '%s\n' "----------" - printf '%s\n' "asdf: Warning: You are executing an asdf command from \$ASDF_DIR, but we detected that some files have been" - printf '%s\n' " erroneously marked as executable. All files under '$ASDF_DIR/lib/commands' must NOT be marked" - printf '%s\n' " as executable. Otherwise, asdf will not be able to source its core files" - printf '%s\n' "----------" - fi >&2 - - exec "$ASDF_CMD_FILE" "${@:${args_offset}}" - elif [ -f "$ASDF_CMD_FILE" ]; then - set -- "${@:${args_offset}}" - # shellcheck source=/dev/null - . "$ASDF_CMD_FILE" - else - local asdf_cmd_dir - asdf_cmd_dir="$(asdf_dir)/lib/commands" - printf "%s\n" "Unknown command: \`asdf ${*}\`" >&2 - # shellcheck source=lib/commands/command-help.bash - . "$asdf_cmd_dir/command-help.bash" >&2 - return 127 - fi -} - -asdf_cmd "$@" diff --git a/bin/private/asdf-exec b/bin/private/asdf-exec deleted file mode 100755 index eec8b74e3..000000000 --- a/bin/private/asdf-exec +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash - -# remove this asdf-exec file when releasing >=0.6.5 -printf "asdf is self upgrading shims to new asdf exec ...\\n" - -asdf_dir="$(dirname "$(dirname "$(dirname "$0")")")" -# shellcheck source=lib/utils.bash -. "$asdf_dir/lib/utils.bash" -rm "$(asdf_data_dir)"/shims/* -"$asdf_dir"/bin/asdf reshim -shim_name=$(basename "$2") - -printf "asdf: now running %s\\n" "$shim_name" - -exec "$shim_name" "${@:3}" diff --git a/lib/commands/command-current.bash b/lib/commands/command-current.bash deleted file mode 100644 index f4724626e..000000000 --- a/lib/commands/command-current.bash +++ /dev/null @@ -1,83 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/plugins.bash -. "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash" - -# shellcheck disable=SC2059 -plugin_current_command() { - local plugin_name=$1 - local terminal_format=$2 - - check_if_plugin_exists "$plugin_name" - - local search_path - search_path=$PWD - local version_and_path - version_and_path=$(find_versions "$plugin_name" "$search_path") - local full_version - full_version=$(cut -d '|' -f 1 <<<"$version_and_path") - local version_file_path - version_file_path=$(cut -d '|' -f 2 <<<"$version_and_path") - local version_not_installed - local description="" - - IFS=' ' read -r -a versions <<<"$full_version" - for version in "${versions[@]}"; do - if ! (check_if_version_exists "$plugin_name" "$version"); then - version_not_installed="$version" - fi - done - check_for_deprecated_plugin "$plugin_name" - - if [ -n "$version_not_installed" ]; then - description="Not installed. Run \"asdf install $plugin $version\"" - printf "$terminal_format" "$plugin" "$version" "$description" 1>&2 - return 1 - elif [ -z "$full_version" ]; then - description="No version is set. Run \"asdf $plugin \"" - printf "$terminal_format" "$plugin" "______" "$description" 1>&2 - return 126 - else - description="$version_file_path" - printf "$terminal_format" "$plugin" "$full_version" "$description" - fi -} - -# shellcheck disable=SC2059 -current_command() { - local terminal_format="%-15s %-15s %-10s\n" - local exit_status=0 - local plugin - - # printf "$terminal_format" "PLUGIN" "VERSION" "SET BY CONFIG" # disable this until we release headings across the board - if [ $# -eq 0 ]; then - # shellcheck disable=SC2119 - for plugin in $(plugin_list_command); do - plugin_current_command "$plugin" "$terminal_format" - done - else - plugin=$1 - plugin_current_command "$plugin" "$terminal_format" - exit_status="$?" - fi - - exit "$exit_status" -} - -# Warn if the plugin isn't using the updated legacy file api. -check_for_deprecated_plugin() { - local plugin_name=$1 - - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - local legacy_config - legacy_config=$(get_asdf_config_value "legacy_version_file") - local deprecated_script="${plugin_path}/bin/get-version-from-legacy-file" - local new_script="${plugin_path}/bin/list-legacy-filenames" - - if [ "$legacy_config" = "yes" ] && [ -f "$deprecated_script" ] && [ ! -f "$new_script" ]; then - printf "Heads up! It looks like your %s plugin is out of date. You can update it with:\n\n" "$plugin_name" - printf " asdf plugin-update %s\n\n" "$plugin_name" - fi -} - -current_command "$@" diff --git a/lib/commands/command-env.bash b/lib/commands/command-env.bash deleted file mode 100644 index 30cf7ec4e..000000000 --- a/lib/commands/command-env.bash +++ /dev/null @@ -1,24 +0,0 @@ -# -*- sh -*- - -shim_env_command() { - local shim_name="$1" - local env_cmd="${2}" - local env_args=("${@:3}") - - if [ -z "$shim_name" ]; then - printf "usage: asdf env \n" - exit 1 - fi - - if [ -z "$env_cmd" ]; then - env_cmd="env" - fi - - shim_env() { - "$env_cmd" "${env_args[@]}" - } - - with_shim_executable "$shim_name" shim_env || exit $? -} - -shim_env_command "$@" diff --git a/lib/commands/command-exec.bash b/lib/commands/command-exec.bash deleted file mode 100644 index cb56b36c3..000000000 --- a/lib/commands/command-exec.bash +++ /dev/null @@ -1,34 +0,0 @@ -# -*- sh -*- - -shim_exec_command() { - local shim_name - shim_name=$(basename "$1") - local shim_args=("${@:2}") - - if [ -z "$shim_name" ]; then - printf "usage: asdf exec \n" - exit 1 - fi - - exec_shim() { - local plugin_name="$1" - local version="$2" - local executable_path="$3" - - if [ ! -x "$executable_path" ]; then - printf "No %s executable found for %s %s\n" "$shim_name" "$plugin_name" "$version" >&2 - exit 2 - fi - - asdf_run_hook "pre_${plugin_name}_${shim_name}" "${shim_args[@]}" - pre_status=$? - if [ "$pre_status" -ne 0 ]; then - return "$pre_status" - fi - exec "$executable_path" "${shim_args[@]}" - } - - with_shim_executable "$shim_name" exec_shim || exit $? -} - -shim_exec_command "$@" diff --git a/lib/commands/command-export-shell-version.bash b/lib/commands/command-export-shell-version.bash deleted file mode 100644 index 92ace8abe..000000000 --- a/lib/commands/command-export-shell-version.bash +++ /dev/null @@ -1,71 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/versions.bash -. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" - -# Output from this command must be executable shell code -shell_command() { - local asdf_shell="$1" - shift - - if [ "$#" -lt "2" ]; then - printf "Usage: asdf shell {|--unset}\n" >&2 - printf "false\n" - exit 1 - fi - - local plugin=$1 - local version=$2 - - local upcase_name - upcase_name=$(tr '[:lower:]-' '[:upper:]_' <<<"$plugin") - local version_env_var="ASDF_${upcase_name}_VERSION" - - if [ "$version" = "--unset" ]; then - case "$asdf_shell" in - fish) - printf "set -e %s\n" "$version_env_var" - ;; - elvish) - # Elvish doesn't have a `source` command, and eval is banned, so the - # var name and value are printed on separate lines for asdf.elv to parse - # and pass to unset-env. - printf "unset-env\n%s" "$version_env_var" - ;; - pwsh) - printf '%s\n' "if (\$(Test-Path Env:$version_env_var) -eq 'True') { Remove-Item Env:$version_env_var }" - ;; - *) - printf "unset %s\n" "$version_env_var" - ;; - esac - exit 0 - fi - if [ "$version" = "latest" ]; then - version=$(latest_command "$plugin") - fi - if ! (check_if_version_exists "$plugin" "$version"); then - version_not_installed_text "$plugin" "$version" 1>&2 - printf "false\n" - exit 1 - fi - - case "$asdf_shell" in - fish) - printf "set -gx %s \"%s\"\n" "$version_env_var" "$version" - ;; - elvish) - # Elvish doesn't have a `source` command, and eval is banned, so the - # var name and value are printed on separate lines for asdf.elv to parse - # and pass to set-env. - printf "set-env\n%s\n%s" "$version_env_var" "$version" - ;; - pwsh) - printf '%s\n' "\$Env:$version_env_var = '$version'" - ;; - *) - printf "export %s=\"%s\"\n" "$version_env_var" "$version" - ;; - esac -} - -shell_command "$@" diff --git a/lib/commands/command-global.bash b/lib/commands/command-global.bash deleted file mode 100644 index 89f525188..000000000 --- a/lib/commands/command-global.bash +++ /dev/null @@ -1,5 +0,0 @@ -# -*- sh -*- - -# shellcheck source=lib/commands/version_commands.bash -. "$(dirname "$ASDF_CMD_FILE")/version_commands.bash" -version_command global "$@" diff --git a/lib/commands/command-help.bash b/lib/commands/command-help.bash deleted file mode 100644 index 77f1bb27c..000000000 --- a/lib/commands/command-help.bash +++ /dev/null @@ -1,116 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/versions.bash -. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" - -asdf_help() { - printf "version: %s\n\n" "$(asdf_version)" - cat "$(asdf_dir)/help.txt" -} - -asdf_moto() { - cat </dev/null)" - if [[ -n $ext_cmds ]]; then - printf "\nPLUGIN %s\n" "$plugin" - for ext_cmd in $ext_cmds; do - ext_cmd_name="$(basename "$ext_cmd")" - sed "s/-/ /g;s/.bash//;s/command-*/ asdf $plugin/;" <<<"$ext_cmd_name" - done | sort - fi - done -} - -help_command() { - local plugin_name="$1" - local tool_version="$2" - local plugin_path - - # If plugin name is present as first argument output plugin help info - if [ -n "$plugin_name" ]; then - plugin_path=$(get_plugin_path "$plugin_name") - - if [ -d "$plugin_path" ]; then - if [ -f "${plugin_path}/bin/help.overview" ]; then - if [ -n "$tool_version" ]; then - - # TODO: Refactor this code out into helper functions in utils.bash - IFS=':' read -r -a version_info <<<"$tool_version" - if [ "${version_info[0]}" = "ref" ]; then - local install_type="${version_info[0]}" - local version="${version_info[1]}" - else - local install_type="version" - - if [ "${version_info[0]}" = "latest" ]; then - local version - version=$(latest_command "$plugin_name" "${version_info[1]}") - else - local version="${version_info[0]}" - fi - fi - - local install_path - install_path=$(get_install_path "$plugin_name" "$install_type" "$version") - - ( - # shellcheck disable=SC2031 - export ASDF_INSTALL_TYPE=$install_type - # shellcheck disable=SC2031 - export ASDF_INSTALL_VERSION=$version - # shellcheck disable=SC2031 - export ASDF_INSTALL_PATH=$install_path - - print_plugin_help "$plugin_path" - ) - else - (print_plugin_help "$plugin_path") - fi - else - printf "No documentation for plugin %s\n" "$plugin_name" >&2 - exit 1 - fi - else - printf "No plugin named %s\n" "$plugin_name" >&2 - exit 1 - fi - else - # Otherwise output general asdf help - asdf_help - asdf_extension_cmds - asdf_moto - fi -} - -print_plugin_help() { - local plugin_path=$1 - - # Eventually @jthegedus or someone else will format the output from these - # scripts in a certain way. - "${plugin_path}"/bin/help.overview - - if [ -f "${plugin_path}"/bin/help.deps ]; then - "${plugin_path}"/bin/help.deps - fi - - if [ -f "${plugin_path}"/bin/help.config ]; then - "${plugin_path}"/bin/help.config - fi - - if [ -f "${plugin_path}"/bin/help.links ]; then - "${plugin_path}"/bin/help.links - fi -} - -help_command "$@" diff --git a/lib/commands/command-info.bash b/lib/commands/command-info.bash deleted file mode 100644 index d4e761cf4..000000000 --- a/lib/commands/command-info.bash +++ /dev/null @@ -1,18 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/plugins.bash -. "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash" - -info_command() { - printf "%s:\n%s\n\n" "OS" "$(uname -a)" - printf "%s:\n%s\n\n" "SHELL" "$("$SHELL" --version)" - printf "%s:\n%s\n\n" "BASH VERSION" "$BASH_VERSION" - printf "%s:\n%s\n\n" "ASDF VERSION" "$(asdf_version)" - printf '%s\n' 'ASDF INTERNAL VARIABLES:' - printf 'ASDF_DEFAULT_TOOL_VERSIONS_FILENAME=%s\n' "${ASDF_DEFAULT_TOOL_VERSIONS_FILENAME}" - printf 'ASDF_DATA_DIR=%s\n' "${ASDF_DATA_DIR}" - printf 'ASDF_DIR=%s\n' "${ASDF_DIR}" - printf 'ASDF_CONFIG_FILE=%s\n\n' "${ASDF_CONFIG_FILE}" - printf "%s:\n%s\n\n" "ASDF INSTALLED PLUGINS" "$(plugin_list_command --urls --refs)" -} - -info_command "$@" diff --git a/lib/commands/command-install.bash b/lib/commands/command-install.bash deleted file mode 100644 index 44c4d3fc8..000000000 --- a/lib/commands/command-install.bash +++ /dev/null @@ -1,9 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/versions.bash -. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" -# shellcheck source=lib/commands/reshim.bash -. "$(dirname "$ASDF_CMD_FILE")/reshim.bash" -# shellcheck source=lib/functions/installs.bash -. "$(dirname "$(dirname "$0")")/lib/functions/installs.bash" - -install_command "$@" diff --git a/lib/commands/command-latest.bash b/lib/commands/command-latest.bash deleted file mode 100644 index da478e30a..000000000 --- a/lib/commands/command-latest.bash +++ /dev/null @@ -1,5 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/versions.bash -. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" - -latest_command "$@" diff --git a/lib/commands/command-list-all.bash b/lib/commands/command-list-all.bash deleted file mode 100644 index 730a9af90..000000000 --- a/lib/commands/command-list-all.bash +++ /dev/null @@ -1,5 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/versions.bash -. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" - -list_all_command "$@" diff --git a/lib/commands/command-list.bash b/lib/commands/command-list.bash deleted file mode 100644 index 6cb4cd4b9..000000000 --- a/lib/commands/command-list.bash +++ /dev/null @@ -1,59 +0,0 @@ -# -*- sh -*- - -list_command() { - local plugin_name=$1 - local query=$2 - - if [ -z "$plugin_name" ]; then - local plugins_path - plugins_path=$(get_plugin_path) - - if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then - for plugin_path in "$plugins_path"/*/; do - plugin_name=$(basename "$plugin_path") - printf "%s\n" "$plugin_name" - display_installed_versions "$plugin_name" "$query" - done - else - printf "%s\n" 'No plugins installed' - fi - else - check_if_plugin_exists "$plugin_name" - display_installed_versions "$plugin_name" "$query" - fi -} - -display_installed_versions() { - local plugin_name=$1 - local query=$2 - local versions - local current_version - local flag - - versions=$(list_installed_versions "$plugin_name") - - if [[ $query ]]; then - versions=$(printf "%s\n" "$versions" | grep -E "^\s*$query") - - if [ -z "${versions}" ]; then - display_error "No compatible versions installed ($plugin_name $query)" - exit 1 - fi - fi - - if [ -n "${versions}" ]; then - current_version=$(cut -d '|' -f 1 <<<"$(find_versions "$plugin_name" "$PWD")") - - for version in $versions; do - flag=" " - if [[ "$version" == "$current_version" ]]; then - flag=" *" - fi - printf "%s%s\n" "$flag" "$version" - done - else - display_error ' No versions installed' - fi -} - -list_command "$@" diff --git a/lib/commands/command-local.bash b/lib/commands/command-local.bash deleted file mode 100644 index 034e8ac0e..000000000 --- a/lib/commands/command-local.bash +++ /dev/null @@ -1,6 +0,0 @@ -# -*- sh -*- - -# shellcheck source=lib/commands/version_commands.bash -. "$(dirname "$ASDF_CMD_FILE")/version_commands.bash" - -local_command "$@" diff --git a/lib/commands/command-plugin-add.bash b/lib/commands/command-plugin-add.bash deleted file mode 100644 index be9c8603d..000000000 --- a/lib/commands/command-plugin-add.bash +++ /dev/null @@ -1,5 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/plugins.bash -. "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash" - -plugin_add_command "$@" diff --git a/lib/commands/command-plugin-list-all.bash b/lib/commands/command-plugin-list-all.bash deleted file mode 100644 index 232b69cd6..000000000 --- a/lib/commands/command-plugin-list-all.bash +++ /dev/null @@ -1,29 +0,0 @@ -# -*- sh -*- - -plugin_list_all_command() { - initialize_or_update_plugin_repository - - local plugins_index_path - plugins_index_path="$(asdf_data_dir)/repository/plugins" - - local plugins_local_path - plugins_local_path="$(get_plugin_path)" - - if find "$plugins_index_path" -mindepth 1 -type d &>/dev/null; then - ( - for index_plugin in "$plugins_index_path"/*; do - index_plugin_name=$(basename "$index_plugin") - source_url=$(get_plugin_source_url "$index_plugin_name") - installed_flag=" " - - [[ -d "${plugins_local_path}/${index_plugin_name}" ]] && installed_flag='*' - - printf "%s\t%s\n" "$index_plugin_name" "$installed_flag$source_url" - done - ) | awk '{ printf("%-28s", $1); sub(/^[^*]/, " &", $2); $1=""; print $0 }' - else - printf "%s%s\n" "error: index of plugins not found at " "$plugins_index_path" - fi -} - -plugin_list_all_command "$@" diff --git a/lib/commands/command-plugin-list.bash b/lib/commands/command-plugin-list.bash deleted file mode 100644 index a9465894f..000000000 --- a/lib/commands/command-plugin-list.bash +++ /dev/null @@ -1,5 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/plugins.bash -. "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash" - -plugin_list_command "$@" diff --git a/lib/commands/command-plugin-push.bash b/lib/commands/command-plugin-push.bash deleted file mode 100644 index e09a222f2..000000000 --- a/lib/commands/command-plugin-push.bash +++ /dev/null @@ -1,19 +0,0 @@ -# -*- sh -*- - -plugin_push_command() { - local plugin_name=$1 - if [ "$plugin_name" = "--all" ]; then - for dir in "$(asdf_data_dir)"/plugins/*/; do - printf "Pushing %s...\n" "$(basename "$dir")" - (cd "$dir" && git push) - done - else - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - check_if_plugin_exists "$plugin_name" - printf "Pushing %s...\n" "$plugin_name" - (cd "$plugin_path" && git push) - fi -} - -plugin_push_command "$@" diff --git a/lib/commands/command-plugin-remove.bash b/lib/commands/command-plugin-remove.bash deleted file mode 100644 index 7b959bdf7..000000000 --- a/lib/commands/command-plugin-remove.bash +++ /dev/null @@ -1,36 +0,0 @@ -# -*- sh -*- - -plugin_remove_command() { - local plugin_name=$1 - check_if_plugin_exists "$plugin_name" - - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - - asdf_run_hook "pre_asdf_plugin_remove" "$plugin_name" - asdf_run_hook "pre_asdf_plugin_remove_${plugin_name}" - - if [ -f "${plugin_path}/bin/pre-plugin-remove" ]; then - ( - export ASDF_PLUGIN_PATH=$plugin_path - "${plugin_path}/bin/pre-plugin-remove" - ) - fi - - rm -rf "$plugin_path" - rm -rf "$(asdf_data_dir)/installs/${plugin_name}" - rm -rf "$(asdf_data_dir)/downloads/${plugin_name}" - - for f in "$(asdf_data_dir)"/shims/*; do - if [ -f "$f" ]; then # nullglob may not be set - if grep -q "asdf-plugin: ${plugin_name}" "$f"; then - rm -f "$f" - fi - fi - done - - asdf_run_hook "post_asdf_plugin_remove" "$plugin_name" - asdf_run_hook "post_asdf_plugin_remove_${plugin_name}" -} - -plugin_remove_command "$@" diff --git a/lib/commands/command-plugin-test.bash b/lib/commands/command-plugin-test.bash deleted file mode 100644 index c12af6c17..000000000 --- a/lib/commands/command-plugin-test.bash +++ /dev/null @@ -1,187 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/versions.bash -. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" -# shellcheck source=lib/functions/plugins.bash -. "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash" -# shellcheck source=lib/commands/reshim.bash -. "$(dirname "$ASDF_CMD_FILE")/reshim.bash" -# shellcheck source=lib/functions/installs.bash -. "$(dirname "$(dirname "$0")")/lib/functions/installs.bash" - -plugin_test_command() { - - local plugin_name="$1" - local plugin_url="$2" - shift 2 - - local exit_code - local TEST_DIR - - fail_test() { - printf "FAILED: %s\n" "$1" - rm -rf "$TEST_DIR" - exit 1 - } - - if [ -z "$plugin_name" ] || [ -z "$plugin_url" ]; then - fail_test "please provide a plugin name and url" - fi - - local plugin_gitref - local tool_version - local interpret_args_literally - local skip_next_arg - - for arg; do - shift - if [ -n "${skip_next_arg}" ]; then - skip_next_arg= - elif [ -n "${interpret_args_literally}" ]; then - set -- "$@" "${arg}" - else - case "${arg}" in - --asdf-plugin-gitref) - plugin_gitref="$1" - skip_next_arg=true - ;; - --asdf-tool-version) - tool_version="$1" - skip_next_arg=true - ;; - --) - interpret_args_literally=true - ;; - *) - set -- "$@" "${arg}" - ;; - esac - fi - done - - if [ -z "$plugin_gitref" ]; then - plugin_remote_default_branch=$(git ls-remote --symref "$plugin_url" HEAD | awk '{ sub(/refs\/heads\//, ""); print $2; exit }') - plugin_gitref=${3:-${plugin_remote_default_branch}} - fi - - if [ "$#" -eq 1 ]; then - set -- "${SHELL:-sh}" -c "$1" - fi - - TEST_DIR=$(mktemp -dt asdf.XXXXXX) - cp -R "$(asdf_dir)/bin" "$TEST_DIR" - cp -R "$(asdf_dir)/lib" "$TEST_DIR" - cp "$(asdf_dir)/asdf.sh" "$TEST_DIR" - - plugin_test() { - export ASDF_DIR=$TEST_DIR - export ASDF_DATA_DIR=$TEST_DIR - - # shellcheck disable=SC1090 - . "$ASDF_DIR/asdf.sh" - - if ! (plugin_add_command "$plugin_name" "$plugin_url"); then - fail_test "could not install $plugin_name from $plugin_url" - fi - - # shellcheck disable=SC2119 - if ! (plugin_list_command | grep -q "^$plugin_name$"); then - fail_test "$plugin_name was not properly installed" - fi - - if ! (plugin_update_command "$plugin_name" "$plugin_gitref"); then - fail_test "failed to checkout $plugin_name gitref: $plugin_gitref" - fi - - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - local list_all="$plugin_path/bin/list-all" - if grep -q api.github.com "$list_all"; then - if ! grep -q Authorization "$list_all"; then - printf "\nLooks like %s/bin/list-all relies on GitHub releases\n" "$plugin_name" - printf "but it does not properly sets an Authorization header to prevent\n" - printf "GitHub API rate limiting.\n\n" - printf "See https://github.com/asdf-vm/asdf/blob/master/docs/creating-plugins.md#github-api-rate-limiting\n" - - fail_test "$plugin_name/bin/list-all does not set GitHub Authorization token" - fi - - # test for most common token names we have on plugins. If both are empty show this warning - if [ -z "$OAUTH_TOKEN" ] && [ -z "$GITHUB_API_TOKEN" ]; then - printf "%s/bin/list-all is using GitHub API, just be sure you provide an API Authorization token\n" "$plugin_name" - printf "via your CI env GITHUB_API_TOKEN. This is the current rate_limit:\n\n" - curl -s https://api.github.com/rate_limit - printf "\n" - fi - fi - - local versions - # shellcheck disable=SC2046 - if ! read -r -a versions <<<$(list_all_command "$plugin_name"); then - fail_test "list-all exited with an error" - fi - - if [ ${#versions} -eq 0 ]; then - fail_test "list-all did not return any version" - fi - - local version - - # Use the version passed in if it was set. Otherwise grab the latest - # version from the versions list - if [ -z "$tool_version" ] || [[ "$tool_version" == *"latest"* ]]; then - version="$(latest_command "$plugin_name" "$(sed -e 's#latest##;s#^:##' <<<"$tool_version")")" - if [ -z "$version" ]; then - fail_test "could not get latest version" - fi - else - version="$tool_version" - fi - - if ! (install_command "$plugin_name" "$version"); then - fail_test "install exited with an error" - fi - - cd "$TEST_DIR" || fail_test "could not cd $TEST_DIR" - - if ! (local_command "$plugin_name" "$version"); then - fail_test "install did not add the requested version" - fi - - if ! (reshim_command "$plugin_name"); then - fail_test "could not reshim plugin" - fi - - if [ "$#" -gt 0 ]; then - "$@" - exit_code=$? - if [ $exit_code != 0 ]; then - fail_test "$* failed with exit code $exit_code" - fi - fi - - # Assert the scripts in bin are executable by asdf - for filename in "$ASDF_DIR/plugins/$plugin_name/bin"/*; do - if [ ! -x "$filename" ]; then - fail_test "Incorrect permissions on $filename. Must be executable by asdf" - fi - done - - # Assert that a license file exists in the plugin repo and is not empty - license_file="$ASDF_DIR/plugins/$plugin_name/LICENSE" - if [ -f "$license_file" ]; then - if [ ! -s "$license_file" ]; then - fail_test "LICENSE file in the plugin repository must not be empty" - fi - else - fail_test "LICENSE file must be present in the plugin repository" - fi - } - - # run test in a subshell - (plugin_test "$@") - exit_code=$? - rm -rf "$TEST_DIR" - exit $exit_code -} - -plugin_test_command "$@" diff --git a/lib/commands/command-plugin-update.bash b/lib/commands/command-plugin-update.bash deleted file mode 100644 index 6ed9da749..000000000 --- a/lib/commands/command-plugin-update.bash +++ /dev/null @@ -1,5 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/plugins.bash -. "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash" - -plugin_update_command "$@" diff --git a/lib/commands/command-reshim.bash b/lib/commands/command-reshim.bash deleted file mode 100644 index 933862465..000000000 --- a/lib/commands/command-reshim.bash +++ /dev/null @@ -1,6 +0,0 @@ -# -*- sh -*- - -# shellcheck source=lib/commands/reshim.bash -. "$(dirname "$ASDF_CMD_FILE")/reshim.bash" - -reshim_command "$@" diff --git a/lib/commands/command-shim-versions.bash b/lib/commands/command-shim-versions.bash deleted file mode 100644 index 9a8181a1e..000000000 --- a/lib/commands/command-shim-versions.bash +++ /dev/null @@ -1,8 +0,0 @@ -# -*- sh -*- - -shim_versions_command() { - local shim_name=$1 - shim_plugin_versions "$shim_name" -} - -shim_versions_command "$@" diff --git a/lib/commands/command-uninstall.bash b/lib/commands/command-uninstall.bash deleted file mode 100644 index 2e1774f9b..000000000 --- a/lib/commands/command-uninstall.bash +++ /dev/null @@ -1,56 +0,0 @@ -# -*- sh -*- - -# shellcheck source=lib/commands/reshim.bash -. "$(dirname "$ASDF_CMD_FILE")/reshim.bash" - -uninstall_command() { - local plugin_name=$1 - local full_version=$2 - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - - check_if_plugin_exists "$plugin_name" - - IFS=':' read -r -a version_info <<<"$full_version" - if [ "${version_info[0]}" = "ref" ]; then - local install_type="${version_info[0]}" - local version="${version_info[1]}" - else - local install_type="version" - local version="${version_info[0]}" - fi - - local install_path - install_path=$(get_install_path "$plugin_name" "$install_type" "$version") - - if [ ! -d "$install_path" ]; then - display_error "No such version" - exit 1 - fi - - asdf_run_hook "pre_asdf_uninstall_${plugin_name}" "$full_version" - remove_shims_for_version "$plugin_name" "$full_version" - - if [ -f "${plugin_path}/bin/uninstall" ]; then - ( - export ASDF_INSTALL_TYPE=$install_type - export ASDF_INSTALL_VERSION=$version - export ASDF_INSTALL_PATH=$install_path - "${plugin_path}/bin/uninstall" - ) - else - rm -rf "$install_path" - fi - - asdf_run_hook "post_asdf_uninstall_${plugin_name}" "$full_version" -} - -remove_shims_for_version() { - local plugin_name=$1 - local full_version=$2 - for shim_path in $(plugin_shims "$plugin_name" "$full_version"); do - remove_shim_for_version "$plugin_name" "$full_version" "$shim_path" - done -} - -uninstall_command "$@" diff --git a/lib/commands/command-update.bash b/lib/commands/command-update.bash deleted file mode 100644 index 5e19c39cc..000000000 --- a/lib/commands/command-update.bash +++ /dev/null @@ -1,9 +0,0 @@ -# -*- sh -*- - -update_command() { - printf "Upgrading asdf via asdf update is no longer supported. Please use your OS\npackage manager (Homebrew, APT, etc...) to upgrade asdf or download the\nlatest asdf binary manually from the asdf website.\n\nPlease visit https://asdf-vm.com/ or https://github.com/asdf-vm/asdf for more\ndetails.\n" - - exit 1 -} - -update_command "$@" diff --git a/lib/commands/command-version.bash b/lib/commands/command-version.bash deleted file mode 100644 index 3512457a6..000000000 --- a/lib/commands/command-version.bash +++ /dev/null @@ -1,2 +0,0 @@ -# -*- sh -*- -asdf_version diff --git a/lib/commands/command-where.bash b/lib/commands/command-where.bash deleted file mode 100644 index d2b9d0396..000000000 --- a/lib/commands/command-where.bash +++ /dev/null @@ -1,50 +0,0 @@ -# -*- sh -*- - -where_command() { - local plugin_name=$1 - local full_version=$2 - check_if_plugin_exists "$plugin_name" - - local version - local install_type="version" - if [[ -z ${full_version} ]]; then - local version_and_path - local versions - version_and_path=$(find_versions "$plugin_name" "$PWD") - versions=$(cut -d '|' -f 1 <<<"$version_and_path") - IFS=' ' read -r -a plugin_versions <<<"$versions" - version="${plugin_versions[0]}" - else - local -a version_info - IFS=':' read -r -a version_info <<<"$full_version" - if [ "${version_info[0]}" = "ref" ]; then - install_type="${version_info[0]}" - version="${version_info[1]}" - else - version="${version_info[0]}" - fi - fi - - if [ -z "$version" ]; then - display_no_version_set "$plugin_name" - exit 1 - fi - - local install_path - install_path=$(get_install_path "$plugin_name" "$install_type" "$version") - - if [ -d "$install_path" ]; then - printf "%s\n" "$install_path" - exit 0 - else - if [ "$version" = "system" ]; then - printf "System version is selected\n" - exit 1 - else - printf "Version not installed\n" - exit 1 - fi - fi -} - -where_command "$@" diff --git a/lib/commands/command-which.bash b/lib/commands/command-which.bash deleted file mode 100644 index 8696dbf3e..000000000 --- a/lib/commands/command-which.bash +++ /dev/null @@ -1,29 +0,0 @@ -# -*- sh -*- - -which_command() { - local shim_name - shim_name=$(basename "$1") - - if [ -z "$shim_name" ]; then - printf "usage: asdf which \n" - exit 1 - fi - - print_exec() { - local plugin_name="$1" - local version="$2" - local executable_path="$3" - - if [ ! -x "$executable_path" ]; then - printf "No %s executable found for %s %s\n" "$shim_name" "$plugin_name" "$version" >&2 - exit 1 - fi - - printf "%s\n" "$executable_path" - exit 0 - } - - with_shim_executable "$shim_name" print_exec || exit 1 -} - -which_command "$@" diff --git a/lib/commands/reshim.bash b/lib/commands/reshim.bash deleted file mode 100644 index 2317973a1..000000000 --- a/lib/commands/reshim.bash +++ /dev/null @@ -1,156 +0,0 @@ -remove_shim_for_version() { - local plugin_name=$1 - local version=$2 - local shim_name - - shim_name=$(basename "$3") - - local shim_path - shim_path="$(asdf_data_dir)/shims/$shim_name" - - local count_installed - count_installed=$(list_installed_versions "$plugin_name" | wc -l) - - if ! grep -x "# asdf-plugin: $plugin_name $version" "$shim_path" &>/dev/null; then - return 0 - fi - - sed -i.bak -e "/# asdf-plugin: $plugin_name $version"'$/d' "$shim_path" - rm "$shim_path".bak - - if ! grep -q "# asdf-plugin:" "$shim_path" || - [ "$count_installed" -eq 0 ]; then - rm -f "$shim_path" - fi -} - -reshim_command() { - local plugin_name=$1 - local full_version=$2 - - if [ -z "$plugin_name" ]; then - local plugins_path - plugins_path=$(get_plugin_path) - - if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then - for plugin_path in "$plugins_path"/*/; do - plugin_name=$(basename "$plugin_path") - reshim_command "$plugin_name" - done - fi - return 0 - fi - - check_if_plugin_exists "$plugin_name" - ensure_shims_dir - - if [ "$full_version" != "" ]; then - # generate for the whole package version - asdf_run_hook "pre_asdf_reshim_$plugin_name" "$full_version" - generate_shims_for_version "$plugin_name" "$full_version" - asdf_run_hook "post_asdf_reshim_$plugin_name" "$full_version" - else - # generate for all versions of the package - local plugin_installs_path - plugin_installs_path="$(asdf_data_dir)/installs/${plugin_name}" - - for install in "${plugin_installs_path}"/*/; do - local full_version_name - full_version_name=$(basename "$install" | sed 's/ref\-/ref\:/') - asdf_run_hook "pre_asdf_reshim_$plugin_name" "$full_version_name" - generate_shims_for_version "$plugin_name" "$full_version_name" - remove_obsolete_shims "$plugin_name" "$full_version_name" - asdf_run_hook "post_asdf_reshim_$plugin_name" "$full_version_name" - done - fi - -} - -ensure_shims_dir() { - # Create shims dir if doesn't exist - if [ ! -d "$(asdf_data_dir)/shims" ]; then - mkdir "$(asdf_data_dir)/shims" - fi -} - -write_shim_script() { - local plugin_name=$1 - local version=$2 - local executable_path=$3 - - if ! is_executable "$executable_path"; then - return 0 - fi - - local executable_name - executable_name=$(basename "$executable_path") - - local shim_path - shim_path="$(asdf_data_dir)/shims/$executable_name" - - local temp_dir - temp_dir=${TMPDIR:-/tmp} - - local temp_versions_path - temp_versions_path=$(mktemp "$temp_dir/asdf-command-reshim-write-shims.XXXXXX") - cat <"$temp_versions_path" -# asdf-plugin: ${plugin_name} ${version} -EOF - - if [ -f "$shim_path" ]; then - grep '^#\sasdf-plugin:\s' <"$shim_path" >>"$temp_versions_path" - fi - - cat <"$shim_path" -#!/usr/bin/env bash -$(sort -u <"$temp_versions_path") -exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@" # asdf_allow: ' asdf ' -EOF - - rm "$temp_versions_path" - - chmod +x "$shim_path" -} - -generate_shims_for_version() { - local plugin_name=$1 - local full_version=$2 - local all_executable_paths - IFS=$'\n' read -rd '' -a all_executable_paths <<<"$(plugin_executables "$plugin_name" "$full_version")" - for executable_path in "${all_executable_paths[@]}"; do - write_shim_script "$plugin_name" "$full_version" "$executable_path" - done -} - -remove_obsolete_shims() { - local plugin_name=$1 - local full_version=$2 - - local shims - shims=$(plugin_shims "$plugin_name" "$full_version" | xargs -IX basename X | sort) - - local exec_names - exec_names=$(plugin_executables "$plugin_name" "$full_version" | xargs -IX basename X | sort) - - local obsolete_shims - local formatted_shims - local formatted_exec_names - - local temp_dir - temp_dir=${TMPDIR:-/tmp} - - # comm only takes to files, so we write this data to temp files so we can - # pass it to comm. - formatted_shims="$(mktemp "$temp_dir/asdf-command-reshim-formatted-shims.XXXXXX")" - printf "%s\n" "$shims" >"$formatted_shims" - - formatted_exec_names="$(mktemp "$temp_dir/asdf-command-reshim-formatted-exec-names.XXXXXX")" - printf "%s\n" "$exec_names" >"$formatted_exec_names" - - obsolete_shims=$(comm -23 "$formatted_shims" "$formatted_exec_names") - rm -f "$formatted_exec_names" "$formatted_shims" - - for shim_name in $obsolete_shims; do - remove_shim_for_version "$plugin_name" "$full_version" "$shim_name" - done -} diff --git a/lib/commands/version_commands.bash b/lib/commands/version_commands.bash deleted file mode 100644 index 5c2773c0f..000000000 --- a/lib/commands/version_commands.bash +++ /dev/null @@ -1,3 +0,0 @@ -# -*- sh -*- -# shellcheck source=lib/functions/versions.bash -. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" diff --git a/lib/functions/installs.bash b/lib/functions/installs.bash deleted file mode 100644 index 124a51696..000000000 --- a/lib/functions/installs.bash +++ /dev/null @@ -1,256 +0,0 @@ -handle_failure() { - local install_path="$1" - rm -rf "$install_path" - exit 1 -} - -handle_cancel() { - local install_path="$1" - printf "\nreceived sigint, cleaning up" - handle_failure "$install_path" -} - -install_command() { - local plugin_name=$1 - local full_version=$2 - local extra_args="${*:3}" - - if [ "$plugin_name" = "" ] && [ "$full_version" = "" ]; then - install_local_tool_versions "$extra_args" - elif [[ $# -eq 1 ]]; then - install_one_local_tool "$plugin_name" - else - install_tool_version "$plugin_name" "$full_version" "$extra_args" - fi -} - -get_concurrency() { - local asdf_concurrency= - - if [ -n "$ASDF_CONCURRENCY" ]; then - asdf_concurrency="$ASDF_CONCURRENCY" - else - asdf_concurrency=$(get_asdf_config_value 'concurrency') - fi - - if [ "$asdf_concurrency" = 'auto' ]; then - if command -v nproc &>/dev/null; then - asdf_concurrency=$(nproc) - elif command -v sysctl &>/dev/null && sysctl hw.ncpu &>/dev/null; then - asdf_concurrency=$(sysctl -n hw.ncpu) - elif [ -f /proc/cpuinfo ]; then - asdf_concurrency=$(grep -c processor /proc/cpuinfo) - else - asdf_concurrency="1" - fi - fi - - printf "%s\n" "$asdf_concurrency" -} - -install_one_local_tool() { - local plugin_name=$1 - local search_path - search_path=$PWD - - local plugin_versions - - local plugin_version - - local plugin_version_and_path - plugin_version_and_path="$(find_versions "$plugin_name" "$search_path")" - - if [ -n "$plugin_version_and_path" ]; then - local plugin_version - plugin_versions=$(cut -d '|' -f 1 <<<"$plugin_version_and_path") - for plugin_version in $plugin_versions; do - install_tool_version "$plugin_name" "$plugin_version" - done - else - printf "No versions specified for %s in config files or environment\n" "$plugin_name" - exit 1 - fi -} - -install_local_tool_versions() { - local plugins_path - plugins_path=$(get_plugin_path) - - local search_path - search_path=$PWD - - local some_tools_installed - local some_plugin_not_installed - - local tool_versions_path - tool_versions_path=$(find_tool_versions) - - # Locate all the plugins installed in the system - local plugins_installed - if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then - for plugin_path in "$plugins_path"/*/; do - local plugin_name - plugin_name=$(basename "$plugin_path") - plugins_installed="$plugins_installed $plugin_name" - done - plugins_installed=$(printf "%s" "$plugins_installed" | tr " " "\n") - fi - - if [ -z "$plugins_installed" ]; then - printf "Install plugins first to be able to install tools\n" - exit 1 - fi - - # Locate all the plugins defined in the versions file. - local tools_file - if [ -f "$tool_versions_path" ]; then - tools_file=$(strip_tool_version_comments "$tool_versions_path" | cut -d ' ' -f 1) - for plugin_name in $tools_file; do - if ! printf '%s\n' "${plugins_installed[@]}" | grep -q "^$plugin_name\$"; then - printf "%s plugin is not installed\n" "$plugin_name" - some_plugin_not_installed='yes' - fi - done - fi - - if [ -n "$some_plugin_not_installed" ]; then - exit 1 - fi - - if [ -n "$plugins_installed" ]; then - for plugin_name in $plugins_installed; do - local plugin_version_and_path - plugin_version_and_path="$(find_versions "$plugin_name" "$search_path")" - - if [ -n "$plugin_version_and_path" ]; then - local plugin_version - some_tools_installed='yes' - plugin_versions=$(cut -d '|' -f 1 <<<"$plugin_version_and_path") - for plugin_version in $plugin_versions; do - install_tool_version "$plugin_name" "$plugin_version" - done - fi - done - fi - - if [ -z "$some_tools_installed" ]; then - printf "Either specify a tool & version in the command\n" - printf "OR add .tool-versions file in this directory\n" - printf "or in a parent directory\n" - exit 1 - fi -} - -install_tool_version() { - local plugin_name=$1 - local full_version=$2 - local flags=$3 - local keep_download - local plugin_path - - plugin_path=$(get_plugin_path "$plugin_name") - check_if_plugin_exists "$plugin_name" - - for flag in $flags; do - case "$flag" in - "--keep-download") - keep_download=true - shift - ;; - *) - shift - ;; - esac - done - - if [ "$full_version" = "system" ]; then - return - fi - - IFS=':' read -r -a version_info <<<"$full_version" - if [ "${version_info[0]}" = "ref" ]; then - local install_type="${version_info[0]}" - local version="${version_info[1]}" - else - local install_type="version" - - if [ "${version_info[0]}" = "latest" ]; then - local version - version=$(latest_command "$plugin_name" "${version_info[1]}") - full_version=$version - else - local version="${version_info[0]}" - fi - fi - - local install_path - install_path=$(get_install_path "$plugin_name" "$install_type" "$version") - local download_path - download_path=$(get_download_path "$plugin_name" "$install_type" "$version") - local concurrency - concurrency=$(get_concurrency) - trap 'handle_cancel $install_path' INT - - if [ -d "$install_path" ]; then - printf "%s %s is already installed\n" "$plugin_name" "$full_version" - else - - if [ -f "${plugin_path}/bin/download" ]; then - # Not a legacy plugin - # Run the download script - ( - # shellcheck disable=SC2030 - export ASDF_INSTALL_TYPE=$install_type - # shellcheck disable=SC2030 - export ASDF_INSTALL_VERSION=$version - # shellcheck disable=SC2030 - export ASDF_INSTALL_PATH=$install_path - # shellcheck disable=SC2030 - export ASDF_DOWNLOAD_PATH=$download_path - mkdir -p "$download_path" - asdf_run_hook "pre_asdf_download_${plugin_name}" "$full_version" - "${plugin_path}"/bin/download - ) - fi - - local download_exit_code=$? - if [ $download_exit_code -eq 0 ]; then - ( - # shellcheck disable=SC2031 - export ASDF_INSTALL_TYPE=$install_type - # shellcheck disable=SC2031 - export ASDF_INSTALL_VERSION=$version - # shellcheck disable=SC2031 - export ASDF_INSTALL_PATH=$install_path - # shellcheck disable=SC2031 - export ASDF_DOWNLOAD_PATH=$download_path - # shellcheck disable=SC2031 - export ASDF_CONCURRENCY=$concurrency - mkdir "$install_path" - asdf_run_hook "pre_asdf_install_${plugin_name}" "$full_version" - "${plugin_path}"/bin/install - ) - fi - - local install_exit_code=$? - if [ $install_exit_code -eq 0 ] && [ $download_exit_code -eq 0 ]; then - # If the download directory should be kept, but isn't available, warn the user - always_keep_download=$(get_asdf_config_value "always_keep_download") - if [ "$keep_download" = "true" ] || [ "$always_keep_download" = "yes" ]; then - if [ ! -d "$download_path" ]; then - printf '%s\n' "asdf: Warn: You have configured asdf to preserve downloaded files (with always_keep_download=yes or --keep-download). But" >&2 - printf '%s\n' "asdf: Warn: the current plugin ($plugin_name) does not support that. Downloaded files will not be preserved." >&2 - fi - # Otherwise, remove the download directory if it exists - elif [ -d "$download_path" ]; then - rm -rf "$download_path" - fi - - reshim_command "$plugin_name" "$full_version" - - asdf_run_hook "post_asdf_install_${plugin_name}" "$full_version" - else - handle_failure "$install_path" - fi - fi -} diff --git a/lib/functions/plugins.bash b/lib/functions/plugins.bash deleted file mode 100644 index 6f7a9435c..000000000 --- a/lib/functions/plugins.bash +++ /dev/null @@ -1,169 +0,0 @@ -plugin_list_command() { - local plugins_path - plugins_path=$(get_plugin_path) - - local show_repo - local show_ref - - while [ -n "$*" ]; do - case "$1" in - "--urls") - show_repo=true - shift - ;; - "--refs") - show_ref=true - shift - ;; - *) - shift - ;; - esac - done - - if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then - ( - for plugin_path in "$plugins_path"/*/; do - plugin_name=$(basename "$plugin_path") - printf "%s" "$plugin_name" - - if [ -n "$show_repo" ]; then - printf "\t%s" "$(get_plugin_remote_url "$plugin_name")" - fi - - if [ -n "$show_ref" ]; then - printf "\t%s\t%s" \ - "$(get_plugin_remote_branch "$plugin_name")" \ - "$(get_plugin_remote_gitref "$plugin_name")" - fi - - printf "\n" - done - ) | awk '{ if (NF > 1) { printf("%-28s", $1) ; $1="" }; print $0}' - else - display_error 'No plugins installed' - exit 0 - fi -} - -plugin_add_command() { - if [[ $# -lt 1 || $# -gt 2 ]]; then - display_error "usage: asdf plugin add []" - exit 1 - fi - - local plugin_name=$1 - - local regex="^[[:lower:][:digit:]_-]+$" - if ! printf "%s" "$plugin_name" | grep -q -E "$regex"; then - display_error "$plugin_name is invalid. Name may only contain lowercase letters, numbers, '_', and '-'" - exit 1 - fi - - if [ -n "$2" ]; then - local source_url=$2 - else - initialize_or_update_plugin_repository - local source_url - source_url=$(get_plugin_source_url "$plugin_name") - fi - - if [ -z "$source_url" ]; then - display_error "plugin $plugin_name not found in repository" - exit 1 - fi - - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - - [ -d "$(asdf_data_dir)/plugins" ] || mkdir -p "$(asdf_data_dir)/plugins" - - if [ -d "$plugin_path" ]; then - printf '%s\n' "Plugin named $plugin_name already added" - exit 0 - else - asdf_run_hook "pre_asdf_plugin_add" "$plugin_name" - asdf_run_hook "pre_asdf_plugin_add_${plugin_name}" - - if ! git clone -q "$source_url" "$plugin_path"; then - exit 1 - fi - - if [ -f "${plugin_path}/bin/post-plugin-add" ]; then - ( - export ASDF_PLUGIN_SOURCE_URL=$source_url - # shellcheck disable=SC2030 - export ASDF_PLUGIN_PATH=$plugin_path - "${plugin_path}/bin/post-plugin-add" - ) - fi - - asdf_run_hook "post_asdf_plugin_add" "$plugin_name" - asdf_run_hook "post_asdf_plugin_add_${plugin_name}" - fi -} - -plugin_update_command() { - if [ "$#" -lt 1 ]; then - display_error "usage: asdf plugin-update { [git-ref] | --all}" - exit 1 - fi - - local plugin_name="$1" - local gitref="${2}" - local plugins= - - if [ "$plugin_name" = "--all" ]; then - if [ -d "$(asdf_data_dir)"/plugins ]; then - plugins=$(find "$(asdf_data_dir)"/plugins -mindepth 1 -maxdepth 1 -type d) - while IFS= read -r dir; do - update_plugin "$(basename "$dir")" "$dir" "$gitref" & - done <<<"$plugins" - wait - fi - else - local plugin_path - plugin_path="$(get_plugin_path "$plugin_name")" - check_if_plugin_exists "$plugin_name" - update_plugin "$plugin_name" "$plugin_path" "$gitref" - fi -} - -update_plugin() { - local plugin_name=$1 - local plugin_path=$2 - plugin_remote_default_branch=$(git --git-dir "$plugin_path/.git" --work-tree "$plugin_path" ls-remote --symref origin HEAD | awk '{ sub(/refs\/heads\//, ""); print $2; exit }') - local gitref=${3:-${plugin_remote_default_branch}} - logfile=$(mktemp) - - local common_git_options=(--git-dir "$plugin_path/.git" --work-tree "$plugin_path") - local prev_ref= - local post_ref= - { - printf "Location of %s plugin: %s\n" "$plugin_name" "$plugin_path" - asdf_run_hook "pre_asdf_plugin_update" "$plugin_name" - asdf_run_hook "pre_asdf_plugin_update_${plugin_name}" - - printf "Updating %s to %s\n" "$plugin_name" "$gitref" - - git "${common_git_options[@]}" fetch --prune --update-head-ok origin "$gitref:$gitref" - prev_ref=$(git "${common_git_options[@]}" rev-parse --short HEAD) - post_ref=$(git "${common_git_options[@]}" rev-parse --short "${gitref}") - git "${common_git_options[@]}" -c advice.detachedHead=false checkout --force "$gitref" - - if [ -f "${plugin_path}/bin/post-plugin-update" ]; then - ( - # shellcheck disable=SC2031 - export ASDF_PLUGIN_PATH=$plugin_path - export ASDF_PLUGIN_PREV_REF=$prev_ref - export ASDF_PLUGIN_POST_REF=$post_ref - "${plugin_path}/bin/post-plugin-update" - ) - fi - - asdf_run_hook "post_asdf_plugin_update" "$plugin_name" - asdf_run_hook "post_asdf_plugin_update_${plugin_name}" - } >"$logfile" 2>&1 - cat "$logfile" - rm "$logfile" -} diff --git a/lib/functions/versions.bash b/lib/functions/versions.bash deleted file mode 100644 index 7d1d941d6..000000000 --- a/lib/functions/versions.bash +++ /dev/null @@ -1,240 +0,0 @@ -version_command() { - local cmd=$1 - local plugin_name=$2 - - if [ "$#" -lt "3" ]; then - if [ "$cmd" = "global" ]; then - printf "Usage: asdf global \n" - else - printf "Usage: asdf local \n" - fi - exit 1 - fi - - shift 2 - local versions=("$@") - - local file_name - local file - - file_name="$(asdf_tool_versions_filename)" - - if [ "$cmd" = "global" ]; then - file="$HOME/$file_name" - elif [ "$cmd" = "local-tree" ]; then - file=$(find_tool_versions) - else # cmd = local - file="$PWD/$file_name" - fi - - if [ -L "$file" ]; then - # Resolve file path if symlink - file="$(resolve_symlink "$file")" - fi - - check_if_plugin_exists "$plugin_name" - - declare -a resolved_versions - local item - for item in "${!versions[@]}"; do - IFS=':' read -r -a version_info <<<"${versions[$item]}" - if [ "${version_info[0]}" = "latest" ] && [ -n "${version_info[1]}" ]; then - version=$(latest_command "$plugin_name" "${version_info[1]}") - elif [ "${version_info[0]}" = "latest" ] && [ -z "${version_info[1]}" ]; then - version=$(latest_command "$plugin_name") - else - # if branch handles ref: || path: || normal versions - version="${versions[$item]}" - fi - - # check_if_version_exists should probably handle if either param is empty string - if [ -z "$version" ]; then - exit 1 - fi - - if ! (check_if_version_exists "$plugin_name" "$version"); then - version_not_installed_text "$plugin_name" "$version" 1>&2 - exit 1 - fi - - resolved_versions+=("$version") - done - - if [ -f "$file" ] && grep -q "^$plugin_name " "$file"; then - local temp_dir - temp_dir=${TMPDIR:-/tmp} - - local temp_tool_versions_file - temp_tool_versions_file=$(mktemp "$temp_dir/asdf-tool-versions-file.XXXXXX") - - cp -f "$file" "$temp_tool_versions_file" - sed -e "s|^$plugin_name .*$|$plugin_name ${resolved_versions[*]}|" "$temp_tool_versions_file" >"$file" - rm -f "$temp_tool_versions_file" - else - # Add a trailing newline at the end of the file if missing - [[ -f "$file" && -n "$(tail -c1 "$file")" ]] && printf '\n' >>"$file" - - # Add a new version line to the end of the file - printf "%s %s\n" "$plugin_name" "${resolved_versions[*]}" >>"$file" - fi -} - -list_all_command() { - local plugin_name=$1 - local query=$2 - local plugin_path - local std_out_file - local std_err_file - local output - plugin_path=$(get_plugin_path "$plugin_name") - check_if_plugin_exists "$plugin_name" - - local temp_dir - temp_dir=${TMPDIR:-/tmp} - - # Capture return code to allow error handling - std_out_file="$(mktemp "$temp_dir/asdf-command-list-all-${plugin_name}.stdout.XXXXXX")" - std_err_file="$(mktemp "$temp_dir/asdf-command-list-all-${plugin_name}.stderr.XXXXXX")" - return_code=0 && "${plugin_path}/bin/list-all" >"$std_out_file" 2>"$std_err_file" || return_code=$? - - if [[ $return_code -ne 0 ]]; then - # Printing all output to allow plugin to handle error formatting - printf "Plugin %s's list-all callback script failed with output:\n" "${plugin_name}" >&2 - printf "%s\n" "$(cat "$std_err_file")" >&2 - printf "%s\n" "$(cat "$std_out_file")" >&2 - rm "$std_out_file" "$std_err_file" - exit 1 - fi - - if [[ $query ]]; then - output=$(tr ' ' '\n' <"$std_out_file" | - grep -E "^\\s*$query" | - tr '\n' ' ') - else - output=$(cat "$std_out_file") - fi - - if [ -z "$output" ]; then - display_error "No compatible versions available ($plugin_name $query)" - exit 1 - fi - - IFS=' ' read -r -a versions_list <<<"$output" - - for version in "${versions_list[@]}"; do - printf "%s\n" "${version}" - done - - # Remove temp files if they still exist - rm "$std_out_file" "$std_err_file" || true -} - -latest_command() { - DEFAULT_QUERY="[0-9]" - - local plugin_name=$1 - local query=$2 - local plugin_path - - if [ "$plugin_name" = "--all" ]; then - latest_all - fi - - [[ -z $query ]] && query="$DEFAULT_QUERY" - - plugin_path=$(get_plugin_path "$plugin_name") - check_if_plugin_exists "$plugin_name" - - local versions - - if [ -f "${plugin_path}/bin/latest-stable" ]; then - versions=$("${plugin_path}"/bin/latest-stable "$query") - if [ -z "${versions}" ]; then - # this branch requires this print to mimic the error from the list-all branch - printf "No compatible versions available (%s %s)\n" "$plugin_name" "$query" >&2 - exit 1 - fi - else - # pattern from xxenv-latest (https://github.com/momo-lab/xxenv-latest) - versions=$(list_all_command "$plugin_name" "$query" | - grep -ivE "(^Available versions:|-src|-dev|-latest|-stm|[-\\.]rc|-milestone|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)" | - sed 's/^[[:space:]]\+//' | - tail -1) - if [ -z "${versions}" ]; then - exit 1 - fi - fi - - printf "%s\n" "$versions" -} - -latest_all() { - local plugins_path - plugins_path=$(get_plugin_path) - - if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then - for plugin_path in "$plugins_path"/*/; do - plugin_name=$(basename "$plugin_path") - - # Retrieve the version of the plugin - local version - if [ -f "${plugin_path}/bin/latest-stable" ]; then - # We can't filter by a concrete query because different plugins might - # have different queries. - version=$("${plugin_path}"/bin/latest-stable "") - if [ -z "${version}" ]; then - version="unknown" - fi - else - # pattern from xxenv-latest (https://github.com/momo-lab/xxenv-latest) - version=$(list_all_command "$plugin_name" | - grep -ivE "(^Available version:|-src|-dev|-latest|-stm|[-\\.]rc|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)" | - sed 's/^[[:space:]]\+//' | - tail -1) - if [ -z "${version}" ]; then - version="unknown" - fi - fi - - local installed_status - installed_status="missing" - - local installed_versions - installed_versions=$(list_installed_versions "$plugin_name") - - if [ -n "$installed_versions" ] && printf '%s\n' "$installed_versions" | grep -q "^$version\$"; then - installed_status="installed" - fi - printf "%s\t%s\t%s\n" "$plugin_name" "$version" "$installed_status" - done - else - printf "%s\n" 'No plugins installed' - fi - exit 0 -} - -local_command() { - local parent=false - local positional=() - - while [[ $# -gt 0 ]]; do - case $1 in - -p | --parent) - parent="true" - shift # past value - ;; - *) - positional+=("$1") # save it in an array for later - shift # past argument - ;; - esac - done - - set -- "${positional[@]}" # restore positional parameters - - if [ $parent = true ]; then - version_command local-tree "$@" - else - version_command local "$@" - fi -} diff --git a/lib/utils.bash b/lib/utils.bash deleted file mode 100644 index 21978a929..000000000 --- a/lib/utils.bash +++ /dev/null @@ -1,898 +0,0 @@ -# We shouldn't rely on the user's grep settings to be correct. If we set these -# here anytime asdf invokes grep it will be invoked with these options -# shellcheck disable=SC2034 -GREP_OPTIONS="--color=never" -# shellcheck disable=SC2034 -GREP_COLORS= - -asdf_version() { - local version git_rev - version="v$(cat "$(asdf_dir)/version.txt")" - if [ -d "$(asdf_dir)/.git" ]; then - git_rev="$(git --git-dir "$(asdf_dir)/.git" rev-parse --short HEAD)" - printf "%s-%s\n" "$version" "$git_rev" - else - printf "%s\n" "$version" - fi -} - -asdf_tool_versions_filename() { - printf '%s\n' "${ASDF_DEFAULT_TOOL_VERSIONS_FILENAME:-.tool-versions}" -} - -asdf_config_file() { - printf '%s\n' "${ASDF_CONFIG_FILE:-$HOME/.asdfrc}" -} - -asdf_data_dir() { - local data_dir - - if [ -n "${ASDF_DATA_DIR}" ]; then - data_dir="${ASDF_DATA_DIR}" - elif [ -n "$HOME" ]; then - data_dir="$HOME/.asdf" - else - data_dir=$(asdf_dir) - fi - - printf "%s\n" "$data_dir" -} - -asdf_dir() { - if [ -z "$ASDF_DIR" ]; then - local current_script_path=${BASH_SOURCE[0]} - printf '%s\n' "$( - cd -- "$(dirname "$(dirname "$current_script_path")")" || exit - printf '%s\n' "$PWD" - )" - else - printf '%s\n' "$ASDF_DIR" - fi -} - -asdf_plugin_repository_url() { - printf "https://github.com/asdf-vm/asdf-plugins.git\n" -} - -get_install_path() { - local plugin=$1 - local install_type=$2 - local version=$3 - - local install_dir - install_dir="$(asdf_data_dir)/installs" - - [ -d "${install_dir}/${plugin}" ] || mkdir -p "${install_dir}/${plugin}" - - if [ "$install_type" = "version" ]; then - printf "%s/%s/%s\n" "$install_dir" "$plugin" "$version" - elif [ "$install_type" = "path" ]; then - printf "%s\n" "$version" - else - printf "%s/%s/%s-%s\n" "$install_dir" "$plugin" "$install_type" "$version" - fi -} - -get_download_path() { - local plugin=$1 - local install_type=$2 - local version=$3 - - local download_dir - download_dir="$(asdf_data_dir)/downloads" - - [ -d "${download_dir}/${plugin}" ] || mkdir -p "${download_dir}/${plugin}" - - if [ "$install_type" = "version" ]; then - printf "%s/%s/%s\n" "$download_dir" "$plugin" "$version" - elif [ "$install_type" = "path" ]; then - return - else - printf "%s/%s/%s-%s\n" "$download_dir" "$plugin" "$install_type" "$version" - fi -} - -list_installed_versions() { - local plugin_name=$1 - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - - local plugin_installs_path - plugin_installs_path="$(asdf_data_dir)/installs/${plugin_name}" - - if [ -d "$plugin_installs_path" ]; then - for install in "${plugin_installs_path}"/*/; do - [[ -e "$install" ]] || break - basename "$install" | sed 's/^ref-/ref:/' - done - fi -} - -check_if_plugin_exists() { - local plugin_name=$1 - - # Check if we have a non-empty argument - if [ -z "${1}" ]; then - display_error "No plugin given" - exit 1 - fi - - if [ ! -d "$(asdf_data_dir)/plugins/$plugin_name" ]; then - display_error "No such plugin: $plugin_name" - exit 1 - fi -} - -check_if_version_exists() { - local plugin_name=$1 - local version=$2 - - check_if_plugin_exists "$plugin_name" - - local install_path - install_path=$(find_install_path "$plugin_name" "$version") - - if [ "$version" != "system" ] && [ ! -d "$install_path" ]; then - exit 1 - fi -} - -version_not_installed_text() { - local plugin_name=$1 - local version=$2 - - printf "version %s is not installed for %s\n" "$version" "$plugin_name" -} - -get_plugin_path() { - if [ -n "$1" ]; then - printf "%s\n" "$(asdf_data_dir)/plugins/$1" - else - printf "%s\n" "$(asdf_data_dir)/plugins" - fi -} - -display_error() { - printf "%s\n" "$1" >&2 -} - -get_version_in_dir() { - local plugin_name=$1 - local search_path=$2 - local legacy_filenames=$3 - - local asdf_version - - file_name=$(asdf_tool_versions_filename) - asdf_version=$(parse_asdf_version_file "$search_path/$file_name" "$plugin_name") - - if [ -n "$asdf_version" ]; then - printf "%s\n" "$asdf_version|$search_path/$file_name" - return 0 - fi - - for filename in $legacy_filenames; do - local legacy_version - legacy_version=$(parse_legacy_version_file "$search_path/$filename" "$plugin_name") - - if [ -n "$legacy_version" ]; then - printf "%s\n" "$legacy_version|$search_path/$filename" - return 0 - fi - done -} - -find_versions() { - local plugin_name=$1 - local search_path=$2 - - local version - version=$(get_version_from_env "$plugin_name") - if [ -n "$version" ]; then - local upcase_name - upcase_name=$(printf "%s\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_') - local version_env_var="ASDF_${upcase_name}_VERSION" - - printf "%s\n" "$version|$version_env_var environment variable" - return 0 - fi - - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - local legacy_config - legacy_config=$(get_asdf_config_value "legacy_version_file") - local legacy_list_filenames_script - legacy_list_filenames_script="${plugin_path}/bin/list-legacy-filenames" - local legacy_filenames="" - - if [ "$legacy_config" = "yes" ] && [ -f "$legacy_list_filenames_script" ]; then - legacy_filenames=$("$legacy_list_filenames_script") - fi - - while [ "$search_path" != "/" ]; do - version=$(get_version_in_dir "$plugin_name" "$search_path" "$legacy_filenames") - if [ -n "$version" ]; then - printf "%s\n" "$version" - return 0 - fi - search_path=$(dirname "$search_path") - done - - get_version_in_dir "$plugin_name" "$HOME" "$legacy_filenames" - - if [ -f "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" ]; then - versions=$(parse_asdf_version_file "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$plugin_name") - if [ -n "$versions" ]; then - printf "%s\n" "$versions|$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" - return 0 - fi - fi -} - -display_no_version_set() { - local plugin_name=$1 - printf "No version is set for %s; please run \`asdf %s \`\n" "$plugin_name" "$plugin_name" -} - -get_version_from_env() { - local plugin_name=$1 - local upcase_name - upcase_name=$(printf "%s\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_') - local version_env_var="ASDF_${upcase_name}_VERSION" - local version=${!version_env_var:-} - printf "%s\n" "$version" -} - -find_install_path() { - local plugin_name=$1 - local version=$2 - - # shellcheck disable=SC2162 - IFS=':' read -a version_info <<<"$version" - - if [ "$version" = "system" ]; then - printf "\n" - elif [ "${version_info[0]}" = "ref" ]; then - local install_type="${version_info[0]}" - local version="${version_info[1]}" - get_install_path "$plugin_name" "$install_type" "$version" - elif [ "${version_info[0]}" = "path" ]; then - # This is for people who have the local source already compiled - # Like those who work on the language, etc - # We'll allow specifying path:/foo/bar/project in .tool-versions - # And then use the binaries there - local install_type="path" - local version="path" - - util_resolve_user_path "${version_info[1]}" - printf "%s\n" "${util_resolve_user_path_reply}" - else - local install_type="version" - local version="${version_info[0]}" - get_install_path "$plugin_name" "$install_type" "$version" - fi -} - -get_custom_executable_path() { - local plugin_path=$1 - local install_path=$2 - local executable_path=$3 - - # custom plugin hook for executable path - if [ -x "${plugin_path}/bin/exec-path" ]; then - cmd=$(basename "$executable_path") - local relative_path - # shellcheck disable=SC2001 - relative_path=$(printf "%s\n" "$executable_path" | sed -e "s|${install_path}/||") - relative_path="$("${plugin_path}/bin/exec-path" "$install_path" "$cmd" "$relative_path")" - executable_path="$install_path/$relative_path" - fi - - printf "%s\n" "$executable_path" -} - -get_executable_path() { - local plugin_name=$1 - local version=$2 - local executable_path=$3 - - check_if_version_exists "$plugin_name" "$version" - - if [ "$version" = "system" ]; then - path=$(remove_path_from_path "$PATH" "$(asdf_data_dir)/shims") - cmd=$(basename "$executable_path") - cmd_path=$(PATH=$path command -v "$cmd" 2>&1) - # shellcheck disable=SC2181 - if [ $? -ne 0 ]; then - return 1 - fi - printf "%s\n" "$cmd_path" - else - local install_path - install_path=$(find_install_path "$plugin_name" "$version") - printf "%s\n" "${install_path}"/"${executable_path}" - fi -} - -parse_asdf_version_file() { - local file_path=$1 - local plugin_name=$2 - - if [ -f "$file_path" ]; then - local version - version=$(strip_tool_version_comments "$file_path" | grep "^${plugin_name} " | sed -e "s/^${plugin_name} //") - - if [ -n "$version" ]; then - if [[ "$version" == path:* ]]; then - util_resolve_user_path "${version#path:}" - printf "%s\n" "path:${util_resolve_user_path_reply}" - else - printf "%s\n" "$version" - fi - - return 0 - fi - fi -} - -parse_legacy_version_file() { - local file_path=$1 - local plugin_name=$2 - - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - local parse_legacy_script - parse_legacy_script="${plugin_path}/bin/parse-legacy-file" - - if [ -f "$file_path" ]; then - if [ -f "$parse_legacy_script" ]; then - "$parse_legacy_script" "$file_path" - else - cat "$file_path" - fi - fi -} - -get_preset_version_for() { - local plugin_name=$1 - local search_path - search_path=$PWD - local version_and_path - version_and_path=$(find_versions "$plugin_name" "$search_path") - local version - version=$(cut -d '|' -f 1 <<<"$version_and_path") - - printf "%s\n" "$version" -} - -get_asdf_config_value_from_file() { - local config_path=$1 - local key=$2 - - if [ ! -f "$config_path" ]; then - return 1 - fi - - util_validate_no_carriage_returns "$config_path" - - local result - result=$(grep -E "^\s*$key\s*=\s*" "$config_path" | head | sed -e 's/^[^=]*= *//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') - if [ -n "$result" ]; then - printf "%s\n" "$result" - return 0 - fi - - return 2 -} - -get_asdf_config_value() { - local key=$1 - local config_path= - config_path=$(asdf_config_file) - local default_config_path=${ASDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"} - - local local_config_path - local_config_path="$(find_file_upwards ".asdfrc")" - - get_asdf_config_value_from_file "$local_config_path" "$key" || - get_asdf_config_value_from_file "$config_path" "$key" || - get_asdf_config_value_from_file "$default_config_path" "$key" -} - -# Whether the plugin shortname repo needs to be synced -# 0: if no sync needs to occur -# 1: if sync needs to occur -repository_needs_update() { - local plugin_repository_last_check_duration - local sync_required - - plugin_repository_last_check_duration="$(get_asdf_config_value "plugin_repository_last_check_duration")" - - if [ "never" != "$plugin_repository_last_check_duration" ]; then - local update_file_dir - local update_file_name - update_file_dir="$(asdf_data_dir)/tmp" - update_file_name="repo-updated" - # `find` outputs filename if it has not been modified in plugin_repository_last_check_duration setting. - sync_required=$(find "$update_file_dir" -name "$update_file_name" -type f -mmin +"${plugin_repository_last_check_duration:-60}" -print) - fi - - [ "$sync_required" ] -} - -initialize_or_update_plugin_repository() { - local repository_url - local repository_path - - disable_plugin_short_name_repo="$(get_asdf_config_value "disable_plugin_short_name_repository")" - if [ "yes" = "$disable_plugin_short_name_repo" ]; then - printf "Short-name plugin repository is disabled\n" >&2 - exit 1 - fi - - repository_url=$(asdf_plugin_repository_url) - repository_path=$(asdf_data_dir)/repository - - if [ ! -d "$repository_path" ]; then - printf "initializing plugin repository..." - git clone "$repository_url" "$repository_path" - elif repository_needs_update; then - printf "updating plugin repository..." - git -C "$repository_path" fetch - git -C "$repository_path" reset --hard origin/master - fi - - [ -d "$(asdf_data_dir)/tmp" ] || mkdir -p "$(asdf_data_dir)/tmp" - touch "$(asdf_data_dir)/tmp/repo-updated" -} - -get_plugin_source_url() { - local plugin_name=$1 - local plugin_config - - plugin_config="$(asdf_data_dir)/repository/plugins/$plugin_name" - - if [ -f "$plugin_config" ]; then - grep "repository" "$plugin_config" | awk -F'=' '{print $2}' | sed 's/ //' - fi -} - -find_tool_versions() { - find_file_upwards "$(asdf_tool_versions_filename)" -} - -find_file_upwards() { - local name="$1" - local search_path - search_path=$PWD - while [ "$search_path" != "/" ]; do - if [ -f "$search_path/$name" ]; then - util_validate_no_carriage_returns "$search_path/$name" - - printf "%s\n" "${search_path}/$name" - return 0 - fi - search_path=$(dirname "$search_path") - done -} - -resolve_symlink() { - local symlink - symlink="$1" - - # This seems to be the only cross-platform way to resolve symlink paths to - # the real file path. - # shellcheck disable=SC2012 - resolved_path=$(ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|') # asdf_allow: ls ' - - # Check if resolved path is relative or not by looking at the first character. - # If it is a slash we can assume it's root and absolute. Otherwise we treat it - # as relative - case $resolved_path in - /*) - printf "%s\n" "$resolved_path" - ;; - *) - ( - cd "$(dirname "$symlink")" || exit 1 - printf "%s\n" "$PWD/$resolved_path" - ) - ;; - esac -} - -list_plugin_bin_paths() { - local plugin_name=$1 - local version=$2 - local install_type=$3 - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - local install_path - install_path=$(get_install_path "$plugin_name" "$install_type" "$version") - - if [ -f "${plugin_path}/bin/list-bin-paths" ]; then - local space_separated_list_of_bin_paths - - # shellcheck disable=SC2030 - space_separated_list_of_bin_paths=$( - export ASDF_INSTALL_TYPE=$install_type - export ASDF_INSTALL_VERSION=$version - export ASDF_INSTALL_PATH=$install_path - "${plugin_path}/bin/list-bin-paths" - ) - else - local space_separated_list_of_bin_paths="bin" - fi - printf "%s\n" "$space_separated_list_of_bin_paths" -} - -list_plugin_exec_paths() { - local plugin_name=$1 - local full_version=$2 - check_if_plugin_exists "$plugin_name" - - IFS=':' read -r -a version_info <<<"$full_version" - if [ "${version_info[0]}" = "ref" ]; then - local install_type="${version_info[0]}" - local version="${version_info[1]}" - elif [ "${version_info[0]}" = "path" ]; then - local install_type="${version_info[0]}" - local version="${version_info[1]}" - else - local install_type="version" - local version="${version_info[0]}" - fi - - local plugin_shims_path - plugin_shims_path=$(get_plugin_path "$plugin_name")/shims - if [ -d "$plugin_shims_path" ]; then - printf "%s\n" "$plugin_shims_path" - fi - - space_separated_list_of_bin_paths="$(list_plugin_bin_paths "$plugin_name" "$version" "$install_type")" - IFS=' ' read -r -a all_bin_paths <<<"$space_separated_list_of_bin_paths" - - local install_path - install_path=$(get_install_path "$plugin_name" "$install_type" "$version") - - for bin_path in "${all_bin_paths[@]}"; do - printf "%s\n" "$install_path/$bin_path" - done -} - -with_plugin_env() { - local plugin_name=$1 - local full_version=$2 - local callback=$3 - - IFS=':' read -r -a version_info <<<"$full_version" - if [ "${version_info[0]}" = "ref" ]; then - local install_type="${version_info[0]}" - local version="${version_info[1]}" - else - local install_type="version" - local version="${version_info[0]}" - fi - - if [ "$version" = "system" ]; then - # execute as is for system - "$callback" - return $? - fi - - local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") - - # add the plugin listed exec paths to PATH - local path exec_paths - exec_paths="$(list_plugin_exec_paths "$plugin_name" "$full_version")" - - # exec_paths contains a trailing newline which is converted to a colon, so no - # colon is needed between the subshell and the PATH variable in this string - path="$(tr '\n' ':' <<<"$exec_paths")$PATH" - - # If no custom exec-env transform, just execute callback - if [ ! -f "${plugin_path}/bin/exec-env" ]; then - PATH=$path "$callback" - return $? - fi - - # Load the plugin custom environment - local install_path - install_path=$(find_install_path "$plugin_name" "$full_version") - - # shellcheck source=/dev/null - ASDF_INSTALL_TYPE=$install_type \ - ASDF_INSTALL_VERSION=$version \ - ASDF_INSTALL_PATH=$install_path \ - . "${plugin_path}/bin/exec-env" - - PATH=$path "$callback" -} - -plugin_executables() { - local plugin_name=$1 - local full_version=$2 - local all_bin_paths - IFS=$'\n' read -rd '' -a all_bin_paths <<<"$(list_plugin_exec_paths "$plugin_name" "$full_version")" - for bin_path in "${all_bin_paths[@]}"; do - for executable_file in "$bin_path"/*; do - if is_executable "$executable_file"; then - printf "%s\n" "$executable_file" - fi - done - done -} - -is_executable() { - local executable_path=$1 - if [[ (-f "$executable_path") && (-x "$executable_path") ]]; then - return 0 - fi - return 1 -} - -plugin_shims() { - local plugin_name=$1 - local full_version=$2 - grep -lx "# asdf-plugin: $plugin_name $full_version" "$(asdf_data_dir)/shims"/* 2>/dev/null -} - -shim_plugin_versions() { - local executable_name - executable_name=$(basename "$1") - local shim_path - shim_path="$(asdf_data_dir)/shims/${executable_name}" - if [ -x "$shim_path" ]; then - grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | uniq - else - printf "asdf: unknown shim %s\n" "$executable_name" - return 1 - fi -} - -shim_plugins() { - local executable_name - executable_name=$(basename "$1") - local shim_path - shim_path="$(asdf_data_dir)/shims/${executable_name}" - if [ -x "$shim_path" ]; then - grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | cut -d' ' -f 1 | uniq - else - printf "asdf: unknown shim %s\n" "$executable_name" - return 1 - fi -} - -strip_tool_version_comments() { - local tool_version_path="$1" - # Use sed to strip comments from the tool version file - # Breakdown of sed command: - # This command represents 3 steps, separated by a semi-colon (;), that run on each line. - # 1. Delete line if it starts with any blankspace and a #. - # 2. Find a # and delete it and everything after the #. - # 3. Remove any whitespace from the end of the line. - # Finally, the command will print the lines that are not empty. - sed '/^[[:blank:]]*#/d;s/#.*//;s/[[:blank:]]*$//' "$tool_version_path" -} - -asdf_run_hook() { - local hook_name=$1 - local hook_cmd - hook_cmd="$(get_asdf_config_value "$hook_name")" - if [ -n "$hook_cmd" ]; then - asdf_hook_fun() { - unset asdf_hook_fun - ev'al' "$hook_cmd" # ignore banned command just here - } - asdf_hook_fun "${@:2}" - fi -} - -get_shim_versions() { - shim_name=$1 - shim_plugin_versions "${shim_name}" - shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | awk '{print$1" system"}' -} - -preset_versions() { - shim_name=$1 - shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c ". $(asdf_dir)/lib/utils.bash; printf \"%s %s\n\" PLUGIN \$(get_preset_version_for PLUGIN)" -} - -select_from_preset_version() { - local shim_name=$1 - local shim_versions - local preset_versions - - shim_versions=$(get_shim_versions "$shim_name") - if [ -n "$shim_versions" ]; then - preset_versions=$(preset_versions "$shim_name") - grep -F "$shim_versions" <<<"$preset_versions" | head -n 1 | xargs -IVERSION printf "%s\n" VERSION - fi -} - -select_version() { - shim_name=$1 - # First, we get the all the plugins where the - # current shim is available. - # Then, we iterate on all versions set for each plugin - # Note that multiple plugin versions can be set for a single plugin. - # These are separated by a space. e.g. python 3.7.2 2.7.15 - # For each plugin/version pair, we check if it is present in the shim - local search_path - search_path=$PWD - local shim_versions - IFS=$'\n' read -rd '' -a shim_versions <<<"$(get_shim_versions "$shim_name")" - - local plugins - IFS=$'\n' read -rd '' -a plugins <<<"$(shim_plugins "$shim_name")" - - for plugin_name in "${plugins[@]}"; do - local version_and_path - local version_string - local usable_plugin_versions - local _path - version_and_path=$(find_versions "$plugin_name" "$search_path") - IFS='|' read -r version_string _path <<<"$version_and_path" - IFS=' ' read -r -a usable_plugin_versions <<<"$version_string" - for plugin_version in "${usable_plugin_versions[@]}"; do - for plugin_and_version in "${shim_versions[@]}"; do - local plugin_shim_name - local plugin_shim_version - IFS=' ' read -r plugin_shim_name plugin_shim_version <<<"$plugin_and_version" - if [[ "$plugin_name" == "$plugin_shim_name" ]]; then - if [[ "$plugin_version" == "$plugin_shim_version" ]]; then - printf "%s\n" "$plugin_name $plugin_version" - return - elif [[ "$plugin_version" == "path:"* ]]; then - printf "%s\n" "$plugin_name $plugin_version" - return - fi - fi - done - done - done -} - -with_shim_executable() { - local shim_name - shim_name=$(basename "$1") - local shim_exec="${2}" - - if [ ! -f "$(asdf_data_dir)/shims/${shim_name}" ]; then - printf "%s %s %s\n" "unknown command:" "${shim_name}." "Perhaps you have to reshim?" >&2 - return 1 - fi - - local selected_version - selected_version="$(select_version "$shim_name")" - - if [ -z "$selected_version" ]; then - selected_version="$(select_from_preset_version "$shim_name")" - fi - - if [ -n "$selected_version" ]; then - local plugin_name - local full_version - local plugin_path - - IFS=' ' read -r plugin_name full_version <<<"$selected_version" - plugin_path=$(get_plugin_path "$plugin_name") - - # This function does get invoked, but shellcheck sees it as unused code - # shellcheck disable=SC2317 - run_within_env() { - local path - path=$(remove_path_from_path "$PATH" "$(asdf_data_dir)/shims") - - executable_path=$(PATH=$path command -v "$shim_name") - - if [ -x "${plugin_path}/bin/exec-path" ]; then - install_path=$(find_install_path "$plugin_name" "$full_version") - executable_path=$(get_custom_executable_path "${plugin_path}" "${install_path}" "${executable_path:-${shim_name}}") - fi - - "$shim_exec" "$plugin_name" "$full_version" "$executable_path" - } - - with_plugin_env "$plugin_name" "$full_version" run_within_env - return $? - fi - - ( - local preset_plugin_versions - preset_plugin_versions=() - local closest_tool_version - closest_tool_version=$(find_tool_versions) - - local shim_plugins - IFS=$'\n' read -rd '' -a shim_plugins <<<"$(shim_plugins "$shim_name")" - for shim_plugin in "${shim_plugins[@]}"; do - local shim_versions - local version_string - version_string=$(get_preset_version_for "$shim_plugin") - IFS=' ' read -r -a shim_versions <<<"$version_string" - local usable_plugin_versions - for shim_version in "${shim_versions[@]}"; do - preset_plugin_versions+=("$shim_plugin $shim_version") - done - done - - if [ -n "${preset_plugin_versions[*]}" ]; then - printf "%s %s\n" "No preset version installed for command" "$shim_name" - printf "%s\n\n" "Please install a version by running one of the following:" - for preset_plugin_version in "${preset_plugin_versions[@]}"; do - printf "%s %s\n" "asdf install" "$preset_plugin_version" - done - printf "\n%s %s\n" "or add one of the following versions in your config file at" "$closest_tool_version" - else - printf "%s %s\n" "No version is set for command" "$shim_name" - printf "%s %s\n" "Consider adding one of the following versions in your config file at" "$closest_tool_version" - fi - shim_plugin_versions "${shim_name}" - ) >&2 - - return 126 -} - -substitute() { - # Use Bash substitution rather than sed as it will handle escaping of all - # strings for us. - local input=$1 - local find_str=$2 - local replace=$3 - printf "%s" "${input//"$find_str"/"$replace"}" -} - -remove_path_from_path() { - # A helper function for removing an arbitrary path from the PATH variable. - # Output is a new string suitable for assignment to PATH - local PATH=$1 - local path=$2 - substitute "$PATH" "$path" "" | sed -e "s|::|:|g" -} - -# @description Strings that began with a ~ are always paths. In -# that case, then ensure ~ it handled like a shell -util_resolve_user_path() { - util_resolve_user_path_reply= - local path="$1" - - # shellcheck disable=SC2088 - if [ "${path::2}" = '~/' ]; then - util_resolve_user_path_reply="${HOME}/${path:2}" - else - util_resolve_user_path_reply="$path" - fi -} - -# @description Check if a file contains carriage returns. If it does, print a warning. -util_validate_no_carriage_returns() { - local file_path="$1" - - if grep -qr $'\r' "$file_path"; then - printf '%s\n' "asdf: Warning: File $file_path contains carriage returns. Please remove them." >&2 - fi -} - -get_plugin_remote_url() { - local plugin_name="$1" - local plugin_path - plugin_path="$(get_plugin_path "$plugin_name")" - git --git-dir "$plugin_path/.git" remote get-url origin 2>/dev/null -} - -get_plugin_remote_branch() { - local plugin_name="$1" - local plugin_path - plugin_path="$(get_plugin_path "$plugin_name")" - git --git-dir "$plugin_path/.git" rev-parse --abbrev-ref HEAD 2>/dev/null -} - -get_plugin_remote_gitref() { - local plugin_name="$1" - local plugin_path - plugin_path="$(get_plugin_path "$plugin_name")" - git --git-dir "$plugin_path/.git" rev-parse --short HEAD 2>/dev/null -} diff --git a/test/test_helpers.bash b/test/test_helpers.bash index dc5c7de92..bc590bf36 100644 --- a/test/test_helpers.bash +++ b/test/test_helpers.bash @@ -3,7 +3,7 @@ bats_require_minimum_version 1.7.0 # shellcheck source=lib/utils.bash -. "$(dirname "$BATS_TEST_DIRNAME")"/lib/utils.bash +. "$(dirname "$BATS_TEST_DIRNAME")"/test/utils.bash setup_asdf_dir() { if [ "$BATS_TEST_NAME" = 'test_shim_exec_should_use_path_executable_when_specified_version_path-3a-3cpath-3e' ]; then diff --git a/test/utils.bash b/test/utils.bash new file mode 100644 index 000000000..c92fef4e7 --- /dev/null +++ b/test/utils.bash @@ -0,0 +1,33 @@ +asdf_data_dir() { + local data_dir + + if [ -n "${ASDF_DATA_DIR}" ]; then + data_dir="${ASDF_DATA_DIR}" + elif [ -n "$HOME" ]; then + data_dir="$HOME/.asdf" + else + data_dir=$(asdf_dir) + fi + + printf "%s\n" "$data_dir" +} + +asdf_dir() { + if [ -z "$ASDF_DIR" ]; then + local current_script_path=${BASH_SOURCE[0]} + printf '%s\n' "$( + cd -- "$(dirname "$(dirname "$current_script_path")")" || exit + printf '%s\n' "$PWD" + )" + else + printf '%s\n' "$ASDF_DIR" + fi +} + +get_plugin_path() { + if [ -n "$1" ]; then + printf "%s\n" "$(asdf_data_dir)/plugins/$1" + else + printf "%s\n" "$(asdf_data_dir)/plugins" + fi +}