This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
_zsh-sdkman.sh
259 lines (228 loc) · 8.16 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
#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 removes all unecessery things 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}
}
# 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 candidate_dir="${ZSH_SDKMAN_CACHE}/candidates/$1"
local versions_file="${candidate_dir}/versions"
# Create cache directory if not present
[[ ! -d "$candidate_dir" ]] && mkdir -p "$candidate_dir"
# If file is not present or older than 12 hours, recreate cache file
if [[ ! -e "$versions_file" || -n "$versions_file"(.mm+11N) ]]
then
local -a versions
# The formatting of java is different from other outputs:
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
# Save the versions to the cache file.
echo $versions > "$versions_file"
fi
# Load list from cache file. (ZSH will sort it automatically.)
echo $(<"$versions_file")
}
# 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) )
}
# 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}
}
########################################################
##### FIRST ARG FUNCTIONS
########################################################
__describe_commands() {
local -a commands
commands=(
'install: install a program'
'uninstall: uninstal an existing program'
'list: list all available packages or '
'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
}
########################################################
##### 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
}
# 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
}
__describe_offline() {
local -a offline
offline=(
'enable'
'disable'
)
_describe -t offline "Offline" offline && ret=0
}
__describe_env() {
local -a env=( 'init: Create a .sdkmanrc in the current directory' )
_describe -t env "Environment creation" env && ret=0
}
########################################################
##### 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
}
# 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
}
# 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
}
########################################################
##### 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 "$@"
# 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