Skip to content
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

feat(zk_toolbox): Add installation script #2569

Merged
merged 27 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
98ee48f
Add check_prerequisites
matias-gonz Aug 2, 2024
159c16f
Add parse_args
matias-gonz Aug 2, 2024
3688340
Add install_local
matias-gonz Aug 2, 2024
6584261
Fix installing message
matias-gonz Aug 2, 2024
34cd9e5
Update binaries
matias-gonz Aug 2, 2024
b451f74
Add install_from_repo
matias-gonz Aug 2, 2024
e92f5bd
Update install_from_repo
matias-gonz Aug 5, 2024
bbf7c1f
Add add_to_path
matias-gonz Aug 5, 2024
4001447
Add install script
matias-gonz Aug 5, 2024
4546ed2
Improve messages
matias-gonz Aug 5, 2024
630760b
Add success
matias-gonz Aug 5, 2024
1c14aba
Merge branch 'main' into matias-zkup
matias-gonz Aug 5, 2024
1f42449
Uodate usage
matias-gonz Aug 5, 2024
3bb627f
Merge branch 'matias-zkup' of github.com:matter-labs/zksync-era into …
matias-gonz Aug 5, 2024
a744f8c
Update Readme
matias-gonz Aug 5, 2024
c2472d7
fmt
matias-gonz Aug 5, 2024
533ec64
Remove OL
matias-gonz Aug 5, 2024
4f841e4
Remove spinner
matias-gonz Aug 12, 2024
67fd901
Remove generic msg
matias-gonz Aug 12, 2024
4ad68de
Add banner and specific success msg
matias-gonz Aug 12, 2024
1f5aa9c
Merge branch 'main' into matias-zkup
Deniallugo Aug 12, 2024
4c4caa1
Better string parsing, remove tailing spaces.
yorik Aug 12, 2024
fdc2e8d
Add alias option
matias-gonz Aug 13, 2024
1224cc2
Merge branch 'main' into matias-zkup
matias-gonz Aug 13, 2024
5d51f53
Add short alias option
matias-gonz Aug 13, 2024
7984fc3
Merge branch 'matias-zkup' of github.com:matter-labs/zksync-era into …
matias-gonz Aug 13, 2024
3cbf0b0
Add alias help
matias-gonz Aug 13, 2024
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
73 changes: 73 additions & 0 deletions zk_toolbox/zkup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# zkup - zk_toolbox Installer

`zkup` is a script designed to simplify the installation of
[zk_toolbox](https://github.com/matter-labs/zksync-era/tree/main/zk_toolbox). It allows you to install the tool from a
local directory or directly from a GitHub repository.

## Getting Started

To install `zkup`, run the following command:

```bash
curl -L https://raw.githubusercontent.com/matter-labs/zksync-era/main/zk_toolbox/zkup/install | bash
```

After installing `zkup`, you can use it to install `zk_toolbox` with:

```bash
zkup
```

## Usage

The `zkup` script provides various options for installing `zk_toolbox`:

### Options

- `-p, --path <path>`
Specify a local path to install `zk_toolbox` from. This option is ignored if `--repo` is provided.

- `-r, --repo <repo>`
GitHub repository to install from (e.g., "matter-labs/zksync-era"). Defaults to "matter-labs/zksync-era".

- `-b, --branch <branch>`
Git branch to use when installing from a repository. Ignored if `--commit` or `--version` is provided.

- `-c, --commit <commit>`
Git commit hash to use when installing from a repository. Ignored if `--branch` or `--version` is provided.

- `-v, --version <version>`
Git tag to use when installing from a repository. Ignored if `--branch` or `--commit` is provided.

- `--skip-zk-supervisor`
Skip the installation of the `zk_supervisor` binary.

### Local Installation

If you provide a local path using the `-p` or `--path` option, `zkup` will install `zk_toolbox` from that directory.
Note that repository-specific arguments (`--repo`, `--branch`, `--commit`, `--version`) will be ignored in this case to
preserve git state.

### Repository Installation

By default, `zkup` installs `zk_toolbox` from the "matter-labs/zksync-era" GitHub repository. You can specify a
different repository, branch, commit, or version using the respective options. If multiple arguments are provided,
`zkup` will prioritize them as follows:

- `--version`
- `--commit`
- `--branch`

### Examples

**Install from a GitHub repository with a specific version:**

```bash
zkup --repo matter-labs/zksync-era --version 0.1.1
```

**Install from a local path, skipping `zk_supervisor`:**

```bash
zkup --path /path/to/local/zk_toolbox --skip-zk-supervisor
```
55 changes: 55 additions & 0 deletions zk_toolbox/zkup/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
set -eo pipefail

BASE_DIR=${XDG_CONFIG_HOME:-$HOME}
ZKT_DIR=${ZKT_DIR:-"$BASE_DIR/.zkt"}
ZKT_BIN_DIR="$ZKT_DIR/bin"

BIN_URL="https://raw.githubusercontent.com/matter-labs/zksync-era/main/zk_toolbox/zkup/zkup"
BIN_PATH="$ZKT_BIN_DIR/zkup"

mkdir -p "$ZKT_BIN_DIR"
curl -sSfL "$BIN_URL" -o "$BIN_PATH"
chmod +x "$BIN_PATH"

if [[ ":$PATH:" == *":${ZKT_BIN_DIR}:"* ]]; then
echo "zkup: found ${ZKT_BIN_DIR} in PATH"
exit 0
fi

case $SHELL in
*/zsh)
PROFILE="${ZDOTDIR-"$HOME"}/.zshenv"
;;
*/bash)
PROFILE="$HOME/.bashrc"
;;
*/fish)
PROFILE="$HOME/.config/fish/config.fish"
;;
*/ash)
PROFILE="$HOME/.profile"
;;
*)
echo "zkup: could not detect shell, manually add ${ZKT_BIN_DIR} to your PATH."
exit 1
;;
esac

