2017-03-07 11:18:20 -07:00
|
|
|
# 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
|
|
|
|
GREP_OPTIONS="--color=never"
|
|
|
|
GREP_COLORS=
|
|
|
|
|
2014-11-29 09:28:11 -07:00
|
|
|
asdf_version() {
|
2017-04-15 19:47:20 -07:00
|
|
|
# Move to the asdf repo, then report the current tag
|
|
|
|
(
|
|
|
|
cd $(asdf_dir)
|
|
|
|
git describe --tags
|
|
|
|
)
|
2014-11-29 09:28:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
asdf_dir() {
|
2015-05-17 02:09:18 -07:00
|
|
|
if [ -z $ASDF_DIR ]; then
|
|
|
|
local current_script_path=${BASH_SOURCE[0]}
|
|
|
|
export ASDF_DIR=$(cd $(dirname $(dirname $current_script_path)); echo $(pwd))
|
2014-11-29 23:02:57 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo $ASDF_DIR
|
2014-11-29 09:28:11 -07:00
|
|
|
}
|
|
|
|
|
2014-12-13 09:26:56 -07:00
|
|
|
get_install_path() {
|
2015-05-29 09:24:31 -07:00
|
|
|
local plugin=$1
|
2014-12-13 09:26:56 -07:00
|
|
|
local install_type=$2
|
|
|
|
local version=$3
|
2015-05-29 09:24:31 -07:00
|
|
|
mkdir -p $(asdf_dir)/installs/${plugin}
|
2014-12-13 09:26:56 -07:00
|
|
|
|
2014-12-14 01:13:43 -07:00
|
|
|
if [ $install_type = "version" ]
|
|
|
|
then
|
2015-05-29 09:24:31 -07:00
|
|
|
echo $(asdf_dir)/installs/${plugin}/${version}
|
2014-12-14 01:13:43 -07:00
|
|
|
else
|
2015-05-29 09:24:31 -07:00
|
|
|
echo $(asdf_dir)/installs/${plugin}/${install_type}-${version}
|
2014-12-14 01:13:43 -07:00
|
|
|
fi
|
2014-12-13 09:26:56 -07:00
|
|
|
}
|
|
|
|
|
2016-12-10 12:54:22 -07:00
|
|
|
list_installed_versions() {
|
|
|
|
local plugin_name=$1
|
|
|
|
local plugin_path=$(get_plugin_path $plugin_name)
|
|
|
|
|
|
|
|
local plugin_installs_path=$(asdf_dir)/installs/${plugin_name}
|
|
|
|
|
|
|
|
if [ -d $plugin_installs_path ]; then
|
|
|
|
for install in $(ls -d ${plugin_installs_path}/*/ 2>/dev/null); do
|
|
|
|
echo "$(basename $install)"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-05-17 11:20:51 -07:00
|
|
|
check_if_plugin_exists() {
|
2016-07-05 16:19:15 -07:00
|
|
|
# Check if we have a non-empty argument
|
|
|
|
if [ -z "${1+set}" ]; then
|
2016-07-05 23:32:57 -07:00
|
|
|
display_error "No plugin given"
|
2016-07-05 16:19:15 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -d $(asdf_dir)/plugins/$1 ]; then
|
2015-05-17 11:20:51 -07:00
|
|
|
display_error "No such plugin"
|
2014-11-29 09:28:11 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-04-24 08:11:33 -07:00
|
|
|
check_if_version_exists() {
|
2016-07-05 23:32:57 -07:00
|
|
|
local plugin_name=$1
|
2016-04-24 08:11:33 -07:00
|
|
|
local version=$2
|
2016-12-18 23:25:29 -07:00
|
|
|
|
2016-07-05 23:32:57 -07:00
|
|
|
check_if_plugin_exists $plugin_name
|
|
|
|
|
2017-07-29 10:47:21 -07:00
|
|
|
local install_path=$(find_install_path $plugin_name $version)
|
|
|
|
|
|
|
|
if [ "$version" != "system" ] && [ ! -d $install_path ]; then
|
2016-07-05 23:32:57 -07:00
|
|
|
display_error "version $version is not installed for $plugin_name"
|
2016-04-24 08:11:33 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-05-17 11:20:51 -07:00
|
|
|
get_plugin_path() {
|
|
|
|
echo $(asdf_dir)/plugins/$1
|
2014-11-29 09:28:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
display_error() {
|
2016-07-05 16:19:15 -07:00
|
|
|
echo >&2 $1
|
2014-11-29 09:28:11 -07:00
|
|
|
}
|
2015-05-14 03:13:04 -07:00
|
|
|
|
2016-10-29 04:08:54 -07:00
|
|
|
get_version_in_dir() {
|
|
|
|
local plugin_name=$1
|
|
|
|
local search_path=$2
|
|
|
|
local legacy_filenames=$3
|
|
|
|
|
|
|
|
local asdf_version=$(parse_asdf_version_file "$search_path/.tool-versions" $plugin_name)
|
|
|
|
|
|
|
|
if [ -n "$asdf_version" ]; then
|
2016-11-11 22:30:13 -07:00
|
|
|
echo "$asdf_version|$search_path/.tool-versions"
|
2016-10-29 04:08:54 -07:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
for filename in $legacy_filenames; do
|
|
|
|
local legacy_version=$(parse_legacy_version_file "$search_path/$filename" $plugin_name)
|
|
|
|
|
|
|
|
if [ -n "$legacy_version" ]; then
|
2016-11-11 22:30:13 -07:00
|
|
|
echo "$legacy_version|$search_path/$filename"
|
2016-10-29 04:08:54 -07:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-08-27 21:43:54 -07:00
|
|
|
find_version() {
|
2016-07-24 17:12:40 -07:00
|
|
|
local plugin_name=$1
|
2016-08-27 21:15:56 -07:00
|
|
|
local search_path=$2
|
2016-07-24 17:12:40 -07:00
|
|
|
|
2016-11-10 12:01:19 -07:00
|
|
|
local version=$(get_version_from_env "$plugin_name")
|
|
|
|
if [ -n "$version" ]; then
|
|
|
|
echo "$version"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2016-07-24 17:12:40 -07:00
|
|
|
local plugin_path=$(get_plugin_path "$plugin_name")
|
|
|
|
local legacy_config=$(get_asdf_config_value "legacy_version_file")
|
|
|
|
local legacy_list_filenames_script="${plugin_path}/bin/list-legacy-filenames"
|
2016-08-27 21:15:56 -07:00
|
|
|
local legacy_filenames=""
|
2016-07-24 08:47:17 -07:00
|
|
|
|
2016-07-24 17:12:40 -07:00
|
|
|
if [ "$legacy_config" = "yes" ] && [ -f $legacy_list_filenames_script ]; then
|
2016-08-27 21:15:56 -07:00
|
|
|
legacy_filenames=$(bash "$legacy_list_filenames_script")
|
2016-07-24 17:12:40 -07:00
|
|
|
fi
|
2015-05-14 21:36:21 -07:00
|
|
|
|
|
|
|
while [ "$search_path" != "/" ]; do
|
2016-11-10 12:01:19 -07:00
|
|
|
version=$(get_version_in_dir "$plugin_name" "$search_path" "$legacy_filenames")
|
2016-10-29 04:08:54 -07:00
|
|
|
if [ -n "$version" ]; then
|
|
|
|
echo "$version"
|
2016-08-27 21:15:56 -07:00
|
|
|
return 0
|
|
|
|
fi
|
2015-12-22 17:00:18 -07:00
|
|
|
search_path=$(dirname "$search_path")
|
2015-05-14 21:36:21 -07:00
|
|
|
done
|
2016-10-29 04:08:54 -07:00
|
|
|
|
|
|
|
get_version_in_dir "$plugin_name" "$HOME" "$legacy_filenames"
|
2015-05-14 03:13:04 -07:00
|
|
|
}
|
|
|
|
|
2016-11-10 12:01:19 -07:00
|
|
|
get_version_from_env () {
|
|
|
|
local plugin_name=$1
|
|
|
|
local upcase_name=$(echo $plugin_name | tr '[a-z]' '[A-Z]')
|
|
|
|
local version_env_var="ASDF_${upcase_name}_VERSION"
|
|
|
|
local version=${!version_env_var}
|
|
|
|
echo "$version"
|
|
|
|
}
|
|
|
|
|
2016-07-01 02:25:34 -07:00
|
|
|
find_install_path() {
|
|
|
|
local plugin_name=$1
|
|
|
|
local version=$2
|
|
|
|
|
|
|
|
IFS=':' read -a version_info <<< "$version"
|
|
|
|
|
|
|
|
if [ $version = "system" ]; then
|
|
|
|
echo ""
|
|
|
|
elif [ "${version_info[0]}" = "ref" ]; then
|
|
|
|
local install_type="${version_info[0]}"
|
|
|
|
local version="${version_info[1]}"
|
|
|
|
echo $(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"
|
|
|
|
echo "${version_info[1]}"
|
|
|
|
else
|
|
|
|
local install_type="version"
|
|
|
|
local version="${version_info[0]}"
|
|
|
|
echo $(get_install_path $plugin_name $install_type $version)
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
get_executable_path() {
|
|
|
|
local plugin_name=$1
|
|
|
|
local version=$2
|
|
|
|
local executable_path=$3
|
|
|
|
|
2016-07-05 23:32:57 -07:00
|
|
|
check_if_version_exists $plugin_name $version
|
|
|
|
|
2016-07-01 02:25:34 -07:00
|
|
|
if [ $version = "system" ]; then
|
2017-03-27 08:04:52 -07:00
|
|
|
path=$(echo $PATH | sed -e "s|$ASDF_DIR/shims||g; s|::|:|g")
|
2016-07-01 02:25:34 -07:00
|
|
|
cmd=$(basename $executable_path)
|
|
|
|
cmd_path=$(PATH=$path which $cmd 2>&1)
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
echo $cmd_path
|
|
|
|
else
|
|
|
|
local install_path=$(find_install_path $plugin_name $version)
|
|
|
|
echo ${install_path}/${executable_path}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-07-24 17:12:40 -07:00
|
|
|
parse_asdf_version_file() {
|
|
|
|
local file_path=$1
|
|
|
|
local plugin_name=$2
|
2015-05-14 03:13:04 -07:00
|
|
|
|
2016-10-27 00:24:34 -07:00
|
|
|
if [ -f "$file_path" ]; then
|
2017-09-02 20:05:40 -07:00
|
|
|
local version=$(grep "${plugin_name} " "$file_path" | sed -e "s/^${plugin_name} //")
|
2016-11-11 22:30:13 -07:00
|
|
|
if [ -n "$version" ]; then
|
|
|
|
echo $version
|
|
|
|
return 0
|
|
|
|
fi
|
2016-08-27 21:15:56 -07:00
|
|
|
fi
|
2015-06-24 06:46:49 -07:00
|
|
|
}
|
|
|
|
|
2016-07-24 17:12:40 -07:00
|
|
|
parse_legacy_version_file() {
|
|
|
|
local file_path=$1
|
|
|
|
local plugin_name=$2
|
2015-06-24 06:46:49 -07:00
|
|
|
|
2016-07-24 17:12:40 -07:00
|
|
|
local plugin_path=$(get_plugin_path "$plugin_name")
|
|
|
|
local parse_legacy_script="${plugin_path}/bin/parse-legacy-file"
|
2015-05-14 03:13:04 -07:00
|
|
|
|
2017-09-02 20:05:40 -07:00
|
|
|
if [ -f "$file_path" ]; then
|
2016-08-27 21:15:56 -07:00
|
|
|
if [ -f $parse_legacy_script ]; then
|
|
|
|
echo $(bash "$parse_legacy_script" "$file_path")
|
|
|
|
else
|
|
|
|
echo $(cat $file_path)
|
|
|
|
fi
|
2015-11-28 15:27:54 -07:00
|
|
|
fi
|
|
|
|
}
|
2016-04-24 16:18:12 -07:00
|
|
|
|
2016-07-24 17:12:40 -07:00
|
|
|
get_preset_version_for() {
|
|
|
|
local plugin_name=$1
|
2016-08-27 21:43:54 -07:00
|
|
|
local search_path=$(pwd)
|
|
|
|
local version_and_path=$(find_version "$plugin_name" "$search_path")
|
2016-11-11 22:30:13 -07:00
|
|
|
local version=$(cut -d '|' -f 1 <<< "$version_and_path");
|
2016-08-27 21:15:56 -07:00
|
|
|
|
2016-08-27 21:43:54 -07:00
|
|
|
echo "$version"
|
2016-07-24 17:12:40 -07:00
|
|
|
}
|
2016-04-24 16:18:12 -07:00
|
|
|
|
2016-04-24 06:20:05 -07:00
|
|
|
get_asdf_config_value_from_file() {
|
2016-07-05 23:32:57 -07:00
|
|
|
local config_path=$1
|
|
|
|
local key=$2
|
2016-04-24 06:20:05 -07:00
|
|
|
|
2016-07-05 23:32:57 -07:00
|
|
|
if [ ! -f $config_path ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
2016-04-24 06:20:05 -07:00
|
|
|
|
2016-07-05 23:32:57 -07:00
|
|
|
local result=$(grep -E "^\s*$key\s*=" $config_path | awk -F '=' '{ gsub(/ /, "", $2); print $2 }')
|
|
|
|
if [ -n "$result" ]; then
|
|
|
|
echo $result
|
|
|
|
fi
|
2016-04-24 06:20:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
get_asdf_config_value() {
|
2016-07-05 23:32:57 -07:00
|
|
|
local key=$1
|
|
|
|
local config_path=${AZDF_CONFIG_FILE:-"$HOME/.asdfrc"}
|
|
|
|
local default_config_path=${AZDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"}
|
2016-04-24 06:20:05 -07:00
|
|
|
|
2016-07-05 23:32:57 -07:00
|
|
|
local result=$(get_asdf_config_value_from_file $config_path $key)
|
2016-04-24 06:20:05 -07:00
|
|
|
|
2016-07-05 23:32:57 -07:00
|
|
|
if [ -n "$result" ]; then
|
|
|
|
echo $result
|
|
|
|
else
|
|
|
|
get_asdf_config_value_from_file $default_config_path $key
|
|
|
|
fi
|
2016-04-24 06:20:05 -07:00
|
|
|
}
|
2017-03-26 16:44:22 -07:00
|
|
|
|
|
|
|
asdf_repository_url() {
|
2017-07-26 09:20:05 -07:00
|
|
|
echo "https://github.com/asdf-vm/asdf-plugins.git"
|
2017-03-26 16:44:22 -07:00
|
|
|
}
|
|
|
|
|
2017-08-24 19:29:23 -07:00
|
|
|
repository_needs_update() {
|
|
|
|
local update_file_dir="$(asdf_dir)/tmp"
|
|
|
|
local update_file_name="repo-updated"
|
|
|
|
# `find` outputs filename if it has not been modified in the last day
|
|
|
|
local find_result=$(find $update_file_dir -name "$update_file_name" -type f -mtime +1 -print)
|
|
|
|
[ -n "$find_result" ]
|
|
|
|
}
|
|
|
|
|
2017-03-26 16:44:22 -07:00
|
|
|
initialize_or_update_repository() {
|
2017-04-19 20:37:03 -07:00
|
|
|
local repository_url
|
|
|
|
local repository_path
|
2017-03-26 16:44:22 -07:00
|
|
|
|
2017-04-19 20:37:03 -07:00
|
|
|
repository_url=$(asdf_repository_url)
|
|
|
|
repository_path=$(asdf_dir)/repository
|
|
|
|
|
2017-08-24 19:29:23 -07:00
|
|
|
if [ ! -d "$repository_path" ]; then
|
2017-03-26 16:44:22 -07:00
|
|
|
echo "initializing plugin repository..."
|
2017-04-19 20:37:03 -07:00
|
|
|
git clone "$repository_url" "$repository_path"
|
2017-08-24 19:29:23 -07:00
|
|
|
elif repository_needs_update; then
|
|
|
|
echo "updating plugin repository..."
|
|
|
|
(cd "$repository_path" && git fetch && git reset --hard origin/master)
|
2017-03-26 16:44:22 -07:00
|
|
|
fi
|
2017-08-24 19:29:23 -07:00
|
|
|
|
|
|
|
mkdir -p "$(asdf_dir)/tmp"
|
|
|
|
touch "$(asdf_dir)/tmp/repo-updated"
|
2017-03-26 16:44:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
get_plugin_source_url() {
|
2017-04-19 20:40:06 -07:00
|
|
|
local plugin_name=$1
|
2017-04-19 20:37:03 -07:00
|
|
|
local plugin_config
|
|
|
|
|
|
|
|
plugin_config="$(asdf_dir)/repository/plugins/$plugin_name"
|
|
|
|
|
2017-03-26 16:44:22 -07:00
|
|
|
|
2017-04-19 20:37:03 -07:00
|
|
|
if [ -f "$plugin_config" ]; then
|
|
|
|
grep "repository" "$plugin_config" | awk -F'=' '{print $2}' | sed 's/ //'
|
2017-03-26 16:44:22 -07:00
|
|
|
fi
|
|
|
|
}
|