-
Notifications
You must be signed in to change notification settings - Fork 1
/
versions.sh
422 lines (341 loc) · 17.8 KB
/
versions.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env bash
# Version Manager Functions
# [goup (pronounced Go Up) is an elegant Go version manager](https://github.com/owenthereal/goup)
# install latest go version
function goupInstallLatest() {
local GO_VER_LATEST GO_VER_INSTALLED
[[ ! -x "$(command -v goup)" ]] && colorEcho "${FUCHSIA}goup${RED} is not installed!" && return 1
[[ "${THE_WORLD_BLOCKED}" == "true" && -z "${GOUP_GO_HOST}" ]] && export GOUP_GO_HOST="golang.google.cn"
GO_VER_LATEST=$(curl "${CURL_CHECK_OPTS[@]}" "https://${GOUP_GO_HOST:-"go.dev"}/VERSION?m=text" 2>/dev/null | grep -Eo -m1 'go([0-9]{1,}\.)+[0-9]{1,}' | head -n1)
[[ -z "${GO_VER_LATEST}" ]] && colorEcho "${RED}Can't get ${FUCHSIA}golang${RED} latest version!" && return 1
GO_VER_INSTALLED=$(goup list 2>&1 | grep -Eo '([0-9]{1,}\.)+[0-9]{1,}')
if grep -q "${GO_VER_LATEST//go/}" <<<"${GO_VER_INSTALLED}"; then
colorEcho "${FUCHSIA}${GO_VER_LATEST}${BLUE} already installed."
else
colorEcho "${BLUE}Installing ${FUCHSIA}${GO_VER_LATEST}${BLUE}..."
# fix: proxyconnect tcp: dial tcp: lookup socks5h: no such host
if echo "${all_proxy}" | grep -q 'socks5h'; then
proxy_socks5h_to_socks5 goup install "${GO_VER_LATEST}"
else
goup install "${GO_VER_LATEST}"
fi
fi
}
# update goup & latest go version
function goupUpgrade() {
local GO_VER_LATEST GO_VER_INSTALLED
[[ ! -x "$(command -v goup)" ]] && colorEcho "${FUCHSIA}goup${RED} is not installed!" && return 1
colorEcho "${BLUE}Updating ${FUCHSIA} Go toolchains and goup${BLUE}..."
# fix: proxyconnect tcp: dial tcp: lookup socks5h: no such host
if echo "${all_proxy}" | grep -q 'socks5h'; then
proxy_socks5h_to_socks5 sudo "$(which goup)" upgrade
else
sudo "$(which goup)" upgrade
fi
[[ "${THE_WORLD_BLOCKED}" == "true" && -z "${GOUP_GO_HOST}" ]] && export GOUP_GO_HOST="golang.google.cn"
GO_VER_LATEST=$(curl "${CURL_CHECK_OPTS[@]}" "https://${GOUP_GO_HOST:-"go.dev"}/VERSION?m=text" 2>/dev/null | grep -Eo -m1 'go([0-9]{1,}\.)+[0-9]{1,}' | head -n1)
[[ -z "${GO_VER_LATEST}" ]] && colorEcho "${RED}Can't get ${FUCHSIA}golang${RED} latest version!" && return 1
GO_VER_INSTALLED=$(goup list 2>&1 | grep -Eo '([0-9]{1,}\.)+[0-9]{1,}')
if ! grep -q "${GO_VER_LATEST//go/}" <<<"${GO_VER_INSTALLED}"; then
colorEcho "${BLUE}Installing ${FUCHSIA}${GO_VER_LATEST}${BLUE}..."
# fix: proxyconnect tcp: dial tcp: lookup socks5h: no such host
if echo "${all_proxy}" | grep -q 'socks5h'; then
proxy_socks5h_to_socks5 goup install "${GO_VER_LATEST}"
else
goup install "${GO_VER_LATEST}"
fi
fi
}
# remove unuse installed go version but keep the latest minor version
# e.g.: 1.19 1.20.5 1.20.6 1.20.7 1.21.0 will remove 1.20.5, 1.20.6
function goupRemoveUnuse() {
local GO_VER_LATEST GO_VER_UNUSE GO_VER_MINOR TargetVer
[[ ! -x "$(command -v goup)" ]] && colorEcho "${FUCHSIA}goup${RED} is not installed!" && return 1
[[ "${THE_WORLD_BLOCKED}" == "true" && -z "${GOUP_GO_HOST}" ]] && export GOUP_GO_HOST="golang.google.cn"
GO_VER_LATEST=$(curl "${CURL_CHECK_OPTS[@]}" "https://${GOUP_GO_HOST:-"go.dev"}/VERSION?m=text" 2>/dev/null | grep -Eo -m1 'go([0-9]{1,}\.)+[0-9]{1,}' | head -n1)
[[ -z "${GO_VER_LATEST}" ]] && colorEcho "${RED}Can't get ${FUCHSIA}golang${RED} latest version!" && return 1
GO_VER_UNUSE=$(goup list 2>&1 | grep -v '\*' | grep -Eo '([0-9]{1,}\.)+[0-9]{1,}' | sort -rV)
GO_VER_MINOR=$(awk -F'.' '{print $1"."$2}' <<<"${GO_VER_LATEST//go/}")
while read -r TargetVer; do
[[ -z "${TargetVer}" || -z "${GO_VER_MINOR}" ]] && continue
[[ "${TargetVer}" == *"${GO_VER_MINOR}"* ]] && goup remove "go${TargetVer}"
GO_VER_MINOR=$(awk -F'.' '{print $1"."$2}' <<<"${TargetVer}")
done <<<"${GO_VER_UNUSE}"
colorEcho "${BLUE}Installed go versions:"
goup list
}
# rbenv: install ruby version
function rbenvInstallRuby() {
local RUBY_VERSION=$1
local RUBY_MINOR_VERSION download_filename download_url
[[ -z "${RUBY_VERSION}" ]] && colorEcho "${RED}Ruby version can't empty!" && return 1
RUBY_MINOR_VERSION=$(cut -d'.' -f1-2 <<<"${RUBY_VERSION}")
download_url="${RUBY_BUILD_MIRROR_URL:-"https://cache.ruby-lang.org"}/pub/ruby/${RUBY_MINOR_VERSION}/ruby-${RUBY_VERSION}.tar.gz"
download_filename="$(rbenv root)/cache/ruby-${RUBY_VERSION}.tar.gz"
colorEcho "${BLUE}Downloading ${FUCHSIA}ruby ${RUBY_VERSION}${BLUE} from ${ORANGE}${download_url}..."
axel "${AXEL_DOWNLOAD_OPTS[@]}" -o "${download_filename}" "${download_url}" || curl "${CURL_DOWNLOAD_OPTS[@]}" -o "${download_filename}" "${download_url}"
curl_rtn_code=$?
if [[ ${curl_rtn_code} -eq 0 ]]; then
colorEcho "${BLUE}Installing ${FUCHSIA}ruby ${YELLOW}${RUBY_VERSION}${BLUE}..."
rbenv install "${RUBY_VERSION}" && rbenv rehash
fi
}
# rbenv: fallback to system installed version
function rbenvFallbackSystemVersion() {
rbenv global system
}
# [The front-end to your dev env (formerly called "rtx")](https://mise.jdx.dev/)
function mise_App_Update() {
# Usage:
# mise_App_Update all
# mise_App_Update neovim
# mise_App_Update nodejs lts
local appName=${1:-"all"}
local appVersion=$2
local InstalledPlugins InstalledApp allVersion currentVersion currentVerNum majorVersion matchVersion latestVersion
local appInstallStatus=0
[[ ! "$(command -v mise)" ]] && colorEcho "${FUCHSIA}mise${RED} is not installed!" && return 1
if [[ "${appName}" == "all" ]]; then
colorEcho "${BLUE}Checking update for all installed ${FUCHSIA}mise plugins${BLUE}..."
mise plugins update
InstalledPlugins=$(mise plugins ls 2>/dev/null)
else
colorEcho "${BLUE}Checking update for ${FUCHSIA}mise plugin ${ORANGE}${appName}${BLUE}..."
mise plugins update "${appName}"
InstalledPlugins="${appName}"
fi
while read -r InstalledApp; do
[[ -z "${InstalledApp}" ]] && continue
colorEcho "${BLUE} Checking latest version for ${FUCHSIA}${InstalledApp}${BLUE}..."
appInstallStatus=0
allVersion=""
latestVersion=""
currentVersion=$(mise current "${InstalledApp}" 2>/dev/null)
[[ -z "${currentVersion}" ]] && continue # no installed version
if [[ -n "${appVersion}" ]]; then
matchVersion="${appVersion}"
else
currentVerNum=$(grep -Eo '([0-9]{1,}\.)+[0-9]{1,}' <<<"${currentVersion}")
if [[ -z "${currentVerNum}" ]]; then
matchVersion="${currentVersion}"
elif [[ "${currentVerNum}" == "${currentVersion}" ]]; then
matchVersion=""
else
# fetch major version from current version string: {major}.{minor}.{revision}
# zulu-17.40.19 → zulu-17
majorVersion=$(cut -d'.' -f1 <<<"${currentVerNum}")
matchVersion="${currentVersion/${currentVerNum}/}${majorVersion}"
fi
fi
if [[ -n "${matchVersion}" ]]; then
allVersion=$(mise ls-remote "${InstalledApp}" 2>/dev/null | grep "${matchVersion}" 2>/dev/null | grep -Ev 'alpha|beta|rc|_[0-9]+$')
else
allVersion=$(mise ls-remote "${InstalledApp}" 2>/dev/null | grep -E '([0-9]{1,}\.)+[0-9]{1,}' 2>/dev/null | grep -Ev 'alpha|beta|rc|_[0-9]+$')
fi
[[ -z "${allVersion}" ]] && allVersion=$(mise ls-remote "${InstalledApp}" 2>/dev/null | grep -Ev 'alpha|beta|rc|_[0-9]+$')
[[ -n "${allVersion}" ]] && latestVersion=$(sort -rV <<<"${allVersion}" | head -n1)
[[ -z "${latestVersion}" ]] && continue
# Alwarys reinstall if specify appVersion (stable, lts...)
[[ -z "${appVersion}" && "${latestVersion}" == "${currentVersion}" ]] && continue
# Uninstall first if specify appVersion (stable, lts...)
[[ -n "${appVersion}" && "${latestVersion}" == "${currentVersion}" ]] && mise uninstall "${InstalledApp}@${currentVersion}"
mise install "${InstalledApp}@${latestVersion}"
appInstallStatus=$?
if [[ ${appInstallStatus} -eq 0 ]]; then
# Set the global runtime version to latest installed version
mise global "${InstalledApp}@${latestVersion}"
# Uninstall old version
[[ -z "${appVersion}" ]] && mise uninstall "${InstalledApp}@${currentVersion}"
fi
done <<<"${InstalledPlugins}"
}
# asdf: Extendable version manager with support for Ruby, Node.js, Elixir, Erlang & more
# https://asdf-vm.com/
# https://github.com/asdf-vm/asdf
function asdf_App_Install() {
# Usage:
# asdf_App_Install neovim stable
# asdf_App_Install java openjdk-11
# asdf_App_Install nodejs lts
local appName=$1
local appVersion=${2:-"latest"}
local allPlugin allVersion currentVersion latestVersion
local appInstallStatus=0
[[ ! "$(command -v asdf)" ]] && colorEcho "${FUCHSIA}asdf${RED} is not installed!" && return 1
[[ -z "${appName}" ]] && \
colorEcho "${FUCHSIA}asdf plugin${RED} name can't empty!" && \
return 1
colorEcho "${BLUE}Checking latest version for ${FUCHSIA}asdf plugin ${ORANGE}${appName}${BLUE}..."
# List All in Short-name Repository
allPlugin=$(asdf plugin list all 2>/dev/null | awk '{print $1}')
if [[ -z "${allPlugin}" ]]; then
colorEcho "${RED}Get all ${FUCHSIA}asdf plugins${RED} list failed!"
return 1
fi
if ! echo "${allPlugin}" | grep -q "^${appName}$"; then
colorEchoN "${ORANGE}${appName}${RED} is not a valid ${FUCHSIA}asdf plugin${RED}!"
colorEcho " More: ${FUCHSIA}https://asdf-vm.com/#/plugins-all"
return 1
fi
# List Installed
if ! asdf plugin list 2>/dev/null | grep -q "^${appName}$"; then
asdf plugin add "${appName}"
fi
if ! asdf plugin list 2>/dev/null | grep -q "^${appName}$"; then
colorEcho "${RED}Install ${FUCHSIA}asdf plugin ${ORANGE}${appName}${RED} failed!"
return 1
fi
## List All Available Versions
# asdf list all "${appName}"
# asdf list all "${appName}" "${appVersion}"
# asdf list all java | cut -d'-' -f1 | sort -V | uniq
# If `appVersion` is not a valid version, get latest version from all versions
allVersion=$(asdf list all "${appName}" "${appVersion}" 2>/dev/null | grep -Ev 'alpha|beta|rc|_[0-9]+$')
[[ -z "${allVersion}" ]] && allVersion=$(asdf list all "${appName}" 2>/dev/null | grep -Ev 'alpha|beta|rc|_[0-9]+$')
# https://stackoverflow.com/questions/4493205/unix-sort-of-version-numbers
# sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n
# To reverse the order: sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr
[[ -n "${allVersion}" ]] && latestVersion=$(echo "${allVersion}" | sort -rV | head -n1)
[[ -z "${latestVersion}" ]] && latestVersion="latest"
# Current Version
# currentVersion=$(asdf current "${appName}" 2>/dev/null | sed 's/ */ /g' | cut -d' ' -f2)
# currentVersion=$(asdf current "${appName}" 2>/dev/null | tr -s ' ' | cut -d' ' -f2)
currentVersion=$(asdf current "${appName}" 2>/dev/null | awk '{print $2}')
if [[ "${latestVersion}" == "${currentVersion}" ]]; then
colorEcho "${FUCHSIA}${appName} ${latestVersion}${GREEN} is already installed!"
return 0
fi
colorEcho "${BLUE} Installing ${FUCHSIA}${appName} ${YELLOW}${latestVersion}${BLUE}..."
# Install Version
if [[ "${appName}" == "nodejs" ]]; then
[[ -z "${THE_WORLD_BLOCKED}" ]] && set_proxy_mirrors_env
if [[ "${THE_WORLD_BLOCKED}" == "true" ]]; then
NODEJS_CHECK_SIGNATURES="no" NODEJS_ORG_MIRROR="https://npmmirror.com/mirrors/node/" \
asdf install "${appName}" "${latestVersion}"
appInstallStatus=$?
else
NODEJS_CHECK_SIGNATURES="no" asdf install "${appName}" "${latestVersion}"
appInstallStatus=$?
fi
else
asdf install "${appName}" "${latestVersion}"
appInstallStatus=$?
fi
# Set Current Version
if [[ ${appInstallStatus} -eq 0 ]]; then
asdf global "${appName}" "${latestVersion}"
# asdf shell "${appName}" "${latestVersion}"
# asdf local "${appName}" "${latestVersion}"
# [[ -n "${currentVersion}" ]] && asdf uninstall "${appName}" "${currentVersion}"
fi
## Fallback to System Version
# asdf local ${appName} system
# View Current Version
asdf current "${appName}"
## Show Latest Stable Version
# asdf latest "${appName}"
# asdf latest "${appName}" "${latestVersion}"
## List Installed Versions
# asdf list "${appName}"
## Uninstall Installed Version
# asdf uninstall "${appName}" "${latestVersion}"
## Update all
# asdf plugin update --all
}
function asdf_App_Update() {
# Usage:
# asdf_App_Update all
# asdf_App_Update neovim
# asdf_App_Update nodejs lts
local appName=${1:-"all"}
local appVersion=$2
local InstalledPlugins InstalledApp allVersion currentVersion currentVerNum majorVersion matchVersion latestVersion
local appInstallStatus=0
[[ ! "$(command -v asdf)" ]] && colorEcho "${FUCHSIA}asdf${RED} is not installed!" && return 1
if [[ "${appName}" == "all" ]]; then
colorEcho "${BLUE}Checking update for all installed ${FUCHSIA}asdf plugins${BLUE}..."
InstalledPlugins=$(asdf plugin list 2>/dev/null)
else
colorEcho "${BLUE}Checking update for ${FUCHSIA}asdf plugin ${ORANGE}${appName}${BLUE}..."
InstalledPlugins="${appName}"
fi
while read -r InstalledApp; do
[[ -z "${InstalledApp}" ]] && continue
colorEcho "${BLUE} Checking latest version for ${FUCHSIA}${InstalledApp}${BLUE}..."
appInstallStatus=0
allVersion=""
latestVersion=""
currentVersion=$(asdf current "${InstalledApp}" 2>/dev/null | awk '{print $2}')
[[ -z "${currentVersion}" ]] && continue # no installed version
if [[ -n "${appVersion}" ]]; then
matchVersion="${appVersion}"
else
currentVerNum=$(echo "${currentVersion}" | grep -Eo '([0-9]{1,}\.)+[0-9]{1,}')
if [[ -z "${currentVerNum}" ]]; then
matchVersion="${currentVersion}"
elif [[ "${currentVerNum}" == "${currentVersion}" ]]; then
matchVersion=""
else
# fetch major version from current version string: {major}.{minor}.{revision}
# zulu-17.40.19 → zulu-17
majorVersion=$(echo "${currentVerNum}" | cut -d'.' -f1)
matchVersion="${currentVersion/${currentVerNum}/}${majorVersion}"
fi
fi
[[ -n "${matchVersion}" ]] && allVersion=$(asdf list all "${InstalledApp}" "${matchVersion}" 2>/dev/null | grep -Ev 'alpha|beta|rc|_[0-9]+$')
[[ -z "${allVersion}" ]] && allVersion=$(asdf list all "${InstalledApp}" 2>/dev/null | grep -Ev 'alpha|beta|rc|_[0-9]+$')
[[ -n "${allVersion}" ]] && latestVersion=$(echo "${allVersion}" | sort -rV | head -n1)
# [[ -z "${latestVersion}" ]] && latestVersion="latest"
[[ -z "${latestVersion}" ]] && continue
# Alwarys reinstall if specify appVersion (stable, lts...)
[[ -z "${appVersion}" && "${latestVersion}" == "${currentVersion}" ]] && continue
colorEcho "${BLUE} Updating ${FUCHSIA}${InstalledApp}${BLUE} to ${YELLOW}${latestVersion}${BLUE}..."
asdf plugin update "${InstalledApp}"
# Uninstall first if specify appVersion (stable, lts...)
[[ -n "${appVersion}" && "${latestVersion}" == "${currentVersion}" ]] && asdf uninstall "${InstalledApp}" "${currentVersion}"
if [[ "${appName}" == "nodejs" ]]; then
[[ -z "${THE_WORLD_BLOCKED}" ]] && set_proxy_mirrors_env
if [[ "${THE_WORLD_BLOCKED}" == "true" ]]; then
NODEJS_CHECK_SIGNATURES="no" NODEJS_ORG_MIRROR="https://npmmirror.com/mirrors/node/" \
asdf install "${InstalledApp}" "${latestVersion}"
appInstallStatus=$?
else
NODEJS_CHECK_SIGNATURES="no" asdf install "${InstalledApp}" "${latestVersion}"
appInstallStatus=$?
fi
else
asdf install "${InstalledApp}" "${latestVersion}"
appInstallStatus=$?
fi
if [[ ${appInstallStatus} -eq 0 ]]; then
# Set the global runtime version to latest installed version
asdf global "${InstalledApp}" "${latestVersion}"
# Uninstall old version
[[ -z "${appVersion}" ]] || asdf uninstall "${InstalledApp}" "${currentVersion}"
fi
done <<<"${InstalledPlugins}"
}
# [List AUR packages installed and only AUR packages](https://bbs.archlinux.org/viewtopic.php?id=76218)
# `pacman -Qqm` lists foreign packages. which, for must users, means AUR
# `pacman -Qqe` lists packages that were explicitely installed
function yayGetAurFlagOutdatedPackages() {
local InstalledPackages TargetPackage PackageSearchResult OutdatePackages
InstalledPackages=$(yay -Qqm)
OutdatePackages=()
while read -r TargetPackage; do
[[ -z "${TargetPackage}" ]] && continue
# [How to show and update echo on same line](https://stackoverflow.com/questions/12628327/how-to-show-and-update-echo-on-same-line)
colorEchoN "${BLUE}Checking ${FUCHSIA}${TargetPackage}${BLUE}...\033[0K\r"
# PackageSearchResult=$(yay -Ss "${TargetPackage}" 2>/dev/null | grep -i '(installed)')
PackageSearchResult=$(yay -Ss "${TargetPackage}" 2>/dev/null | grep -i '(installed)' | grep -i 'out-of-date')
[[ -n "${PackageSearchResult}" ]] && OutdatePackages+=("${PackageSearchResult}")
done <<<"${InstalledPackages}"
if [[ -z "${OutdatePackages[*]}" ]]; then
colorEcho "${GREEN}Everything looks ok! \033[0K\r"
else
colorEcho "${RED}Out-of-date packages:\033[0K\r"
for TargetPackage in "${OutdatePackages[@]}"; do
colorEcho "${CYAN}${TargetPackage}"
done
fi
}