File: github.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 (308 lines) | stat: -rw-r--r-- 10,279 bytes parent folder | download | duplicates (3)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
__zplug::sources::github::check()
{
    local    repo="$1"
    local -A tags

    tags[dir]="$(
    __zplug::core::core::run_interfaces \
        'dir' \
        "$repo"
    )"

    [[ -d $tags[dir] ]]
    return $status
}

__zplug::sources::github::install()
{
    local repo="$1"

    __zplug::utils::git::clone "$repo"
    return $status
}

__zplug::sources::github::update()
{
    local    repo="$1"
    local    rev_local rev_remote rev_base
    local -A tags

    tags[dir]="$(__zplug::core::core::run_interfaces 'dir' "$repo")"
    tags[at]="$(__zplug::core::core::run_interfaces 'at' "$repo")"

    __zplug::utils::git::merge \
        --dir    "$tags[dir]" \
        --branch "$tags[at]" \
        --repo "$repo"

    return $status
}

__zplug::sources::github::get_url()
{
    local repo="$1" url_format

    case "$ZPLUG_PROTOCOL" in
        HTTPS | https)
            # Create the format of URL used to git clone
            # When vim-plug clones a repository, it injects git::@ into the URL
            # It's a little hack to avoid username/password prompt
            # from git when the repository doesn't exist.
            # Such thing can happen when there's a typo in the argument,
            # or when the repository is removed from GitHub
            # For more information, see also vim-plug wiki.
            # https://git::@github.com/%s.git
            url_format="https://git::@github.com/${repo}.git"

            # However, Git 2.3.0 introduced $GIT_TERMINAL_PROMPT
            # which can be used to suppress user prompt
            if __zplug::base::base::git_version 2.3; then
                # (git 2.3+) https://github.com/%s.git
                export GIT_TERMINAL_PROMPT=0
                url_format="https://github.com/${repo}.git"
            fi
            ;;
        SSH | ssh)
            # git@github.com:%s.git
            url_format="git@github.com:${repo}.git"
            ;;
    esac

    echo "$url_format"
}

