Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for changing the location of plugin scripts #1185

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/manage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ legacy_version_file = no
use_release_candidates = no
always_keep_download = no
plugin_repository_last_check_duration = 60
use_asdf_plugin_configuration = no
```

### `legacy_version_file`
Expand Down Expand Up @@ -96,6 +97,18 @@ Configure the duration since the last asdf plugin repository sync to the next. C
| `0` | Sync on each trigger event |
| `never` | Never sync |

### `use_asdf_plugin_configuration`

Settings to use the configuration file `.asdf-plugin` provided by the plugin.

When using a plugin that requires this setting, set to yes.

| Options | Description |
| :--------------------------------------------------------- |:-----------------------------------------------------------------------|
| `no` <Badge type="tip" text="default" vertical="middle" /> | Do not use the .asdf-plugin configuration file provided by the plugin. |
| `yes` | Use the configuration file .asdf-plugin provided by the plugin. |


## Environment Variables

- `ASDF_CONFIG_FILE` - Defaults to `~/.asdfrc` as described above. Can be set to any location.
Expand Down
25 changes: 25 additions & 0 deletions docs/plugins/create.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,28 @@ cmd="$cmd $releases_path"
To make it easier on your users, you can add your plugin to the official plugins repository to have your plugin listed and easily installable using a shorter command, e.g. `asdf plugin add my-plugin`.

Follow the instruction at the plugins repository: [asdf-vm/asdf-plugins](https://github.com/asdf-vm/asdf-plugins).

## Plugin configuration

Add an `.asdf-plugin` file to your plugin root directory and asdf will use the settings specified in the file. The file below shows the required format with the default values to demonstrate:

```:no-line-numbers
plugin_directory =
```

To enable this, add the following to your `asdf` configuration file `$HOME/.asdfrc`:

```
use_asdf_plugin_configuration = yes
```

See the [configuration](/manage/configuration.md) reference page for more config options.

### `plugin_directory`

If you want to place plugin scripts (e.g. bin, lib) in a subdirectory, specify a path relative to the repository root.
For example, to place asdf plugin scripts under `contrib/asdf`, adding the following to .asdf-plugin:

```:no-line-numbers
plugin_directory = contrib/asdf
```
5 changes: 4 additions & 1 deletion lib/commands/command-plugin-remove.bash
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ plugin_remove_command() {
)
fi

rm -rf "$plugin_path"
local plugin_root
plugin_root=$(get_plugin_root "${plugin_name}")

rm -rf "$plugin_root"
rm -rf "$(asdf_data_dir)/installs/${plugin_name}"
rm -rf "$(asdf_data_dir)/downloads/${plugin_name}"

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-plugin-test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ plugin_test_command() {
fi

# Assert the scripts in bin are executable by asdf
for filename in "$ASDF_DIR/plugins/$plugin_name/bin"/*; do
for filename in "$plugin_path/bin"/*; do
if [ ! -x "$filename" ]; then
fail_test "Incorrect permissions on $filename. Must be executable by asdf"
fi
Expand Down
17 changes: 10 additions & 7 deletions lib/functions/plugins.bash
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,25 @@ plugin_add_command() {
exit 1
fi

local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
local plugin_root
plugin_root=$(get_plugin_root "$plugin_name")

mkdir -p "$(asdf_data_dir)/plugins"

if [ -d "$plugin_path" ]; then
if [ -d "$plugin_root" ]; then
display_error "Plugin named $plugin_name already added"
exit 2
else
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
if ! git clone -q "$source_url" "$plugin_root"; then
exit 1
fi

local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")

if [ -f "${plugin_path}/bin/post-plugin-add" ]; then
(
export ASDF_PLUGIN_SOURCE_URL=$source_url
Expand Down Expand Up @@ -122,10 +125,10 @@ plugin_update_command() {
wait
fi
else
local plugin_path
plugin_path="$(get_plugin_path "$plugin_name")"
local plugin_root
plugin_root="$(get_plugin_root "$plugin_name")"
check_if_plugin_exists "$plugin_name"
update_plugin "$plugin_name" "$plugin_path" "$gitref"
update_plugin "$plugin_name" "$plugin_root" "$gitref"
fi
}

Expand Down
25 changes: 24 additions & 1 deletion lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,37 @@ version_not_installed_text() {
printf "version %s is not installed for %s\\n" "$version" "$plugin_name"
}

get_plugin_path() {
get_plugin_root() {
if test -n "$1"; then
printf "%s\\n" "$(asdf_data_dir)/plugins/$1"
else
printf "%s\\n" "$(asdf_data_dir)/plugins"
fi
}

get_plugin_path() {
local plugins_dir=
plugins_dir="$(asdf_data_dir)/plugins"

if test -n "$1"; then
if [ "$(get_asdf_config_value "use_asdf_plugin_configuration")" = "yes" ]; then
local config_file=
config_file="$plugins_dir/$1/.asdf-plugin"
if [ -f "$config_file" ]; then
local plugin_directory=
plugin_directory=$(get_asdf_config_value_from_file "$config_file" plugin_directory)
if [ -n "$plugin_directory" ] && [ -d "$plugins_dir/$1/$plugin_directory" ]; then
printf "%s\\n" "$plugins_dir/$1/$plugin_directory"
return
fi
fi
fi
printf "%s\\n" "$plugins_dir/$1"
else
printf "%s\\n" "$plugins_dir"
fi
}

display_error() {
printf "%s\\n" "$1" >&2
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/dummy_subdir_plugin/.asdf-plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plugin_directory = asdf-plugin
20 changes: 20 additions & 0 deletions test/fixtures/dummy_subdir_plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Akash Manohar J

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions test/fixtures/dummy_subdir_plugin/asdf-plugin/bin/download
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

exit 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

get_legacy_version() {
current_directory=$1
version_file="$current_directory/.dummy-version"

if [ -f "$version_file" ]; then
cat "$version_file"
fi
}

get_legacy_version "$1"
10 changes: 10 additions & 0 deletions test/fixtures/dummy_subdir_plugin/asdf-plugin/bin/help.overview
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

echo "Dummy plugin documentation"
echo
echo "Dummy plugin is a plugin only used for unit tests"

if [ -n "$ASDF_INSTALL_VERSION" ]; then
echo
echo "Details specific for version $ASDF_INSTALL_VERSION"
fi
27 changes: 27 additions & 0 deletions test/fixtures/dummy_subdir_plugin/asdf-plugin/bin/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

# We want certain versions to fail installation for various reasons in the tests
check_dummy_versions() {
local bad_versions=" other-dummy "
if [[ "$bad_versions" == *" $ASDF_INSTALL_VERSION "* ]]; then
echo "Dummy couldn't install version: $ASDF_INSTALL_VERSION (on purpose)"
exit 1
fi
}

check_dummy_versions
mkdir -p "$ASDF_INSTALL_PATH"
env >"$ASDF_INSTALL_PATH/env"
echo "$ASDF_INSTALL_VERSION" >"$ASDF_INSTALL_PATH/version"

# create the dummy executable
mkdir -p "$ASDF_INSTALL_PATH/bin"
cat <<EOF >"$ASDF_INSTALL_PATH/bin/dummy"
echo This is Dummy ${ASDF_INSTALL_VERSION}! \$2 \$1
EOF
chmod +x "$ASDF_INSTALL_PATH/bin/dummy"
mkdir -p "$ASDF_INSTALL_PATH/bin/subdir"
cat <<EOF >"$ASDF_INSTALL_PATH/bin/subdir/other_bin"
echo This is Other Bin ${ASDF_INSTALL_VERSION}! \$2 \$1
EOF
chmod +x "$ASDF_INSTALL_PATH/bin/subdir/other_bin"
10 changes: 10 additions & 0 deletions test/fixtures/dummy_subdir_plugin/asdf-plugin/bin/latest-stable
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

get_latest_stable() {
query=$1

version_list=(1.0.0 1.1.0 2.0.0)
printf "%s\n" "${version_list[@]}" | grep -E "^\\s*$query" | tail -1
}

get_latest_stable "$1"
6 changes: 6 additions & 0 deletions test/fixtures/dummy_subdir_plugin/asdf-plugin/bin/list-all
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

versions_list=(1.0.0 1.1.0 2.0.0)
echo "${versions_list[@]}"
# Sending message to STD error to ensure that it is ignored
echo "ignore this error" >&2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo ".dummy-version .dummyrc"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

# shellcheck disable=SC2020
tr <"$1" -d "dummy-"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo "plugin add path=${ASDF_PLUGIN_PATH} source_url=${ASDF_PLUGIN_SOURCE_URL}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo "plugin updated path=${ASDF_PLUGIN_PATH} old git-ref=${ASDF_PLUGIN_PREV_REF} new git-ref=${ASDF_PLUGIN_POST_REF}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo "plugin-remove ${ASDF_PLUGIN_PATH}"
82 changes: 82 additions & 0 deletions test/plugin_subdir.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env bats

load test_helpers

setup() {
setup_asdf_dir
echo 'use_asdf_plugin_configuration = yes' >$HOME/.asdfrc
}

teardown() {
clean_asdf_dir
}

@test "plugin_add install subdirectory plugin" {
install_mock_subdir_plugin "dummy-subdir"

run asdf plugin-list
# whitespace between 'elixir' and url is from printf %-15s %s format
[ "$output" = "dummy-subdir" ]
}

@test "plugin_remove command removes the subdirectory plugin directory" {
install_mock_subdir_plugin "dummy-subdir"

run asdf plugin-remove "dummy-subdir"
[ "$status" -eq 0 ]
[ ! -d "$ASDF_DIR/plugins/dummy-subdir" ]
}

@test "list_all_command lists available versions" {
install_mock_subdir_plugin "dummy-subdir"

run asdf list-all dummy-subdir
[ "$(echo -e "1.0.0\n1.1.0\n2.0.0")" == "$output" ]
[ "$status" -eq 0 ]
}

@test "install_command installs the correct version" {
install_mock_subdir_plugin "dummy-subdir"

run asdf install dummy-subdir 1.1.0
# whitespace between 'elixir' and url is from printf %-15s %s format
[ "$(cat "$ASDF_DIR/installs/dummy-subdir/1.1.0/version")" = "1.1.0" ]
}

@test "list_command should list plugins with installed versions" {
install_mock_subdir_plugin "dummy-subdir"

run asdf install dummy-subdir 1.0.0
run asdf install dummy-subdir 1.1.0
run asdf list
[[ "$output" == "$(echo -e "dummy-subdir\n 1.0.0\n 1.1.0")"* ]]
[ "$status" -eq 0 ]
}

@test "[latest_command - dummy_plugin] shows latest stable version" {
install_mock_subdir_plugin "dummy-subdir"

run asdf latest "dummy-subdir"
[ "2.0.0" == "$output" ]
[ "$status" -eq 0 ]
}

@test "plugin_test_command works with no options provided for subdirectory plugin" {
install_mock_subdir_plugin_repo dummy-subdir

run asdf plugin-test dummy-subdir "${BASE_DIR}/repo-dummy-subdir"
echo "status = ${status}"
echo "output = ${output}"
[ "$status" -eq 0 ]
}

@test "asdf plugin-update should pull latest default branch (refs/remotes/origin/HEAD) for subdirectory plugin" {
install_mock_subdir_plugin_repo dummy-subdir
run asdf plugin add "dummy-subdir" "${BASE_DIR}/repo-dummy-subdir"

run asdf plugin-update dummy-subdir
repo_head="$(git --git-dir "$ASDF_DIR/plugins/dummy-subdir/.git" --work-tree "$ASDF_DIR/plugins/dummy-subdir" rev-parse --abbrev-ref HEAD)"
[ "$status" -eq 0 ]
[[ "$output" =~ "Updating dummy-subdir to master"* ]]
[ "$repo_head" = "master" ]
}
17 changes: 17 additions & 0 deletions test/test_helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ install_mock_broken_plugin() {
cp -r "$BATS_TEST_DIRNAME/fixtures/dummy_broken_plugin" "$location/plugins/$plugin_name"
}

install_mock_subdir_plugin() {
local plugin_name=$1
local location="${2:-$ASDF_DIR}"
cp -r "$BATS_TEST_DIRNAME/fixtures/dummy_subdir_plugin" "$location/plugins/$plugin_name"
}

install_mock_plugin_repo() {
local plugin_name=$1
local location="${BASE_DIR}/repo-${plugin_name}"
Expand All @@ -54,6 +60,17 @@ install_mock_plugin_repo() {
git -C "${location}" commit -q -m "asdf ${plugin_name} plugin"
}

install_mock_subdir_plugin_repo() {
local plugin_name=$1
local location="${BASE_DIR}/repo-${plugin_name}"
cp -r "$BATS_TEST_DIRNAME/fixtures/dummy_subdir_plugin" "${location}"
git -C "${location}" init -q
git -C "${location}" config user.name "Test"
git -C "${location}" config user.email "[email protected]"
git -C "${location}" add -A
git -C "${location}" commit -q -m "asdf ${plugin_name} plugin"
}

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