Skip to content

Add script #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 66 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
4480dca
wip
DelevoXDG Dec 11, 2024
5b513bc
Include uninstall docs for 'scarb' and 'starknet-foundry'
DelevoXDG Dec 11, 2024
ba89422
Improve plugin installation; Misc
DelevoXDG Dec 11, 2024
e4771ed
Add bold info messages on supported platforms
DelevoXDG Dec 11, 2024
abb7286
Replace bulk installation with individual version installation
DelevoXDG Dec 11, 2024
bd3bedd
Refactor global version setting
DelevoXDG Dec 11, 2024
eb3222a
Reinstall foundry to ensure latest version of USC is installed
DelevoXDG Dec 11, 2024
e2acb49
refactor
DelevoXDG Dec 11, 2024
8492df3
refactor
DelevoXDG Dec 11, 2024
7b97b9a
wip
DelevoXDG Dec 13, 2024
b8e5bbe
wip
DelevoXDG Dec 13, 2024
b97cd29
Add version and help args
DelevoXDG Dec 13, 2024
a095e67
wip
DelevoXDG Dec 13, 2024
92ce4a1
wip
DelevoXDG Dec 13, 2024
5c0d947
Fix `uninstall_latest_version`
DelevoXDG Dec 13, 2024
4cd9a6b
Add interactive installation for asdf-vm
DelevoXDG Dec 13, 2024
6434249
update description
DelevoXDG Dec 13, 2024
7932825
update shell-based installation instructions
DelevoXDG Dec 13, 2024
503ee7e
Drop support for all shels except bash, zshrc, ash
DelevoXDG Dec 13, 2024
12b03c5
Update asdf-vm installation instructions to use new documentation link
DelevoXDG Dec 13, 2024
bedaacf
wip
DelevoXDG Dec 16, 2024
6d12210
misc
DelevoXDG Dec 16, 2024
940a5cb
Add basic CI
DelevoXDG Dec 16, 2024
9230a1f
Update CI
DelevoXDG Dec 16, 2024
d35a563
Fix format checks
DelevoXDG Dec 16, 2024
491aded
improve ci
DelevoXDG Dec 16, 2024
426cf35
fix prints
DelevoXDG Dec 16, 2024
7e25f6b
misc
DelevoXDG Dec 16, 2024
1ed9000
misc
DelevoXDG Dec 16, 2024
4fb7c8c
Fetch and install latest version of asdf
DelevoXDG Dec 18, 2024
e395d8b
fix
DelevoXDG Dec 19, 2024
79123fe
Use 2 spaces instead of tabs
DelevoXDG Jan 13, 2025
f298591
Add shell source instruction on completion
DelevoXDG Jan 13, 2025
ddf7b65
Format
DelevoXDG Jan 14, 2025
fc13f93
Add .editorconfig with correct indent
DelevoXDG Jan 14, 2025
e65b87a
Improve handling of asdf version fetching
DelevoXDG Jan 14, 2025
41f16f7
update ci per review (remove pwsh use)
DelevoXDG Jan 21, 2025
af5daa8
fmt
DelevoXDG Jan 21, 2025
96a71a3
add sh support; drop ash
DelevoXDG Jan 21, 2025
238f412
Remove if around touch
DelevoXDG Jan 24, 2025
4cf66d3
update error msg
DelevoXDG Jan 24, 2025
2714c80
remove `set +e` around curl
DelevoXDG Jan 24, 2025
dacf543
add `--fail` to curl
DelevoXDG Jan 24, 2025
20ac8da
Revert "remove `set +e` around curl"
DelevoXDG Jan 24, 2025
50fb38a
update snfoundry uninstall instructions
DelevoXDG Jan 24, 2025
25e7407
misc: improve foundry uninst instructions
DelevoXDG Jan 24, 2025
5a550dd
misc: update error message for negative asdf install prompt answer
DelevoXDG Jan 24, 2025
57ddcf4
remove shellcheck
DelevoXDG Jan 24, 2025
7b411b3
drop `local` to make shellcheck happy (posix shell compat)
DelevoXDG Jan 24, 2025
09a73cd
simplify `install_asdf_interactively`
DelevoXDG Jan 24, 2025
71a1685
fmt
DelevoXDG Jan 24, 2025
aff9ee8
simplify fetching asdf version
DelevoXDG Jan 27, 2025
a0fa3e7
install_asdf_interactively: fix completion msg
DelevoXDG Jan 27, 2025
8412992
misc reorder cases
DelevoXDG Jan 27, 2025
d2434f4
install asdf silently
DelevoXDG Jan 27, 2025
5411f72
install USC manually
DelevoXDG Jan 27, 2025
9191854
fix `say` to support newlines
DelevoXDG Jan 28, 2025
eae4d48
fix plugin check
DelevoXDG Jan 28, 2025
96da134
centralize non-asdf install check; fix installation check
DelevoXDG Jan 28, 2025
55adea0
add `check_asdf_plugin_installed`
DelevoXDG Jan 28, 2025
4ad983f
update usc only if non-latest ver is installed
DelevoXDG Jan 28, 2025
a446699
enforce asdf prompt in non-interactive shells
DelevoXDG Feb 3, 2025
7976d20
always use asdf 0.15.0
DelevoXDG Feb 4, 2025
72b44d0
Use correct shell for usc install
DelevoXDG Feb 4, 2025
3a94220
fix `install_universal_sierra_compiler`
DelevoXDG Feb 4, 2025
662b9bc
add final usc check
DelevoXDG Feb 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.sh]
indent_style = space
indent_size = 2
70 changes: 70 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: CI

