Repository navigation
Expand file tree
/
Copy pathslotup
More file actions
executable file
·299 lines (244 loc) · 8.67 KB
/
Copy pathslotup
File metadata and controls
executable file
·299 lines (244 loc) · 8.67 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
#!/usr/bin/env bash
set -e
BASE_DIR=${XDG_CONFIG_HOME:-$HOME}
SLOT_DIR=${SLOT_DIR-"$BASE_DIR/.slot"}
SLOT_BIN_DIR="$SLOT_DIR/bin"
BINS=(slot)
export RUSTFLAGS="-C target-cpu=native"
main() {
need_cmd git
need_cmd curl
while [[ $1 ]]; do
case $1 in
--) shift; break;;
-r|--repo) shift; SLOTUP_REPO=$1;;
-b|--branch) shift; SLOTUP_BRANCH=$1;;
-v|--version) shift; SLOTUP_VERSION=$1;;
-p|--path) shift; SLOTUP_LOCAL_REPO=$1;;
-P|--pr) shift; SLOTUP_PR=$1;;
-C|--commit) shift; SLOTUP_COMMIT=$1;;
-h|--help)
usage
exit 0
;;
*)
warn "unknown option: $1"
usage
exit 1
esac; shift
done
# Print the banner after successfully parsing args
banner
if [ -n "$SLOTUP_PR" ]; then
if [ -z "$SLOTUP_BRANCH" ]; then
SLOTUP_BRANCH="refs/pull/$SLOTUP_PR/head"
else
err "can't use --pr and --branch at the same time"
fi
fi
# Installs slot from a local repository if --path parameter is provided
if [[ -n "$SLOTUP_LOCAL_REPO" ]]; then
need_cmd cargo
# Ignore branches/versions as we do not want to modify local git state
if [ -n "$SLOTUP_REPO" ] || [ -n "$SLOTUP_BRANCH" ] || [ -n "$SLOTUP_VERSION" ]; then
warn "--branch, --version, and --repo arguments are ignored during local install"
fi
# Enter local repo and build
say "installing from $SLOTUP_LOCAL_REPO"
cd "$SLOTUP_LOCAL_REPO"
ensure cargo build --release # need 4 speed
for bin in "${BINS[@]}"; do
# Remove prior installations if they exist
rm -f "$SLOT_BIN_DIR/$bin"
# Symlink from local repo binaries to bin dir
ensure ln -s "$PWD/target/release/$bin" "$SLOT_BIN_DIR/$bin"
done
say "done"
exit 0
fi
SLOTUP_REPO=${SLOTUP_REPO-cartridge-gg/slot}
# Install by downloading binaries
if [[ "$SLOTUP_REPO" == "cartridge-gg/slot" && -z "$SLOTUP_BRANCH" && -z "$SLOTUP_COMMIT" ]]; then
SLOTUP_VERSION=${SLOTUP_VERSION-stable}
SLOTUP_TAG=$SLOTUP_VERSION
# Normalize versions (handle channels, versions without v prefix
if [[ "$SLOTUP_VERSION" == "stable" ]]; then
# Fetch the list of releases from the GitHub API and filter out `prerelease`` releases and `alpha`` releases
SLOTUP_TAG=$(curl -s "https://api.github.com/repos/${SLOTUP_REPO}/releases" \
| grep -oE '"tag_name": "[^"]*"|"prerelease": (true|false)' \
| grep -B1 '"prerelease": false' \
| grep '"tag_name":' \
| grep -oE '"v[0-9]*\.[0-9]*\.[0-9]*"' \
| tr -d '"' \
| head -n 1)
SLOTUP_VERSION=$SLOTUP_TAG
elif [[ "$SLOTUP_VERSION" == nightly* ]]; then
SLOTUP_VERSION="nightly"
elif [[ "$SLOTUP_VERSION" == [[:digit:]]* ]]; then
# Add v prefix
SLOTUP_VERSION="v${SLOTUP_VERSION}"
SLOTUP_TAG="${SLOTUP_VERSION}"
fi
say "installing slot (version ${SLOTUP_VERSION}, tag ${SLOTUP_TAG})"
PLATFORM="$(uname -s)"
EXT="tar.gz"
case $PLATFORM in
Linux)
PLATFORM="linux"
;;
Darwin)
PLATFORM="darwin"
;;
MINGW*)
EXT="zip"
PLATFORM="win32"
;;
*)
err "unsupported platform: $PLATFORM"
;;
esac
ARCHITECTURE="$(uname -m)"
if [ "${ARCHITECTURE}" = "x86_64" ]; then
# Redirect stderr to /dev/null to avoid printing errors if non Rosetta.
if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null)" = "1" ]; then
ARCHITECTURE="arm64" # Rosetta.
else
ARCHITECTURE="amd64" # Intel.
fi
elif [ "${ARCHITECTURE}" = "arm64" ] ||[ "${ARCHITECTURE}" = "aarch64" ] ; then
ARCHITECTURE="arm64" # Arm.
else
ARCHITECTURE="amd64" # Amd.
fi
# Compute the URL of the release tarball in the Slot repository.
RELEASE_URL="https://github.com/${SLOTUP_REPO}/releases/download/${SLOTUP_TAG}/"
BIN_ARCHIVE_URL="${RELEASE_URL}slot_${SLOTUP_VERSION}_${PLATFORM}_${ARCHITECTURE}.$EXT"
echo $BIN_ARCHIVE_URL
# Download and extract the binaries archive
say "downloading latest slot"
if [ "$PLATFORM" = "win32" ]; then
tmp="$(mktemp -d 2>/dev/null || echo ".")/slot.zip"
ensure download "$BIN_ARCHIVE_URL" "$tmp"
ensure unzip "$tmp" -d "$SLOT_BIN_DIR"
rm -f "$tmp"
else
ensure download "$BIN_ARCHIVE_URL" | ensure tar -xzC "$SLOT_BIN_DIR"
fi
for bin in "${BINS[@]}"; do
bin_path="$SLOT_BIN_DIR/$bin"
# Print installed msg
say "installed - $(ensure "$bin_path" --version)"
# Check if the default path of the binary is not in SLOT_BIN_DIR
which_path="$(which "$bin")"
if [ "$which_path" != "$bin_path" ]; then
warn ""
cat 1>&2 <<EOF
There are multiple binaries with the name '$bin' present in your 'PATH'.
This may be the result of installing '$bin' using another method,
like Cargo or other package managers.
You may need to run 'rm $which_path' or move '$SLOT_BIN_DIR'
in your 'PATH' to allow the newly installed version to take precedence!
EOF
fi
done
say "done!"
# Install by cloning the repo with the provided branch/tag
else
need_cmd cargo
SLOTUP_BRANCH=${SLOTUP_BRANCH-main}
REPO_PATH="$SLOT_DIR/$SLOTUP_REPO"
# If repo path does not exist, grab the author from the repo, make a directory in .slot, cd to it and clone.
if [ ! -d "$REPO_PATH" ]; then
AUTHOR="$(echo "$SLOTUP_REPO" | cut -d'/' -f1 -)"
ensure mkdir -p "$SLOT_DIR/$AUTHOR"
cd "$SLOT_DIR/$AUTHOR"
ensure git clone "https://github.com/$SLOTUP_REPO"
fi
# Force checkout, discarding any local changes
cd "$REPO_PATH"
ensure git fetch origin "${SLOTUP_BRANCH}:remotes/origin/${SLOTUP_BRANCH}"
ensure git checkout "origin/${SLOTUP_BRANCH}"
# If set, checkout specific commit from branch
if [ -n "$SLOTUP_COMMIT" ]; then
say "installing at commit $SLOTUP_COMMIT"
ensure git checkout "$SLOTUP_COMMIT"
fi
# Build the repo and install the binaries locally to the .slot bin directory.
# --root appends /bin to the directory it is given, so we pass SLOT_DIR.
ensure cargo install --path ./slot --bin slot --locked --force --root "$SLOT_DIR"
say "done"
fi
}
usage() {
cat 1>&2 <<EOF
The installer for Slot.
Update or revert to a specific Slot version with ease.
USAGE:
SLOTUP <OPTIONS>
OPTIONS:
-h, --help Print help information
-v, --version Install a specific version
-b, --branch Install a specific branch
-P, --pr Install a specific Pull Request
-C, --commit Install a specific commit
-r, --repo Install from a remote GitHub repo (uses default branch if no other options are set)
-p, --path Install a local repository
EOF
}
say() {
printf "SLOTUP: %s\n" "$1"
}
warn() {
say "warning: ${1}" >&2
}
err() {
say "$1" >&2
exit 1
}
need_cmd() {
if ! check_cmd "$1"; then
err "need '$1' (command not found)"
fi
}
check_cmd() {
command -v "$1" &>/dev/null
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing
# command.
ensure() {
if ! "$@"; then err "command failed: $*"; fi
}
# Downloads $1 into $2 or stdout
download() {
if [ "$2" ]; then
# output into $2
if check_cmd curl; then
curl -#o "$2" -L "$1"
else
wget --show-progress -qO "$2" "$1"
fi
else
# output to stdout
if check_cmd curl; then
curl -#L "$1"
else
wget --show-progress -qO- "$1"
fi
fi
}
# Banner Function for Slot
banner() {
printf '
═════════════════════════════════════════════════════════════════════════
███████╗██╗ ██████╗ ████████╗
██╔════╝██║ ██╔═══██╗╚══██╔══╝
███████╗██║ ██║ ██║ ██║
╚════██║██║ ██║ ██║ ██║
███████║███████╗╚██████╔╝ ██║
╚══════╝╚══════╝ ╚═════╝ ╚═╝
Repo : https://github.com/cartridge-gg/slot
═════════════════════════════════════════════════════════════════════════
'
}
main "$@" || exit 1