]> arthur.barton.de Git - ax-zsh.git/commitdiff
Implement plugin loading from GitHub
authorAlexander Barton <alex@barton.de>
Fri, 6 May 2016 10:42:00 +0000 (12:42 +0200)
committerAlexander Barton <alex@barton.de>
Fri, 6 May 2016 10:42:28 +0000 (12:42 +0200)
Simply use "<user-name>/<project-name>" 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
ax.zsh
bin/axzshctl

index a9813ace5b330fff368281daf5bda3cf8e061e1b..ccaed662a3868ffbf7a78987a7c03a5777d23005 100644 (file)
@@ -2,3 +2,4 @@ active_plugins/
 custom_plugins/
 active_theme
 custom_themes/
+repos/
diff --git a/ax.zsh b/ax.zsh
index 59e172d46197f64a770782a08777d1dbd3e02072..ea16c4e34e892a949f355ff592ccc246c0b1b267 100644 (file)
--- 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
index 74bc3f55734ea8155848bd7007df48338bc2f391..9372a33d000f11f9aa719e77f8b84d33493ca6eb 100755 (executable)
@@ -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