Skip to content

Commit 8ecc62a

Browse files
authored
Install new asdf (v0.16+) (#5)
- If asdf is not found, install new asdf version instead of old one - Don't block further installation of the tools after installing asdf (following up on #1 (comment)) - Don't cancel asdf install on unrecognized shell, give instructions to update PATH instead - Minor improvements to asdf install confirmation prompt: detect non-interactive shell and display the relevant err msg
1 parent 87c2718 commit 8ecc62a

File tree

2 files changed

+89
-71
lines changed

2 files changed

+89
-71
lines changed

.github/workflows/_check.yml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,6 @@ jobs:
6464
steps:
6565
- uses: actions/checkout@v4
6666

67-
- name: Setup asdf
68-
run: |
69-
if [ "${{ runner.os }}" = "Linux" ]; then
70-
ASDF_PLATFORM="linux-amd64"
71-
else
72-
ASDF_PLATFORM="darwin-arm64"
73-
fi
74-
75-
curl -fsSL "https://github.com/asdf-vm/asdf/releases/download/${ASDF_VERSION}/asdf-${ASDF_VERSION}-${ASDF_PLATFORM}.tar.gz" | tar xzf - -C /usr/local/bin
76-
echo "${ASDF_DATA_DIR:-$HOME/.asdf}/shims" >> $GITHUB_PATH
77-
7867
- name: Fetch latest Scarb version from GitHub releases
7968
id: scarb_version
8069
run: |
@@ -97,7 +86,12 @@ jobs:
9786
echo "LATEST=$latest_version" >> $GITHUB_OUTPUT
9887
9988
- name: Run starkup
100-
run: ./starkup.sh
89+
shell: bash
90+
env:
91+
SHELL: /bin/bash
92+
run: |
93+
./starkup.sh -y
94+
echo "${ASDF_DATA_DIR:-$HOME/.asdf}/shims" >> $GITHUB_PATH
10195
10296
- name: Check Scarb latest
10397
run: scarb --version | grep "scarb ${{ steps.scarb_version.outputs.LATEST }}"

starkup.sh

Lines changed: 83 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ set -eu
55
ASDF_INSTALL_DOCS="https://asdf-vm.com/guide/getting-started.html"
66
SCARB_UNINSTALL_INSTRUCTIONS="For uninstallation instructions, refer to https://docs.swmansion.com/scarb/download#uninstall"
77
# TODO(#2): Link snfoundry uninstall docs once they are available
8-
STARKNET_FOUNDRY_UNINSTALL_INSTRUCTIONS="Try removing snforge and sncast binaries from $HOME/.local/bin"
9-
SCRIPT_VERSION="0.1.0"
10-
DEFAULT_ASDF_VERSION="v0.15.0"
8+
LOCAL_BIN="${HOME}/.local/bin"
9+
LOCAL_BIN_ESCAPED="\${HOME}/.local/bin"
10+
ASDF_SHIMS="${ASDF_DATA_DIR:-$HOME/.asdf}/shims"
11+
ASDF_SHIMS_ESCAPED="\${ASDF_DATA_DIR:-\$HOME/.asdf}/shims"
12+
STARKNET_FOUNDRY_UNINSTALL_INSTRUCTIONS="Try removing snforge and sncast binaries from ${LOCAL_BIN}"
13+
SCRIPT_VERSION="0.2.0"
14+
DEFAULT_ASDF_VERSION="v0.16.2"
1115

1216
usage() {
1317
cat <<EOF
@@ -18,11 +22,14 @@ Usage: $0 [OPTIONS]
1822
Options:
1923
-h, --help Print help
2024
-V, --version Print script version
25+
-y, --yes Disable confirmation prompt
2126
2227
EOF
2328
}
2429

2530
main() {
31+
_need_interaction=true
32+
2633
for arg in "$@"; do
2734
case "$arg" in
2835
-h | --help)
@@ -33,13 +40,16 @@ main() {
3340
say "starkup $SCRIPT_VERSION"
3441
exit 0
3542
;;
43+
-y | --yes)
44+
_need_interaction=false
45+
;;
3646
*)
3747
err "invalid option '$arg'. For more information, try '--help'."
3848
;;
3949
esac
4050
done
4151

