Skip to content

Commit

Permalink
🔨 Refactor install.sh and add evmrup
Browse files Browse the repository at this point in the history
  • Loading branch information
beskay committed Aug 25, 2023
1 parent 64c6480 commit 7130d3f
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 108 deletions.
111 changes: 111 additions & 0 deletions evmrup
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env bash
set -e

# -----------------------------------------------------------
# Forked from Foundry.
# https://github.com/foundry-rs/foundry/tree/master/foundryup
# -----------------------------------------------------------

# Define variables
EVMR_DIR="${HOME}/.evm-runners"
EVMR_BIN_DIR="${EVMR_DIR}/bin"
ENV_FILE="${EVMR_DIR}/.env"

APP_NAME="evm-runners"
APP_NAME_ALT="evmr"

VERSION="v0.4.1"

main() {
need_cmd curl

PLATFORM="$(uname -s)"
case $PLATFORM in
Linux)
PLATFORM="linux"
;;
Darwin)
PLATFORM="darwin"
;;
*)
err "unsupported platform: $PLATFORM"
;;
esac

# Determine architecture
ARCH="$(uname -m)"

if [ "$ARCH" = "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
ARCH="arm64" # Rosetta
else
ARCH="amd64" # Intel
fi
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
ARCH="arm64" # Arm
else
ARCH="amd64" # Amd
fi

# Download release binary
DOWNLOAD_URL="https://github.com/ethernautdao/evm-runners-cli/releases/download/$VERSION/$APP_NAME-$PLATFORM-$ARCH"

say "Installing evm-runners version $VERSION for $PLATFORM-$ARCH"

# Download binary, make it executable, and move it to the bin directory
ensure curl -# -L $DOWNLOAD_URL -o $APP_NAME
ensure chmod +x $APP_NAME
ensure mv $APP_NAME $EVMR_BIN_DIR
# create evmr symlink
ensure ln -sf $EVMR_BIN_DIR/$APP_NAME $EVMR_BIN_DIR/$APP_NAME_ALT

# Create .env file and add/update the EVMR_VERSION environment variable
if [ ! -f "$ENV_FILE" ]; then
# If the .env file doesn't exist, create it and add EVMR_VERSION
echo "EVMR_VERSION=$VERSION" > "$ENV_FILE"
elif grep -q "^EVMR_VERSION=" "$ENV_FILE"; then
# If EVMR_VERSION exists, update it
sed -i "s/^EVMR_VERSION=.*/EVMR_VERSION=$VERSION/" "$ENV_FILE"
else
# If EVMR_VERSION doesn't exist, append it to the file
echo "EVMR_VERSION=$VERSION" >> "$ENV_FILE"
fi

echo && say "$APP_NAME version $VERSION installed successfully!"
echo && say "If this is your first install, start a new terminal session to use evm-runners."
say "Then run 'evmr help', or alternatively 'evm-runners help', to get started."
echo && say "To update evm-runners in the future, just run 'evmrup'."
}

say() {
printf 'evmrup: %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
143 changes: 35 additions & 108 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,118 +1,45 @@
#!/bin/bash
#!/usr/bin/env bash
set -e

echo "Installing evmrup..."

# Define variables
APP_NAME="evm-runners"
APP_NAME_ALT="evmr"
EVMR_DIR="${HOME}/.${APP_NAME}"
EVMR_DIR="${HOME}/.evm-runners"
EVMR_BIN_DIR="${EVMR_DIR}/bin"
GITHUB_USER="ethernautdao"
GITHUB_REPO="evm-runners-cli"
VERSION="v0.4.1"

main() {
say "Installing evm-runners..."

PLATFORM="$(uname -s)"
case $PLATFORM in
Linux)
PLATFORM="linux"
;;
Darwin)
PLATFORM="darwin"
;;
*)
err "unsupported platform: $PLATFORM"
;;
esac

# Determine architecture
ARCH="$(uname -m)"

