aboutsummaryrefslogtreecommitdiff
path: root/plugins/tpm/scripts
diff options
context:
space:
mode:
authorCody Hiar <codyfh@gmail.com>2017-02-13 09:54:26 -0700
committerCody Hiar <codyfh@gmail.com>2017-02-13 09:54:26 -0700
commit783ab79ab721593b0ac85ab4d8ea9b74fa5fe72e (patch)
tree03ac1d680ab401874ed95a78887c56d6eb268a61 /plugins/tpm/scripts
parent2eede125238144e9d37cf856aaaeda3c5de5aab3 (diff)
Adding tpm
Diffstat (limited to 'plugins/tpm/scripts')
-rwxr-xr-xplugins/tpm/scripts/check_tmux_version.sh78
-rwxr-xr-xplugins/tpm/scripts/clean_plugins.sh41
-rw-r--r--plugins/tpm/scripts/helpers/plugin_functions.sh86
-rw-r--r--plugins/tpm/scripts/helpers/shell_echo_functions.sh7
-rw-r--r--plugins/tpm/scripts/helpers/tmux_echo_functions.sh28
-rw-r--r--plugins/tpm/scripts/helpers/tmux_utils.sh3
-rw-r--r--plugins/tpm/scripts/helpers/utility.sh17
-rwxr-xr-xplugins/tpm/scripts/install_plugins.sh66
-rwxr-xr-xplugins/tpm/scripts/source_plugins.sh41
-rwxr-xr-xplugins/tpm/scripts/update_plugin.sh71
-rwxr-xr-xplugins/tpm/scripts/update_plugin_prompt_handler.sh18
-rw-r--r--plugins/tpm/scripts/variables.sh13
12 files changed, 469 insertions, 0 deletions
diff --git a/plugins/tpm/scripts/check_tmux_version.sh b/plugins/tpm/scripts/check_tmux_version.sh
new file mode 100755
index 0000000..b0aedec
--- /dev/null
+++ b/plugins/tpm/scripts/check_tmux_version.sh
@@ -0,0 +1,78 @@
+#!/usr/bin/env bash
+
+VERSION="$1"
+UNSUPPORTED_MSG="$2"
+
+get_tmux_option() {
+ local option=$1
+ local default_value=$2
+ local option_value=$(tmux show-option -gqv "$option")
+ if [ -z "$option_value" ]; then
+ echo "$default_value"
+ else
+ echo "$option_value"
+ fi
+}
+
+# Ensures a message is displayed for 5 seconds in tmux prompt.
+# Does not override the 'display-time' tmux option.
+display_message() {
+ local message="$1"
+
+ # display_duration defaults to 5 seconds, if not passed as an argument
+ if [ "$#" -eq 2 ]; then
+ local display_duration="$2"
+ else
+ local display_duration="5000"
+ fi
+
+ # saves user-set 'display-time' option
+ local saved_display_time=$(get_tmux_option "display-time" "750")
+
+ # sets message display time to 5 seconds
+ tmux set-option -gq display-time "$display_duration"
+
+ # displays message
+ tmux display-message "$message"
+
+ # restores original 'display-time' value
+ tmux set-option -gq display-time "$saved_display_time"
+}
+
+# this is used to get "clean" integer version number. Examples:
+# `tmux 1.9` => `19`
+# `1.9a` => `19`
+get_digits_from_string() {
+ local string="$1"
+ local only_digits="$(echo "$string" | tr -dC '[:digit:]')"
+ echo "$only_digits"
+}
+
+tmux_version_int() {
+ local tmux_version_string=$(tmux -V)
+ echo "$(get_digits_from_string "$tmux_version_string")"
+}
+
+unsupported_version_message() {
+ if [ -n "$UNSUPPORTED_MSG" ]; then
+ echo "$UNSUPPORTED_MSG"
+ else
+ echo "Error, Tmux version unsupported! Please install Tmux version $VERSION or greater!"
+ fi
+}
+
+exit_if_unsupported_version() {
+ local current_version="$1"
+ local supported_version="$2"
+ if [ "$current_version" -lt "$supported_version" ]; then
+ display_message "$(unsupported_version_message)"
+ exit 1
+ fi
+}
+
+main() {
+ local supported_version_int="$(get_digits_from_string "$VERSION")"
+ local current_version_int="$(tmux_version_int)"
+ exit_if_unsupported_version "$current_version_int" "$supported_version_int"
+}
+main
diff --git a/plugins/tpm/scripts/clean_plugins.sh b/plugins/tpm/scripts/clean_plugins.sh
new file mode 100755
index 0000000..a025524
--- /dev/null
+++ b/plugins/tpm/scripts/clean_plugins.sh
@@ -0,0 +1,41 @@
+#!/usr/bin/env bash
+
+CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+HELPERS_DIR="$CURRENT_DIR/helpers"
+
+source "$HELPERS_DIR/plugin_functions.sh"
+source "$HELPERS_DIR/utility.sh"
+
+if [ "$1" == "--tmux-echo" ]; then # tmux-specific echo functions
+ source "$HELPERS_DIR/tmux_echo_functions.sh"
+else # shell output functions
+ source "$HELPERS_DIR/shell_echo_functions.sh"
+fi
+
+clean_plugins() {
+ local plugins plugin plugin_directory
+ plugins="$(tpm_plugins_list_helper)"
+
+ for plugin_directory in "$(tpm_path)"/*; do
+ [ -d "${plugin_directory}" ] || continue
+ plugin="$(plugin_name_helper "${plugin_directory}")"
+ case "${plugins}" in
+ *"${plugin}"*) : ;;
+ *)
+ [ "${plugin}" = "tpm" ] && continue
+ echo_ok "Removing \"$plugin\""
+ rm -rf "${plugin_directory}" >/dev/null 2>&1
+ [ -d "${plugin_directory}" ] &&
+ echo_err " \"$plugin\" clean fail" ||
+ echo_ok " \"$plugin\" clean success"
+ ;;
+ esac
+ done
+}
+
+main() {
+ ensure_tpm_path_exists
+ clean_plugins
+ exit_value_helper
+}
+main
diff --git a/plugins/tpm/scripts/helpers/plugin_functions.sh b/plugins/tpm/scripts/helpers/plugin_functions.sh
new file mode 100644
index 0000000..d2778d5
--- /dev/null
+++ b/plugins/tpm/scripts/helpers/plugin_functions.sh
@@ -0,0 +1,86 @@
+# using @tpm_plugins is now deprecated in favor of using @plugin syntax
+tpm_plugins_variable_name="@tpm_plugins"
+
+# manually expanding tilde char or `$HOME` variable.
+_manual_expansion() {
+ local path="$1"
+ local expanded_tilde="${path/#\~/$HOME}"
+ echo "${expanded_tilde/#\$HOME/$HOME}"
+}
+
+_tpm_path() {
+ local string_path="$(tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=)/"
+ _manual_expansion "$string_path"
+}
+
+_CACHED_TPM_PATH="$(_tpm_path)"
+
+_tmux_conf_contents() {
+ cat /etc/tmux.conf ~/.tmux.conf 2>/dev/null
+ if [ "$1" == "full" ]; then # also output content from sourced files
+ local file
+ for file in $(_sourced_files); do
+ cat $(_manual_expansion "$file") 2>/dev/null
+ done
+ fi
+}
+
+# return files sourced from tmux config files
+_sourced_files() {
+ _tmux_conf_contents |
+ awk '/^[ \t]*source(-file)? +/ { gsub(/'\''/,""); gsub(/'\"'/,""); print $2 }'
+}
+
+# Want to be able to abort in certain cases
+trap "exit 1" TERM
+export TOP_PID=$$
+
+_fatal_error_abort() {
+ echo >&2 "Aborting."
+ kill -s TERM $TOP_PID
+}
+
+# PUBLIC FUNCTIONS BELOW
+
+tpm_path() {
+ if [ "$_CACHED_TPM_PATH" == "/" ]; then
+ echo >&2 "FATAL: Tmux Plugin Manager not configured in tmux.conf"
+ _fatal_error_abort
+ fi
+ echo "$_CACHED_TPM_PATH"
+}
+
+tpm_plugins_list_helper() {
+ # lists plugins from @tpm_plugins option
+ echo "$(tmux start-server\; show-option -gqv "$tpm_plugins_variable_name")"
+
+ # read set -g @plugin "tmux-plugins/tmux-example-plugin" entries
+ _tmux_conf_contents "full" |
+ awk '/^[ \t]*set(-option)? +-g +@plugin/ { gsub(/'\''/,""); gsub(/'\"'/,""); print $4 }'
+}
+
+# Allowed plugin name formats:
+# 1. "git://github.com/user/plugin_name.git"
+# 2. "user/plugin_name"
+plugin_name_helper() {
+ local plugin="$1"
+ # get only the part after the last slash, e.g. "plugin_name.git"
+ local plugin_basename="$(basename "$plugin")"
+ # remove ".git" extension (if it exists) to get only "plugin_name"
+ local plugin_name="${plugin_basename%.git}"
+ echo "$plugin_name"
+}
+
+plugin_path_helper() {
+ local plugin="$1"
+ local plugin_name="$(plugin_name_helper "$plugin")"
+ echo "$(tpm_path)${plugin_name}/"
+}
+
+plugin_already_installed() {
+ local plugin="$1"
+ local plugin_path="$(plugin_path_helper "$plugin")"
+ [ -d "$plugin_path" ] &&
+ cd "$plugin_path" &&
+ git remote >/dev/null 2>&1
+}
diff --git a/plugins/tpm/scripts/helpers/shell_echo_functions.sh b/plugins/tpm/scripts/helpers/shell_echo_functions.sh
new file mode 100644
index 0000000..ecaa37e
--- /dev/null
+++ b/plugins/tpm/scripts/helpers/shell_echo_functions.sh
@@ -0,0 +1,7 @@
+echo_ok() {
+ echo "$*"
+}
+
+echo_err() {
+ fail_helper "$*"
+}
diff --git a/plugins/tpm/scripts/helpers/tmux_echo_functions.sh b/plugins/tpm/scripts/helpers/tmux_echo_functions.sh
new file mode 100644
index 0000000..7a6ef0a
--- /dev/null
+++ b/plugins/tpm/scripts/helpers/tmux_echo_functions.sh
@@ -0,0 +1,28 @@
+_has_emacs_mode_keys() {
+ $(tmux show -gw mode-keys | grep -q emacs)
+}
+
+tmux_echo() {
+ local message="$1"
+ tmux run-shell "echo '$message'"
+}
+
+echo_ok() {
+ tmux_echo "$*"
+}
+
+echo_err() {
+ tmux_echo "$*"
+}
+
+end_message() {
+ if _has_emacs_mode_keys; then
+ local continue_key="ESCAPE"
+ else
+ local continue_key="ENTER"
+ fi
+ tmux_echo ""
+ tmux_echo "TMUX environment reloaded."
+ tmux_echo ""
+ tmux_echo "Done, press $continue_key to continue."
+}
diff --git a/plugins/tpm/scripts/helpers/tmux_utils.sh b/plugins/tpm/scripts/helpers/tmux_utils.sh
new file mode 100644
index 0000000..e39946a
--- /dev/null
+++ b/plugins/tpm/scripts/helpers/tmux_utils.sh
@@ -0,0 +1,3 @@
+reload_tmux_environment() {
+ tmux source-file ~/.tmux.conf >/dev/null 2>&1
+}
diff --git a/plugins/tpm/scripts/helpers/utility.sh b/plugins/tpm/scripts/helpers/utility.sh
new file mode 100644
index 0000000..de6eb35
--- /dev/null
+++ b/plugins/tpm/scripts/helpers/utility.sh
@@ -0,0 +1,17 @@
+ensure_tpm_path_exists() {
+ mkdir -p "$(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/plugins/tpm/scripts/install_plugins.sh b/plugins/tpm/scripts/install_plugins.sh
new file mode 100755
index 0000000..7958ab5
--- /dev/null
+++ b/plugins/tpm/scripts/install_plugins.sh
@@ -0,0 +1,66 @@
+#!/usr/bin/env bash
+
+CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+HELPERS_DIR="$CURRENT_DIR/helpers"
+
+source "$HELPERS_DIR/plugin_functions.sh"
+source "$HELPERS_DIR/utility.sh"
+
+if [ "$1" == "--tmux-echo" ]; then # tmux-specific echo functions
+ source "$HELPERS_DIR/tmux_echo_functions.sh"
+else # shell output functions
+ source "$HELPERS_DIR/shell_echo_functions.sh"
+fi
+
+clone() {
+ local plugin="$1"
+ cd "$(tpm_path)" &&
+ GIT_TERMINAL_PROMPT=0 git clone --recursive "$plugin" >/dev/null 2>&1
+}
+
+# tries cloning:
+# 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"
+ clone "$plugin" ||
+ clone "https://git::@github.com/$plugin"
+}
+
+# clone plugin and produce output
+install_plugin() {
+ local plugin="$1"
+ local plugin_name="$(plugin_name_helper "$plugin")"
+
+ if plugin_already_installed "$plugin"; then
+ echo_ok "Already installed \"$plugin_name\""
+ else
+ echo_ok "Installing \"$plugin_name\""
+ clone_plugin "$plugin" &&
+ echo_ok " \"$plugin_name\" download success" ||
+ echo_err " \"$plugin_name\" download fail"
+ fi
+}
+
+install_plugins() {
+ local plugins="$(tpm_plugins_list_helper)"
+ for plugin in $plugins; do
+ install_plugin "$plugin"
+ done
+}
+
+verify_tpm_path_permissions() {
+ local path="$(tpm_path)"
+ # check the write permission flag for all users to ensure
+ # that we have proper access
+ [ -w "$path" ] ||
+ echo_err "$path is not writable!"
+}
+
+main() {
+ ensure_tpm_path_exists
+ verify_tpm_path_permissions
+ install_plugins
+ exit_value_helper
+}
+main
diff --git a/plugins/tpm/scripts/source_plugins.sh b/plugins/tpm/scripts/source_plugins.sh
new file mode 100755
index 0000000..bb79c26
--- /dev/null
+++ b/plugins/tpm/scripts/source_plugins.sh
@@ -0,0 +1,41 @@
+#!/usr/bin/env bash
+
+CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+HELPERS_DIR="$CURRENT_DIR/helpers"
+
+source "$HELPERS_DIR/plugin_functions.sh"
+
+plugin_dir_exists() {
+ [ -d "$1" ]
+}
+
+# Runs all *.tmux files from the plugin directory.
+# Files are ran as executables.
+# No errors if the plugin dir does not exist.
+silently_source_all_tmux_files() {
+ local plugin_path="$1"
+ local plugin_tmux_files="$plugin_path*.tmux"
+ if plugin_dir_exists "$plugin_path"; then
+ for tmux_file in $plugin_tmux_files; do
+ # if the glob didn't find any files this will be the
+ # unexpanded glob which obviously doesn't exist
+ [ -f "$tmux_file" ] || continue
+ # runs *.tmux file as an executable
+ $tmux_file >/dev/null 2>&1
+ done
+ fi
+}
+
+source_plugins() {
+ local plugin plugin_path
+ local plugins="$(tpm_plugins_list_helper)"
+ for plugin in $plugins; do
+ plugin_path="$(plugin_path_helper "$plugin")"
+ silently_source_all_tmux_files "$plugin_path"
+ done
+}
+
+main() {
+ source_plugins
+}
+main
diff --git a/plugins/tpm/scripts/update_plugin.sh b/plugins/tpm/scripts/update_plugin.sh
new file mode 100755
index 0000000..7d856ee
--- /dev/null
+++ b/plugins/tpm/scripts/update_plugin.sh
@@ -0,0 +1,71 @@
+#!/usr/bin/env bash
+
+# this script handles core logic of updating plugins
+
+CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+HELPERS_DIR="$CURRENT_DIR/helpers"
+
+source "$HELPERS_DIR/plugin_functions.sh"
+source "$HELPERS_DIR/utility.sh"
+
+if [ "$1" == "--tmux-echo" ]; then # tmux-specific echo functions
+ source "$HELPERS_DIR/tmux_echo_functions.sh"
+else # shell output functions
+ source "$HELPERS_DIR/shell_echo_functions.sh"
+fi
+
+# from now on ignore first script argument
+shift
+
+pull_changes() {
+ local plugin="$1"
+ local plugin_path="$(plugin_path_helper "$plugin")"
+ cd "$plugin_path" &&
+ GIT_TERMINAL_PROMPT=0 git pull &&
+ GIT_TERMINAL_PROMPT=0 git submodule update --init --recursive
+}
+
+update() {
+ local plugin="$1"
+ $(pull_changes "$plugin" > /dev/null 2>&1) &&
+ echo_ok " \"$plugin\" update success" ||
+ echo_err " \"$plugin\" update fail"
+}
+
+update_all() {
+ echo_ok "Updating all plugins!"
+ echo_ok ""
+ local plugins="$(tpm_plugins_list_helper)"
+ for plugin in $plugins; do
+ local plugin_name="$(plugin_name_helper "$plugin")"
+ # updating only installed plugins
+ if plugin_already_installed "$plugin_name"; then
+ update "$plugin_name" &
+ fi
+ done
+ wait
+}
+
+update_plugins() {
+ local plugins="$*"
+ for plugin in $plugins; do
+ local plugin_name="$(plugin_name_helper "$plugin")"
+ if plugin_already_installed "$plugin_name"; then
+ update "$plugin_name" &
+ else
+ echo_err "$plugin_name not installed!" &
+ fi
+ done
+ wait
+}
+
+main() {
+ ensure_tpm_path_exists
+ if [ "$1" == "all" ]; then
+ update_all
+ else
+ update_plugins "$*"
+ fi
+ exit_value_helper
+}
+main "$*"
diff --git a/plugins/tpm/scripts/update_plugin_prompt_handler.sh b/plugins/tpm/scripts/update_plugin_prompt_handler.sh
new file mode 100755
index 0000000..5e1f7d9
--- /dev/null
+++ b/plugins/tpm/scripts/update_plugin_prompt_handler.sh
@@ -0,0 +1,18 @@
+#!/usr/bin/env bash
+
+CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+HELPERS_DIR="$CURRENT_DIR/helpers"
+
+if [ $# -eq 0 ]; then
+ exit 0
+fi
+
+source "$HELPERS_DIR/tmux_echo_functions.sh"
+source "$HELPERS_DIR/tmux_utils.sh"
+
+main() {
+ "$CURRENT_DIR/update_plugin.sh" --tmux-echo "$*"
+ reload_tmux_environment
+ end_message
+}
+main "$*"
diff --git a/plugins/tpm/scripts/variables.sh b/plugins/tpm/scripts/variables.sh
new file mode 100644
index 0000000..5601a86
--- /dev/null
+++ b/plugins/tpm/scripts/variables.sh
@@ -0,0 +1,13 @@
+install_key_option="@tpm-install"
+default_install_key="I"
+
+update_key_option="@tpm-update"
+default_update_key="U"
+
+clean_key_option="@tpm-clean"
+default_clean_key="M-u"
+
+SUPPORTED_TMUX_VERSION="1.9"
+
+DEFAULT_TPM_ENV_VAR_NAME="TMUX_PLUGIN_MANAGER_PATH"
+DEFAULT_TPM_PATH="$HOME/.tmux/plugins/"