on:
push:
branches:
- main
pull_request:

jobs:
test:
name: test ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- name: Setup asdf
uses: asdf-vm/actions/setup@v3

- name: Fetch latest Scarb version from GitHub releases
id: scarb_version
run: |
latest_version=$(curl -s --fail https://api.github.com/repos/software-mansion/scarb/releases/latest | grep \"tag_name\": | awk '{print $2}' | tr -d 'v",')
echo "Latest Scarb version found is $latest_version"
echo "LATEST=$latest_version" >> $GITHUB_OUTPUT

- name: Fetch latest Starknet Foundry version from GitHub releases
id: snfoundry_version
run: |
latest_version=$(curl -s --fail https://api.github.com/repos/foundry-rs/starknet-foundry/releases/latest | grep \"tag_name\": | awk '{print $2}' | tr -d 'v",')
echo "Latest Starknet Foundry version found is $latest_version"
echo "LATEST=$latest_version" >> $GITHUB_OUTPUT

- name: Fetch latest Universal Sierra Compiler version from GitHub releases
id: usc_version
run: |
latest_version=$(curl -s --fail https://api.github.com/repos/software-mansion/universal-sierra-compiler/releases/latest | grep \"tag_name\": | awk '{print $2}' | tr -d 'v",')
echo "Latest Universal Sierra Compiler version found is $latest_version"
echo "LATEST=$latest_version" >> $GITHUB_OUTPUT

- name: Run starknetup
run: ./starknetup.sh

- name: Check Scarb latest
run: scarb --version | grep "scarb ${{ steps.scarb_version.outputs.LATEST }}"

- name: Check Starknet Foundry latest
run: |
snforge --version | grep "snforge ${{ steps.snfoundry_version.outputs.LATEST }}"
sncast --version | grep "sncast ${{ steps.snfoundry_version.outputs.LATEST }}"

- name: Check Universal Sierra Compiler latest
run: universal-sierra-compiler --version | grep "universal-sierra-compiler ${LATEST}"

shellcheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: shellcheck starknetup.sh

shfmt:
runs-on: ubuntu-latest
steps:
- uses: mfinelli/setup-shfmt@v3
- uses: actions/checkout@v4
- run: shfmt --diff starknetup.sh
262 changes: 262 additions & 0 deletions starknetup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
#!/bin/sh

set -eu

ASDF_INSTALL_DOCS="https://asdf-vm.com/guide/getting-started.html"
SCARB_UNINSTALL_INSTRUCTIONS="For uninstallation instructions, refer to https://docs.swmansion.com/scarb/download#uninstall"
# TODO(#2): Link snfoundry uninstall docs once they are available
STARKNET_FOUNDRY_UNINSTALL_INSTRUCTIONS="Try removing snforge and sncast binaries from $HOME/.local/bin"
SCRIPT_VERSION="0.1.0"
DEFAULT_ASDF_VERSION="v0.15.0"

usage() {
cat <<EOF
The installer for Starknet tools. Installs the latest versions of Scarb, Starknet Foundry and Universal Sierra Compiler using asdf.

Usage: $0 [OPTIONS]

Options:
-h, --help Print help
-V, --version Print script version

EOF
}

main() {
for arg in "$@"; do
case "$arg" in
-h | --help)
usage
exit 0
;;
-V | --version)
say "starknetup $SCRIPT_VERSION"
exit 0
;;
*)
err "invalid option '$arg'. For more information, try '--help'."
;;
esac
done

assert_dependencies
assert_not_installed_outside_asdf

install_latest_asdf_plugin "scarb"
install_latest_version "scarb"
set_global_latest_version "scarb"

install_universal_sierra_compiler

install_latest_asdf_plugin "starknet-foundry"
install_latest_version "starknet-foundry"
set_global_latest_version "starknet-foundry"

_shell_config=""
_completion_message=""

case ${SHELL:-""} in
*/zsh)
_shell_config="$HOME/.zshrc"
_completion_message="Run 'source ${_shell_config}'"
;;
*/bash)
if [ "$(uname)" = "Darwin" ]; then
_shell_config="$HOME/.bash_profile"
else
_shell_config="$HOME/.bashrc"
fi
_completion_message="Run 'source ${_shell_config}'"
;;
*/sh)
_shell_config="$HOME/.profile"
_completion_message="Run '. ${_shell_config}'"
;;
*)
_completion_message="Source your shell configuration file"
;;
esac

if ! check_cmd universal-sierra-compiler; then
_local_bin="${HOME}/.local/bin"
say "Couldn't finish universal-sierra-compiler installation, try manually adding ${_local_bin} to your PATH."
fi