42-
assert_dependencies
52+
assert_dependencies "$_need_interaction"
4353
assert_not_installed_outside_asdf
4454

4555
install_latest_asdf_plugin "scarb"
@@ -52,7 +62,6 @@ main() {
5262
install_latest_version "starknet-foundry"
5363
set_global_latest_version "starknet-foundry"
5464

55-
_shell_config=""
5665
_completion_message=""
5766

5867
case ${SHELL:-""} in
@@ -73,23 +82,20 @@ main() {
7382
_completion_message="Run '. ${_shell_config}'"
7483
;;
7584
*)
85+
say "Could not detect shell. Make sure ${LOCAL_BIN_ESCAPED} and ${ASDF_SHIMS_ESCAPED} are added to your PATH."
7686
_completion_message="Source your shell configuration file"
7787
;;
7888
esac
7989

80-
if ! check_cmd universal-sierra-compiler; then
81-
_local_bin="${HOME}/.local/bin"
82-
say "Couldn't finish universal-sierra-compiler installation, try manually adding ${_local_bin} to your PATH."
83-
fi
84-
8590
say "Installation complete. ${_completion_message} or start a new terminal session to use the installed tools."
8691
}
8792

8893
assert_dependencies() {
94+
_need_interaction="$1"
8995
need_cmd curl
9096
need_cmd git
9197
if ! check_cmd asdf; then
92-
install_asdf_interactively
98+
install_asdf "$_need_interaction"
9399
fi
94100
}
95101

@@ -224,61 +230,79 @@ is_asdf_legacy() {
224230
printf '%s\n%s' "$_version" "0.16.0" | sort -V | head -n1 | grep -xqvF "0.16.0"
225231
}
226232

227-
install_asdf_interactively() {
228-
_profile=""
229-
_pref_shell=""
230-
_completion_message=""
231-
_asdf_path="$HOME/.asdf"
232-
233-
case ${SHELL:-""} in
234-
*/zsh)
235-
_profile=$HOME/.zshrc
236-
_pref_shell=zsh
237-
_completion_message="Run 'source ${_profile}'"
238-
;;
239-
*/bash)
240-
if [ "$(uname)" = "Darwin" ]; then
241-
_profile=$HOME/.bash_profile
233+
install_asdf() {
234+
_need_interaction="$1"
235+
_answer=""
236+
if "$_need_interaction"; then
237+
say "asdf-vm is required but not found.\nFor seamless updates, install it using a package manager (e.g., Homebrew, AUR helpers). See details: ${ASDF_INSTALL_DOCS}.\nAlternatively, the script can install asdf-vm directly, but manual updates might be needed later.\nProceed with direct installation? (y/N):"
238+
if [ ! -t 0 ]; then
239+
# Starkup is going to want to ask for confirmation by
240+
# reading stdin. This script may be piped into `sh` though
241+
# and wouldn't have stdin to pass to its children. Instead we're
242+
# going to explicitly connect /dev/tty to the installer's stdin.
243+
if [ ! -t 1 ] || [ ! -r /dev/tty ]; then
244+
err "Unable to run interactively."
245+
fi
246+
read -r _answer </dev/tty
242247
else
243-
_profile=$HOME/.bashrc
248+
read -r _answer
244249
fi
245-
_pref_shell=bash
246-
_completion_message="Run 'source ${_profile}'"
247-
;;
248-
*/sh)
249-
_profile=$HOME/.profile
250-
_pref_shell="sh"
251-
_completion_message="Run '. ${_profile}'"
252-
;;
253-
esac
254-
255-
if [ -z "$_profile" ] || [ -z "$_pref_shell" ]; then
256-
err "asdf-vm is required. Please install it manually and re-run this script. For installation instructions, refer to ${ASDF_INSTALL_DOCS}."
250+
else
251+
_answer="y"
257252
fi
258253

