diff --git a/autoload/commands/__clear__ b/autoload/commands/__clear__ index 6d9d776..cef6af5 100644 --- a/autoload/commands/__clear__ +++ b/autoload/commands/__clear__ @@ -29,5 +29,6 @@ done rm -rf \ "$ZPLUG_CACHE_FILE" \ + "$ZPLUG_CACHE_FILE/cache" \ "$ZPLUG_HOME/zcompdump" \ "$ZPLUG_HOME/zcompdump.zwc" diff --git a/autoload/commands/__load__ b/autoload/commands/__load__ index 885cff0..42824bf 100644 --- a/autoload/commands/__load__ +++ b/autoload/commands/__load__ @@ -1,28 +1,20 @@ -#!/bin/zsh +#!/usr/bin/env zsh +# Description: +# Source installed plugins and add installed commands to $PATH -# Check if cache is up-to-date -if __zplug::core::cache::loadable; then - echo "zplug: load from cache" - __zplug::core::load::cache - return $status -fi - -local repo +local repo arg local -a repos -local is_verbose=false -local arg local -A tags +local -F SECONDS -repos=( - "${(k)zplugs[@]}" -) +repos=( "${(k)zplugs[@]}" ) while (( $# > 0 )) do arg="$1" case "$arg" in --verbose) - is_verbose=true + zstyle ':zplug:core:load' verbose yes ;; -*|--*) __zplug::core::options::unknown "$arg" @@ -42,30 +34,14 @@ do shift done -if [[ -f $ZPLUG_CACHE_FILE ]]; then - rm -rf "$ZPLUG_CACHE_FILE" +# Check if cache is up-to-date +if __zplug::core::cache::loadable; then + return 0 fi -mkdir -p "$ZPLUG_CACHE_FILE" -rm -f "$_zplug_cache[before_plugin]" -rm -f "$_zplug_cache[after_plugin]" -rm -f "$_zplug_cache[plugin]" -rm -f "$_zplug_cache[lazy_plugin]" -rm -f "$_zplug_cache[theme]" -rm -f "$_zplug_cache[command]" -rm -f "$_zplug_cache[fpath]" -rm -f "$_zplug_cache[hook-load]" -touch "$_zplug_cache[before_plugin]" -touch "$_zplug_cache[after_plugin]" -touch "$_zplug_cache[plugin]" -touch "$_zplug_cache[lazy_plugin]" -touch "$_zplug_cache[theme]" -touch "$_zplug_cache[command]" -touch "$_zplug_cache[fpath]" -touch "$_zplug_cache[hook-load]" -local -F SECONDS -set +m -for repo in "$repos[@]" +__zplug::core::load::prepare + +for repo in "${repos[@]}" do # Generate cache for each as:type in parallel { @@ -89,7 +65,6 @@ do --func \ "as tag is invalid value (%s)\n" \ "$fg[green]$repo$reset_color" - return 1 ;; esac #echo "$repo ($SECONDS) - $tags[as]" @@ -97,6 +72,7 @@ do done wait +zstyle ':zplug:core:load' verbose no # Load packages from cache -__zplug::core::load::cache --verbose +__zplug::core::load::from_cache return $status diff --git a/base/core/cache.zsh b/base/core/cache.zsh index dd64175..644ba0b 100644 --- a/base/core/cache.zsh +++ b/base/core/cache.zsh @@ -1,10 +1,117 @@ +__zplug::core::cache::expose() +{ + if [[ -f $ZPLUG_HOME/.cache/cache ]]; then + cat "$ZPLUG_HOME/.cache/cache" + fi +} + +__zplug::core::cache::update() +{ + __zplug::core::interface::expose \ + >|"$ZPLUG_HOME/.cache/cache" +} + +__zplug::core::cache::commit() +{ + local pkg hook pair + local -A hook_load + local -A reply_hash + local -A load_commands + local -aU load_plugins load_fpaths lazy_plugins nice_plugins + local -aU unclassified_plugins + + reply_hash=( "$argv[@]" ) + lazy_plugins=( ${(@f)reply_hash[lazy_plugins]} ) + load_fpaths=( ${(@f)reply_hash[load_fpaths]} ) + load_plugins=( ${(@f)reply_hash[load_plugins]} ) + load_themes=( ${(@f)reply_hash[load_themes]} "$unclassified_plugins[@]" ) + nice_plugins=( ${(@f)reply_hash[nice_plugins]} ) + unclassified_plugins=( ${(@f)reply_hash[unclassified_plugins]} ) + for pair in ${(@f)reply_hash[load_commands]} + do + load_commands+=( ${(@s:\0:)pair} ) # Each line (pair) is null character-separated + done + for pair in ${(@f)reply_hash[hook_load]} + do + hook_load+=( ${(@s:\0:)pair} ) # Each line (pair) is null character-separated + done + + # Record packages to cache file + if (( $#load_plugins > 0 )); then + for pkg in "$load_plugins[@]" + do + __zplug::job::state::flock "$_zplug_cache[plugin]" "source ${(qqq)pkg}" + done + fi + if (( $#nice_plugins > 0 )); then + for pkg in "$nice_plugins[@]" + do + __zplug::job::state::flock "$_zplug_cache[before_plugin]" "source ${(qqq)pkg}" + __zplug::job::state::flock "$_zplug_cache[after_plugin]" "source ${(qqq)pkg}" + done + fi + if (( $#lazy_plugins > 0 )); then + for pkg in "$lazy_plugin[@]" + do + __zplug::job::state::flock "$_zplug_cache[lazy_plugin]" "source ${(qqq)pkg}" + done + fi + if (( $#load_fpaths > 0 )); then + for pkg in "$load_fpaths[@]" + do + __zplug::job::state::flock "$_zplug_cache[fpath]" "fpath+=(${(qqq)pkg})" + done + fi + if (( $#hook_load > 0 )); then + for hook in "${(k)hook_load[@]}" + do + __zplug::job::state::flock "$_zplug_cache[hook-load]" "$hook_load[$hook]" + done + fi + if (( $#load_commands > 0 )); then + for pkg in "${(k)load_commands[@]}" + do + __zplug::job::state::flock "$_zplug_cache[command]" "chmod 755 ${(qqq)pkg}" + __zplug::job::state::flock "$_zplug_cache[command]" "ln -snf ${(qqq)pkg} ${(qqq)load_commands[$pkg]}" + done + fi + if (( $#load_themes > 0 )); then + for pkg in "$load_themes[@]" + do + __zplug::job::state::flock "$_zplug_cache[theme]" "source ${(qqq)pkg}" + done + fi +} + __zplug::core::cache::loadable() { - # Check if cache is up-to-date + local key + + $ZPLUG_USE_CACHE || return 2 + if [[ -e $ZPLUG_CACHE_FILE ]]; then - # TODO: - return 0 + 2> >(__zplug::io::log::capture) >/dev/null \ + diff -b \ + <(__zplug::core::cache::expose) \ + <(__zplug::core::interface::expose) + + case $status in + 0) + # same + __zplug::core::load::from_cache + return $status + ;; + 1) + # differ + ;; + 2) + # error + ;; + esac fi + + # if cache file doesn't find, + # returns non-zero exit code return 1 } @@ -12,12 +119,6 @@ __zplug::core::cache::plugins() { local repo="${1:?}" local -A tags - local -aU unclassified_plugins - local -aU load_plugins load_fpaths lazy_plugins nice_plugins - local -aU ignore_patterns - local -A reply_hash - local -A hook_load - local pkg hook __zplug::core::tags::parse "$repo" tags=( "${reply[@]}" ) @@ -64,54 +165,8 @@ __zplug::core::cache::plugins() "load_plugin" \ "$tags[from]" \ "$repo" - reply_hash=( "$reply[@]" ) - # Temporary array until files get sorted into - # {load,lazy,nice}_plugins - unclassified_plugins=( ${(@f)reply_hash[unclassified_plugins]} ) - # Plugins - load_plugins=( ${(@f)reply_hash[load_plugins]} ) - lazy_plugins=( ${(@f)reply_hash[lazy_plugins]} ) - nice_plugins=( ${(@f)reply_hash[nice_plugins]} ) - # fpath - load_fpaths=( ${(@f)reply_hash[load_fpaths]} ) - - for pair in ${(@f)reply_hash[hook_load]} - do - hook_load+=( ${(@s:\0:)pair} ) - done - - if (( $#load_plugins > 0 )); then - for pkg in "$load_plugins[@]" - do - __zplug::job::state::flock "$_zplug_cache[plugin]" "source ${(qqq)pkg}" - done - fi - if (( $#nice_plugins > 0 )); then - for pkg in "$nice_plugins[@]" - do - __zplug::job::state::flock "$_zplug_cache[before_plugin]" "source ${(qqq)pkg}" - __zplug::job::state::flock "$_zplug_cache[after_plugin]" "source ${(qqq)pkg}" - done - fi - if (( $#lazy_plugins > 0 )); then - for pkg in "$lazy_plugin[@]" - do - __zplug::job::state::flock "$_zplug_cache[lazy_plugin]" "source ${(qqq)pkg}" - done - fi - if (( $#load_fpaths > 0 )); then - for pkg in "$load_fpaths[@]" - do - __zplug::job::state::flock "$_zplug_cache[fpath]" "fpath+=(${(qqq)pkg})" - done - fi - if (( $#hook_load > 0 )); then - for hook in "${(k)hook_load[@]}" - do - __zplug::job::state::flock "$_zplug_cache[hook-load]" "$hook_load[$hook]" - done - fi + __zplug::core::cache::commit "$reply[@]" fi } @@ -119,14 +174,6 @@ __zplug::core::cache::commands() { local repo="${1:?}" local -A tags - local -aU unclassified_plugins - local -aU ignore_patterns - local -A reply_hash - local -A load_commands - local pkg hook - local pair - local -aU load_plugins load_fpaths lazy_plugins nice_plugins - local -A hook_load __zplug::core::tags::parse "$repo" tags=( "${reply[@]}" ) @@ -172,40 +219,8 @@ __zplug::core::cache::commands() "load_command" \ "$tags[from]" \ "$repo" - reply_hash=( "$reply[@]" ) - load_fpaths+=( ${(@f)reply_hash[load_fpaths]} ) - - for pair in ${(@f)reply_hash[load_commands]} - do - # Each line (pair) is null character-separated - load_commands+=( ${(@s:\0:)pair} ) - done - - for pair in ${(@f)reply_hash[hook_load]} - do - hook_load+=( ${(@s:\0:)pair} ) - done - - if (( $#load_commands > 0 )); then - for pkg in "${(k)load_commands[@]}" - do - __zplug::job::state::flock "$_zplug_cache[command]" "chmod 755 ${(qqq)pkg}" - __zplug::job::state::flock "$_zplug_cache[command]" "ln -snf ${(qqq)pkg} ${(qqq)load_commands[$pkg]}" - done - fi - if (( $#load_fpaths > 0 )); then - for pkg in "$load_fpaths[@]" - do - __zplug::job::state::flock "$_zplug_cache[fpath]" "fpath+=(${(qqq)pkg})" - done - fi - if (( $#hook_load > 0 )); then - for hook in "${(k)hook_load[@]}" - do - __zplug::job::state::flock "$_zplug_cache[hook-load]" "$hook_load[$hook]" - done - fi + __zplug::core::cache::commit "$reply[@]" fi } @@ -213,12 +228,6 @@ __zplug::core::cache::themes() { local repo="${1:?}" local -A tags - local -aU unclassified_plugins - local -aU load_plugins load_fpaths lazy_plugins nice_plugins - local -aU ignore_patterns - local -A reply_hash - local pkg hook - local -A hook_load __zplug::core::tags::parse "$repo" tags=( "${reply[@]}" ) @@ -265,26 +274,9 @@ __zplug::core::cache::themes() "load_theme" \ "$tags[from]" \ "$repo" - reply_hash=( "$reply[@]" ) - load_themes=( ${(@f)reply_hash[load_themes]} "$unclassified_plugins[@]" ) - for pair in ${(@f)reply_hash[hook_load]} - do - hook_load+=( ${(@s:\0:)pair} ) - done - - if (( $#load_themes > 0 )); then - for pkg in "$load_themes[@]" - do - __zplug::job::state::flock "$_zplug_cache[theme]" "source ${(qqq)pkg}" - done - fi - if (( $#hook_load > 0 )); then - for hook in "${(k)hook_load[@]}" - do - __zplug::job::state::flock "$_zplug_cache[hook-load]" "$hook_load[$hook]" - done - fi + __zplug::core::cache::commit "$reply[@]" fi + setopt prompt_subst } diff --git a/base/core/interface.zsh b/base/core/interface.zsh new file mode 100644 index 0000000..1081ed2 --- /dev/null +++ b/base/core/interface.zsh @@ -0,0 +1,11 @@ +__zplug::core::interface::expose() +{ + local name + + for name in "${(ok)zplugs[@]}" + do + # In order to sort $zplugs[$name], + # do not quate this string + echo "${name}${zplugs[$name]:+, ${(os:, :)zplugs[$name]}}" + done +} diff --git a/base/core/load.zsh b/base/core/load.zsh index 2f93f21..2e35ed4 100644 --- a/base/core/load.zsh +++ b/base/core/load.zsh @@ -1,5 +1,36 @@ -__zplug::core::load::cache() +__zplug::core::load::prepare() { + unsetopt monitor + + if [[ -f $ZPLUG_CACHE_FILE ]]; then + rm -rf "$ZPLUG_CACHE_FILE" + fi + + mkdir -p "$ZPLUG_CACHE_FILE" + + local file + for file in "${(k)_zplug_cache[@]}" + do + rm -f "$_zplug_cache[$file]" + touch "$_zplug_cache[$file]" + done +} + +__zplug::core::load::from_cache() +{ + local is_verbose=false + + zstyle -s ':zplug:core:load' verbose is_verbose + + if (( $_zplug_boolean_true[(I)$is_verbose] )); then + __zplug::io::print::f \ + --zplug \ + "$fg[yellow]Load from cache$reset_color\n" + fi + + # Default + setopt monitor + # Load the cache in order { source "$_zplug_cache[fpath]" @@ -11,20 +42,25 @@ __zplug::core::load::cache() compinit -C -d /Users/b4b4r07/.zplug/zcompdump source "$_zplug_cache[before_plugin]" source "$_zplug_cache[after_plugin]" - } #&>/dev/null + } &>/dev/null + + # Cache in background + { + __zplug::core::cache::update + } &! } -__zplug::core::load::plugins() +__zplug::core::load::as_plugin() { : } -__zplug::core::load::commands() +__zplug::core::load::as_command() { : } -__zplug::core::load::themes() +__zplug::core::load::as_theme() { : }