say "Installation complete. ${_completion_message} or start a new terminal session to use the installed tools."
}

assert_dependencies() {
need_cmd curl
need_cmd git
if ! check_cmd asdf; then
install_asdf_interactively
fi
}

assert_not_installed_outside_asdf() {
_installed_tools=""

for _tool in "scarb" "starknet-foundry"; do
_uninst_instructions=""
_tool_cmds=""

case "$_tool" in
"scarb")
_uninst_instructions="$SCARB_UNINSTALL_INSTRUCTIONS"
_tool_cmds="scarb"
;;
"starknet-foundry")
_uninst_instructions="$STARKNET_FOUNDRY_UNINSTALL_INSTRUCTIONS"
_tool_cmds="snforge sncast"
;;
esac

if ! check_asdf_plugin_installed "$_tool"; then
for _cmd in $_tool_cmds; do
if check_cmd "$_cmd"; then
_installed_tools="${_installed_tools}${_installed_tools:+\n} - $_cmd (from $_tool). $_uninst_instructions"
fi
done
fi
done

if [ -n "$_installed_tools" ]; then
err "The following tool(s) are already installed outside of asdf:\n$_installed_tools"
fi
}

install_latest_asdf_plugin() {
_plugin="$1"
if check_asdf_plugin_installed "$_plugin"; then
ensure asdf plugin update "$_plugin"
else
ensure asdf plugin add "$_plugin"
fi
}

check_asdf_plugin_installed() {
_plugin="$1"
asdf plugin list | grep -xq "$_plugin"
}

install_latest_version() {
_tool="$1"
ensure asdf install "$_tool" latest
}

uninstall_latest_version() {
_tool="$1"
_latest_version=$(asdf latest "$_tool")

if asdf list "$_tool" "^${_latest_version}$" >/dev/null 2>&1; then
ensure asdf uninstall "$_tool" "$_latest_version"
fi
}

set_global_latest_version() {
_tool="$1"
ensure asdf global "$_tool" latest
}

get_latest_gh_version() {
_repo="$1"
curl -sS --fail "https://api.github.com/repos/${_repo}/releases/latest" | awk -F'"' '/"tag_name"/ {print $4}'
}

install_universal_sierra_compiler() {
_version=""
_latest_version=""
if check_cmd universal-sierra-compiler; then
_version=$(universal-sierra-compiler --version 2>/dev/null | awk '{print $2}')
_latest_version=$(get_latest_gh_version "software-mansion/universal-sierra-compiler")
fi

if [ -z "$_version" ] || [ "$_version" != "$_latest_version" ]; then
curl -sSL --fail https://raw.githubusercontent.com/software-mansion/universal-sierra-compiler/master/scripts/install.sh | ${SHELL:-sh}
fi
}

say() {
printf 'starknetup: %b\n' "$1"
}

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
}

ensure() {
if ! "$@"; then err "command failed: $*"; fi
}

install_asdf_interactively() {
_profile=""
_pref_shell=""
_completion_message=""
_asdf_path="$HOME/.asdf"

case ${SHELL:-""} in
*/zsh)
_profile=$HOME/.zshrc
_pref_shell=zsh
_completion_message="Run 'source ${_profile}'"
;;
*/bash)
if [ "$(uname)" = "Darwin" ]; then
_profile=$HOME/.bash_profile
else
_profile=$HOME/.bashrc
fi
_pref_shell=bash
_completion_message="Run 'source ${_profile}'"
;;
*/sh)
_profile=$HOME/.profile
_pref_shell="sh"
_completion_message="Run '. ${_profile}'"
;;
esac

if [ -z "$_profile" ] || [ -z "$_pref_shell" ]; then
err "asdf-vm is required. Please install it manually and re-run this script. For installation instructions, refer to ${ASDF_INSTALL_DOCS}."
fi

touch "$_profile"

say "asdf-vm is required. Do you want to install it now? (y/N): "
# Starknetup is going to want to ask for confirmation by
# reading stdin. This script may be piped into `sh` though
# and wouldn't have stdin to pass to its children. Instead we're
# going to explicitly connect /dev/tty to the installer's stdin.
if [ ! -t 0 ] && [ -r /dev/tty ]; then
read -r answer </dev/tty
else
read -r answer
fi
if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
# TODO: https://github.com/software-mansion/scarb/issues/1938
# Support newer versions of asdf-vm
_version="$DEFAULT_ASDF_VERSION"
say "Installing asdf-vm ${_version}..."
git clone --quiet -c advice.detachedHead=false https://github.com/asdf-vm/asdf.git "$_asdf_path" --branch "$_version"

echo >>"$_profile" && echo ". ${_asdf_path}/asdf.sh" >>"$_profile"

say "asdf-vm has been installed. ${_completion_message} or start a new terminal session and re-run this script."
exit 0
else
err "cancelled asdf-vm installation. Please install it manually and re-run this script. For installation instructions, refer to ${ASDF_INSTALL_DOCS}."
fi
}

main "$@" || exit 1