From 40ba8e58dcc3007ef6b1d8c38dc74c2bbf0d0447 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Wed, 29 Jul 2015 23:36:49 +0200 Subject: [PATCH] Enable installing plugins via cli executable --- bin/install_plugins | 9 +++++++ bindings/install_plugins | 14 +++++++++++ scripts/install_plugins.sh | 49 +++++++++++++++++++++++++++---------- scripts/shared_functions.sh | 18 ++++++++++++-- tpm | 3 ++- 5 files changed, 77 insertions(+), 16 deletions(-) create mode 100755 bin/install_plugins create mode 100755 bindings/install_plugins diff --git a/bin/install_plugins b/bin/install_plugins new file mode 100755 index 0000000..9c45152 --- /dev/null +++ b/bin/install_plugins @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +SCRIPTS_DIR="$CURRENT_DIR/../scripts" + +main() { + "$SCRIPTS_DIR/install_plugins.sh" # has correct exit code +} +main diff --git a/bindings/install_plugins b/bindings/install_plugins new file mode 100755 index 0000000..3ae21b8 --- /dev/null +++ b/bindings/install_plugins @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +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/install_plugins.sh" --tmux-echo >/dev/null 2>&1 + reload_tmux_environment + end_message +} +main diff --git a/scripts/install_plugins.sh b/scripts/install_plugins.sh index 0dfa163..c6024fc 100755 --- a/scripts/install_plugins.sh +++ b/scripts/install_plugins.sh @@ -4,9 +4,36 @@ 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 + clone() { - local plugin=$1 - cd $SHARED_TPM_PATH && + local plugin="$1" + cd "$SHARED_TPM_PATH" && GIT_TERMINAL_PROMPT=0 git clone --recursive $plugin } @@ -14,25 +41,23 @@ clone() { # 1. plugin name directly - works if it's a valid git url # 2. expands the plugin name to point to a github repo and tries cloning again clone_plugin() { - local plugin=$1 + local plugin="$1" clone "$plugin" || clone "https://git::@github.com/$plugin" } -# pull new changes or clone plugin +# clone plugin and produce output install_plugin() { local plugin="$1" local plugin_name="$(shared_plugin_name "$plugin")" if plugin_already_installed "$plugin"; then - # plugin is already installed - echo_message "Already installed \"$plugin_name\"" + echo_ok "Already installed \"$plugin_name\"" else - # plugin wasn't cloned so far - clone it - echo_message "Installing \"$plugin_name\"" + echo_ok "Installing \"$plugin_name\"" clone_plugin "$plugin" && - echo_message " \"$plugin_name\" download success" || - echo_message " \"$plugin_name\" download fail" + echo_ok " \"$plugin_name\" download success" || + echo_err " \"$plugin_name\" download fail" fi } @@ -50,12 +75,10 @@ verify_tpm_path_permissions() { } main() { - reload_tmux_environment shared_set_tpm_path_constant ensure_tpm_path_exists verify_tpm_path_permissions install_plugins - reload_tmux_environment - end_message + exit_value_helper } main diff --git a/scripts/shared_functions.sh b/scripts/shared_functions.sh index 7053df4..ef3ef7e 100644 --- a/scripts/shared_functions.sh +++ b/scripts/shared_functions.sh @@ -6,7 +6,7 @@ SHARED_TPM_PATH="" # sets a "global variable" for the current file shared_set_tpm_path_constant() { - local string_path="$(tmux show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=)/" + local string_path="$(tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=)/" # manually expanding tilde or `$HOME` variable. string_path="${string_path/#\~/$HOME}" SHARED_TPM_PATH="${string_path/#\$HOME/$HOME}" @@ -18,7 +18,7 @@ _tmux_conf_contents() { shared_get_tpm_plugins_list() { # DEPRECATED: lists plugins from @tpm_plugins option - echo "$(tmux show-option -gqv "$tpm_plugins_variable_name")" + echo "$(tmux start-server\; show-option -gqv "$tpm_plugins_variable_name")" # read set -g @plugin "tmux-plugins/tmux-example-plugin" entries _tmux_conf_contents | @@ -96,3 +96,17 @@ display_message() { ensure_tpm_path_exists() { mkdir -p "$SHARED_TPM_PATH" } + +fail_helper() { + local message="$1" + echo "$message" >&2 + FAIL="true" +} + +exit_value_helper() { + if [ "$FAIL" == "true" ]; then + exit 1 + else + exit 0 + fi +} diff --git a/tpm b/tpm index deaab37..d9f9bba 100755 --- a/tpm +++ b/tpm @@ -1,6 +1,7 @@ #!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +BINDINGS_DIR="$CURRENT_DIR/bindings" SUPPORTED_TMUX_VERSION="1.9" @@ -31,7 +32,7 @@ source_plugins() { # prefix + alt + u - remove unused TPM plugins and reloads TMUX environment set_tpm_key_bindings() { local install_key=$(get_tmux_option "$install_key_option" "$default_install_key") - tmux bind-key "$install_key" run-shell "$CURRENT_DIR/scripts/install_plugins.sh >/dev/null 2>&1" + 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 "$CURRENT_DIR/scripts/update_plugin_prompt.sh"