Add config option to install plugins in $HOME/.asdf

This commit is contained in:
Jose Luis Salas 2018-06-10 10:54:39 +02:00
parent 6e9da8af0c
commit c86707c55f
No known key found for this signature in database
GPG Key ID: 2C938A963B1DBC31
11 changed files with 63 additions and 27 deletions

View File

@ -1,11 +1,11 @@
#!/usr/bin/env fish #!/usr/bin/env fish
set -l asdf_dir (dirname (status -f)) set -l asdf_data_dir (dirname (status -f))
# we get an ugly warning when setting the path if shims does not exist # we get an ugly warning when setting the path if shims does not exist
mkdir -p $asdf_dir/shims mkdir -p $asdf_data_dir/shims
for x in $asdf_dir/{bin,shims} for x in $asdf_data_dir/{bin,shims}
if not contains $x $PATH if not contains $x $PATH
and test -d $x and test -d $x
set -gx PATH $x $PATH set -gx PATH $x $PATH

View File

@ -17,8 +17,10 @@ ASDF_DIR="$(dirname "$current_script_path")"
# replace all occurrences - ${parameter//pattern/string} # replace all occurrences - ${parameter//pattern/string}
ASDF_BIN="${ASDF_DIR}/bin" ASDF_BIN="${ASDF_DIR}/bin"
ASDF_SHIMS="${ASDF_DIR}/shims" ASDF_SHIMS="${ASDF_DIR}/shims"
ASDF_USER_SHIMS="${HOME}/.asdf/shims"
[[ ":$PATH:" == *":${ASDF_BIN}:"* ]] && PATH="${PATH//$ASDF_BIN:/}" [[ ":$PATH:" == *":${ASDF_BIN}:"* ]] && PATH="${PATH//$ASDF_BIN:/}"
[[ ":$PATH:" == *":${ASDF_SHIMS}:"* ]] && PATH="${PATH//$ASDF_SHIMS:/}" [[ ":$PATH:" == *":${ASDF_SHIMS}:"* ]] && PATH="${PATH//$ASDF_SHIMS:/}"
[[ ":$PATH:" == *":${ASDF_USER_SHIMS}:"* ]] && PATH="${PATH//$ASDF_USER_SHIMS:/}"
# add to front of $PATH # add to front of $PATH
PATH="${ASDF_BIN}:$PATH" PATH="${ASDF_BIN}:$PATH"
PATH="${ASDF_SHIMS}:$PATH" PATH="${ASDF_SHIMS}:$PATH"

View File