__zplug::sources::github::load_plugin()
{
    local    repo="${1:?}"
    local -A tags default_tags
    local -a \
        unclassified_plugins \
        load_fpaths \
        defer_1_plugins \
        defer_2_plugins \
        defer_3_plugins \
        load_plugins \
        lazy_plugins

    __zplug::core::tags::parse "$repo"
    tags=( "${reply[@]}" )
    default_tags[use]="$(__zplug::core::core::run_interfaces 'use')"
    load_fpaths=()

    # If that is an autoload plugin
    if (( $_zplug_boolean_true[(I)$tags[lazy]] )); then
        if [[ $tags[use] == $default_tags[use] ]]; then
            unclassified_plugins+=( \
                "$tags[dir]"/${repo:t}(N.) \
                "$tags[dir]/autoload"/*(N.) \
                "$tags[dir]/functions"/*(N.) \
                )
            load_fpaths+=( \
                "$tags[dir]"(N/) \
                "$tags[dir]/autoload"(N/) \
                "$tags[dir]/functions"(N/) \
                )
        else
            unclassified_plugins+=( ${(@f)"$( \
                __zplug::utils::shell::expand_glob "$tags[dir]/$tags[use]" "(N-.)"
            )"} )
            load_fpaths+=( $unclassified_plugins:h(N/) )
        fi
    else
        if [[ $tags[use] == $default_tags[use] ]]; then
            unclassified_plugins+=( "$tags[dir]"/*.plugin.zsh(N-.) )
        fi
        if (( $#unclassified_plugins == 0 )); then
            unclassified_plugins+=( ${(@f)"$( \
                __zplug::utils::shell::expand_glob "$tags[dir]/$tags[use]" "(N-.)"
            )"} )
            # If $tags[use] is a directory,
            # expect to expand to $tags[dir]/*.zsh
            if (( $#unclassified_plugins == 0 )); then
                unclassified_plugins+=( ${(@f)"$( \
                    __zplug::utils::shell::expand_glob "$tags[dir]/$tags[use]/$default_tags[use]" "(N-.)"
                )"} )
                # Add the parent directory to fpath
                load_fpaths+=( "$tags[dir]/$tags[use]"/_*(N.:h) )
            fi
        fi
        load_fpaths+=( "$tags[dir]"/_*(N.:h) )
    fi

    # unclassified_plugins -> {defer_N_plugins,lazy_plugins,load_plugins}
    # the order of loading of plugin files
    case "$tags[defer]" in
        0)
            if (( $_zplug_boolean_true[(I)$tags[lazy]] )); then
                lazy_plugins+=( "${unclassified_plugins[@]}" )
            else
                load_plugins+=( "${unclassified_plugins[@]}" )
            fi
            ;;
        1)
            defer_1_plugins+=( "${unclassified_plugins[@]}" )
            ;;
        2)
            defer_2_plugins+=( "${unclassified_plugins[@]}" )
            ;;
        3)
            defer_3_plugins+=( "${unclassified_plugins[@]}" )
            ;;
        *)
            : # Error
            ;;
    esac
    unclassified_plugins=()

    if [[ -n $tags[ignore] ]]; then
        ignore_patterns=( $(
        zsh -c "$_ZPLUG_CONFIG_SUBSHELL; echo ${tags[dir]}/${~tags[ignore]}" \
            2> >(__zplug::log::capture::error)
        )(N) )
        for ignore in "${ignore_patterns[@]}"
        do
            # Commands
            if [[ -n $load_commands[(i)$ignore] ]]; then
                unset "load_commands[$ignore]"
            fi
            # Plugins
            load_plugins=( "${(R)load_plugins[@]:#$ignore}" )
            defer_1_plugins=( "${(R)defer_1_plugins[@]:#$ignore}" )
            defer_2_plugins=( "${(R)defer_2_plugins[@]:#$ignore}" )
            defer_3_plugins=( "${(R)defer_3_plugins[@]:#$ignore}" )
            lazy_plugins=( "${(R)lazy_plugins[@]:#$ignore}" )
            # fpath
            load_fpaths=( "${(R)load_fpaths[@]:#$ignore}" )
        done
    fi

    reply=()
    [[ -n $load_plugins ]] && reply+=( "load_plugins" "${(F)load_plugins}" )
    [[ -n $defer_1_plugins ]] && reply+=( "defer_1_plugins" "${(F)defer_1_plugins}" )
    [[ -n $defer_2_plugins ]] && reply+=( "defer_2_plugins" "${(F)defer_2_plugins}" )
    [[ -n $defer_3_plugins ]] && reply+=( "defer_3_plugins" "${(F)defer_3_plugins}" )
    [[ -n $lazy_plugins ]] && reply+=( "lazy_plugins" "${(F)lazy_plugins}" )
    [[ -n $load_fpaths ]] && reply+=( "load_fpaths" "${(F)load_fpaths}" )
    [[ -n $tags[hook-load] ]] && reply+=( "hook_load" "$tags[name]\0$tags[hook-load]")
}

__zplug::sources::github::load_command()
{
    local    repo="${1:?}"
    local -A tags default_tags
    local    src dst
    local -a sources
    local -a load_fpaths load_commands
    local -A rename_hash

    __zplug::core::tags::parse "$repo"
    tags=( "${reply[@]}" )
    default_tags[use]="$(__zplug::core::core::run_interfaces 'use')"
    tags[dir]="${tags[dir]%/}"
    load_commands=()
    load_fpaths=()

    # Append dst to each element so that load_commands becomes:
    #
    # load_commands=(
    #   path/to/cmd1\0dst
    #   path/to/cmd2\0dst
    #   ...
    # )
    #
    # where \0 is a null character used to separate the two strings.
    #
    # In the caller function (__load__), each line is decomposed into an
    # element in an associative array, thus, in the example above, the line:
    #
    #   path/to/cmd1\0dst
    #
    # becomes an element where the key is "path/to/cmd" and the value is
    # "dst".
    if [[ $tags[use] == *(*)* && $tags[rename-to] == *\$* ]]; then
        # If it's captured by `use` tag and referenced by `rename-to` tag,
        # it's expanded with `__zplug::utils::shell::zglob`
        if (( $#rename_hash == 0 )) && [[ -n $tags[rename-to] ]]; then
            rename_hash=( $(__zplug::utils::shell::zglob \
                "$tags[dir]/$tags[use]" \
                "$ZPLUG_BIN/$tags[rename-to]")
            )
        fi
    else
        if [[ $tags[use] == $default_tags[use] ]]; then
            # If no $tags[use] is given by the user,
            # automatically add repo's basename to load-path
            # if it exists as executable file
            if [[ -f $tags[dir]/${repo:t} ]]; then
                sources=( "$tags[dir]/${repo:t}"(N-.) )
            fi
        else
            if [[ $tags[use] == $default_tags[use] || $tags[from] == "gh-r" ]]; then
                tags[use]="*(N-*)"
            fi
            sources=( ${(@f)"$( \
                __zplug::utils::shell::expand_glob "$tags[dir]/$tags[use]" "(N-.)"
            )"} )
        fi
        dst=${${tags[rename-to]:+$ZPLUG_BIN/$tags[rename-to]}:-"$ZPLUG_BIN"}
        for src in "$sources[@]"
        do
            chmod 755 "$src"
            rename_hash+=("$src" "$dst")
        done
    fi
    for src in "${(k)rename_hash[@]}"
    do
        load_commands+=("$src\0$rename_hash[$src]")
    done
    if (( $#rename_hash == 0 )); then
        __zplug::log::write::info \
            "$repo: no matches found, rename_hash is empty"
    fi

    # Add parent directories to fpath if any files starting in _* exist
    load_fpaths+=("$tags[dir]"/{_*,/**/_*}(N-.:h))

    reply=()
    [[ -n $load_fpaths ]] && reply+=( "load_fpaths" "${(F)load_fpaths}" )
    [[ -n $load_commands ]] && reply+=( "load_commands" "${(F)load_commands}" )
    [[ -n $tags[hook-load] ]] && reply+=( "hook_load" "$tags[name]\0$tags[hook-load]")

    return 0
}

