First commit

This commit is contained in:
Bruno Sutic 2014-05-19 00:35:55 +02:00
commit 89b5c18196
6 changed files with 215 additions and 0 deletions

6
scripts/key_binding.sh Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# (I)nstalls all plugins
tmux bind-key I run-shell "$CURRENT_DIR/sync_plugins.sh 2>&1 1>&/dev/null"

22
scripts/set_tpm_path.sh Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
default_tpm_path="$HOME/.tmux/plugins/"
tpm_path_set() {
tmux show-environment -g TMUX_PLUGIN_MANAGER_PATH
}
set_default_tpm_path() {
tmux set-environment -g TMUX_PLUGIN_MANAGER_PATH "$default_tpm_path"
}
ensure_tpm_path() {
if ! tpm_path_set; then
set_default_tpm_path
fi
}
main() {
ensure_tpm_path
}
main

View File

@ -0,0 +1,31 @@
# shared functions and constants
tpm_plugins_variable_name="@tpm_plugins"
SHARED_TPM_PATH=""
# sets a "global variable" for the current file
shared_set_tpm_path_constant() {
SHARED_TPM_PATH=$(tmux show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=)
}
shared_get_tpm_plugins_list() {
tmux show-option -gqv "$tpm_plugins_variable_name"
}
# Allowed plugin name formats:
# 1. "git://github.com/user/plugin_name.git"
# 2. "user/plugin_name"
shared_plugin_name() {
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
}
shared_plugin_path() {
local plugin=$1
local plugin_name=$(shared_plugin_name "$plugin")
echo "$SHARED_TPM_PATH$plugin_name/"
}

34
scripts/source_plugins.sh Executable file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/shared_functions.sh"
# Sources all *.tmux files from the plugin directory.
# Files are sourced as tmux config files, not as shell scripts!
# No errors if the plugin dir does not exist.
silently_source_all_tmux_files() {
local plugin_path=$1
for tmux_file in $plugin_path/*.tmux; do
tmux run-shell "source-file $tmux_file 2>&1 1>&/dev/null"
done
}
source_plugin() {
local plugin=$1
local plugin_path=$(shared_plugin_path "$plugin")
silently_source_all_tmux_files "$plugin_path"
}
source_plugins() {
local plugins=$(shared_get_tpm_plugins_list)
for plugin in $plugins; do
source_plugin "$plugin"
done
}
main() {
shared_set_tpm_path_constant
source_plugins
}
main

87
scripts/sync_plugins.sh Executable file
View File

@ -0,0 +1,87 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/shared_functions.sh"
# TMUX messaging is weird. You only get a nice clean pane if you do it with
# `run-shell` command.
display_message() {
local message=$1
tmux run-shell "echo '$message'"
}
end_message() {
display_message ""
display_message "TMUX environment reloaded."
display_message ""
display_message "Done, press ENTER to continue."
}
clone() {
local plugin=$1
cd $SHARED_TPM_PATH &&
git clone --recursive $plugin
}
# 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://github.com/$plugin"
}
plugin_already_cloned() {
local plugin=$1
local plugin_path=$(shared_plugin_path "$plugin")
cd $plugin_path &&
git remote
}
pull_changes() {
local plugin=$1
local plugin_path=$(shared_plugin_path "$plugin")
cd $plugin_path &&
git pull &&
git submodule update --init --recursive
}
# pull new changes or clone plugin
sync_plugin() {
local plugin=$1
if plugin_already_cloned "$plugin"; then
# plugin is already cloned - update it
display_message "Updating $plugin"
pull_changes "$plugin" &&
display_message " $plugin update success" ||
display_message " $plugin update fail"
else
# plugin wasn't cloned so far - clone it
display_message "Downloading $plugin"
clone_plugin "$plugin" &&
display_message " $plugin download success" ||
display_message " $plugin download fail"
fi
}
sync_plugins() {
local plugins=$(shared_get_tpm_plugins_list)
for plugin in $plugins; do
sync_plugin "$plugin"
done
}
reload_tmux_environment() {
tmux source-file ~/.tmux.conf 2>&1 1>&/dev/null
}
main() {
reload_tmux_environment
shared_set_tpm_path_constant
sync_plugins
reload_tmux_environment
end_message
}
main

35
tpm Executable file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Ensures TMUX_PLUGIN_MANAGER_PATH global env variable is set.
# Default tpm path is "$HOME/.tmux/plugins/". That's where all the plugins are
# downloaded.
#
# Put this in `.tmux.conf` to override the default:
# `set-environment -g TMUX_PLUGIN_MANAGER_PATH "/some/other/path/"`
set_tpm_path() {
$CURRENT_DIR/scripts/set_tpm_path.sh 2>&1 1>&/dev/null
}
# 1. Fetches plugin names from `@tpm_plugins` user variable.
# 2. Creates full plugin path
# 3. Sources all *.tmux files from each of the plugin directories
# - no errors raised if directory does not exist
# Files are sourced as tmux config files, not as shell scripts!
source_plugins() {
$CURRENT_DIR/scripts/source_plugins.sh 2>&1 1>&/dev/null
}
# Defines key binding:
# prefix + I - downloads TPM plugins and reloads TMUX environment.
set_tpm_key_binding() {
$CURRENT_DIR/scripts/key_binding.sh
}
main() {
set_tpm_path
set_tpm_key_binding
source_plugins
}
main