]> arthur.barton.de Git - ax-zsh.git/blobdiff - bin/axzshctl
axzshctl: Handle themes on "disable-plugin" & clean up repositories
[ax-zsh.git] / bin / axzshctl
index 0c2fb0090c2f5bb2d9aab9fff74d59002ea31224..cd463e3ded886e9f5b4ab08bed5925d00b8e9e22 100755 (executable)
@@ -1,7 +1,7 @@
-#!/bin/zsh
+#!/usr/bin/env zsh
 #
 # AX-ZSH: Alex' Modular ZSH Configuration
-# Copyright (c) 2015-2016 Alexander Barton <alex@barton.de>
+# Copyright (c) 2015-2017 Alexander Barton <alex@barton.de>
 #
 
 # Include "ax-common.sh", if available:
@@ -12,50 +12,91 @@ for dir ("$HOME/lib" "$HOME/.ax" /usr/local /opt/ax /usr); do
 done
 if [[ -z "$ax_common_sourced" ]]; then
        function ax_msg {
-               case "$1" in
-                 "1"|"2") echo -n "! "; ;;
-                 *) echo -n "* "; ;;
-               esac
                shift
                echo "$@"
        }
+       function ax_error {
+               ax_msg 2 "$@" >&2
+       }
 fi
 unset dir ax_common ax_common_sourced
 
 function Usage {
        echo "Usage: $NAME <command> [...]"
        echo
+       echo "  enable"
+       echo "    Enable AX-ZSH altogether."
+       echo "  disable"
+       echo "    Disable AX-ZSH altogether."
+       echo
        echo "  enable-plugin <name|directory> [<name|directory> [...]]"
        echo "    Enable plugin(s)."
-       echo
        echo "  disable-plugin <name> [<name> [...]]"
        echo "    Disable plugin(s)."
+       echo "  list-enabled"
+       echo "    List enabled plugins."
        echo
        echo "  reset-plugins"
        echo "    Reset active plugins to the default set."
-       echo
        echo "  enable-default-plugins"
        echo "    Enable all default plugins."
+       echo "  check-plugins"
+       echo "    Detect plugins which are \"useful\" on this system."
        echo
        echo "  set-theme <name>|-"
        echo "    Set active theme to <name>, or to the default."
        echo
        echo "  upgrade"
        echo "    Upgrade AX-ZSH installation (requires Git)."
+       echo "  update-caches"
+       echo "    Force rebuild of all cache files."
        echo
-       exit 2
+       exit 0
+}
+
+function UpdatePluginCache {
+       [[ -r "$AXZSH/cache" ]] || return 0
+
+       ax_msg - "Updating plugin cache ..."
+       rm -rf \
+               $AXZSH/cache/zlogin.cache \
+               $AXZSH/cache/zlogout.cache \
+               $AXZSH/cache/zprofile.cache \
+               $AXZSH/cache/zshrc.cache \
+               || return 1
+       echo "Regenerating cache files ..."
+       zsh -ilc '' >/dev/null
 }
 
 function NormalizedPluginName {
        if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
-               echo "$1" | sed -e 's|/|#|g'
+               echo "${1:gs/\//#}"
        elif [[ "$1" =~ "/" ]]; then
-               basename "$1"
+               echo "${1:t}"
        else
                echo "$1"
        fi
 }
 
