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
- 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

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
# 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

View File

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

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 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" ]
}

View File

@ -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
}

2
tpm
View File

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