-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path_zsh-sdkman.sh
284 lines (238 loc) · 8.23 KB
/
_zsh-sdkman.sh
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
#compdef sdk
zstyle ':completion:*:descriptions' format '%B%d%b'
# TODO See if we keep the following "zstyle" lines (try and find their actual effect)
# zstyle ':completion::complete:sdk:*:commands' group-name commands
# zstyle ':completion::all_candidates:sdk:*:all_candidates' group-name all_candidates
# zstyle ':completion::candidates_to_uninstall:sdk:*:candidates_to_uninstall' group-name candidates_to_uninstall
# zstyle ':completion::complete:sdk::' list-grouped
########################################################
##### UTILITY FUNCTIONS
########################################################
# Gets candidate lists and cleanses output just to get candidate names
__get_candidate_list() {
local -a candidates_csv
# Load the candidates from SDKMAN's candidates file, split at commas
candidates_csv=$(<"${SDKMAN_DIR}/var/candidates")
echo ${(s:,:)candidates_csv}
unset candidates_csv
}
# Gets installed candidates list
__get_current_installed_list() {
( cd "$SDKMAN_DIR/candidates" && echo *(F) )
}
# Gets a candidate available versions (All of them, including already installed, not installed ...)
# Parameters:
# $1 chosen candidate label
__get_installed_candidate_all_versions() {
local candidate=$1
local -a versions
if [[ "$candidate" = "java" ]]
then
# Get everything in the last column of the table, excluding the header.
versions=( $(sdk list "$candidate" | grep '|' | cut -d\| -f6 | grep -v Identifier) )
else
# Replace special chars with spaces, break everything into newlines and clean it up.
versions=( ${=$(sdk list "$candidate" | grep '^ ' | tr '+*>' ' ')} )
fi
# Load list from cache file. (ZSH will sort it automatically.)
echo ${versions}
unset candidate
unset versions
}
# Gets a candidate currently installed versions (the ones preceded by a "*")
# Parameters:
# $1: chosen candidate label
__get_installed_candidate_installed_versions() {
local candidate_dir="${SDKMAN_DIR}/candidates/$1"
( [[ -d "$candidate_dir" ]] && cd "$candidate_dir" && echo *(F) )
unset candidate_dir
}
# Gets versions of a candidate that are not yet installed
# Parameters:
# $1: chosen candidate label
__get_installed_candidate_not_installed_versions() {
local candidate="$1"
local -a installed=( $( __get_installed_candidate_installed_versions "$candidate" ) )
local -a all=( $( __get_installed_candidate_all_versions "$candidate" ) )
echo ${(@)all:|installed}
unset all
unset installed
unset candidate
}
########################################################
##### FIRST ARG FUNCTIONS
########################################################
__describe_commands() {
local -a commands
commands=(
'install: install a program'
'uninstall: uninstall an existing program'
'list: list all available packages or candidates'
'use: change the version of an existing program'
'default: set a program default version'
'home: get the absolute path of a SDK'
'env: manage .sdkmanrc configuration file'
'current: display all programs current running version'
'upgrade: upgrade all current programs or a particular one'
'version: display current sdkman version'
'broadcast: get the latest SDK release notifications'
'help: display commands help'
'offline: Toggle offline mode'
'selfupdate: update sdkman itself'
'update: refresh the candidate cache'
'flush: flush sdkman local state'
)
_describe -t commands "Commands" commands && ret=0
unset commands
}
########################################################
##### SECOND ARG FUNCTIONS
########################################################
# Displays ALL candidates available
# Parameters:
# $1: label to display
__describe_candidate_list() {
local -a candidate_list
candidate_list=( $( __get_candidate_list ) )
_describe -t candidate_list $1 candidate_list && ret=0
unset candidate_list
}
# Displays installed candidates available
# Parameters:
# $1: label to display
__describe_current_installed_list() {
local -a current_installed_list
current_installed_list=( $( __get_current_installed_list ) )
_describe -t current_installed_list $1 current_installed_list && ret=0
unset current_installed_list
}
__describe_offline() {
local -a offline
offline=(
'enable'
'disable'
)
_describe -t offline "Offline" offline && ret=0
unset offline
}
__describe_env() {
local -a env=( 'init: Create a .sdkmanrc in the current directory' )
_describe -t env "Environment creation" env && ret=0
unset env
}
########################################################
##### THIRD ARG FUNCTIONS
########################################################
# Parameters:
# $1: chosen candidate label
__describe_candidate_all_versions() {
local -a installed_candidate_all_versions
installed_candidate_all_versions=( $( __get_installed_candidate_all_versions $1 ) )
_describe -t installed_candidate_all_versions "All versions for $1" installed_candidate_all_versions && ret=0
unset installed_candidate_all_versions
}
# Parameters:
# $1: chosen candidate label
__describe_candidate_installed_versions() {
local -a installed_candidate_installed_versions
installed_candidate_installed_versions=( $( __get_installed_candidate_installed_versions $1 ) )
_describe -t installed_candidate_installed_versions "Installed versions for $1" installed_candidate_installed_versions && ret=0
unset installed_candidate_installed_versions
}
# Parameters:
# $1: chosen candidate label
__describe_candidate_available_versions() {
local -a installed_candidate_available_versions
installed_candidate_available_versions=( $( __get_installed_candidate_not_installed_versions $1 ) )
_describe -t installed_candidate_available_versions "Available (not installed) versions for $1" installed_candidate_available_versions && ret=0
unset installed_candidate_available_versions
}
########################################################
##### MAIN FUNCTION AND EXECUTION
########################################################
function _sdk() {
local ret=1
local target=$words[2]
local candidate=$words[3]
_arguments -C \
'1: :->first_arg' \
'2: :->second_arg' \
'3: :->third_arg' \
&& ret=0
case $state in
first_arg)
__describe_commands
;;
second_arg)
case $target in
install)
__describe_candidate_list "Candidates available for install"
;;
list)
__describe_candidate_list "Candidates available for version listing"
;;
uninstall)
__describe_current_installed_list "Candidates available for uninstall"
;;
use)
__describe_current_installed_list "Candidates available for usage"
;;
default)
__describe_current_installed_list "Candidates available for default setting"
;;
home)
__describe_current_installed_list "Candidates available for usage"
;;
env)
__describe_env
;;
current)
__describe_current_installed_list "Candidates available"
;;
upgrade)
__describe_current_installed_list "Candidates available for default setting"
;;
offline)
__describe_offline
;;
*)
;;
esac
;;
third_arg)
case $target in
install)
__describe_candidate_available_versions $candidate
;;
uninstall)
__describe_candidate_installed_versions $candidate
;;
use)
__describe_candidate_installed_versions $candidate
;;
default)
__describe_candidate_installed_versions $candidate
;;
home)
__describe_candidate_installed_versions $candidate
;;
*)
;;
esac
esac
return $ret
}
_sdk "$@"
unset _sdk
unset __get_candidate_list
unset __get_current_installed_list
unset __get_installed_candidate_all_versions
unset __get_installed_candidate_installed_versions
unset __get_installed_candidate_not_installed_versions
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et