Enable updating plugins via cli executable

This commit is contained in:
Bruno Sutic 2015-08-02 00:41:06 +02:00
parent ce902f906a
commit 7ff8de38a4
No known key found for this signature in database
GPG Key ID: 66D96E4F2F7EF26C
8 changed files with 127 additions and 47 deletions

View File

@ -7,6 +7,7 @@
- improved test runner function - improved test runner function
- switch to using [tmux-test](https://github.com/tmux-plugins/tmux-test) - switch to using [tmux-test](https://github.com/tmux-plugins/tmux-test)
framework framework
- add `bin/update_plugins` cli executable script
### v2.0.0, 2015-07-07 ### v2.0.0, 2015-07-07
- enable overriding default key bindings - enable overriding default key bindings

24
bin/update_plugins Executable file
View File

@ -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 "$*"

View File

@ -1,12 +1,16 @@
#!/usr/bin/env bash #!/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 # - shows a list of installed plugins
# - starts a prompt to enter the name of the plugin that will be updated # - starts a prompt to enter the name of the plugin that will be updated
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 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() { display_plugin_update_list() {
local plugins="$(shared_get_tpm_plugins_list)" local plugins="$(shared_get_tpm_plugins_list)"
@ -31,10 +35,9 @@ display_plugin_update_list() {
update_plugin_prompt() { update_plugin_prompt() {
tmux command-prompt -p 'plugin update:' " \ tmux command-prompt -p 'plugin update:' " \
send-keys C-c; \ send-keys C-c; \
run-shell '$CURRENT_DIR/update_plugin.sh %1'" run-shell '$SCRIPTS_DIR/update_plugin_prompt_handler.sh %1'"
} }
main() { main() {
reload_tmux_environment reload_tmux_environment
shared_set_tpm_path_constant shared_set_tpm_path_constant

View File

@ -1,39 +1,58 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# this script handles core logic of updating plugins
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/shared_functions.sh" source "$CURRENT_DIR/shared_functions.sh"
empty() { TMUX_ECHO_FLAG="$1"
[ -z "$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() { if use_tmux_echo; then
[ "$1" == "all" ] # use tmux specific echo-ing
} echo_ok() {
echo_message "$*"
}
cancel() { echo_err() {
exit 0 echo_message "$*"
} }
else
echo_ok() {
echo "$*"
}
echo_err() {
fail_helper "$*"
}
fi
pull_changes() { pull_changes() {
local plugin="$1" local plugin="$1"
local plugin_path=$(shared_plugin_path "$plugin") local plugin_path="$(shared_plugin_path "$plugin")"
cd $plugin_path && cd "$plugin_path" &&
GIT_TERMINAL_PROMPT=0 git pull && GIT_TERMINAL_PROMPT=0 git pull &&
GIT_TERMINAL_PROMPT=0 git submodule update --init --recursive GIT_TERMINAL_PROMPT=0 git submodule update --init --recursive
} }
update() { update() {
local plugin="$1" local plugin="$1"
echo_message "Updating \"$plugin\"" echo_ok "Updating \"$plugin\""
$(pull_changes "$plugin" > /dev/null 2>&1) && $(pull_changes "$plugin" > /dev/null 2>&1) &&
echo_message " \"$plugin\" update success" || echo_ok " \"$plugin\" update success" ||
echo_message " \"$plugin\" update fail" echo_err " \"$plugin\" update fail"
} }
update_all() { update_all() {
echo_ok "Updating all plugins!"
echo_ok ""
local plugins="$(shared_get_tpm_plugins_list)" local plugins="$(shared_get_tpm_plugins_list)"
for plugin in $plugins; do for plugin in $plugins; do
local plugin_name="$(shared_plugin_name "$plugin")" local plugin_name="$(shared_plugin_name "$plugin")"
@ -44,31 +63,25 @@ update_all() {
done done
} }
handle_plugin_update() { update_plugins() {
local arg="$1" local plugins="$*"
for plugin in $plugins; do
if empty "$arg"; then local plugin_name="$(shared_plugin_name "$plugin")"
cancel if plugin_already_installed "$plugin_name"; then
update "$plugin_name"
elif if_all "$arg"; then else
echo_message "Updating all plugins!" echo_err "$plugin_name not installed!"
echo_message "" fi
update_all done
elif plugin_already_installed "$arg"; then
update "$arg"
else
display_message "It seems this plugin is not installed: $arg"
cancel
fi
} }
main() { main() {
local arg="$1"
shared_set_tpm_path_constant shared_set_tpm_path_constant
handle_plugin_update "$arg" if [ "$1" == "all" ]; then
reload_tmux_environment update_all
end_message else
update_plugins "$*"
fi
exit_value_helper
} }
main "$1" main "$*"

View File

@ -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 "$*"

View File

@ -7,7 +7,7 @@ script_run_helper() {
local script="$1" local script="$1"
local expected_output="$2" local expected_output="$2"
local expected_exit_code="${3:-0}" local expected_exit_code="${3:-0}"
"$script" | $script |
grep "$expected_output" >/dev/null 2>&1 && # grep -q flag quits the script early grep "$expected_output" >/dev/null 2>&1 && # grep -q flag quits the script early
[ "${PIPESTATUS[0]}" -eq "$expected_exit_code" ] [ "${PIPESTATUS[0]}" -eq "$expected_exit_code" ]
} }

View File

@ -13,7 +13,9 @@ manually_install_the_plugin() {
git clone --quiet https://github.com/tmux-plugins/tmux-example-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_tmux_conf_helper <<- HERE
set -g @plugin "tmux-plugins/tmux-example-plugin" set -g @plugin "tmux-plugins/tmux-example-plugin"
run-shell "$TPM_DIR/tpm" run-shell "$TPM_DIR/tpm"
@ -21,12 +23,33 @@ test_plugin_installation() {
manually_install_the_plugin manually_install_the_plugin
# opens tmux and test it with `expect`
"$CURRENT_DIR/expect_successful_update_of_all_plugins" || "$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" || "$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 teardown_helper
} }

2
tpm
View File

@ -36,7 +36,7 @@ set_tpm_key_bindings() {
tmux bind-key "$install_key" run-shell "$BINDINGS_DIR/install_plugins" tmux bind-key "$install_key" run-shell "$BINDINGS_DIR/install_plugins"
local update_key=$(get_tmux_option "$update_key_option" "$default_update_key") 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") 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 "$SCRIPTS_DIR/clean_plugins.sh"