if [[ ! -f "$PROFILE" ]]; then
echo "zkup: Profile file $PROFILE does not exist, creating it."
touch "$PROFILE"
fi

if [[ "$SHELL" == *"/fish"* ]]; then
echo -e "\n# Added by zkup\nfish_add_path -a $ZKT_BIN_DIR" >>"$PROFILE"
echo "zkup: Added $ZKT_BIN_DIR to PATH in $PROFILE using fish_add_path."
else
echo -e "\n# Added by zkup\nexport PATH=\"\$PATH:$ZKT_BIN_DIR\"" >>"$PROFILE"
echo "zkup: Added $ZKT_BIN_DIR to PATH in $PROFILE."
fi

echo
echo "Added zkup to PATH."
echo "Run 'source $PROFILE' or start a new terminal session to use zkup."
echo "Then run 'zkup' to install ZK Toolbox."
223 changes: 223 additions & 0 deletions zk_toolbox/zkup/zkup
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
#!/usr/bin/env bash
set -eo pipefail

BASE_DIR=${XDG_CONFIG_HOME:-$HOME}
ZKT_DIR=${ZKT_DIR:-"$BASE_DIR/.zkt"}
ZKT_BIN_DIR="$ZKT_DIR/bin"

ZKUP_SKIP_ZK_SUPERVISOR=0

BINS=(zk_inception zk_supervisor)

main() {
parse_args "$@"

zktoolbox_banner

check_prerequisites
mkdir -p "$ZKT_BIN_DIR"

if [ -n "$ZKUP_PATH" ]; then
install_local
else
install_from_repo
fi

zktoolbox_banner

for bin in "${BINS[@]}"; do
success "Installed $bin to $ZKT_BIN_DIR/$bin"
done
}

PREREQUISITES=(cargo git)

check_prerequisites() {
say "Checking prerequisites"

failed_prerequisites=()
for prerequisite in "${PREREQUISITES[@]}"; do
if ! check_prerequisite "$prerequisite"; then
failed_prerequisites+=("$prerequisite")
fi
done
if [ ${#failed_prerequisites[@]} -gt 0 ]; then
err "The following prerequisites are missing: ${failed_prerequisites[*]}"
exit 1
fi
}

check_prerequisite() {
command -v "$1" &>/dev/null
}

parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
--)
shift
break
;;

-p | --path)
shift
ZKUP_PATH=$1
;;
-r | --repo)
shift
ZKUP_REPO=$1
;;
-b | --branch)
shift
ZKUP_BRANCH=$1
;;
-c | --commit)
shift
ZKUP_COMMIT=$1
;;
-v | --version)
shift
ZKUP_VERSION=$1
;;
--skip-zk-supervisor) ZKUP_SKIP_ZK_SUPERVISOR=1 ;;
-h | --help)
usage
exit 0
;;
*)
err "Unknown argument: $1"
usage
exit 1
;;
esac
shift
done
}

