|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# shellcheck source=lib/tools-environment-lib.bash |
| 4 | +source "$(dirname "${BASH_SOURCE[0]}")/tools-environment-lib.bash" |
| 5 | + |
| 6 | +function print_usage() { |
| 7 | + echo "Usage: asdf direnv install" |
| 8 | + echo "" |
| 9 | + echo "Installs tools needed for the current directory. This is very similar" |
| 10 | + echo "to 'asdf install' except it installs the tools in the exact order specified" |
| 11 | + echo "in the '.tool-versions' file, and loads the environment of each tool in order." |
| 12 | + echo "" |
| 13 | + echo "This is useful when installing tools that depend on other tools, for" |
| 14 | + echo "example: the poetry plugin expects python to be available." |
| 15 | + echo "" |
| 16 | + echo "Note: this problem isn't entirely unique to asdf-direnv. asdf itself" |
| 17 | + echo "isn't a full-fledged package manager. It simply doesn't understand" |
| 18 | + echo "dependencies between plugins and therefore cannot do a dependency sort of" |
| 19 | + echo "tools. You'll need to manually sort the lines of your '.tool-versions' file" |
| 20 | + echo "for this to work as intended. See these discussions in core asdf for" |
| 21 | + echo "more information:" |
| 22 | + echo "" |
| 23 | + echo " - https://github.com/asdf-vm/asdf/issues/929" |
| 24 | + echo " - https://github.com/asdf-vm/asdf/issues/1127" |
| 25 | + echo " - https://github.com/asdf-vm/asdf/issues/196" |
| 26 | +} |
| 27 | + |
| 28 | +function install_command() { |
| 29 | + while [[ $# -gt 0 ]]; do |
| 30 | + arg=$1 |
| 31 | + shift |
| 32 | + case $arg in |
| 33 | + -h | --help) |
| 34 | + print_usage |
| 35 | + exit 1 |
| 36 | + ;; |
| 37 | + *) |
| 38 | + echo "Unknown option: $arg" |
| 39 | + exit 1 |
| 40 | + ;; |
| 41 | + esac |
| 42 | + done |
| 43 | + |
| 44 | + install_tools |
| 45 | +} |
| 46 | + |
| 47 | +_load_asdf_functions_installs() { |
| 48 | + # `install_tool_version` depends on `reshim_command` from reshim.bash. |
| 49 | + # See https://github.com/asdf-vm/asdf/blob/v0.12.0/lib/functions/installs.bash#L243 |
| 50 | + _load_asdf_lib reshim_command commands/reshim.bash |
| 51 | + |
| 52 | + _load_asdf_lib install_tool_version functions/installs.bash |
| 53 | +} |
| 54 | + |
| 55 | +function maybe_install_tool_version() { |
| 56 | + _load_asdf_functions_installs |
| 57 | + |
| 58 | + local install_path |
| 59 | + install_path=$(get_install_path "$plugin_name" "version" "$version") |
| 60 | + |
| 61 | + if [ -d "$install_path" ]; then |
| 62 | + printf "%s %s is already installed\n" "$plugin_name" "$version" |
| 63 | + else |
| 64 | + |
| 65 | + # NOTE: we temporarily loosen the rules while invoking |
| 66 | + # install_tool_version because it's from core asdf and it doesn't run |
| 67 | + # well under "strict mode" (asdf_run_hook invokes get_asdf_config_value |
| 68 | + # in a way such that if the config piece is missing, the program exits |
| 69 | + # immediately if `set -e` is enabled. |
| 70 | + ( |
| 71 | + set +ue |
| 72 | + install_tool_version "$plugin_name" "$version" |
| 73 | + set -ue |
| 74 | + ) |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | +function install_tools { |
| 79 | + local tools_file |
| 80 | + tools_file="$(_local_versions_file)" |
| 81 | + |
| 82 | + while IFS=$'\n' read -r plugin_name; do |
| 83 | + while IFS=$'\n' read -r version_and_path; do |
| 84 | + local version _path |
| 85 | + IFS='|' read -r version _path <<<"$version_and_path" |
| 86 | + |
| 87 | + # Install this tool version if not already installed. |
| 88 | + maybe_install_tool_version "$plugin_name" "$version" |
| 89 | + |
| 90 | + # Load the tools environment so subsequent installs can use this tool. |
| 91 | + direnv_code=$(_plugin_env_bash "$plugin_name" "$version" ">>> UH OH <<<") |
| 92 | + eval "$direnv_code" |
| 93 | + done <<<"$(_plugin_versions_and_path "$plugin_name")" |
| 94 | + done <<<"$(_plugins_in_file "$tools_file")" |
| 95 | + |
| 96 | + direnv reload |
| 97 | +} |
0 commit comments