diff --git a/autoload/commands/__load__ b/autoload/commands/__load__ index fa58b3d..14f4a5e 100644 --- a/autoload/commands/__load__ +++ b/autoload/commands/__load__ @@ -1,6 +1,7 @@ #!/bin/zsh __import "print/print" +__import "cache/cache" local is_verbose=false is_debug=false local loaded_omz=false @@ -36,17 +37,8 @@ do done # Use cache file -if (( $#funcstack[@] <= 6 )) && $ZPLUG_USE_CACHE; then - # If cache file does not exist, - # load plugins normaly - if [[ -f $_ZPLUG_CACHE_FILE ]]; then - if $is_debug; then - echo "$_ZPLUG_CACHE_FILE" - else - source "$_ZPLUG_CACHE_FILE" - fi - return $status - fi +if __load_cache; then + return 0 fi args=(${(k)zplugs}) @@ -302,23 +294,19 @@ fi __put '[[ $- =~ i ]] || exit\n' __put 'export PATH="%s:$PATH"\n' "$ZPLUG_HOME/bin" __put 'export ZSH=%s\n\n' "$ZPLUG_HOME/repos/$_ZPLUG_OHMYZSH" - __put '_zplug_repos=(${(kv)zplugs})\n' - __put '_zplug_saved=(%s)\n' "${(qqqkv)zplugs}" - __put 'if [[ $_zplug_repos != $_zplug_saved ]]; then\n' - if $is_verbose; then - __put ' ZPLUG_USE_CACHE=false zplug load --verbose\n return $status\n' - else - __put ' ZPLUG_USE_CACHE=false zplug load\n return $status\n' - fi - __put 'fi\n' __put 'if $is_verbose; then\n' __put ' echo "Static loading..." >&2\n' __put 'fi\n' if (( $#load_plugins > 0 )); then __put 'source %s\n' "${(qqq)load_plugins[@]}" fi - __put '\n# fpath\n' - __put 'fpath=(%s $fpath)\n' "${(u)load_fpaths}" + if (( $#load_fpaths > 0 )); then + __put '\n# fpath\n' + __put 'fpath=(\n' + __put '%s\n' ${(u)load_fpaths} + __put '$fpath\n' + __put ')\n' + fi __put 'compinit -C -d %s\n' "$ZPLUG_HOME/zcompdump" if (( $#nice_plugins > 0 )); then __put '\n# Loading after compinit\n' @@ -328,6 +316,8 @@ fi __put '\n# Lazy loading plugins\n' __put 'autoload -Uz %s\n' "${(qqq)lazy_plugins[@]:t}" fi + __put '\nreturn 0\n' + __put '%s\n' "$(__tags_for_cache)" } >|$_ZPLUG_CACHE_FILE if [[ -f $_ZPLUG_CACHE_FILE ]]; then diff --git a/lib/cache/cache.zsh b/lib/cache/cache.zsh new file mode 100644 index 0000000..16e2bee --- /dev/null +++ b/lib/cache/cache.zsh @@ -0,0 +1,47 @@ +#!/bin/zsh + +__tags_for_cache() { + local key + + for key in ${(k)zplugs} + do + echo "name:$key, $zplugs[$key]" + done \ + | awk -f "$ZPLUG_ROOT/src/share/cache.awk" +} + +__load_cache() { + local key + + $ZPLUG_USE_CACHE || return 2 + if [[ -f $_ZPLUG_CACHE_FILE ]]; then + &>/dev/null diff -b \ + <( \ + awk -f "$ZPLUG_ROOT/src/share/read_cache.awk" \ + "$_ZPLUG_CACHE_FILE" \ + ) \ + <( \ + for key in ${(k)zplugs}; do \ + echo "name:$key, $zplugs[$key]"; \ + done \ + | awk -f "$ZPLUG_ROOT/src/share/cache.awk" + ) + + case $status in + 0) + # same + source "$_ZPLUG_CACHE_FILE" + return $status + ;; + 1) + # differ + ;; + 2) + # error + __die "zplug: cache: something wrong\n" + ;; + esac + fi + + return 1 +} diff --git a/src/share/cache.awk b/src/share/cache.awk new file mode 100644 index 0000000..5bd4e21 --- /dev/null +++ b/src/share/cache.awk @@ -0,0 +1,60 @@ +BEGIN { + TAGS_SEP = ", "; +} + +{ + arr[NR] = reconstruct($0); +} + +END { + for (i in arr) + print arr[i]; +} + +function reconstruct(line, tags) { + gsub(/, +/, TAGS_SEP, line) + split(line, tags, TAGS_SEP); + quick_sort(tags, 1, length(tags)); + + return join(tags, 1, length(tags), TAGS_SEP); +} + +function quick_sort(array, left, right, i, j, tmp, pivot) { + if (left < right) { + i = left; + j = right; + pivot = array[int((left + right) / 2)]; + while (i <= j) { + while (array[i] < pivot) { + i++; + } + while (array[j] > pivot) { + j--; + } + if (i <= j) { + tmp = array[i]; + array[i] = array[j]; + array[j] = tmp; + i++; + j--; + } + } + quick_sort(array, left, j); + quick_sort(array, i, right); + } +} + +function join(array, start, end, sep, result, i) { + if (sep == "") { + sep = " "; + } else if (sep == SUBSEP) { + sep = ""; + } + + result = array[start]; + for (i = start + 1; i <= end; i++) { + result = result sep array[i]; + } + + return result; +} diff --git a/src/share/parser.awk b/src/share/parser.awk new file mode 100644 index 0000000..f0dd371 --- /dev/null +++ b/src/share/parser.awk @@ -0,0 +1,63 @@ +BEGIN { + KEY_SEP = ":"; + TAGS_SEP = ", "; +} + +{ + arr[NR] = $0; +} + +END { + if (get_tag(arr, "junegunn/fzf-bin") != 0) + exit 1; +} + +function get_name(arr, tag, n, tags, i, name) { + n = split(tag, tags, "#"); + for (i in tags) { + if (tags[i] ~ /\//) { + name = tags[i]; + break; + } + } + return name; +} + +function get_tag(line, tag, name, i, j, n, tags, line_tags, ret, key_and_value, key, value) { + name = get_name(line, tag); + if (name == "") { + return 1; + } + n = split(tag, tags, "#"); + + for (i in line) { + if (line[i] ~ "name" KEY_SEP name TAGS_SEP) { + m = split(line[i], line_tags, TAGS_SEP); + for (j in line_tags) { + split(line_tags[j], key_and_value, KEY_SEP); + key = key_and_value[1]; + value = key_and_value[2]; + if (n == 1) { + ret[key] = value; + } + if (in_array(key, tags) == 0) { + ret[key] = value; + } + } + } + } + + if (length(ret) == 0) + return 1; + + for (key in ret) + print key KEY_SEP ret[key]; +} + +function in_array(e, arr, i) { + for (i in arr) { + if (arr[i] == e) + return 0; + } + return 1; +} diff --git a/src/share/read_cache.awk b/src/share/read_cache.awk new file mode 100644 index 0000000..a79159b --- /dev/null +++ b/src/share/read_cache.awk @@ -0,0 +1,14 @@ +BEGIN { + flag = 0; +} + +{ + if ($0 == "return 0") { + flag = 1; + next; + } +} + +flag { + print $0; +}