From 19da205b0b37ebddac2b4cc71018b413ec7d24f0 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Mon, 3 Aug 2015 00:29:01 +0200 Subject: [PATCH] Enable removing plugins via CLI executable --- README.md | 2 +- bin/clean_plugins | 14 ++++++++++ bindings/clean_plugins | 17 ++++++++++++ docs/managing_plugins_via_cmd_line.md | 6 +++++ scripts/clean_plugins.sh | 39 ++++++++++++++++++++++----- tests/test_plugin_clean.sh | 39 ++++++++++++++++++++++++--- tpm | 2 +- 7 files changed, 107 insertions(+), 12 deletions(-) create mode 100755 bin/clean_plugins create mode 100755 bindings/clean_plugins diff --git a/README.md b/README.md index de79e44..9559d83 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ find plugin directory there and remove it. - updates plugin(s) `prefix + alt + u` -- uninstall plugins that are not on the plugin list +- remove/uninstall plugins not on the plugin list ### More plugins diff --git a/bin/clean_plugins b/bin/clean_plugins new file mode 100755 index 0000000..12f8730 --- /dev/null +++ b/bin/clean_plugins @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +# Script intended for use via the command line. +# +# `.tmux.conf` needs to be set for TPM. Tmux has to be installed on the system, +# but does not need to be started in order to run this script. + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +SCRIPTS_DIR="$CURRENT_DIR/../scripts" + +main() { + "$SCRIPTS_DIR/clean_plugins.sh" # has correct exit code +} +main diff --git a/bindings/clean_plugins b/bindings/clean_plugins new file mode 100755 index 0000000..ea18752 --- /dev/null +++ b/bindings/clean_plugins @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +# Tmux key-binding script. +# Scripts intended to be used via the command line are in `bin/` directory. + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +SCRIPTS_DIR="$CURRENT_DIR/../scripts" + +source "$SCRIPTS_DIR/shared_functions.sh" + +main() { + reload_tmux_environment + "$SCRIPTS_DIR/clean_plugins.sh" --tmux-echo >/dev/null 2>&1 + reload_tmux_environment + end_message +} +main diff --git a/docs/managing_plugins_via_cmd_line.md b/docs/managing_plugins_via_cmd_line.md index e34136e..4dc6d3b 100644 --- a/docs/managing_plugins_via_cmd_line.md +++ b/docs/managing_plugins_via_cmd_line.md @@ -28,3 +28,9 @@ To update all installed plugins: or update a single plugin: ~/.tmux/plugins/tpm/bin/install_plugins tmux-sensible + +### Removing plugins + +To remove plugins not on the plugin list: + + ~/.tmux/plugins/tpm/bin/clean_plugins diff --git a/scripts/clean_plugins.sh b/scripts/clean_plugins.sh index 2dd1c64..88d9bdf 100755 --- a/scripts/clean_plugins.sh +++ b/scripts/clean_plugins.sh @@ -4,6 +4,33 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/shared_functions.sh" +TMUX_ECHO_FLAG="$1" + +# True if invoked as tmux mapping or tmux command, +# false if invoked via command line wrapper from `bin/` directory. +use_tmux_echo() { + [ "$TMUX_ECHO_FLAG" == "--tmux-echo" ] +} + +if use_tmux_echo; then + # use tmux specific echo-ing + echo_ok() { + echo_message "$*" + } + + echo_err() { + echo_message "$*" + } +else + echo_ok() { + echo "$*" + } + + echo_err() { + fail_helper "$*" + } +fi + clean_plugins() { local plugins plugin plugin_directory plugins="$(shared_get_tpm_plugins_list)" @@ -15,22 +42,20 @@ clean_plugins() { *"${plugin}"*) : ;; *) [ "${plugin}" = "tpm" ] && continue - echo_message "Removing \"$plugin\"" - rm -rf "${plugin_directory}" + echo_ok "Removing \"$plugin\"" + rm -rf "${plugin_directory}" >/dev/null 2>&1 [ -d "${plugin_directory}" ] && - echo_message " \"$plugin\" clean fail" || - echo_message " \"$plugin\" clean success" + echo_err " \"$plugin\" clean fail" || + echo_ok " \"$plugin\" clean success" ;; esac done } main() { - reload_tmux_environment shared_set_tpm_path_constant ensure_tpm_path_exists clean_plugins - reload_tmux_environment - end_message + exit_value_helper } main diff --git a/tests/test_plugin_clean.sh b/tests/test_plugin_clean.sh index d920507..e4c0888 100755 --- a/tests/test_plugin_clean.sh +++ b/tests/test_plugin_clean.sh @@ -14,16 +14,49 @@ manually_install_the_plugin() { git clone --quiet https://github.com/tmux-plugins/tmux-example-plugin } -test_plugin_uninstallation() { +# TMUX KEY-BINDING TESTS + +test_plugin_uninstallation_via_tmux_key_binding() { set_tmux_conf_helper <<- HERE run-shell "$TPM_DIR/tpm" HERE manually_install_the_plugin - # opens tmux and test it with `expect` "$CURRENT_DIR/expect_successful_clean_plugins" || - fail_helper "Clean fails" + fail_helper "[key-binding] clean fails" + + teardown_helper +} + +# SCRIPT TESTS + +test_plugin_uninstallation_via_script() { + set_tmux_conf_helper <<- HERE + run-shell "$TPM_DIR/tpm" + HERE + + manually_install_the_plugin + + script_run_helper "$TPM_DIR/bin/clean_plugins" '"tmux-example-plugin" clean success' || + fail_helper "[script] plugin cleaning fails" + + teardown_helper +} + +test_unsuccessful_plugin_uninstallation_via_script() { + set_tmux_conf_helper <<- HERE + run-shell "$TPM_DIR/tpm" + HERE + + manually_install_the_plugin + chmod 000 "$PLUGINS_DIR/tmux-example-plugin" # disable directory deletion + + local expected_exit_code=1 + script_run_helper "$TPM_DIR/bin/clean_plugins" '"tmux-example-plugin" clean fail' "$expected_exit_code" || + fail_helper "[script] unsuccessful plugin cleaning doesn't fail" + + chmod 755 "$PLUGINS_DIR/tmux-example-plugin" # enable directory deletion teardown_helper } diff --git a/tpm b/tpm index 6887b94..679f001 100755 --- a/tpm +++ b/tpm @@ -39,7 +39,7 @@ set_tpm_key_bindings() { tmux bind-key "$update_key" run-shell "$BINDINGS_DIR/update_plugins" local clean_key=$(get_tmux_option "$clean_key_option" "$default_clean_key") - tmux bind-key "$clean_key" run-shell "$SCRIPTS_DIR/clean_plugins.sh" + tmux bind-key "$clean_key" run-shell "$BINDINGS_DIR/clean_plugins" } supported_tmux_version_ok() {