usage() {
cat <<EOF
zkup - A tool for managing zk_toolbox installation.

Usage: $(basename "$0") [OPTIONS]

Options:
-p, --path <path> Specify a local path to install zk_toolbox from. Ignored if --repo is provided.
-r, --repo <repo> GitHub repository to install from (e.g., "matter-labs/zksync-era"). Defaults to "matter-labs/zksync-era".
-b, --branch <branch> Git branch to use when installing from a repository. Ignored if --commit or --version is provided.
-c, --commit <commit> Git commit hash to use when installing from a repository. Ignored if --branch or --version is provided.
-v, --version <version> Git tag to use when installing from a repository. Ignored if --branch or --commit is provided.
--skip-zk-supervisor Skip installation of the zk_supervisor binary.
-h, --help Show this help message and exit.

Examples:
$(basename "$0") --repo matter-labs/zksync-era --version 0.1.1
$(basename "$0") --path /path/to/local/zk_toolbox --skip-zk-supervisor
EOF
}

install_local() {
if [ ! -d "$ZKUP_PATH/zk_toolbox" ]; then
err "Path $ZKUP_PATH does not contain zk_toolbox"
exit 1
fi

if [ -n "$ZKUP_BRANCH" ] || [ -n "$ZKUP_COMMIT" ] || [ -n "$ZKUP_VERSION" ] || [ -n "$ZKUP_REPO" ]; then
warn "Ignoring --repo, --branch, --commit and --version arguments when installing from local path"
fi

say "Installing zk_toolbox from $ZKUP_PATH"
ensure cd "$ZKUP_PATH"/zk_toolbox

if [ $ZKUP_SKIP_ZK_SUPERVISOR -eq 1 ]; then
BINS=(zk_inception)
fi

for bin in "${BINS[@]}"; do
say "Installing $bin"
ensure cargo install --root $ZKT_DIR --path ./crates/$bin --force
done
}

install_from_repo() {
if [ -n "$ZKUP_PATH" ]; then
warn "Ignoring --path argument when installing from repository"
fi

ZKUP_REPO=${ZKUP_REPO:-"matter-labs/zksync-era"}

say "Installing zk_toolbox from $ZKUP_REPO"

if [ $ZKUP_SKIP_ZK_SUPERVISOR -eq 1 ]; then
BINS=(zk_inception)
fi

if [ -n "$ZKUP_VERSION" ]; then
if [ -n "$ZKUP_COMMIT" ] || [ -n "$ZKUP_BRANCH" ]; then
warn "Ignoring --commit and --branch arguments when installing by version"
fi
ensure cargo install --root $ZKT_DIR --git "https://github.com/$ZKUP_REPO" --tag "zk_toolbox-v$ZKUP_VERSION" --locked "${BINS[@]}" --force
elif [ -n "$ZKUP_COMMIT" ]; then
if [ -n "$ZKUP_BRANCH" ]; then
warn "Ignoring --branch argument when installing by commit"
fi
ensure cargo install --root $ZKT_DIR --git "https://github.com/$ZKUP_REPO" --rev "$ZKUP_COMMIT" --locked "${BINS[@]}" --force
elif [ -n "$ZKUP_BRANCH" ]; then
ensure cargo install --root $ZKT_DIR --git "https://github.com/$ZKUP_REPO" --branch "$ZKUP_BRANCH" --locked "${BINS[@]}" --force
else
ensure cargo install --root $ZKT_DIR --git "https://github.com/$ZKUP_REPO" --locked "${BINS[@]}" --force
fi
}

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

say() {
local action=$(echo "$1" | awk '{print $1}')
local rest=$(echo "$1" | sed 's/^[^ ]* //')

echo -e "\033[1;32m$action\033[0m $rest"
}

success() {
echo -e "\033[1;32m$1\033[0m"
}

warn() {
echo -e "\033[1;33mWARNING: $1\033[0m"
}

err() {
echo -e "\033[1;31mERROR: $1\033[0m" >&2
}

zktoolbox_banner() {
printf '

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

███████╗██╗ ██╗ ████████╗ ██████╗ ██████╗ ██╗ ██████╗ ██████╗ ██╗ ██╗
╚══███╔╝██║ ██╔╝ ╚══██╔══╝██╔═══██╗██╔═══██╗██║ ██╔══██╗██╔═══██╗╚██╗██╔╝
███╔╝ █████╔╝ ██║ ██║ ██║██║ ██║██║ ██████╔╝██║ ██║ ╚███╔╝
███╔╝ ██╔═██╗ ██║ ██║ ██║██║ ██║██║ ██╔══██╗██║ ██║ ██╔██╗
███████╗██║ ██╗ ██║ ╚██████╔╝╚██████╔╝███████╗██████╔╝╚██████╔╝██╔╝ ██╗
╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝╚═════╝ ╚═════╝ ╚═╝ ╚═╝


A Comprehensive Toolkit for Creating and Managing ZK Stack Chains

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Repo : https://github.com/matter-labs/zksync-era/
Docs : https://docs.zksync.io/
Contribute : https://github.com/matter-labs/zksync-era/pulls

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

'
}

main "$@"
Loading