File: cache.zsh

package info (click to toggle)
zplug 2.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,068 kB
  • sloc: sh: 1,504; awk: 235; makefile: 26
file content (215 lines) | stat: -rw-r--r-- 6,069 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
__zplug::core::cache::set_file()
{
    local file="${1:?}"

    if (( ! $+_zplug_cache[$file] )); then
        return 1
    fi

    # Keep compatible with version 2.3.3 or lower
    __zplug::core::migration::cache_file_dir

    rm -f "$_zplug_cache[$file]"
    touch "$_zplug_cache[$file]"
}

__zplug::core::cache::expose()
{
    if [[ -f $_zplug_cache[interface] ]]; then
        cat "$_zplug_cache[interface]"
    fi
}

__zplug::core::cache::update()
{
    __zplug::core::interface::expose \
        >|"$_zplug_cache[interface]"
}

__zplug::core::cache::commit()
{
    local     pkg pair hook
    local -A  hook_load
    local -A  reply_hash
    local -A  load_commands
    local -aU load_plugins load_fpaths lazy_plugins \
        defer_1_plugins \
        defer_2_plugins \
        defer_3_plugins
    local -aU unclassified_plugins
    local     repo param params

    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]} )
    defer_1_plugins=( ${(@f)reply_hash[defer_1_plugins]} )
    defer_2_plugins=( ${(@f)reply_hash[defer_2_plugins]} )
    defer_3_plugins=( ${(@f)reply_hash[defer_3_plugins]} )
    unclassified_plugins=( ${(@f)reply_hash[unclassified_plugins]} )
    for pair (${(@f)reply_hash[load_commands]}) load_commands+=( ${(@s:\0:)pair} )
    for pair in ${reply_hash[hook_load]}
    do
        hook="${${(@s:\0:)pair}[2,-1]}"
    done
    repo="$reply_hash[repo]"

    # Common parameter
    param="--repo ${(qqq)repo}"
    if [[ -n $hook ]]; then
        param+=" --hook ${(qqq)hook}"
    fi

    # Record packages to cache file
    for pkg in "$load_plugins[@]"
    do
        params="$param ${(qqq)pkg}"
        __zplug::job::handle::flock "$_zplug_cache[plugin]" "__zplug::core::load::as_plugin $params"
    done
    for pkg in "$defer_1_plugins[@]"
    do
        params="$param ${(qqq)pkg}"
        __zplug::job::handle::flock "$_zplug_cache[defer_1_plugin]" "__zplug::core::load::as_plugin $params"
    done
    for pkg in "$defer_2_plugins[@]"
    do
        params="$param ${(qqq)pkg}"
        __zplug::job::handle::flock "$_zplug_cache[defer_2_plugin]" "__zplug::core::load::as_plugin $params"
    done
    for pkg in "$defer_3_plugins[@]"
    do
        params="$param ${(qqq)pkg}"
        __zplug::job::handle::flock "$_zplug_cache[defer_3_plugin]" "__zplug::core::load::as_plugin $params"
    done
    for pkg in "$lazy_plugins[@]"
    do
        params="$param ${(qqq)pkg} --lazy"
        __zplug::job::handle::flock "$_zplug_cache[lazy_plugin]" "__zplug::core::load::as_plugin $params"
    done
    for pkg in "$load_fpaths[@]"
    do
        __zplug::job::handle::flock "$_zplug_cache[fpath]" "$pkg"
    done
    for pkg in "${(k)load_commands[@]}"
    do
        params="$param --path ${(qqq)load_commands[$pkg]} ${(qqq)pkg}"
        __zplug::job::handle::flock "$_zplug_cache[command]" "__zplug::core::load::as_command $params"
    done
    for pkg in "$load_themes[@]"
    do
        params="$param ${(qqq)pkg}"
        __zplug::job::handle::flock "$_zplug_cache[theme]" "__zplug::core::load::as_theme $params"
    done
}

__zplug::core::cache::diff()
{
    local key file
    local is_verbose
    zstyle -s ':zplug:core:load' 'verbose' is_verbose

    $ZPLUG_USE_CACHE || return 2

    if [[ -d $ZPLUG_CACHE_DIR ]]; then
        2> >(__zplug::log::capture::error) >/dev/null \
            diff -b \
            <(__zplug::core::cache::expose) \
            <(__zplug::core::interface::expose)

        case $status in
            0)
                # same
                if (( $_zplug_boolean_true[(I)$is_verbose] )); then
                    __zplug::io::print::f --zplug "Loaded from cache ($ZPLUG_CACHE_DIR)\n"
                fi
                return 0
                ;;
            1)
                # differ
                ;;
            2)
                # error
                ;;
        esac
    fi

    for file in "${(k)_zplug_cache[@]}"
    do
        __zplug::core::cache::set_file "$file"
    done

    # if cache file doesn't find,
    # returns non-zero exit code
    return 1
}

__zplug::core::cache::plugins()
{
    local repo="${1:?}"
    local -A tags

    __zplug::core::load::skip_condition "$repo" && return 0

    tags[from]="$(__zplug::core::core::run_interfaces "from" "$repo")"

    # Switch to the revision specified by its tags
    __zplug::utils::git::checkout "$repo"

    if __zplug::core::sources::is_handler_defined "load_plugin" "$tags[from]"; then
        # Custom handler for loading
        __zplug::core::sources::use_handler \
            "load_plugin" \
            "$tags[from]" \
            "$repo"

        __zplug::core::cache::commit repo "$repo" "$reply[@]"
    fi
}

__zplug::core::cache::commands()
{
    local repo="${1:?}"
    local -A tags

    __zplug::core::load::skip_condition "$repo" && return 0

    tags[from]="$(__zplug::core::core::run_interfaces "from" "$repo")"

    # Switch to the revision specified by its tags
    __zplug::utils::git::checkout "$repo"

    if __zplug::core::sources::is_handler_defined "load_command" "$tags[from]"; then
        __zplug::core::sources::use_handler \
            "load_command" \
            "$tags[from]" \
            "$repo"

        __zplug::core::cache::commit repo "$repo" "$reply[@]"
    fi
}

__zplug::core::cache::themes()
{
    local repo="${1:?}"
    local -A tags

    __zplug::core::load::skip_condition "$repo" && return 0

    tags[from]="$(__zplug::core::core::run_interfaces "from" "$repo")"

    # Switch to the revision specified by its tags
    __zplug::utils::git::checkout "$repo"

    if __zplug::core::sources::is_handler_defined "load_theme" "$tags[from]"; then
        # Custom handler for loading
        __zplug::core::sources::use_handler \
            "load_theme" \
            "$tags[from]" \
            "$repo"

        __zplug::core::cache::commit repo "$repo" "$reply[@]"
    fi

    setopt prompt_subst
}