-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall
More file actions
executable file
·225 lines (190 loc) · 5.49 KB
/
Copy pathinstall
File metadata and controls
executable file
·225 lines (190 loc) · 5.49 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
#!/usr/bin/env bash
set -euo pipefail
APP="discode"
OWNER="siisee11"
REPO="discode"
INSTALL_DIR="$HOME/.discode/bin"
NO_MODIFY_PATH=false
while [[ $# -gt 0 ]]; do
case "$1" in
--no-modify-path)
NO_MODIFY_PATH=true
shift
;;
-h|--help)
cat <<'EOF'
Discode installer
Usage:
curl -fsSL https://discode.chat/install | bash
curl -fsSL https://discode.chat/install | bash -s -- --no-modify-path
EOF
exit 0
;;
*)
echo "Warning: Unknown option '$1' (ignored)" >&2
shift
;;
esac
done
mkdir -p "$INSTALL_DIR"
platform_raw=$(uname -s)
arch_raw=$(uname -m)
case "$platform_raw" in
Darwin) platform="darwin" ;;
Linux) platform="linux" ;;
MINGW*|MSYS*|CYGWIN*) platform="windows" ;;
*) echo "Unsupported platform: $platform_raw"; exit 1 ;;
esac
case "$arch_raw" in
aarch64|arm64) arch="arm64" ;;
x86_64|amd64) arch="x64" ;;
*) echo "Unsupported architecture: $arch_raw"; exit 1 ;;
esac
suffix="$platform-$arch"
if [[ "$platform" == "linux" ]]; then
if [[ -f /etc/alpine-release ]] || (command -v ldd >/dev/null 2>&1 && ldd --version 2>&1 | grep -qi musl); then
suffix="$suffix-musl"
fi
fi
if [[ "$arch" == "x64" ]]; then
if [[ "$platform" == "linux" ]] && ! grep -qi avx2 /proc/cpuinfo 2>/dev/null; then
suffix="$platform-$arch-baseline${suffix##*$arch}"
fi
if [[ "$platform" == "darwin" ]]; then
avx2=$(sysctl -n hw.optional.avx2_0 2>/dev/null || echo 0)
if [[ "$avx2" != "1" ]]; then
suffix="$platform-$arch-baseline"
fi
fi
fi
archive_ext="zip"
if [[ "$platform" == "linux" ]]; then
archive_ext="tar.gz"
fi
urlencode_path_segment() {
local value="$1"
value="${value//@/%40}"
value="${value//\//%2f}"
printf '%s' "$value"
}
json_extract_first_string() {
local key="$1"
sed -n "s/.*\"$key\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1/p" | head -n 1
}
npm_latest_version() {
local meta_pkg
meta_pkg="$(urlencode_path_segment "@$OWNER/$REPO")"
curl -fsSL "https://registry.npmjs.org/$meta_pkg/latest" | json_extract_first_string "version"
}
npm_tarball_url() {
local package_name="$1"
local package_version="$2"
local encoded_name
encoded_name="$(urlencode_path_segment "$package_name")"
curl -fsSL "https://registry.npmjs.org/$encoded_name/$package_version" | json_extract_first_string "tarball"
}
github_latest_version() {
curl -fsSL "https://api.github.com/repos/$OWNER/$REPO/releases/latest" | sed -n 's/.*"tag_name": *"v\([^"]*\)".*/\1/p'
}
version="${VERSION:-}"
if [[ -z "$version" ]]; then
version="$(npm_latest_version || true)"
fi
if [[ -z "$version" ]]; then
version="$(github_latest_version || true)"
fi
if [[ -z "$version" ]]; then
echo "Unable to determine release version from npm or GitHub."
exit 1
fi
filename="$APP-$suffix.$archive_ext"
url="https://github.com/$OWNER/$REPO/releases/download/v$version/$filename"
package_name="@$OWNER/$APP-$suffix"
tmp_dir="${TMPDIR:-/tmp}/discode-install-$$"
mkdir -p "$tmp_dir"
trap 'rm -rf "$tmp_dir"' EXIT
binary=""
tarball_url="$(npm_tarball_url "$package_name" "$version" || true)"
if [[ -n "$tarball_url" ]]; then
echo "Installing $APP v$version ($suffix) from npm package $package_name"
curl -fL "$tarball_url" -o "$tmp_dir/package.tgz"
tar -xzf "$tmp_dir/package.tgz" -C "$tmp_dir"
binary="$tmp_dir/package/bin/$APP"
if [[ "$platform" == "windows" && -f "$tmp_dir/package/bin/$APP.exe" ]]; then
binary="$tmp_dir/package/bin/$APP.exe"
fi
else
echo "npm package $package_name@$version not available; falling back to GitHub releases"
curl -fL "$url" -o "$tmp_dir/$filename"
if [[ "$archive_ext" == "zip" ]]; then
unzip -q "$tmp_dir/$filename" -d "$tmp_dir"
else
tar -xzf "$tmp_dir/$filename" -C "$tmp_dir"
fi
binary="$tmp_dir/$APP"
if [[ "$platform" == "windows" ]]; then
binary="$tmp_dir/$APP.exe"
fi
fi
if [[ ! -f "$binary" ]]; then
echo "Downloaded package did not contain $APP binary"
exit 1
fi
cp "$binary" "$INSTALL_DIR/$APP"
chmod 755 "$INSTALL_DIR/$APP"
add_to_path() {
local shell_name="$1"
local command_line
local config_file=""
case "$shell_name" in
fish) command_line="fish_add_path $INSTALL_DIR" ;;
*) command_line="export PATH=\"$INSTALL_DIR:\$PATH\"" ;;
esac
local candidates=()
case "$shell_name" in
zsh)
candidates=("${ZDOTDIR:-$HOME}/.zshrc" "${XDG_CONFIG_HOME:-$HOME/.config}/zsh/.zshrc")
;;
bash)
candidates=("$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.profile")
;;
fish)
candidates=("$HOME/.config/fish/config.fish")
;;
*)
candidates=("$HOME/.profile")
;;
esac
# Prefer an existing writable config file.
for file in "${candidates[@]}"; do
if [[ -f "$file" && -w "$file" ]]; then
config_file="$file"
break
fi
done
# Fall back to creating the first candidate in home config.
if [[ -z "$config_file" ]]; then
config_file="${candidates[0]}"
mkdir -p "$(dirname "$config_file")"
touch "$config_file"
fi
if grep -Fq "$INSTALL_DIR" "$config_file"; then
echo "PATH already configured in: $config_file"
return
fi
{
echo
echo "# discode"
echo "$command_line"
} >> "$config_file"
echo "Added PATH entry to: $config_file"
}
echo "Installed to: $INSTALL_DIR/$APP"
if [[ "$NO_MODIFY_PATH" == "true" ]]; then
echo "Skipped PATH update (--no-modify-path)."
echo "Add to PATH manually: export PATH=\"$INSTALL_DIR:\$PATH\""
else
current_shell="$(basename "${SHELL:-sh}")"
add_to_path "$current_shell"
fi
echo "Run now: export PATH=\"$INSTALL_DIR:\$PATH\""