diff --git a/foundryup.sh b/foundryup.sh new file mode 100644 index 0000000..4045256 --- /dev/null +++ b/foundryup.sh @@ -0,0 +1,247 @@ +#!/usr/bin/env bash +set -e +# 2022.12.20 +# https://raw.githubusercontent.com/foundry-rs/foundry/master/foundryup/foundryup +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 "$FOUNDRYUP_BRANCH" ] || [ -n "$FOUNDRYUP_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" + rm -f "$FOUNDRY_BIN_DIR/anvil" + rm -f "$FOUNDRY_BIN_DIR/chisel" + + # 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" + ensure ln -s "$PWD/target/release/anvil" "$FOUNDRY_BIN_DIR/anvil" + ensure ln -s "$PWD/target/release/chisel" "$FOUNDRY_BIN_DIR/chisel" + + say "done" + exit 0 + fi + + FOUNDRYUP_REPO=${FOUNDRYUP_REPO-foundry-rs/foundry} + + if [[ "$FOUNDRYUP_REPO" == "foundry-rs/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=$(ensure 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, cast, anvil, and chisel" + 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 "installed - $($FOUNDRY_BIN_DIR/anvil --version)" + say "installed - $($FOUNDRY_BIN_DIR/chisel --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 + + if [[ $(which anvil) =~ "cargo" ]]; then + warn "it appears your system has already has anvil installed via cargo. you may need to run 'rm $(which anvil)' to allow foundryup to take precedence!" + fi + + if [[ $(which chisel) =~ "cargo" ]]; then + warn "it appears your system has already has chisel installed via cargo. you may need to run 'rm $(which chisel)' 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} + # checkout submodules for Chisel build + ensure git submodule update --init --recursive + # 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 + # install anvil + RUSTFLAGS="-C target-cpu=native" ensure cargo install --path ./anvil --bin anvil --locked --force --root $FOUNDRY_DIR + # install chisel + RUSTFLAGS="-C target-cpu=native" ensure cargo install --path ./chisel --bin chisel --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 + help2man -N $FOUNDRY_BIN_DIR/anvil > $FOUNDRY_MAN_DIR/anvil.1 + help2man -N $FOUNDRY_BIN_DIR/chisel > $FOUNDRY_MAN_DIR/chisel.1 + fi + say "done" + fi +} + +usage() { + cat 1>&2 < + +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 '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 diff --git a/install b/install index 1c7d730..72f4c90 100755 --- a/install +++ b/install @@ -1,196 +1,56 @@ #!/usr/bin/env bash set -e -echo "Installing foundryup..." +echo Installing foundryup... FOUNDRY_DIR=${FOUNDRY_DIR-"$HOME/.foundry"} FOUNDRY_BIN_DIR="$FOUNDRY_DIR/bin" FOUNDRY_MAN_DIR="$FOUNDRY_DIR/share/man/man1" -# runs with Makefile to generate hash on CI -# shellcheck disable=SC2034 -SCRIPT_COMMIT_SHA="${LOAD_SCRIPT_COMMIT_SHA}" - -PLATFORM="$(uname -s)" - case $PLATFORM in - Linux) - PLATFORM="linux" - ;; - Darwin) - PLATFORM="darwin" - ;; - *) - echo "foundryup: unsupported platform: $PLATFORM" - exit 1 - ;; -esac - -# shellcheck disable=SC2016 -FOUNDRYUP='#!/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" -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;; - -h|--help) FOUNDRYUP_HELP="1";; - *) printf "foundryup: internal error: %q\\n" "$1"; exit 1 - esac; shift -done -if [[ "$FOUNDRYUP_HELP" == "1" ]]; then - echo "Update or revert to a specific Foundry branch with ease. -USAGE: - foundryup -OPTIONS: - -h, --help Print help information - -v, --version Install a specific version - -b, --branch Install a specific branch - -r, --repo Install a forks main branch" - exit 0 -fi -FOUNDRYUP_REPO=${FOUNDRYUP_REPO-gakonst/foundry} -if [[ "$FOUNDRYUP_REPO" == "gakonst/foundry" && -z "$FOUNDRYUP_BRANCH" ]]; 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" == [[:digit:]]* ]]; then - # Add v prefix - FOUNDRYUP_VERSION="v${FOUNDRYUP_VERSION}" - FOUNDRYUP_TAG="${FOUNDRYUP_VERSION}" - fi - PLATFORM="$(uname -s)" - case $PLATFORM in - Linux) - PLATFORM="linux" - ;; - Darwin) - PLATFORM="darwin" - ;; - *) - echo "foundryup: unsupported platform: $PLATFORM" - exit 1 - ;; - 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. - curl -L $BIN_TARBALL_URL | tar -xvzC $FOUNDRY_BIN_DIR - # Download the man tarball and unpack it into the .foundry man directory. - curl -L $MAN_TARBALL_URL | tar -xvzC $FOUNDRY_MAN_DIR - echo foundryup: done! -else - FOUNDRYUP_BRANCH=${FOUNDRYUP_BRANCH-master} - if ! command -v cargo &> /dev/null ; then - # Error if cargo is not already installed. - echo "foundryup: cargo is not installed. Please install it first." - exit 1 - fi - REPO_PATH="${FOUNDRY_DIR}/${FOUNDRYUP_REPO}" - if [ -d $REPO_PATH ]; then - # If the repo path exists move to it and do a force checkout, discarding any local changes - cd $REPO_PATH - git fetch - git reset --hard origin/${FOUNDRYUP_BRANCH} - else - # 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" - mkdir -p "$FOUNDRY_DIR/$AUTHOR" - cd "$FOUNDRY_DIR/$AUTHOR" - git clone https://github.com/${FOUNDRYUP_REPO} - cd $REPO_PATH - git checkout ${FOUNDRYUP_BRANCH} - 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. - 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 - echo "foundryup: done!" -fi' - -BINARY="$FOUNDRY_BIN_DIR/foundryup" +BIN_URL="https://raw.githubusercontent.com/foundry-rs/foundry/master/foundryup/foundryup" +BIN_PATH="$FOUNDRY_BIN_DIR/foundryup" # Create the .foundry bin directory and foundryup binary if it doesn't exist. -mkdir -p "$FOUNDRY_BIN_DIR" -echo "$FOUNDRYUP" >"$BINARY" -chmod +x "$BINARY" +mkdir -p $FOUNDRY_BIN_DIR +curl -# -L $BIN_URL -o $BIN_PATH +chmod +x $BIN_PATH # Create the man directory for future man files if it doesn't exist. -mkdir -p "$FOUNDRY_MAN_DIR" +mkdir -p $FOUNDRY_MAN_DIR # Store the correct profile file (i.e. .profile for bash or .zshrc for ZSH). case $SHELL in */zsh) - PROFILE=$HOME/.zshrc - PREF_SHELL=zsh - ;; + PROFILE=$HOME/.zshrc + PREF_SHELL=zsh + ;; */bash) - PROFILE=$HOME/.bashrc - PREF_SHELL=bash - ;; + PROFILE=$HOME/.bashrc + PREF_SHELL=bash + ;; */fish) - PROFILE=$HOME/.config/fish/config.fish - PREF_SHELL=fish - ;; + PROFILE=$HOME/.config/fish/config.fish + PREF_SHELL=fish + ;; +*/ash) + PROFILE=$HOME/.profile + PREF_SHELL=ash + ;; *) - echo "foundryup: could not detect shell, manually add ${FOUNDRY_BIN_DIR} to your PATH." - exit 1 - ;; - + echo "foundryup: could not detect shell, manually add ${FOUNDRY_BIN_DIR} to your PATH." + exit 1 esac # Only add foundryup if it isn't already in PATH. if [[ ":$PATH:" != *":${FOUNDRY_BIN_DIR}:"* ]]; then - if [[ $PLATFORM == 'darwin'* ]]; then - echo >>"$HOME"/.bash_profile && echo "export PATH=\"\$PATH:$FOUNDRY_BIN_DIR\"" >>"$HOME"/.bash_profile - fi - # Add the foundryup directory to the path and ensure the old PATH variables remain. - echo >>"$PROFILE" && echo "export PATH=\"\$PATH:$FOUNDRY_BIN_DIR\"" >>"$PROFILE" + # Add the foundryup directory to the path and ensure the old PATH variables remain. + echo >> $PROFILE && echo "export PATH=\"\$PATH:$FOUNDRY_BIN_DIR\"" >> $PROFILE fi - -if [[ $SHELL == 'bash'* ]]; then -forge completions bash > /usr/local/etc/bash_completion.d/forge -cast completions bash > /usr/local/etc/bash_completion.d/cast - -echo >> "/usr/local/etc/bash_completion.d/forge">>$$HOME/.bash_profile -echo >> "source /usr/local/etc/bash_completion.d/cast">>$$HOME/.bash_profile -# echo >>"[ -f /usr/local/etc/bash_completion.d ] && . /usr/local/etc/bash_completion.d ">>$$HOME/.bash_profile +# Warn MacOS users that they may need to manually install libusb via Homebrew: +if [[ "$OSTYPE" =~ ^darwin && ! -f /usr/local/opt/libusb/lib/libusb-1.0.0.dylib ]]; then + echo && echo "warning: libusb not found. You may need to install it manually on MacOS via Homebrew (brew install libusb)." fi echo && echo "Detected your preferred shell is ${PREF_SHELL} and added foundryup to PATH. Run 'source ${PROFILE}' or start a new terminal session to use foundryup." echo "Then, simply run 'foundryup' to install Foundry." -sleep 1 -exit 0 diff --git a/src/install b/src/install new file mode 100644 index 0000000..261bdf7 --- /dev/null +++ b/src/install @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +set -e + +echo Installing foundryup... + +FOUNDRY_DIR=${FOUNDRY_DIR-"$HOME/.foundry"} +FOUNDRY_BIN_DIR="$FOUNDRY_DIR/bin" +FOUNDRY_MAN_DIR="$FOUNDRY_DIR/share/man/man1" + +BIN_URL="https://raw.githubusercontent.com/foundry-rs/foundry/master/foundryup/foundryup" +BIN_PATH="$FOUNDRY_BIN_DIR/foundryup" + +# Create the .foundry bin directory and foundryup binary if it doesn't exist. +mkdir -p $FOUNDRY_BIN_DIR +curl -# -L $BIN_URL -o $BIN_PATH +chmod +x $BIN_PATH + +# Create the man directory for future man files if it doesn't exist. +mkdir -p $FOUNDRY_MAN_DIR + +# Store the correct profile file (i.e. .profile for bash or .zshrc for ZSH). +case $SHELL in +*/zsh) + PROFILE=$HOME/.zshrc + PREF_SHELL=zsh + ;; +*/bash) + PROFILE=$HOME/.bashrc + PREF_SHELL=bash + ;; +*/fish) + PROFILE=$HOME/.config/fish/config.fish + PREF_SHELL=fish + ;; +*) + echo "foundryup: could not detect shell, manually add ${FOUNDRY_BIN_DIR} to your PATH." + exit 1 +esac + +# Only add foundryup if it isn't already in PATH. +if [[ ":$PATH:" != *":${FOUNDRY_BIN_DIR}:"* ]]; then + # Add the foundryup directory to the path and ensure the old PATH variables remain. + echo >> $PROFILE && echo "export PATH=\"\$PATH:$FOUNDRY_BIN_DIR\"" >> $PROFILE +fi + +# Warn MacOS users that they may need to manually install libusb via Homebrew: +if [[ "$OSTYPE" =~ ^darwin && ! -f /usr/local/opt/libusb/lib/libusb-1.0.0.dylib ]]; then + echo && echo "warning: libusb not found. You may need to install it manually on MacOS via Homebrew (brew install libusb)." +fi + +echo && echo "Detected your preferred shell is ${PREF_SHELL} and added foundryup to PATH. Run 'source ${PROFILE}' or start a new terminal session to use foundryup." +echo "Then, simply run 'foundryup' to install Foundry."