install-foundry.sh 1.66 KB
Newer Older
1 2 3 4
#!/bin/bash

set -e

5 6
SCRIPTS_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
MONOREPO_DIR=$(cd "$SCRIPTS_DIR/../../" && pwd)
7

8 9 10 11
# Grab the foundry commit hash.
SHA=$(jq -r .foundry < $MONOREPO_DIR/versions.json)

# Check if there is a nightly tag corresponding to the commit hash
12 13
TAG="nightly-$SHA"

14 15 16 17 18 19 20 21 22 23 24 25
# If the foundry repository exists and a branch is checked out, we need to abort
# any changes inside ~/.foundry/foundry-rs/foundry. This is because foundryup will
# attempt to pull the latest changes from the remote repository, which will fail
# if there are any uncommitted changes.
if [ -d ~/.foundry/foundry-rs/foundry ]; then
  echo "Foundry repository exists! Aborting any changes..."
  cd ~/.foundry/foundry-rs/foundry
  git reset --hard
  git clean -fd
  cd -
fi

26 27 28 29 30 31
# Create a temporary directory
TMP_DIR=$(mktemp -d)
echo "Created tempdir @ $TMP_DIR"

# Clone the foundry repo temporarily. We do this to avoid the need for a personal access
# token to interact with the GitHub REST API, and clean it up after we're done.
32
git clone https://github.com/foundry-rs/foundry.git "$TMP_DIR" && cd "$TMP_DIR"
33 34 35 36 37 38

# If the nightly tag exists, we can download the pre-built binaries rather than building
# from source. Otherwise, clone the repository, check out the commit SHA, and build `forge`,
# `cast`, `anvil`, and `chisel` from source.
if git rev-parse "$TAG" >/dev/null 2>&1; then
  echo "Nightly tag exists! Downloading prebuilt binaries..."
39
  foundryup -v "$TAG"
40 41
else
  echo "Nightly tag doesn't exist! Building from source..."
42
  foundryup -C "$SHA"
43 44 45
fi

# Remove the temporary foundry repo; Used just for checking the nightly tag's existence.
46
rm -rf "$TMP_DIR"
47
echo "Removed tempdir @ $TMP_DIR"
48 49