@ -22,7 +22,7 @@ plugin_add_command() {
local plugin_path local plugin_path
plugin_path=$(get_plugin_path "$plugin_name") plugin_path=$(get_plugin_path "$plugin_name")
mkdir -p "$(asdf_dir)/plugins" mkdir -p "$(asdf_data_dir)/plugins"
if [ -d "$plugin_path" ]; then if [ -d "$plugin_path" ]; then
display_error "Plugin named $plugin_name already added" display_error "Plugin named $plugin_name already added"

View File

@ -2,7 +2,7 @@ plugin_list_all_command() {
initialize_or_update_repository initialize_or_update_repository
local plugins_index_path local plugins_index_path
plugins_index_path="$(asdf_dir)/repository/plugins" plugins_index_path="$(asdf_data_dir)/repository/plugins"
local plugins_local_path local plugins_local_path
plugins_local_path="$(get_plugin_path)" plugins_local_path="$(get_plugin_path)"

View File

@ -1,7 +1,7 @@
plugin_push_command() { plugin_push_command() {
local plugin_name=$1 local plugin_name=$1
if [ "$plugin_name" = "--all" ]; then if [ "$plugin_name" = "--all" ]; then
for dir in "$(asdf_dir)"/plugins/*; do for dir in "$(asdf_data_dir)"/plugins/*; do
echo "Pushing $(basename "$dir")..." echo "Pushing $(basename "$dir")..."
(cd "$dir" && git push) (cd "$dir" && git push)
done done

View File

@ -6,7 +6,7 @@ plugin_remove_command() {
plugin_path=$(get_plugin_path "$plugin_name") plugin_path=$(get_plugin_path "$plugin_name")
rm -rf "$plugin_path" rm -rf "$plugin_path"
rm -rf "$(asdf_dir)/installs/${plugin_name}" rm -rf "$(asdf_data_dir)/installs/${plugin_name}"
grep -l "asdf-plugin: ${plugin_name}" "$(asdf_dir)"/shims/* 2>/dev/null | xargs rm -f grep -l "asdf-plugin: ${plugin_name}" "$(asdf_data_dir)"/shims/* 2>/dev/null | xargs rm -f
} }

View File

@ -6,7 +6,7 @@ plugin_update_command() {
local plugin_name=$1 local plugin_name=$1
if [ "$plugin_name" = "--all" ]; then if [ "$plugin_name" = "--all" ]; then
for dir in "$(asdf_dir)"/plugins/*; do for dir in "$(asdf_data_dir)"/plugins/*; do
echo "Updating $(basename "$dir")..." echo "Updating $(basename "$dir")..."
(cd "$dir" && git pull) (cd "$dir" && git pull)
done done

View File

@ -23,7 +23,7 @@ reshim_command() {
else else
# generate for all versions of the package # generate for all versions of the package
local plugin_installs_path local plugin_installs_path
plugin_installs_path="$(asdf_dir)/installs/${plugin_name}" plugin_installs_path="$(asdf_data_dir)/installs/${plugin_name}"
for install in "${plugin_installs_path}"/*/; do for install in "${plugin_installs_path}"/*/; do
local full_version_name local full_version_name
@ -37,8 +37,8 @@ reshim_command() {
ensure_shims_dir() { ensure_shims_dir() {
# Create shims dir if doesn't exist # Create shims dir if doesn't exist
if [ ! -d "$(asdf_dir)/shims" ]; then if [ ! -d "$(asdf_data_dir)/shims" ]; then
mkdir "$(asdf_dir)/shims" mkdir "$(asdf_data_dir)/shims"
fi fi
} }
@ -52,7 +52,7 @@ write_shim_script() {
local plugin_shims_path local plugin_shims_path
plugin_shims_path=$(get_plugin_path "$plugin_name")/shims plugin_shims_path=$(get_plugin_path "$plugin_name")/shims
local shim_path local shim_path
shim_path="$(asdf_dir)/shims/$executable_name" shim_path="$(asdf_data_dir)/shims/$executable_name"
if [ -f "$plugin_shims_path/$executable_name" ]; then if [ -f "$plugin_shims_path/$executable_name" ]; then
cp "$plugin_shims_path/$executable_name" "$shim_path" cp "$plugin_shims_path/$executable_name" "$shim_path"
@ -169,7 +169,7 @@ remove_obsolete_shims() {
local plugin_name=$1 local plugin_name=$1
local full_version=$2 local full_version=$2
local shims_path local shims_path
shims_path="$(asdf_dir)/shims" shims_path="$(asdf_data_dir)/shims"
IFS=':' read -r -a version_info <<< "$full_version" IFS=':' read -r -a version_info <<< "$full_version"
if [ "${version_info[0]}" = "ref" ]; then if [ "${version_info[0]}" = "ref" ]; then
@ -203,7 +203,7 @@ remove_shim_for_version() {
local plugin_shims_path local plugin_shims_path
plugin_shims_path=$(get_plugin_path "$plugin_name")/shims plugin_shims_path=$(get_plugin_path "$plugin_name")/shims
local shim_path local shim_path
shim_path="$(asdf_dir)/shims/$executable_name" shim_path="$(asdf_data_dir)/shims/$executable_name"
local count_installed local count_installed
count_installed=$(list_installed_versions "$plugin_name" | wc -l) count_installed=$(list_installed_versions "$plugin_name" | wc -l)

View File

@ -2,7 +2,7 @@ update_command() {
local update_to_head=$1 local update_to_head=$1
( (
cd "$(asdf_dir)" || exit 1 cd "$(asdf_data_dir)" || exit 1
if [ -f asdf_updates_disabled ]; then if [ -f asdf_updates_disabled ]; then
echo "Update command disabled. Please use the package manager that you used to install asdf to upgrade asdf." echo "Update command disabled. Please use the package manager that you used to install asdf to upgrade asdf."

View File

@ -23,17 +23,40 @@ asdf_repository_url() {
echo "https://github.com/asdf-vm/asdf-plugins.git" echo "https://github.com/asdf-vm/asdf-plugins.git"
} }
get_install_type(){
local install_type
install_type=$(get_asdf_config_value "install_type")
echo "$install_type"
}
asdf_data_dir(){
local data_dir
if [ "$(get_install_type)" = "user" ]; then
data_dir="${HOME}/.asdf"
else
data_dir="$(asdf_dir)"
fi
echo "$data_dir"
}
get_install_path() { get_install_path() {
local plugin=$1 local plugin=$1
local install_type=$2 local install_type=$2
local version=$3 local version=$3
mkdir -p "$(asdf_dir)/installs/${plugin}"
local install_dir
install_dir="$(asdf_data_dir)/installs"
mkdir -p "${install_dir}/${plugin}"
if [ "$install_type" = "version" ] if [ "$install_type" = "version" ]
then then
echo "$(asdf_dir)/installs/${plugin}/${version}" echo "${install_dir}/${plugin}/${version}"
else else
echo "$(asdf_dir)/installs/${plugin}/${install_type}-${version}" echo "${install_dir}/${plugin}/${install_type}-${version}"
fi fi
} }
@ -42,8 +65,11 @@ list_installed_versions() {
local plugin_path local plugin_path
plugin_path=$(get_plugin_path "$plugin_name") plugin_path=$(get_plugin_path "$plugin_name")
local install_dir
install_dir="$(asdf_data_dir)/installs"
local plugin_installs_path local plugin_installs_path
plugin_installs_path=$(asdf_dir)/installs/${plugin_name} plugin_installs_path=${install_dir}/${plugin_name}
if [ -d "$plugin_installs_path" ]; then if [ -d "$plugin_installs_path" ]; then
# shellcheck disable=SC2045 # shellcheck disable=SC2045
@ -62,7 +88,7 @@ check_if_plugin_exists() {
exit 1 exit 1
fi fi
if [ ! -d "$(asdf_dir)/plugins/$plugin_name" ]; then if [ ! -d "$(asdf_data_dir)/plugins/$plugin_name" ]; then
display_error "No such plugin: $plugin_name" display_error "No such plugin: $plugin_name"
exit 1 exit 1
fi fi
@ -84,7 +110,7 @@ check_if_version_exists() {
} }
get_plugin_path() { get_plugin_path() {
echo "$(asdf_dir)/plugins/$1" echo "$(asdf_data_dir)/plugins/$1"
} }
display_error() { display_error() {
@ -303,7 +329,7 @@ get_asdf_config_value() {
repository_needs_update() { repository_needs_update() {
local update_file_dir local update_file_dir
update_file_dir="$(asdf_dir)/tmp" update_file_dir="$(asdf_data_dir)/tmp"
local update_file_name local update_file_name
update_file_name="repo-updated" update_file_name="repo-updated"
# `find` outputs filename if it has not been modified in the last day # `find` outputs filename if it has not been modified in the last day
@ -317,7 +343,7 @@ initialize_or_update_repository() {
local repository_path local repository_path
repository_url=$(asdf_repository_url) repository_url=$(asdf_repository_url)
repository_path=$(asdf_dir)/repository repository_path=$(asdf_data_dir)/repository
if [ ! -d "$repository_path" ]; then if [ ! -d "$repository_path" ]; then
echo "initializing plugin repository..." echo "initializing plugin repository..."
@ -327,15 +353,15 @@ initialize_or_update_repository() {
(cd "$repository_path" && git fetch && git reset --hard origin/master) (cd "$repository_path" && git fetch && git reset --hard origin/master)
fi fi
mkdir -p "$(asdf_dir)/tmp" mkdir -p "$(asdf_data_dir)/tmp"
touch "$(asdf_dir)/tmp/repo-updated" touch "$(asdf_data_dir)/tmp/repo-updated"
} }
get_plugin_source_url() { get_plugin_source_url() {
local plugin_name=$1 local plugin_name=$1
local plugin_config local plugin_config
plugin_config="$(asdf_dir)/repository/plugins/$plugin_name" plugin_config="$(asdf_data_dir)/repository/plugins/$plugin_name"
if [ -f "$plugin_config" ]; then if [ -f "$plugin_config" ]; then

View File

@ -124,6 +124,14 @@ teardown() {
[ "$output" = "0.1.0|$HOME/.tool-versions" ] [ "$output" = "0.1.0|$HOME/.tool-versions" ]
} }
@test "asdf_data_dir should return user dir if configured" {
echo "install_type = user" > $HOME/.asdfrc
run asdf_data_dir
[ "$status" -eq 0 ]
[ "$output" = "$HOME/.asdf" ]
}
@test "find_version should return \$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME if set" { @test "find_version should return \$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME if set" {
ASDF_DEFAULT_TOOL_VERSIONS_FILENAME="$PROJECT_DIR/global-tool-versions" ASDF_DEFAULT_TOOL_VERSIONS_FILENAME="$PROJECT_DIR/global-tool-versions"
echo "dummy 0.1.0" > $ASDF_DEFAULT_TOOL_VERSIONS_FILENAME echo "dummy 0.1.0" > $ASDF_DEFAULT_TOOL_VERSIONS_FILENAME