Skip to content

Commit

Permalink
feat: add update and update-all options (#15)
Browse files Browse the repository at this point in the history
Update plugin(s) to the latest version in the system and the .plugin-versions file.
  • Loading branch information
aabouzaid authored Sep 4, 2023
1 parent 1369b04 commit 426f70e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Hence, `asdf-plugin-manager` fills the gap to manage asdf plugins securely and d
# Dependencies

- [asdf-vm](https://asdf-vm.com/): Tested with `v0.12.0` but probably will work with older versions.
- `bash`, `cat`, `grep`, `tr`, `cut`, `column`: Generic POSIX utilities.
- `bash`, `cat`, `grep`, `tr`, `cut`, `column`, `sed`: Generic POSIX utilities.
- `ASDF_PLUGIN_MANAGER_PLUGIN_VERSIONS_FILENAME`: Set default name for the file with the list of managed plugins.
Default: `.plugin-versions`.

Expand Down
41 changes: 41 additions & 0 deletions cli/asdf-plugin-manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,25 @@ USAGE:
asdf-plugin-manager list : List plugins in .plugin-versions file
asdf-plugin-manager add <plugin-name> : Add named plugin according to .plugin-versions file
asdf-plugin-manager add-all : Add all plugins according to .plugin-versions file
asdf-plugin-manager update <plugin-name> : Update named plugin to latest in the system and in the .plugin-versions file
asdf-plugin-manager update-all : Update all plugins to latest in the system and in the .plugin-versions file
asdf-plugin-manager remove <plugin-name> : Remove named plugin according to .plugin-versions file
asdf-plugin-manager remove-all : Remove all plugins according to .plugin-versions file
EOF
}

print_git_compare_url() {
local provider_url="$1"
local plugin_ref_current="$2"
local plugin_ref_head="$3"

if $(echo "${provider_url}" | grep -q 'github'); then
echo "${plugin_url%.*}/compare/${plugin_ref_current}...${plugin_ref_head}"
elif $(echo "${provider_url}" | grep -q 'gitlab'); then
echo "${plugin_url%.*}/-/compare/${plugin_ref_current}...${plugin_ref_head}"
fi
}

export_plugins() {
asdf plugin-list --refs --urls | tr -s ' ' | cut -d ' ' -f 1,2,4 | column -t
}
Expand Down Expand Up @@ -75,6 +89,27 @@ add_plugins() {
done
}

update_plugins() {
local managed_plugins="$1"
echo "${managed_plugins}" | while read managed_plugin; do
read -r plugin_name plugin_url plugin_ref_current < <(echo ${managed_plugin})

echo "[INFO] Updating: ${plugin_name} ${plugin_url} ${plugin_ref_current} to HEAD"
asdf plugin update "${plugin_name}"
plugin_ref_head="$(export_plugins | egrep "^\b${plugin_name}\b\s+" | sed -e 's/^.*\s//')"

echo "[INFO] Updating git-ref in plugin version file: ${PLUGIN_VERSIONS_FILENAME}"
sed -i "/^\b${plugin_name}\b/ s/${plugin_ref_current}/${plugin_ref_head}/" "${PLUGIN_VERSIONS_FILENAME}"

echo '!!!'
echo '[CAUTION] Please review the changes since last update:'
echo "$(print_git_compare_url ${plugin_url} ${plugin_ref_current} ${plugin_ref_head})"
echo '!!!'

echo "[INFO] Done."
done
}

if [[ -z $1 ]]; then
print_help
exit 1
Expand Down Expand Up @@ -102,6 +137,12 @@ while test -n "$1"; do
add-all)
add_plugins "$(list_plugins)"
;;
update)
update_plugins "$(list_plugins $2)"
;;
update-all)
update_plugins "$(list_plugins)"
;;
remove)
remove_plugins "$(list_plugins $2)"
;;
Expand Down

0 comments on commit 426f70e

Please sign in to comment.