Skip to content

Commit b9fae70

Browse files
committed
Add plugin-list-all command
1 parent 29a29f1 commit b9fae70

File tree

8 files changed

+61
-6
lines changed

8 files changed

+61
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
installs
2-
plugins
2+
/plugins
33
shims
44
repository
55
.vagrant
66
keyrings
7+
/tmp

bin/asdf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ source $(dirname $(dirname $0))/lib/commands/list-all.sh
1515
source $(dirname $(dirname $0))/lib/commands/reshim.sh
1616
source $(dirname $(dirname $0))/lib/commands/plugin-add.sh
1717
source $(dirname $(dirname $0))/lib/commands/plugin-list.sh
18+
source $(dirname $(dirname $0))/lib/commands/plugin-list-all.sh
1819
source $(dirname $(dirname $0))/lib/commands/plugin-update.sh
1920
source $(dirname $(dirname $0))/lib/commands/plugin-remove.sh
2021

@@ -49,7 +50,7 @@ case $1 in
4950

5051
"which")
5152
which_command $callback_args;;
52-
53+
5354
"local")
5455
local_command $callback_args;;
5556

@@ -74,6 +75,9 @@ case $1 in
7475
"plugin-list")
7576
plugin_list_command $callback_args;;
7677

78+
"plugin-list-all")
79+
plugin_list_all_command $callback_args;;
80+
7781
"plugin-update")
7882
plugin_update_command $callback_args;;
7983

lib/commands/plugin-list-all.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plugin_list_all_command() {
2+
initialize_or_update_repository
3+
4+
local plugins_path=$(asdf_dir)/repository/plugins
5+
for plugin in $plugins_path/*; do
6+
local plugin_name="$(basename $plugin)"
7+
echo "$plugin_name"
8+
done
9+
}

lib/utils.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,20 +261,31 @@ asdf_repository_url() {
261261
echo "https://github.com/asdf-vm/asdf-plugins.git"
262262
}
263263

264+
repository_needs_update() {
265+
local update_file_dir="$(asdf_dir)/tmp"
266+
local update_file_name="repo-updated"
267+
# `find` outputs filename if it has not been modified in the last day
268+
local find_result=$(find $update_file_dir -name "$update_file_name" -type f -mtime +1 -print)
269+
[ -n "$find_result" ]
270+
}
271+
264272
initialize_or_update_repository() {
265273
local repository_url
266274
local repository_path
267275

268276
repository_url=$(asdf_repository_url)
269277
repository_path=$(asdf_dir)/repository
270278

271-
if [ -d "$repository_path" ]; then
272-
echo "updating plugin repository..."
273-
(cd "$repository_path" && git fetch && git reset --hard origin/master)
274-
else
279+
if [ ! -d "$repository_path" ]; then
275280
echo "initializing plugin repository..."
276281
git clone "$repository_url" "$repository_path"
282+
elif repository_needs_update; then
283+
echo "updating plugin repository..."
284+
(cd "$repository_path" && git fetch && git reset --hard origin/master)
277285
fi
286+
287+
mkdir -p "$(asdf_dir)/tmp"
288+
touch "$(asdf_dir)/tmp/repo-updated"
278289
}
279290

280291
get_plugin_source_url() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
repository = http://example.com/bar
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
repository = http://example.com/foo

test/plugin_list_all_command.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bats
2+
3+
load test_helpers
4+
5+
. $(dirname $BATS_TEST_DIRNAME)/lib/commands/plugin-list-all.sh
6+
7+
setup() {
8+
setup_asdf_dir
9+
setup_repo
10+
}
11+
12+
teardown() {
13+
clean_asdf_dir
14+
}
15+
16+
@test "plugin_list_all list all plugins in the repository" {
17+
run plugin_list_all_command
18+
local expected="bar
19+
foo"
20+
[ "$status" -eq 0 ]
21+
[ "$output" = "$expected" ]
22+
}

test/test_helpers.bash

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ setup_asdf_dir() {
77
mkdir -p $ASDF_DIR/plugins
88
mkdir -p $ASDF_DIR/installs
99
mkdir -p $ASDF_DIR/shims
10+
mkdir -p $ASDF_DIR/tmp
1011
PATH=$ASDF_DIR/shims:$PATH
1112
}
1213

@@ -33,3 +34,8 @@ clean_asdf_dir() {
3334
rm -rf $BASE_DIR
3435
unset ASDF_DIR
3536
}
37+
38+
setup_repo() {
39+
cp -r $BATS_TEST_DIRNAME/fixtures/dummy_plugins_repo $ASDF_DIR/repository
40+
touch "$(asdf_dir)/tmp/repo-updated"
41+
}

0 commit comments

Comments
 (0)