diff --git a/bin/asdf b/bin/asdf index 0693133da..7a895a21d 100755 --- a/bin/asdf +++ b/bin/asdf @@ -65,6 +65,32 @@ asdf_cmd() { exit 1 fi + # XDG Compliance + ASDF_CONFIG_FILE=$(asdf_config_file) + export ASDF_CONFIG_FILE + + ASDF_DIR=$(asdf_dir) + export ASDF_DIR + + ASDF_DATA_DIR=$(asdf_data_dir) + export ASDF_DATA_DIR + + if [ "$1" = '--internal-get-asdf-config-file' ]; then + printf '%s\n' "$ASDF_CONFIG_FILE" + exit 0 + elif [ "$1" = '--internal-get-asdf-dir' ]; then + printf '%s\n' "$ASDF_DIR" + exit 0 + elif [ "$1" = '--internal-get-asdf-data-dir' ]; then + printf '%s\n' "$ASDF_DATA_DIR" + exit 0 + fi + + mkdir -p "${ASDF_CONFIG_FILE%/*}" + mkdir -p "${ASDF_DIR}" + mkdir -p "${ASDF_DATA_DIR}" + + IFS=' ' read -r ASDF_CMD_FILE args_offset <<<"$(find_asdf_cmd "$@")" if [ -z "$ASDF_CMD_FILE" ]; then IFS=' ' read -r ASDF_CMD_FILE args_offset <<<"$(find_plugin_cmd "$@")" diff --git a/completions/_asdf b/completions/_asdf index f65733547..f91650375 100755 --- a/completions/_asdf +++ b/completions/_asdf @@ -2,7 +2,8 @@ #description tool to manage versions of multiple runtimes local curcontext="$curcontext" state state_descr line subcmd -local asdf_dir="${ASDF_DATA_DIR:-$HOME/.asdf}" +local asdf_dir= +asdf_dir=$(asdf --internal-get-asdf-dir) local -a asdf_commands asdf_commands=( # 'asdf help' lists commands with help text diff --git a/lib/utils.bash b/lib/utils.bash index 5543754f5..7ea8fdcab 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -32,6 +32,18 @@ asdf_dir() { printf "%s\\n" "$ASDF_DIR" } +asdf_config_file() { + if [ -n "$ASDF_CONFIG_FILE" ]; then + printf '%s' "$ASDF_CONFIG_FILE" + elif [ -f "$HOME/.asdfrc" ]; then + printf '%s' "$HOME/.asdfrc" + elif [ "${XDG_CONFIG_HOME::1}" = '/' ]; then + printf '%s\n' "$XDG_CONFIG_HOME/asdf/asdfrc" + else + printf '%s\n' "$HOME/.config/asdf/asdfrc" + fi +} + asdf_repository_url() { printf "https://github.com/asdf-vm/asdf-plugins.git\\n" } @@ -41,10 +53,12 @@ asdf_data_dir() { if [ -n "${ASDF_DATA_DIR}" ]; then data_dir="${ASDF_DATA_DIR}" - elif [ -n "$HOME" ]; then + elif [ -d "$HOME/.asdf" ]; then data_dir="$HOME/.asdf" + elif [ "${XDG_STATE_HOME::1}" = '/' ]; then + printf '%s\n' "$XDG_STATE_HOME/asdf" else - data_dir=$(asdf_dir) + printf '%s\n' "$HOME/.local/state/asdf" fi printf "%s\\n" "$data_dir" @@ -376,7 +390,8 @@ get_asdf_config_value_from_file() { get_asdf_config_value() { local key=$1 - local config_path=${ASDF_CONFIG_FILE:-"$HOME/.asdfrc"} + local config_path + config_path=$(asdf_config_file) local default_config_path=${ASDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"} local local_config_path