__zplug::sources::github::load_theme()
{
    local    repo="${1:?}"
    local -A tags default_tags
    local -a themes_ext
    local -a load_themes load_fpaths

    __zplug::core::tags::parse "$repo"
    tags=( "${reply[@]}" )
    themes_ext=("zsh-theme" "theme-zsh")
    load_themes=()
    load_fpaths=()

    if [[ -n $tags[use] ]]; then
        # e.g. zplug 'foo/bar', as:theme, use:'*.zsh'
        load_themes=( ${(@f)"$( \
            __zplug::utils::shell::expand_glob "$tags[dir]/$tags[use]" "(N-.)"
        )"} )
        if (( $#load_themes == 0 )); then
            # e.g. zplug 'foo/bar', as:theme, use:dir
            load_themes=( ${(@f)"$( \
                __zplug::utils::shell::expand_glob "$tags[dir]/$tags[use]/*.${^themes_ext}" "(N-.)"
            )"} )
        fi
    else
        # e.g. zplug 'foo/bar', as:theme
        load_themes+=( "$tags[dir]"/*.${^themes_ext}(N-.) )
    fi
    load_fpaths+=( "$tags[dir]"/_*(N.:h) )

    reply=()
    [[ -n $load_themes ]] && reply+=( "load_themes" "${(F)load_themes}" )
    [[ -n $load_fpaths ]] && reply+=( "load_fpaths" "${(F)load_fpaths}" )
    [[ -n $tags[hook-load] ]] && reply+=( "hook_load" "$tags[name]\0$tags[hook-load]")
}