-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·215 lines (183 loc) · 5.75 KB
/
install.sh
File metadata and controls
executable file
·215 lines (183 loc) · 5.75 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
#!/usr/bin/env bash
set -euo pipefail
APP_NAME="preview"
REPO="${OPENPREVIEW_REPO:-treadiehq/openpreview}"
REQUESTED_VERSION="${OPENPREVIEW_VERSION:-}"
INSTALL_DIR_OVERRIDE="${OPENPREVIEW_INSTALL_DIR:-}"
TOKEN="${OPENPREVIEW_GITHUB_TOKEN:-${GITHUB_TOKEN:-}}"
if [ -t 1 ]; then
BOLD="$(printf '\033[1m')"
DIM="$(printf '\033[2m')"
BLUE="$(printf '\033[94m')"
YELLOW="$(printf '\033[33m')"
GREEN="$(printf '\033[32m')"
RESET="$(printf '\033[0m')"
else
BOLD=""
DIM=""
BLUE=""
YELLOW=""
GREEN=""
RESET=""
fi
say() {
printf '%b\n' "$*"
}
fail() {
say "${YELLOW}Error:${RESET} $*"
exit 1
}
cleanup_dir=""
cleanup() {
if [ -n "${cleanup_dir}" ] && [ -d "${cleanup_dir}" ]; then
rm -rf "${cleanup_dir}"
fi
}
trap cleanup EXIT
need_cmd() {
command -v "$1" >/dev/null 2>&1 || fail "Missing required command: $1"
}
curl_api() {
if [ -n "${TOKEN}" ]; then
curl -fsSL \
-H "Authorization: Bearer ${TOKEN}" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$@"
else
curl -fsSL "$@"
fi
}
curl_download() {
if [ -n "${TOKEN}" ]; then
curl -fL --progress-bar \
-H "Authorization: Bearer ${TOKEN}" \
-H "Accept: application/octet-stream" \
"$@"
else
curl -fL --progress-bar "$@"
fi
}
detect_os() {
case "$(uname -s)" in
Darwin) echo "darwin" ;;
Linux) echo "linux" ;;
*) fail "Unsupported operating system: $(uname -s). This installer currently supports macOS and Linux." ;;
esac
}
detect_arch() {
case "$(uname -m)" in
arm64|aarch64) echo "arm64" ;;
x86_64|amd64) echo "x64" ;;
*) fail "Unsupported architecture: $(uname -m). Supported targets are arm64 and x64." ;;
esac
}
sha256_file() {
local file="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "${file}" | awk '{print $1}'
else
shasum -a 256 "${file}" | awk '{print $1}'
fi
}
choose_install_dir() {
if [ -n "${INSTALL_DIR_OVERRIDE}" ]; then
printf '%s\n' "${INSTALL_DIR_OVERRIDE}"
return
fi
for candidate in /usr/local/bin /opt/homebrew/bin "${HOME}/.local/bin" "${HOME}/bin"; do
if [ -d "${candidate}" ] || mkdir -p "${candidate}" 2>/dev/null; then
if [ -w "${candidate}" ]; then
printf '%s\n' "${candidate}"
return
fi
fi
done
printf '%s\n' "${HOME}/.local/bin"
}
normalize_tag() {
local value="$1"
if [ -z "${value}" ]; then
printf '%s\n' ""
elif [ "${value#v}" = "${value}" ]; then
printf 'v%s\n' "${value}"
else
printf '%s\n' "${value}"
fi
}
need_cmd curl
need_cmd tar
OS="$(detect_os)"
ARCH="$(detect_arch)"
ASSET_NAME="${APP_NAME}-${OS}-${ARCH}.tar.gz"
LATEST_RELEASE_API="https://api.github.com/repos/${REPO}/releases/latest"
BASE_DOWNLOAD_URL=""
TAG=""
if [ -n "${REQUESTED_VERSION}" ]; then
TAG="$(normalize_tag "${REQUESTED_VERSION}")"
BASE_DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${TAG}/${ASSET_NAME}"
else
RELEASE_JSON="$(curl_api "${LATEST_RELEASE_API}")" \
|| fail "Could not determine the latest release version from GitHub Releases. If the repo is private, set OPENPREVIEW_GITHUB_TOKEN or GITHUB_TOKEN."
TAG="$(printf '%s' "${RELEASE_JSON}" | sed -nE 's/.*"tag_name"[[:space:]]*:[[:space:]]*"([^"]+)".*/\1/p' | head -n 1)"
[ -n "${TAG}" ] || fail "Could not determine the latest release version."
BASE_DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${TAG}/${ASSET_NAME}"
fi
VERSION="${TAG#v}"
CHECKSUM_URL="https://github.com/${REPO}/releases/download/${TAG}/${ASSET_NAME}.sha256"
INSTALL_DIR="$(choose_install_dir)"
cleanup_dir="$(mktemp -d)"
ARCHIVE_PATH="${cleanup_dir}/${ASSET_NAME}"
CHECKSUM_PATH="${cleanup_dir}/${ASSET_NAME}.sha256"
if command -v "${APP_NAME}" >/dev/null 2>&1; then
INSTALLED_VERSION="$("${APP_NAME}" --version 2>/dev/null | awk 'NR==1 {print $2}')"
else
INSTALLED_VERSION="none"
fi
say ""
say "${BOLD}${BLUE}OpenPreview${RESET}"
say "${DIM}Installed version:${RESET} ${INSTALLED_VERSION}"
say ""
say "${DIM}Installing OpenPreview version:${RESET} ${BOLD}${VERSION}${RESET}"
say ""
curl_download "${BASE_DOWNLOAD_URL}" -o "${ARCHIVE_PATH}" \
|| fail "Download failed. Release asset ${ASSET_NAME} was not found for ${TAG}. If the repo is private, set OPENPREVIEW_GITHUB_TOKEN or GITHUB_TOKEN."
curl_download "${CHECKSUM_URL}" -o "${CHECKSUM_PATH}" \
|| fail "Checksum file not found for ${ASSET_NAME}. Refusing to install unverified binary."
EXPECTED_SHA="$(awk 'NR==1 {print $1}' "${CHECKSUM_PATH}")"
ACTUAL_SHA="$(sha256_file "${ARCHIVE_PATH}")"
[ "${EXPECTED_SHA}" = "${ACTUAL_SHA}" ] || fail "Checksum verification failed for ${ASSET_NAME}."
tar -xzf "${ARCHIVE_PATH}" -C "${cleanup_dir}"
mkdir -p "${INSTALL_DIR}"
if [ "${OS}" = "darwin" ] && command -v codesign >/dev/null 2>&1; then
codesign --sign - --force "${cleanup_dir}/${APP_NAME}" 2>/dev/null || true
fi
if command -v install >/dev/null 2>&1; then
install -m 755 "${cleanup_dir}/${APP_NAME}" "${INSTALL_DIR}/${APP_NAME}"
else
cp "${cleanup_dir}/${APP_NAME}" "${INSTALL_DIR}/${APP_NAME}"
chmod 755 "${INSTALL_DIR}/${APP_NAME}"
fi
say ""
say "${GREEN}Installed ${APP_NAME} to ${INSTALL_DIR}/${APP_NAME}${RESET}"
say ""
say "${DIM}To start:${RESET}"
say " ${BOLD}preview"
say " ${BOLD}preview https://docs.example.com${RESET}"
say " ${BOLD}preview --inspect https://docs.example.com${RESET}"
say " ${BOLD}preview update${RESET}"
say ""
say "${DIM}When a page looks wrong:${RESET}"
say " ${BOLD}preview --mode docs <url>${RESET}"
say " ${BOLD}preview --explain <url>${RESET}"
case ":${PATH}:" in
*":${INSTALL_DIR}:"*) ;;
*)
say ""
say "${YELLOW}${INSTALL_DIR} is not in your PATH.${RESET}"
say "Add this to your shell profile:"
say " export PATH=\"${INSTALL_DIR}:\$PATH\""
;;
esac
say ""
say "${DIM}More info:${RESET} https://github.com/${REPO}"