+function EnableAXZSH {
+       for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
+               ln -sv "$AXZSH/ax.zsh" "$f" \
+                       || ax_error "Failed to create symbolic link for \"$f\"!"
+       done
+}
+
+function DisableAXZSH {
+       for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
+               if [ -h "$f" ]; then
+                       rm -v "$f" || ax_msg 2 "Failed to remove \"$f\"!"
+               elif [ -e "$f" ]; then
+                       ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
+               else
+                       ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
+               fi
+       done
+}
+
 function EnablePlugin {
        local plugin=$(NormalizedPluginName "$1")
        local dir="$AXZSH/active_plugins"
@@ -69,10 +110,20 @@ function EnablePlugin {
                # 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" \
+                       ax_msg - "Cloning module from GitHub ..."
+                       git clone --depth=1 "https://github.com/$1.git" \
                         "$AXZSH/repos/$plugin"
                fi
+               # Try to enable a theme in this "foreign module", but ignore
+               # errors: we don't know if this module provides a theme or is
+               # a "regular" plugin ...
+               if SetTheme "${plugin#*#}" 2>/dev/null; then
+                       ax_msg 1 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
+                       # A theme was enabled: So assume that this is a theme
+                       # and don't enable it as plugin.
+                       return 0
+               fi
+               echo "Module \"$1\" will be enabled as plugin ..."
        fi
 
        for dname (
@@ -94,24 +145,46 @@ function EnablePlugin {
                return $?
        done
 
-       ax_msg 2 "Plugin \"$1\" not found!"
+       ax_error "Plugin \"$1\" not found!"
        return 1
 }
 
 function DisablePlugin {
        local plugin=$(NormalizedPluginName "$1")
        local dir="$AXZSH/active_plugins"
+       local r=-1
 
-       if [[ ! -h "$dir/$plugin" ]]; then
-               ax_msg 1 "Plugin \"$1\" not active?"
-               return 1
+       # Active theme?
+       if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
+               rm "$AXZSH/active_theme"; r=$?
+       fi
+
+       # Active plugin?
+       if [[ -h "$dir/$plugin" ]]; then
+               rm "$dir/$plugin"; r=$?
+       fi
+
+       if [[ $r -eq -1 ]]; then
+               ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
+               r=1
+       fi
+
+       if [[ "$plugin" = *"#"* ]]; then
+               # Name matches a cloned repository, try to clean up!
+               echo "Cleaning up cloned repository ..."
+               rm -fr "$AXZSH/repos/$plugin"
        fi
 
-       rm -v "$dir/$plugin"; r=$?
-       [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
        return $r
 }
 
+function ListEnabledPlugins {
+       for plugin ($AXZSH/active_plugins/*(N)); do
+               print ${plugin:t:s/#/\//}
+       done
+       return 0
+}
+
 function ResetPlugins {
        local dir="$AXZSH/active_plugins"
        local r1=0, r2=0
@@ -142,15 +215,9 @@ function EnableDefaultPlugins {
 function SetTheme {
        local link_name="$AXZSH/active_theme"
 
-       if [ $# -ne 1 ]; then
-               echo "Usage: axzsh_set_theme <name|->"
-               return 1
-       fi
-
-       rm -f "$link_name" || return 1
-
        if [ "$1" = "-" ]; then
-               echo "Theme settings have been reset."
+               rm -f "$link_name" || return 1
+               ax_msg 0 "Theme settings have been reset."
                return 0
        fi
 
@@ -161,20 +228,36 @@ function SetTheme {
        elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
                theme="$AXZSH/themes/$1.axzshtheme"
        else
-               echo "Theme \"$1\" not found!"
-               return 1
+               # Look for theme inside of installed plugins:
+               for dname (
+                       "$AXZSH/custom_themes"
+                       "$AXZSH/custom_plugins/"*(N)
+                       "$AXZSH/repos/"*(N)
+               ); do
+                       if [[ -r "$dname/$1.axzshtheme" ]]; then
+                               theme="$dname/$1.axzshtheme"
+                               break
+                       elif [[ -r "$dname/$1.zsh-theme" ]]; then
+                               theme="$dname/$1.zsh-theme"
+                               break
+                       fi
+               done
+               if [[ -z "$theme" ]]; then
+                       ax_error "Theme \"$1\" not found!"
+                       return 1
+               fi
        fi
-       ln -sv "$theme" "$link_name" || return 1
+       ln -fsv "$theme" "$link_name" || return 1
        return $?
 }
 
 function UpgradeAXZSH {
        if [[ $+commands[git] -eq 0 ]]; then
-               ax_msg 2 "The git(1) command is not available!"
+               ax_error "The git(1) command is not available!"
                return 1
        fi
        if [[ ! -d "$AXZSH/.git" ]]; then
-               ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
+               ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
                return 1
        fi
 
@@ -184,57 +267,140 @@ function UpgradeAXZSH {
 
 function UpgradeForeignPlugins {
        if [[ $+commands[git] -eq 0 ]]; then
-               ax_msg 2 "The git(1) command is not available!"
+               ax_error "The git(1) command is not available!"
                return 1
        fi
 
        for dir ($AXZSH/repos/*(N)); do
-               name=$(basename "$dir" | sed -e 's|#|/|g')
+               name=${dir:t:s/#/\//}
                if [ -d "$dir/.git" ]; then
                        ax_msg - "Upgrading \"$name\" [git] ..."
                        (
                                cd "$dir"
-                               git pull --ff-only || ax_msg 2 "Pull failed!"
+                               git pull --ff-only || ax_error "Pull failed!"
                        )
                else
-                       ax_msg 2 "Unknown repository type!"
+                       ax_error "Unknown repository type!"
                fi
        done
 }
 
+function CheckPlugins {
+       missing_plugins=()
+       invalid_plugins=()
+
+       ax_msg - "Checking plugins ..."
+       for dir ($AXZSH/plugins/*(N)); do
+               plugin=${dir:t}
+
+               # Test if plugin is already enabled
+               [[ -e "$AXZSH/active_plugins/$plugin" ]] \
+                       && enabled=" (enabled)" \
+                       || unset enabled
+
+               # Test plugin ...
+               printf " - \"%s\"%s ... " "$plugin" "$enabled"
+               new_plugin=""
+               for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
+                       [[ -r "$script" ]] || continue
+                       AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
+                       if [[ $r -eq 0 ]]; then
+                               new_plugin=$plugin
+                               break
+                       fi
+               done
+               if [[ -n "$new_plugin" ]]; then
+                       detected_plugins+=($new_plugin)
+                       [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
+                       ax_msg 0 "OK."
+               elif [[ $r -eq 91 ]]; then
+                       ax_msg 1 "ignored."
+               elif [[ $r -eq 92 ]]; then
+                       ax_msg 1 "optional."
+               else
+                       [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
+                       ax_msg 2 "failed."
+               fi
+       done
+       echo
+
+       result=0
+       if [[ -n "$missing_plugins" ]]; then
+               ax_msg 1 "Run the following command to enable all missing plugins:"
+               echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
+               echo
+               result=1
+       else
+               ax_msg 0 "All detected plugins are already enabled."
+       fi
+
+       if [[ -n "$invalid_plugins" ]]; then
+               ax_msg 1 "Run the following command to disable all failed plugins:"
+               echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
+               result=1
+       else
+               ax_msg 0 "No failed plugins are enabled."
+       fi
+
+       echo
+       return $result
+}
+
 NAME="$0:t"
 
 [[ $# -gt 0 ]] || Usage
 
-if [[ -z "$AXZSH" || ! -d "$AXZSH" ]]; then
-       ax_msg 2 "Oops, \"AXZSH\" is not set or invalid!"
-       exit 3
+if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
+       [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
+       if [[ ! -r "$AXZSH/ax.zsh" ]]; then
+               ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
+               exit 3
+       fi
 fi
 
 cmd="$1"
 shift
 
 case "$cmd" in
+       "enable")
+               [[ $# -eq 0 ]] || Usage
+               EnableAXZSH
+               ;;
+       "disable")
+               [[ $# -eq 0 ]] || Usage
+               DisableAXZSH
+               ;;
        "enable-plugin")
                [[ $# -gt 0 ]] || Usage
                for plugin in "$@"; do
                        EnablePlugin "$plugin"
                done
+               UpdatePluginCache
                ;;
        "disable-plugin")
                [[ $# -gt 0 ]] || Usage
                for plugin in "$@"; do
                        DisablePlugin "$plugin"
                done
+               UpdatePluginCache
+               ;;
+       "list-enabled")
+               [[ $# -eq 0 ]] || Usage
+               ListEnabledPlugins
                ;;
        "reset-plugins")
                [[ $# -eq 0 ]] || Usage
                ResetPlugins
                EnableDefaultPlugins
+               UpdatePluginCache
                ;;
        "enable-default-plugins")
                [[ $# -eq 0 ]] || Usage
-               EnableDefaultPlugins
+               EnableDefaultPlugins && UpdatePluginCache
+               ;;
+       "check-plugins")
+               [[ $# -eq 0 ]] || Usage
+               CheckPlugins
                ;;
        "set-theme")
                [[ $# -eq 1 ]] || Usage
@@ -244,7 +410,17 @@ case "$cmd" in
                [[ $# -eq 0 ]] || Usage
                UpgradeAXZSH
                UpgradeForeignPlugins
+               UpdatePluginCache
                ;;
-       *)
+       "update-caches")
+               [[ $# -eq 0 ]] || Usage
+               UpdatePluginCache
+               ;;
+       "--help")
                Usage
+               ;;
+       *)
+               ax_error "Invalid command \"$cmd\"!"
+               ax_error "Try \"$0 --help\" for more information."
+               exit 2
 esac