if [ "$ARCH" = "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
ARCH="arm64" # Rosetta
else
ARCH="amd64" # Intel
fi
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
ARCH="arm64" # Arm
else
ARCH="amd64" # Amd
fi

# Download release binary
DOWNLOAD_URL="https://github.com/$GITHUB_USER/$GITHUB_REPO/releases/download/$VERSION/$APP_NAME-$PLATFORM-$ARCH"
EVMRUP_URL="https://raw.githubusercontent.com/ethernautdao/evm-runners-cli/main/evmrup"
EVMRUP_PATH="$EVMR_BIN_DIR/evmrup"

echo
say "Downloading the binary from '$DOWNLOAD_URL'"
# Create bin directory if it doesn't exist and download evmrup
mkdir -p "$EVMR_BIN_DIR"
curl -# -L $EVMRUP_URL -o $EVMRUP_PATH
chmod +x $EVMRUP_PATH

curl -L "$DOWNLOAD_URL" -o "$APP_NAME"
# Determine shell
SHELL_NAME="$(basename "$SHELL")"

# Set executable permissions
chmod +x "$APP_NAME"

echo
say "Moving the binary to $EVMR_BIN_DIR"
echo

mkdir -p "$EVMR_BIN_DIR"
mv "$APP_NAME" "$EVMR_BIN_DIR"
ln -sf "$EVMR_BIN_DIR/$APP_NAME" "$EVMR_BIN_DIR/$APP_NAME_ALT"

# Determine shell
SHELL_NAME="$(basename "$SHELL")"

# Update PATH environment variable in the appropriate shell configuration file
if [ "$SHELL_NAME" = "bash" ]; then
# Determine appropriate shell configuration file
if [ "$SHELL_NAME" = "bash" ]; then
CONFIG=$HOME/.bashrc
elif [ "$SHELL_NAME" = "zsh" ]; then
elif [ "$SHELL_NAME" = "zsh" ]; then
CONFIG=$HOME/.zshrc
elif [ "$SHELL_NAME" = "fish" ]; then
elif [ "$SHELL_NAME" = "fish" ]; then
CONFIG=$HOME/.config/fish/config.fish
else
err "unsupported shell: $SHELL_NAME, manually add ${EVMR_BIN_DIR} to your PATH."
fi

say "Updating PATH..."

# Update PATH environment variable in the shell configuration file
if [[ ":$PATH:" != *":${EVMR_BIN_DIR}:"* ]]; then
echo >> $CONFIG && echo "export PATH=\"\$PATH:$EVMR_BIN_DIR\"" >> $CONFIG
say "Updated $CONFIG with PATH modification"
else
say "$CONFIG already contains PATH modification"
fi

# Create .env file and add/update the EVMR_VERSION environment variable
ENV_FILE="${EVMR_DIR}/.env"

if grep -q "^EVMR_VERSION=" "$ENV_FILE"; then
# If EVMR_VERSION exists, update it
sed -i "s/^EVMR_VERSION=.*/EVMR_VERSION=$VERSION/" "$ENV_FILE"
else
# If EVMR_VERSION doesn't exist, append it to the file
echo "EVMR_VERSION=$VERSION" >> "$ENV_FILE"
fi

echo
say "$APP_NAME version $VERSION installed successfully!"
echo
say "Run 'source $CONFIG' or start a new terminal session to use evm-runners."
say "Then run 'evmr help', or alternatively 'evm-runners help', to get started."
}

say() {
printf '%s\n' "$1"
}

warn() {
say "warning: ${1}" >&2
}

err() {
say "$1" >&2
exit 1
}

main "$@" || exit 1
else
echo "unsupported shell: $SHELL_NAME, manually add ${EVMR_BIN_DIR} to your PATH."
exit 1
fi

# Update PATH environment variable in the shell configuration file if it doesn't exist
if [[ ":$PATH:" != *":${EVMR_BIN_DIR}:"* ]]; then
echo >> $CONFIG && echo "export PATH=\"\$PATH:$EVMR_BIN_DIR\"" >> $CONFIG
echo "Updated $CONFIG with PATH modification"
else
echo "$CONFIG already contains PATH modification"
fi

echo && echo "Detected shell is $SHELL_NAME and added $EVMR_BIN_DIR to PATH."
echo "Continue with installing evm-runners ..." && echo

# Run evmrup
$EVMRUP_PATH

0 comments on commit 7130d3f

Please sign in to comment.