]> arthur.barton.de Git - ax-zsh.git/blobdiff - ax.zsh
30_path: Fix file name in header
[ax-zsh.git] / ax.zsh
diff --git a/ax.zsh b/ax.zsh
index b0b1d733b4d6ddc858425a2a10602538e50ed8c8..edda7093f6fd9e533c8bd4e62d1c9be5041d765e 100644 (file)
--- a/ax.zsh
+++ b/ax.zsh
@@ -9,8 +9,8 @@ script_type="$script_name[2,-1]"
 # - $1: Script name
 # - $2: Stage name (ax-io, zprofile, zshrc, zlogin, zlogout)
 function axzsh_handle_stage {
-       name="$1"
-       type="$2"
+       local name="$1"
+       local type="$2"
 
        [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $name ($type):"
 
@@ -22,17 +22,17 @@ function axzsh_handle_stage {
        # Enable instant prompt. Should stay close to the top of ~/.zshrc.
        # Initialization code that may require console input (password prompts,
        # [y/n] confirmations, etc.) must be executed before this, so all ax-zsh
-       # plugings should do output in their "zprofile" stage!
+       # plugings should do output in their "ax-io" stage only!
        if [[ "$type" == "zprofile" ]]; then
                p10k_instant_prompt="${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
                [[ -r "$p10k_instant_prompt" ]] && source "$p10k_instant_prompt"
        fi
 
        # Initialize cache
-       mkdir -p "$AXZSH/cache"
-       cache_file="$AXZSH/cache/$type.cache"
+       [[ -d "$AXZSH/cache" ]] || mkdir -p "$AXZSH/cache"
+       local cache_file="$AXZSH/cache/$type.cache"
 
-       cat_cmd=${commands[cat]:-cat}
+       local cat_cmd=${commands[cat]:-cat}
 
        if [[ -r "$cache_file" ]]; then
                # Cache file exists, use it!
@@ -46,22 +46,27 @@ function axzsh_handle_stage {
                [[ -n "$AXZSH_DEBUG" ]] \
                        && echo "   - Reading cache file \"$cache_file\" ..."
                source "$cache_file"
-               unfunction ax_plugin_init
+               unfunction axzsh_plugin_init
        else
                # No cache file available.
+               local new_cache_file="$cache_file.NEW"
+
                # Setup list of plugins to load:
+               local plugin_list
                typeset -U plugin_list
                plugin_list=(
-                       "$AXZSH/core/"[0-5]*
+                       "$AXZSH/core/"[0-4]*
                        "$AXZSH/active_plugins/"*(N)
-                       "$AXZSH/core/"[6-9]*
+                       "$AXZSH/core/"[5-9]*
                )
 
                # Create new cache file:
-               if [[ -n "$cache_file" ]]; then
+               if [[ -n "$cache_file" && -w "$new_cache_file" ]]; then
                        [[ -n "$AXZSH_DEBUG" ]] \
-                               && echo "   (Writing new cache file to \"$cache_file\" ...)"
-                       printf "# %s\n\n" "$(LC_ALL=C date)" >"$cache_file"
+                               && echo "   (Writing new cache file to \"$new_cache_file\" ...)"
+                       if ! printf "# %s\n\n" "$(LC_ALL=C date)" >"$new_cache_file"; then
+                               unset new_cache_file
+                       fi
                fi
 
                # Read in all the plugins for the current "type":
@@ -71,16 +76,21 @@ function axzsh_handle_stage {
                        if [[ "$plugin:t" == "99_cleanup" && "$type" = "zshrc" ]]; then
                                if [[ -r "$AXZSH_THEME" ]]; then
                                        source "$AXZSH_THEME"
-                                       if [[ -n "$cache_file" ]]; then
+                                       if [[ -n "$new_cache_file" ]]; then
                                                # Source the theme in the new cache file:
-                                               echo "# BEGIN Theme" >>"$cache_file"
-                                               echo 'source "$AXZSH_THEME"' >>"$cache_file"
-                                               echo "# END Theme" >>"$cache_file"
+                                               echo "# BEGIN Theme" >>"$new_cache_file"
+                                               echo 'source "$AXZSH_THEME"' >>"$new_cache_file"
+                                               echo "# END Theme" >>"$new_cache_file"
                                        fi
                                fi
                        fi
-                       axzsh_load_plugin "$plugin" "$type" "$cache_file"
+                       axzsh_load_plugin "$plugin" "$type" "$new_cache_file"
                done
+
+               if [[ -n "$cache_file" && -n "$new_cache_file" && -r "$new_cache_file" ]]; then
+                       # Move newly created cache file in place:
+                       mv "$new_cache_file" "$cache_file"
+               fi
        fi
 }
 
@@ -126,6 +136,10 @@ function axzsh_load_plugin {
                        # Oh My ZSH plugin with "zsh-" prefix stripped
                        type="plugin.zsh"
                        fname="$dname/${plugin_short##zsh-}.plugin.zsh"
+               elif [[ -r "$dname/${plugin%.plugin.zsh}.plugin.zsh" ]]; then
+                       # Oh My ZSH plugin with ".plugin.zsh" suffix in its name
+                       type="plugin.zsh"
+                       fname="$dname/${plugin}"
                elif [[ -r "$dname/init.zsh" ]]; then
                        # Prezto module
                        type="init.zsh"
@@ -175,7 +189,7 @@ function axzsh_load_plugin {
 
                if [[ -n "$cache_file" ]]; then
                        # Add plugin data to cache
-                       printf "# BEGIN: %s\nax_plugin_init()\n{\n" "$fname" >>"$cache_file"
+                       printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$fname" >>"$cache_file"
                        case "$fname" in
                                *"/repos/"*)
                                        echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type): \"$fname\" ...'" >>$cache_file
@@ -185,7 +199,7 @@ function axzsh_load_plugin {
                                        echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type, cached) ...'" >>$cache_file
                                        "$cat_cmd" "$fname" >>"$cache_file"
                        esac
-                       printf "}\nax_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
+                       printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
                fi
        fi