File: parallel.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 (178 lines) | stat: -rw-r--r-- 6,215 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
__zplug::job::parallel::init()
{
    local     caller="${${(M)funcstack[@]:#__*__}:gs:_:}"
    local     is_parallel=false is_select=false
    local     filter repo starting_message
    local -aU repos status_ok

    repos=( "$argv[@]" )

    case "$caller" in
        install)
            starting_message="install"
            # If no argument is given,
            # use non-installed plugins as an installation target
            if (( $#repos == 0 )); then
                repos=( $(__zplug::core::core::run_interfaces 'check' '--debug') )
                if (( $#repos == 0 )); then
                    __zplug::io::print::f \
                        --zplug --die \
                        "no packages to install\n"
                    return 1
                fi
            fi
            rm -f \
                "$_zplug_build_log[success]" \
                "$_zplug_build_log[failure]" \
                "$_zplug_build_log[timeout]" \
                "$_zplug_log[install]"
            touch "$_zplug_log[install]"
            ;;
        update)
            starting_message="update"
            zstyle -s ':zplug:core:update' 'select' is_select
            zstyle ':zplug:core:update' 'select' no
            rm -f \
                "$_zplug_build_log[success]" \
                "$_zplug_build_log[failure]" \
                "$_zplug_build_log[timeout]" \
                "$_zplug_log[update]"
            touch "$_zplug_log[update]"
            ;;
        status)
            starting_message="get remote status"
            zstyle -s ':zplug:core:status' 'select' is_select
            zstyle ':zplug:core:status' 'select' no
            rm -f "$_zplug_log[status]"
            touch "$_zplug_log[status]"
            ;;
        *)
            return 1
            ;;
    esac

    if (( $_zplug_boolean_true[(I)$is_select] )); then
        filter="$(
        __zplug::utils::shell::search_commands \
            "$ZPLUG_FILTER"
        )"
        if [[ -z $filter ]]; then
            __zplug::io::print::f \
                --die \
                --zplug \
                --error \
                --func \
                "There is no available filter in ZPLUG_FILTER\n"
            return 1
        fi
        repos+=( ${(@f)"$(echo "${(Fk)zplugs[@]}" | eval "$filter")"} )

        # Cace of type Ctrl-C
        if (( $#repos == 0 )); then
            return 1
        fi
    fi

    if (( $#repos == 0 )); then
        repos=( "${(k)zplugs[@]:gs:@::}" )
    fi

    # Check the number of arguments
    if (( $#repos > 1 )); then
        is_parallel=true
    fi

    for repo in "${repos[@]}"
    do
        if ! __zplug::base::base::zpluged "$repo"; then
            __zplug::io::print::f \
                --die \
                --zplug \
                "$repo: no such package\n"
            return 1
        fi
    done

    # Suppress outputs
    setopt nonotify nomonitor
    # Hide the cursor
    tput civis

    __zplug::io::print::f \
        --zplug \
        "Start to %s %d plugin${is_parallel:+"s"} %s\n\n" \
        "$starting_message" \
        $#repos \
        "${is_parallel:+"in parallel"}"

    reply=("$repos[@]")
}

__zplug::job::parallel::deinit()
{
    local caller="${${(M)funcstack[@]:#__*__}:gs:_:}"

    case "$caller" in
        update)
            if (( ${(k)#status_codes[(R)$_zplug_status[failure]]} == 0 )); then
                builtin printf "$fg_bold[default] ==> Updating finished successfully!$reset_color\n"
            else
                builtin printf "$fg_bold[red] ==> Updating failed for following packages:$reset_color\n"
                # Listing the packages that have failed to update
                for repo in "${(k)status_codes[@]}"
                do
                    if [[ $status_codes[$repo] == $_zplug_status[failure] ]]; then
                        builtin printf " - %s\n" "$repo"
                    fi
                done
            fi
            # Run rollback if hook-build failed
            __zplug::job::rollback::message
            # Cache clear automatically after running update command
            status_ok=( ${(@f)"$(cat "$_zplug_log[update]" 2>/dev/null \
                | __zplug::utils::awk::ltsv 'key("status")==0')"} )
            if (( $#status_ok > 0 )); then
                __zplug::core::core::run_interfaces 'clear'
            fi
            ;;
        install)
            if (( ${(k)#status_codes[(R)$_zplug_status[failure]]} == 0 )); then
                builtin printf "$fg_bold[default] ==> Installation finished successfully!$reset_color\n"
            else
                builtin printf "$fg_bold[red] ==> Installation failed for following packages:$reset_color\n"
                # Listing the packages that have failed to install
                for repo in "${(k)status_codes[@]}"
                do
                    if [[ $status_codes[$repo] == $_zplug_status[failure] ]]; then
                        builtin printf " - %s\n" "$repo"
                    fi
                done
            fi
            # Run rollback if hook-build failed
            __zplug::job::rollback::message
            # Cache clear automatically after running install command
            status_ok=( ${(@f)"$(cat "$_zplug_log[install]" 2>/dev/null \
                | __zplug::utils::awk::ltsv 'key("status")==0')"} )
            if (( $#status_ok > 0 )); then
                __zplug::core::core::run_interfaces 'clear'
            fi
            ;;
        status)
            if (( ${(k)#status_codes[(R)$_zplug_status[out_of_date]]} == 0 )); then
                builtin printf "$fg_bold[default] ==> All packages are up-to-date!$reset_color\n"
            else
                builtin printf "$fg_bold[red] ==> Run 'zplug update'. These packages are local out of date:$reset_color\n"
                # Listing the packages that have failed to install
                for repo in "${(k)status_codes[@]}"
                do
                    if [[ $status_codes[$repo] == $_zplug_status[out_of_date] ]]; then
                        builtin printf " - %s\n" "$repo"
                    fi
                done
            fi
            ;;
    esac

    # Display the cursor
    tput cnorm
}