diff --git a/CHANGELOG.md b/CHANGELOG.md index 0791c05..5b22377 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - improved test runner function - switch to using [tmux-test](https://github.com/tmux-plugins/tmux-test) framework +- add `bin/update_plugins` cli executable script ### v2.0.0, 2015-07-07 - enable overriding default key bindings diff --git a/bin/update_plugins b/bin/update_plugins new file mode 100755 index 0000000..30a5646 --- /dev/null +++ b/bin/update_plugins @@ -0,0 +1,24 @@ +#!/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" +PROGRAM_NAME="$0" + +if [ $# -eq 0 ]; then + echo "usage:" + echo " $PROGRAM_NAME all update all plugins" + echo " $PROGRAM_NAME tmux-foo update plugin 'tmux-foo'" + echo " $PROGRAM_NAME tmux-bar tmux-baz update multiple plugins" + exit 1 +fi + +main() { + "$SCRIPTS_DIR/update_plugin.sh" --shell-echo "$*" # has correct exit code +} +main "$*" + diff --git a/scripts/update_plugin_prompt.sh b/bindings/update_plugins similarity index 77% rename from scripts/update_plugin_prompt.sh rename to bindings/update_plugins index 4f26951..8626a73 100755 --- a/scripts/update_plugin_prompt.sh +++ b/bindings/update_plugins @@ -1,12 +1,16 @@ #!/usr/bin/env bash -# when invoked with `prefix + U` this script: +# Tmux key-binding script. +# Scripts intended to be used via the command line are in `bin/` directory. + +# This script: # - shows a list of installed plugins # - starts a prompt to enter the name of the plugin that will be updated CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +SCRIPTS_DIR="$CURRENT_DIR/../scripts" -source "$CURRENT_DIR/shared_functions.sh" +source "$SCRIPTS_DIR/shared_functions.sh" display_plugin_update_list() { local plugins="$(shared_get_tpm_plugins_list)" @@ -31,10 +35,9 @@ display_plugin_update_list() { update_plugin_prompt() { tmux command-prompt -p 'plugin update:' " \ send-keys C-c; \ - run-shell '$CURRENT_DIR/update_plugin.sh %1'" + run-shell '$SCRIPTS_DIR/update_plugin_prompt_handler.sh %1'" } - main() { reload_tmux_environment shared_set_tpm_path_constant diff --git a/scripts/update_plugin.sh b/scripts/update_plugin.sh index 8504606..4a3f8c6 100755 --- a/scripts/update_plugin.sh +++ b/scripts/update_plugin.sh @@ -1,39 +1,58 @@ #!/usr/bin/env bash +# this script handles core logic of updating plugins + CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/shared_functions.sh" -empty() { - [ -z "$1" ] +TMUX_ECHO_FLAG="$1" +shift + +# 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_all() { - [ "$1" == "all" ] -} +if use_tmux_echo; then + # use tmux specific echo-ing + echo_ok() { + echo_message "$*" + } -cancel() { - exit 0 -} + echo_err() { + echo_message "$*" + } +else + echo_ok() { + echo "$*" + } + + echo_err() { + fail_helper "$*" + } +fi pull_changes() { local plugin="$1" - local plugin_path=$(shared_plugin_path "$plugin") - cd $plugin_path && + local plugin_path="$(shared_plugin_path "$plugin")" + cd "$plugin_path" && GIT_TERMINAL_PROMPT=0 git pull && GIT_TERMINAL_PROMPT=0 git submodule update --init --recursive } update() { local plugin="$1" - echo_message "Updating \"$plugin\"" + echo_ok "Updating \"$plugin\"" $(pull_changes "$plugin" > /dev/null 2>&1) && - echo_message " \"$plugin\" update success" || - echo_message " \"$plugin\" update fail" + echo_ok " \"$plugin\" update success" || + echo_err " \"$plugin\" update fail" } - update_all() { + echo_ok "Updating all plugins!" + echo_ok "" local plugins="$(shared_get_tpm_plugins_list)" for plugin in $plugins; do local plugin_name="$(shared_plugin_name "$plugin")" @@ -44,31 +63,25 @@ update_all() { done } -handle_plugin_update() { - local arg="$1" - - if empty "$arg"; then - cancel - - elif if_all "$arg"; then - echo_message "Updating all plugins!" - echo_message "" - update_all - - elif plugin_already_installed "$arg"; then - update "$arg" - - else - display_message "It seems this plugin is not installed: $arg" - cancel - fi +update_plugins() { + local plugins="$*" + for plugin in $plugins; do + local plugin_name="$(shared_plugin_name "$plugin")" + if plugin_already_installed "$plugin_name"; then + update "$plugin_name" + else + echo_err "$plugin_name not installed!" + fi + done } main() { - local arg="$1" shared_set_tpm_path_constant - handle_plugin_update "$arg" - reload_tmux_environment - end_message + if [ "$1" == "all" ]; then + update_all + else + update_plugins "$*" + fi + exit_value_helper } -main "$1" +main "$*" diff --git a/scripts/update_plugin_prompt_handler.sh b/scripts/update_plugin_prompt_handler.sh new file mode 100755 index 0000000..9a7dc10 --- /dev/null +++ b/scripts/update_plugin_prompt_handler.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +if [ $# -eq 0]; then + exit 0 +fi + +source "$CURRENT_DIR/shared_functions.sh" + +main() { + "$CURRENT_DIR/update_plugin.sh" --tmux-echo "$*" + reload_tmux_environment + end_message +} +main "$*" diff --git a/tests/helpers/tpm.sh b/tests/helpers/tpm.sh index ecf29e7..436330e 100644 --- a/tests/helpers/tpm.sh +++ b/tests/helpers/tpm.sh @@ -7,7 +7,7 @@ script_run_helper() { local script="$1" local expected_output="$2" local expected_exit_code="${3:-0}" - "$script" | + $script | grep "$expected_output" >/dev/null 2>&1 && # grep -q flag quits the script early [ "${PIPESTATUS[0]}" -eq "$expected_exit_code" ] } diff --git a/tests/test_plugin_update.sh b/tests/test_plugin_update.sh index ac3d67a..93fced6 100755 --- a/tests/test_plugin_update.sh +++ b/tests/test_plugin_update.sh @@ -13,7 +13,9 @@ manually_install_the_plugin() { git clone --quiet https://github.com/tmux-plugins/tmux-example-plugin } -test_plugin_installation() { +# TMUX KEY-BINDING TESTS + +test_plugin_installation_via_tmux_key_binding() { set_tmux_conf_helper <<- HERE set -g @plugin "tmux-plugins/tmux-example-plugin" run-shell "$TPM_DIR/tpm" @@ -21,12 +23,33 @@ test_plugin_installation() { manually_install_the_plugin - # opens tmux and test it with `expect` "$CURRENT_DIR/expect_successful_update_of_all_plugins" || - fail_helper "Tmux 'update all plugins' fails" + fail_helper "[key-binding] 'update all plugins' fails" "$CURRENT_DIR/expect_successful_update_of_a_single_plugin" || - fail_helper "Tmux 'update single plugin' fails" + fail_helper "[key-binding] 'update single plugin' fails" + + teardown_helper +} + +# SCRIPT TESTS + +test_plugin_installation_via_script() { + set_tmux_conf_helper <<- HERE + set -g @plugin "tmux-plugins/tmux-example-plugin" + run-shell "$TPM_DIR/tpm" + HERE + + manually_install_the_plugin + + script_run_helper "$TPM_DIR/bin/update_plugins" 'usage' 1 || + fail_helper "[script] running update plugins without args should fail" + + script_run_helper "$TPM_DIR/bin/update_plugins tmux-example-plugin" '"tmux-example-plugin" update success' || + fail_helper "[script] plugin update fails" + + script_run_helper "$TPM_DIR/bin/update_plugins all" '"tmux-example-plugin" update success' || + fail_helper "[script] update all plugins fails" teardown_helper } diff --git a/tpm b/tpm index 2c77317..6887b94 100755 --- a/tpm +++ b/tpm @@ -36,7 +36,7 @@ set_tpm_key_bindings() { tmux bind-key "$install_key" run-shell "$BINDINGS_DIR/install_plugins" local update_key=$(get_tmux_option "$update_key_option" "$default_update_key") - tmux bind-key "$update_key" run-shell "$SCRIPTS_DIR/update_plugin_prompt.sh" + 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"