Skip to content

Commit

Permalink
feat: Add versioning to plugin add command
Browse files Browse the repository at this point in the history
Allows user to specify version along with URL when adding plugin to lock
to a specific version instead of always relying on HEAD.

Relates to #234
  • Loading branch information
theoretick committed Apr 5, 2021
1 parent 456d8e3 commit 12c438e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/commands/command-plugin-add.bash
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- sh -*-

plugin_add_command() {
if [[ $# -lt 1 || $# -gt 2 ]]; then
display_error "usage: asdf plugin-add <name> [<git-url>]"
if [[ $# -lt 1 || $# -gt 3 ]]; then
display_error "usage: asdf plugin-add <name> [<git-url>] [<git-ref>]"
exit 1
fi

local plugin_name=$1
local plugin_ref=""

if [ -n "$2" ]; then
local source_url=$2
Expand All @@ -16,6 +17,11 @@ plugin_add_command() {
source_url=$(get_plugin_source_url "$plugin_name")
fi

if [ -n "$3" ]; then
plugin_ref="$3"
plugin_name=$plugin_name-$3
fi

if [ -z "$source_url" ]; then
display_error "plugin $plugin_name not found in repository"
exit 1
Expand All @@ -33,8 +39,14 @@ plugin_add_command() {
asdf_run_hook "pre_asdf_plugin_add" "$plugin_name"
asdf_run_hook "pre_asdf_plugin_add_${plugin_name}"

if ! git clone -q "$source_url" "$plugin_path"; then
exit 1
if [ -z "$plugin_ref" ]; then
if ! git clone -q "$source_url" "$plugin_path"; then
exit 1
fi
else
if ! git clone -q -b "$plugin_ref" "$source_url" "$plugin_path"; then
exit 1
fi
fi

if [ -f "${plugin_path}/bin/post-plugin-add" ]; then
Expand Down
14 changes: 14 additions & 0 deletions test/plugin_add_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ teardown() {
[ "$output" = "dummy" ]
}

@test "plugin_add command with URL and git-ref specified adds a plugin using repo" {
install_mock_plugin_repo_with_ref "dummy"

run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy" "tagname"
[ "$status" -eq 0 ]

repo_head="$(git --git-dir "${BASE_DIR}/repo-dummy/.git" --work-tree "$ASDF_DIR/plugins/dummy" describe --tags)"
[ "$status" -eq 0 ]
[ "$repo_head" = "tagname" ]

run asdf plugin-list
[ "$output" = "dummy-tagname" ]
}

@test "plugin_add command with URL specified run twice returns error second time" {
install_mock_plugin_repo "dummy"

Expand Down
7 changes: 7 additions & 0 deletions test/test_helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ install_mock_plugin_repo() {
git -C "${location}" commit -q -m "asdf ${plugin_name} plugin"
}

install_mock_plugin_repo_with_ref() {
install_mock_plugin_repo $1
local plugin_name=$1
local location="${BASE_DIR}/repo-${plugin_name}"
git -C "${location}" tag "tagname"
}

install_mock_plugin_version() {
local plugin_name=$1
local plugin_version=$2
Expand Down

0 comments on commit 12c438e

Please sign in to comment.