forked from Jon-Becker/heimdall-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbifrost
executable file
·228 lines (183 loc) · 6.92 KB
/
bifrost
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
#!/usr/bin/env bash
set -e
BIFROST_PATH=${BIFROST_PATH:-"$HOME/.bifrost"}
BIFROST_BIN_DIR="$BIFROST_PATH/bin"
main() {
# ensuring git, curl, and cargo are installed
requires_cmd git
requires_cmd curl
requires_cmd cargo
# parsing parameters
while [[ $1 ]]; do
case $1 in
--) shift; break ;;
-u|--upgrade|--update) shift;
echo "bifrost: removing old binaries"
rm -rf "$BIFROST_PATH"
ensure curl -L https://raw.githubusercontent.com/Jon-Becker/heimdall-rs/main/bifrost/install | bash
exit 0
;;
-v|--version) shift;
TARGET_VERSION=$1
shift
;;
-B|--binary|--bin) shift; USE_BINARY=true ;;
+nightly) shift; NIGHTLY_CHANNEL=true ;;
-h|--help)
usage
exit 0
;;
-l|--list|--versions) shift;
versions
exit 0
;;
*)
echo "bifrost: option '$1' not recognized"
exit 1
;;
esac;
done
# print channel
if [ -n "$NIGHTLY_CHANNEL" ]; then
echo "bifrost: using nightly channel"
else
echo "bifrost: using stable channel"
fi
# remove the current heimdall installation if it exists
ensure rm -f "$BIFROST_BIN_DIR/heimdall"
# make the build path if it doesn't exist
BUILD_PATH="${BIFROST_PATH}/build"
if [ ! -d $BUILD_PATH ]; then
ensure mkdir -p $BUILD_PATH
fi
# remove the source directory if it exists
ensure rm -rf "$BUILD_PATH/heimdall-rs"
# clone heimdall-rs and cd into it
cd $BUILD_PATH
echo "bifrost: cloning 'Jon-Becker/heimdall-rs'."
ensure git clone "https://github.com/Jon-Becker/heimdall-rs" > /dev/null 2>&1
cd "heimdall-rs"
ensure git fetch origin
# if we are nightly, use `main` branch
if [ -n "$NIGHTLY_CHANNEL" ]; then
ensure git checkout main > /dev/null 2>&1
# get the latest short commit hash
TARGET_VERSION=$(git rev-parse --short HEAD)
# get the latest tag
tag=$(git describe --tags `git rev-list --tags --max-count=1`)
# build nightly version
nightly_version="$tag+nightly.$TARGET_VERSION"
echo "bifrost: installing version $nightly_version."
# if they specified a version, checkout that tag or branch
elif [ -n "$TARGET_VERSION" ]; then
echo "bifrost: installing version $TARGET_VERSION."
ensure git checkout $TARGET_VERSION > /dev/null 2>&1
else
# checkout the latest tag
tag=$(git describe --tags `git rev-list --tags --max-count=1`)
echo "bifrost: installing version $tag."
TARGET_VERSION=$tag
ensure git checkout $tag -b latest > /dev/null 2>&1
fi
# if the user wants to use the precompiled binary, download it
if [ -n "$USE_BINARY" ]; then
# nightly binaries are not available
if [ -n "$NIGHTLY_CHANNEL" ]; then
echo "bifrost: nightly binaries are not available."
exit 1
fi
# cd into the binary directory
ensure cd $BIFROST_BIN_DIR
echo "bifrost: fetching binary."
# download the binary
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
ensure curl -k -L -s --compressed "https://github.com/Jon-Becker/heimdall-rs/releases/download/$TARGET_VERSION/heimdall-linux-amd64" -o heimdall
elif [[ "$OSTYPE" == "darwin"* ]]; then
ensure curl -k -L -s --compressed "https://github.com/Jon-Becker/heimdall-rs/releases/download/$TARGET_VERSION/heimdall-macos-amd64" -o heimdall
else
echo "bifrost: unsupported operating system: $OSTYPE"
exit 1
fi
echo "bifrost: installing binary."
# make the binary executable
ensure chmod +x heimdall
else
# get current package version
VERSION=$(grep -m1 "version" Cargo.toml | sed -E 's/.*([0-9]+\.[0-9]+\.[0-9]+).*/\1/')
# remove periods from version and cast as int
VERSION=$(echo $VERSION | sed -E 's/\.//g')
# if nightly, we need to update cargo.toml versions (hacky lol)
if [ -n "$NIGHTLY_CHANNEL" ]; then
find . -name 'Cargo.toml' -type f | while read -r file; do
set_version "$file" "$nightly_version"
done
fi
# if VERSION > 0.6.0 (60), use the new build system
if [ $VERSION -ge 60 ]; then
RUSTFLAGS="-C target-cpu=native -C codegen-units=1" CARGO_PROFILE_RELEASE_LTO=true ensure cargo install --path ./cli --bins --locked --force --root $BIFROST_PATH
else
# using legacy build system
echo "bifrost: installing with old build system."
RUSTFLAGS="-C target-cpu=native -C codegen-units=1" CARGO_PROFILE_RELEASE_LTO=true ensure cargo install --path ./heimdall --locked --force --root $BIFROST_PATH
fi
fi
echo "bifrost: installation complete."
}
# list all available versions of heimdall
versions() {
if [ "$NIGHTLY_CHANNEL" = true ]; then
msg="Available versions of Heimdall (including nightly builds):"
tag_filter="cat" # Do not filter any tags
else
msg="Available versions of Heimdall:"
tag_filter="grep -v '+nightly'" # Exclude nightly builds
fi
cat 1>&2 <<EOF
$msg
Version | Tag |
----------------------------------------- | -------------------------- |
EOF
git ls-remote --tags "https://github.com/Jon-Becker/heimdall-rs" \
| eval $tag_filter \
| awk '{line[NR]=$0} END {for (i=NR; i>0; i--) print line[i]}'
}
# usage prints the usage message
usage() {
cat 1>&2 <<EOF
Bifrost is the version manager for Heimdall.
Install and manage specific versions of Heimdall and it's packages.
USAGE:
bifrost [FLAGS] <OPTIONS>
OPTIONS:
-h, --help Print help information
-u, --update Update bifrost to the latest version
-B, --binary Install a precompiled binary instead of building from source
-v, --version Install a specific version
-l, --list List all available versions
FLAGS:
+nightly Install the latest nightly build
EOF
}
# ensure runs a command and exits if it fails
ensure() {
if ! "$@"; then echo "bifrost: required command '$*' failed."; exit 1; fi
}
# command_exists checks if a command exists
command_exists() {
command -v "$1" > /dev/null 2>&1
}
# requires_cmd checks if a command exists and exits if it doesn't
requires_cmd() {
if ! command_exists "$1"; then
echo "bifrost: '$1' is required but not installed on this system"
exit 1
fi
}
# set the version of $1 to $2
set_version() {
local file=$1
local version=$2
sed -i "" "s/^version.*/version = \"${version}\"/" $file
}
# run main
main "$@" || exit 1