259-
touch "$_profile"
254+
if [ "$_answer" = "y" ] || [ "$_answer" = "Y" ]; then
255+
need_cmd tar
256+
257+
# shellcheck disable=SC2015
258+
_latest_version=$(get_latest_gh_version "asdf-vm/asdf") && [ -n "$_latest_version" ] || {
259+
say "Failed to fetch latest asdf version (possibly due to GitHub server rate limit or error). Using default version ${DEFAULT_ASDF_VERSION}."
260+
_latest_version="$DEFAULT_ASDF_VERSION"
261+
}
262+
263+
say "Installing asdf-vm ${_latest_version}..."
264+
265+
_os="$(uname -s)"
266+
_arch="$(uname -m)"
267+
case "${_os}-${_arch}" in
268+
"Linux-x86_64") _platform="linux-amd64" ;;
269+
"Linux-aarch64") _platform="linux-arm64" ;;
270+
"Linux-i386" | "Linux-i686") _platform="linux-386" ;;
271+
"Darwin-x86_64") _platform="darwin-amd64" ;;
272+
"Darwin-arm64") _platform="darwin-arm64" ;;
273+
*) err "Unsupported platform ${_os}-${_arch}. Please install asdf-vm manually and re-run this script. For installation instructions, refer to ${ASDF_INSTALL_DOCS}." ;;
274+
esac
260275

261-
say "asdf-vm is required. Do you want to install it now? (y/N): "
262-
# Starkup is going to want to ask for confirmation by
263-
# reading stdin. This script may be piped into `sh` though
264-
# and wouldn't have stdin to pass to its children. Instead we're
265-
# going to explicitly connect /dev/tty to the installer's stdin.
266-
if [ ! -t 0 ] && [ -r /dev/tty ]; then
267-
read -r answer </dev/tty
268-
else
269-
read -r answer
270-
fi
271-
if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
272-
# TODO: https://github.com/software-mansion/scarb/issues/1938
273-
# Support newer versions of asdf-vm
274-
_version="$DEFAULT_ASDF_VERSION"
275-
say "Installing asdf-vm ${_version}..."
276-
git clone --quiet -c advice.detachedHead=false https://github.com/asdf-vm/asdf.git "$_asdf_path" --branch "$_version"
276+
mkdir -p "$LOCAL_BIN"
277+
278+
curl -sSL --fail "https://github.com/asdf-vm/asdf/releases/download/${_latest_version}/asdf-${_latest_version}-${_platform}.tar.gz" | tar xzf - -C "$LOCAL_BIN"
277279

278-
echo >>"$_profile" && echo ". ${_asdf_path}/asdf.sh" >>"$_profile"
280+
export PATH="${LOCAL_BIN}:$PATH"
281+
export PATH="${ASDF_SHIMS}:$PATH"
279282

280-
say "asdf-vm has been installed. ${_completion_message} or start a new terminal session and re-run this script."
281-
exit 0
283+
_profile=""
284+
case ${SHELL:-""} in
285+
*/zsh)
286+
_profile=$HOME/.zshrc
287+
;;
288+
*/bash)
289+
if [ "$(uname)" = "Darwin" ]; then
290+
_profile=$HOME/.bash_profile
291+
else
292+
_profile=$HOME/.bashrc
293+
fi
294+
;;
295+
*/sh)
296+
_profile=$HOME/.profile
297+
;;
298+
esac
299+
300+
if [ -n "$_profile" ]; then
301+
touch "$_profile"
302+
echo >>"$_profile" && echo "export PATH=\"${LOCAL_BIN_ESCAPED}:\$PATH\"" >>"$_profile"
303+
echo >>"$_profile" && echo "export PATH=\"${ASDF_SHIMS_ESCAPED}:\$PATH\"" >>"$_profile"
304+
fi
305+
say "asdf-vm has been installed."
282306
else
283307
err "cancelled asdf-vm installation. Please install it manually and re-run this script. For installation instructions, refer to ${ASDF_INSTALL_DOCS}."
284308
fi

0 commit comments

Comments
 (0)