forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoundryup
executable file
·223 lines (189 loc) · 7.08 KB
/
foundryup
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
#!/usr/bin/env bash
set -e
FOUNDRY_DIR=${FOUNDRY_DIR-"$HOME/.foundry"}
FOUNDRY_BIN_DIR="$FOUNDRY_DIR/bin"
FOUNDRY_MAN_DIR="$FOUNDRY_DIR/share/man/man1"
main() {
need_cmd git
need_cmd curl
while [[ $1 ]]; do
case $1 in
--) shift; break;;
-r|--repo) shift; FOUNDRYUP_REPO=$1;;
-b|--branch) shift; FOUNDRYUP_BRANCH=$1;;
-v|--version) shift; FOUNDRYUP_VERSION=$1;;
-p|--path) shift; FOUNDRYUP_LOCAL_REPO=$1;;
-P|--pr) shift; FOUNDRYUP_PR=$1;;
-C|--commit) shift; FOUNDRYUP_COMMIT=$1;;
-h|--help)
usage
exit 0
;;
*)
err "internal error: unknown option "$1"\n";;
esac; shift
done
if [ ! -z "$FOUNDRYUP_PR" ]; then
if [ -z "$FOUNDRYUP_BRANCH" ]; then
FOUNDRYUP_BRANCH="refs/pull/$FOUNDRYUP_PR/head"
else
err "can't use --pr and --branch at the same time"
fi
fi
# Installs foundry from a local repository if --path parameter is provided
if [[ -n "$FOUNDRYUP_LOCAL_REPO" ]]; then
need_cmd cargo
# Ignore branches/versions as we do not want to modify local git state
if [ -n "$FOUNDRYUP_REPO" ] || [ -n "$FOUNDRY_BRANCH" ] || [ -n "$FOUNDRY_VERSION" ]; then
warn "--branch, --version, and --repo arguments are ignored during local install"
fi
# Enter local repo and build
say "installing from $FOUNDRYUP_LOCAL_REPO"
cd $FOUNDRYUP_LOCAL_REPO
RUSTFLAGS="-C target-cpu=native" ensure cargo build --release # need 4 speed
# Remove prior installations if they exist
rm -f "$FOUNDRY_BIN_DIR/forge"
rm -f "$FOUNDRY_BIN_DIR/cast"
# Symlink from local repo binaries to bin dir
ensure ln -s "$PWD/target/release/forge" "$FOUNDRY_BIN_DIR/forge"
ensure ln -s "$PWD/target/release/cast" "$FOUNDRY_BIN_DIR/cast"
say "done"
exit 0
fi
FOUNDRYUP_REPO=${FOUNDRYUP_REPO-gakonst/foundry}
if [[ "$FOUNDRYUP_REPO" == "gakonst/foundry" && -z "$FOUNDRYUP_BRANCH" && -z "$FOUNDRYUP_COMMIT" ]]; then
FOUNDRYUP_VERSION=${FOUNDRYUP_VERSION-nightly}
FOUNDRYUP_TAG=$FOUNDRYUP_VERSION
# Normalize versions (handle channels, versions without v prefix
if [[ "$FOUNDRYUP_VERSION" == "nightly" ]]; then
# Locate real nightly tag
SHA=$(curl -sSf https://api.github.com/repos/${FOUNDRYUP_REPO}/git/refs/tags/nightly \
| grep -Eo '"sha"[^,]*' \
| grep -Eo '[^:]*$' \
| tr -d '"' \
| tr -d ' ')
FOUNDRYUP_TAG="nightly-${SHA}"
elif [[ "$FOUNDRYUP_VERSION" == nightly* ]]; then
FOUNDRYUP_VERSION="nightly"
elif [[ "$FOUNDRYUP_VERSION" == [[:digit:]]* ]]; then
# Add v prefix
FOUNDRYUP_VERSION="v${FOUNDRYUP_VERSION}"
FOUNDRYUP_TAG="${FOUNDRYUP_VERSION}"
fi
say "installing foundry (version ${FOUNDRYUP_VERSION}, tag ${FOUNDRYUP_TAG})"
PLATFORM="$(uname -s)"
case $PLATFORM in
Linux)
PLATFORM="linux"
;;
Darwin)
PLATFORM="darwin"
;;
*)
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 Foundry repository.
RELEASE_URL="https://github.com/${FOUNDRYUP_REPO}/releases/download/${FOUNDRYUP_TAG}/"
BIN_TARBALL_URL="${RELEASE_URL}foundry_${FOUNDRYUP_VERSION}_${PLATFORM}_${ARCHITECTURE}.tar.gz"
MAN_TARBALL_URL="${RELEASE_URL}foundry_man_${FOUNDRYUP_VERSION}.tar.gz"
# Download the binaries tarball and unpack it into the .foundry bin directory.
say "downloading latest forge and cast"
ensure curl -# -L $BIN_TARBALL_URL | tar -xzC $FOUNDRY_BIN_DIR
# Download the man tarball and unpack it into the .foundry man directory.
say "downloading manpages"
ensure curl -# -L $MAN_TARBALL_URL | tar -xzC $FOUNDRY_MAN_DIR
say "installed - $($FOUNDRY_BIN_DIR/forge --version)"
say "installed - $($FOUNDRY_BIN_DIR/cast --version)"
say "done"
if [[ $(which forge) =~ "cargo" ]]; then
warn "it appears your system has already has forge installed via cargo. you may need to run 'rm $(which forge)' to allow foundryup to take precedence!"
fi
if [[ $(which cast) =~ "cargo" ]]; then
warn "it appears your system has already has cast installed via cargo. you may need to run 'rm $(which cast)' to allow foundryup to take precedence!"
fi
else
need_cmd cargo
FOUNDRYUP_BRANCH=${FOUNDRYUP_BRANCH-master}
REPO_PATH="${FOUNDRY_DIR}/${FOUNDRYUP_REPO}"
if [ ! -d $REPO_PATH ]; then
# Repo path did not exist, grab the author from the repo, make a directory in .foundry, cd to it and clone.
IFS="/" read -ra AUTHOR <<< "$FOUNDRYUP_REPO"
ensure mkdir -p "$FOUNDRY_DIR/$AUTHOR"
cd "$FOUNDRY_DIR/$AUTHOR"
ensure git clone https://github.com/${FOUNDRYUP_REPO}
fi
# force checkout, discarding any local changes
cd $REPO_PATH
ensure git fetch origin ${FOUNDRYUP_BRANCH}:remotes/origin/${FOUNDRYUP_BRANCH}
ensure git checkout origin/${FOUNDRYUP_BRANCH}
# If set, checkout specific commit from branch
if [ ! -z $FOUNDRYUP_COMMIT ]; then
say "installing at commit ${FOUNDRYUP_COMMIT}"
ensure git checkout ${FOUNDRYUP_COMMIT}
fi
# Build the repo and install it locally to the .foundry bin directory.
# --root appends /bin to the directory it is given, so we pass FOUNDRY_DIR.
RUSTFLAGS="-C target-cpu=native" ensure cargo install --path ./cli --bins --locked --force --root $FOUNDRY_DIR
# If help2man is installed, use it to add Foundry man pages.
if command -v help2man &> /dev/null ; then
help2man -N $FOUNDRY_BIN_DIR/forge > $FOUNDRY_MAN_DIR/forge.1
help2man -N $FOUNDRY_BIN_DIR/cast > $FOUNDRY_MAN_DIR/cast.1
fi
say "done"
fi
}
usage() {
cat 1>&2 <<EOF
The installer for Foundry.
Update or revert to a specific Foundry version with ease.
USAGE:
foundryup <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 a forks main branch
-p, --path Install a local repository
EOF
}
say() {
printf 'foundryup: %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 2>&1
}
# 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
}
main "$@" || exit 1