From dcd4f625804c30e7444ec0531f6e57adfcfe169e Mon Sep 17 00:00:00 2001 From: Alexander Barton Date: Fri, 6 May 2016 12:42:00 +0200 Subject: [PATCH] Implement plugin loading from GitHub Simply use "/" als plugin name when calling "axzshctl enable-plugin" and "axzshctl disable-plugin". Example: axzshctl enable-plugin zsh-users/zsh-autosuggestions The remote repository is cloned into the local "$AXZSH/repos" directory and deleted by "axzshctl disable-plugin" when it is no longer needed. "axzshctl upgrade" upgrades all remote repositories as well. --- .gitignore | 1 + ax.zsh | 2 ++ bin/axzshctl | 47 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index a9813ac..ccaed66 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ active_plugins/ custom_plugins/ active_theme custom_themes/ +repos/ diff --git a/ax.zsh b/ax.zsh index 59e172d..ea16c4e 100644 --- a/ax.zsh +++ b/ax.zsh @@ -13,6 +13,8 @@ function axzsh_load_plugin { [[ -z "$2" ]] && type="zshrc" || type="$2" fname="$dname/$plugin.$type" + [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-) + if [[ ! -d "$dname" ]]; then # Plugin not found! if [[ -n "$AXZSH_DEBUG" ]]; then diff --git a/bin/axzshctl b/bin/axzshctl index 74bc3f5..9372a33 100755 --- a/bin/axzshctl +++ b/bin/axzshctl @@ -46,19 +46,35 @@ function Usage { exit 2 } +function NormalizedPluginName { + echo "$1" | sed -e 's|/|#|g' +} + function EnablePlugin { + local plugin=$(NormalizedPluginName "$1") local dir="$AXZSH/active_plugins" - if [[ -h "$dir/$1" ]]; then + if [[ -h "$dir/$plugin" ]]; then ax_msg 1 "Plugin \"$1\" already active!" return 1 fi + if [[ "$1" =~ "/" ]]; then + # GitHub plugin + mkdir -p "$AXZSH/repos" + if [[ ! -e "$AXZSH/repos/$plugin" ]]; then + ax_msg - "Cloning plugin from GitHub ..." + git clone "https://github.com/$1.git" \ + "$AXZSH/repos/$plugin" + fi + fi + for dname ( "$plugin:A" "$AXZSH_PLUGIN_D/$plugin" "$ZSH_CUSTOM/$plugin" "$AXZSH/custom_plugins/$plugin" + "$AXZSH/repos/$plugin" "$AXZSH/plugins/$plugin" "$AXZSH/default_plugins/$plugin" "$AXZSH/core/$plugin" @@ -77,15 +93,17 @@ function EnablePlugin { } function DisablePlugin { + local plugin=$(NormalizedPluginName "$1") local dir="$AXZSH/active_plugins" - if [[ ! -h "$dir/$1" ]]; then + if [[ ! -h "$dir/$plugin" ]]; then ax_msg 1 "Plugin \"$1\" not active?" return 1 fi - rm -v "$dir/$1" - return $? + rm -v "$dir/$plugin"; r=$? + [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin" + return $r } function ResetPlugins { @@ -153,6 +171,26 @@ function UpgradeAXZSH { ( cd "$AXZSH" && git pull --ff-only ) } +function UpgradeForeignPlugins { + if [[ $+commands[git] -eq 0 ]]; then + ax_msg 2 "The git(1) command is not available!" + return 1 + fi + + for dir ($AXZSH/repos/*(N)); do + name=$(basename "$dir" | sed -e 's|#|/|g') + if [ -d "$dir/.git" ]; then + ax_msg - "Upgrading \"$name\" [git] ..." + ( + cd "$dir" + git pull --ff-only || ax_msg 2 "Pull failed!" + ) + else + ax_msg 2 "Unknown repository type!" + fi + done +} + NAME="$0:t" [[ $# -gt 0 ]] || Usage @@ -194,6 +232,7 @@ case "$cmd" in "upgrade") [[ $# -eq 0 ]] || Usage UpgradeAXZSH + UpgradeForeignPlugins ;; *) Usage -- 2.39.2