-
Notifications
You must be signed in to change notification settings - Fork 476
/
versions.sh
executable file
·183 lines (165 loc) · 4.28 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
#!/usr/bin/env bash
set -Eeuo pipefail
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
versions=( "$@" )
if [ ${#versions[@]} -eq 0 ]; then
versions=( */ )
json='{}'
else
json="$(< versions.json)"
fi
versions=( "${versions[@]%/}" )
tmp="$(mktemp -d)"
rmTmp="$(printf 'rm -rf %q' "$tmp")"
trap "$rmTmp" EXIT
_get() {
local url="$1"; shift
local file="${url////_}"
file="${file//%/_}"
file="${file//+/_}"
file="${file//:/_}"
file="$tmp/$file"
if [ ! -s "$file" ]; then
curl -fsSL "$url" -o "$file" --retry 5 || return 1
fi
if [ "$#" -gt 0 ]; then
grep "$@" "$file"
else
cat "$file"
fi
}
abs-url() {
local url="$1"; shift
local base="$1"; shift
case "$url" in
http://* | https://* ) ;;
/*)
local extra="${base#*://*/}"
local baseBase="${base%$extra}"
baseBase="${baseBase%/}"
url="$baseBase$url"
;;
*)
echo >&2 "error: TODO parse '$url' relative to '$base'"
exit 1
;;
esac
echo "$url"
}
jdk-java-net-download-url() {
local javaVersion="$1"; shift
local fileSuffix="$1"; shift
_get "https://jdk.java.net/$javaVersion/" \
-Eom1 "https://download.java.net/[^\"]+$fileSuffix"
}
jdk-java-net-download-version() {
local javaVersion="$1"; shift
local downloadUrl="$1"; shift
downloadVersion="$(grep -Eom1 "openjdk-$javaVersion[^_]*_" <<<"$downloadUrl")" || return 1
downloadVersion="${downloadVersion%_}"
downloadVersion="${downloadVersion#openjdk-}"
echo "$downloadVersion"
}
# see https://stackoverflow.com/a/2705678/433558
sed_escape_rhs() {
sed -e 's/[\/&]/\\&/g' <<<"$*" | sed -e ':a;N;$!ba;s/\n/\\n/g'
}
sed_s() {
local lhs="$1"; shift
local rhs="$1"; shift
rhs="$(sed_escape_rhs "$rhs")"
echo -n "s/$lhs/$rhs/g"
}
sed_s_pre() {
local lhs="$1"; shift
local rhs="$1"; shift
rhs="$(sed_escape_rhs "$rhs")"
echo -n "s/^($lhs) .*$/\1 $rhs/"
}
for version in "${versions[@]}"; do
export version
doc='{}'
possibleArches=(
# https://jdk.java.net/24/
'linux-aarch64'
'linux-x64'
'linux-x64-musl'
'windows-x64'
)
for arch in "${possibleArches[@]}"; do
downloadSuffix="_${arch}_bin"
case "$arch" in
linux-*) downloadSuffix+='.tar.gz'; bashbrewArch= ;;
windows-*) downloadSuffix+='.zip'; bashbrewArch='windows-' ;;
*) echo >&2 "error: unknown Oracle arch: '$arch'"; exit 1 ;;
esac
jqExprPrefix=
if [[ "$arch" == *-musl ]]; then
jqExprPrefix='.alpine'
fi
if downloadUrl="$(jdk-java-net-download-url "$version" "$downloadSuffix")" \
&& [ -n "$downloadUrl" ] \
&& downloadSha256="$(_get "$downloadUrl.sha256")" \
&& [ -n "$downloadSha256" ] \
; then
downloadVersion="$(jdk-java-net-download-version "$version" "$downloadUrl")"
currentVersion="$(jq <<<"$doc" -r "$jqExprPrefix.version // \"\"")"
if [ -n "$currentVersion" ] && [ "$currentVersion" != "$downloadVersion" ]; then
echo >&2 "error: Oracle version mismatch: '$currentVersion' vs '$downloadVersion'"
exit 1
elif [ -z "$currentVersion" ]; then
echo "$version: $downloadVersion${jqExprPrefix:+ (alpine)}"
fi
case "$arch" in
*-aarch64*) bashbrewArch+='arm64v8' ;;
*-x64*) bashbrewArch+='amd64' ;;
*) echo >&2 "error: unknown Oracle arch: '$arch'"; exit 1 ;;
esac
export arch bashbrewArch downloadUrl downloadSha256 downloadVersion
doc="$(jq <<<"$doc" -c '
'"$jqExprPrefix"'.version = env.downloadVersion
| '"$jqExprPrefix"'.jdk.arches[env.bashbrewArch] = {
url: env.downloadUrl,
sha256: env.downloadSha256,
}
')"
fi
done
if ! jq <<<"$doc" -e '[ .. | objects | select(has("arches")) | .arches | has("amd64") ] | all' &> /dev/null; then
echo >&2 "error: missing 'amd64' for '$version'; cowardly refusing to continue! (because this is almost always a scraping flake or similar bug)"
exit 1
fi
json="$(jq <<<"$json" -c --argjson doc "$doc" '
.[env.version] = $doc + {
variants: [
(
"9",
"8",
empty
| "oraclelinux" + .),
(
"bookworm",
"bullseye",
empty
| ., "slim-" + .),
if $doc.alpine then
"3.19",
"3.18",
empty
| "alpine" + . else empty end,
if $doc.jdk.arches | keys | any(startswith("windows-")) then
(
"ltsc2022",
"1809",
empty
| "windows/windowsservercore-" + .),
(
"1809",
empty
| "windows/nanoserver-" + .)
else empty end
],
}
')"
done
jq <<<"$json" -S . > versions.json