|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Colors and symbols |
| 6 | +RED='\033[0;31m' |
| 7 | +GREEN='\033[0;32m' |
| 8 | +BLUE='\033[0;34m' |
| 9 | +NC='\033[0m' |
| 10 | +SUCCESS="✓" |
| 11 | +ERROR="✗" |
| 12 | + |
| 13 | +# Utility functions |
| 14 | +print_spinner() { |
| 15 | + local pid=$1 |
| 16 | + local delay=0.1 |
| 17 | + local spinstr='|/-\' |
| 18 | + while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do |
| 19 | + local temp=${spinstr#?} |
| 20 | + printf " [%c] " "$spinstr" |
| 21 | + local spinstr=$temp${spinstr%"$temp"} |
| 22 | + sleep $delay |
| 23 | + printf "\b\b\b\b\b\b" |
| 24 | + done |
| 25 | + printf " \b\b\b\b" |
| 26 | +} |
| 27 | + |
| 28 | +get_bb_version_for_noir() { |
| 29 | + local noir_version=$1 |
| 30 | + local url="" |
| 31 | + local resolved_version="" |
| 32 | + |
| 33 | + if [ "$noir_version" = "stable" ] || [ "$noir_version" = "nightly" ]; then |
| 34 | + # Get releases from GitHub API |
| 35 | + local releases=$(curl -s "https://api.github.com/repos/noir-lang/noir/releases") |
| 36 | + |
| 37 | + if [ "$noir_version" = "stable" ]; then |
| 38 | + resolved_version=$(echo "$releases" | grep -o '"tag_name": "[^"]*"' | grep -v "aztec\|nightly" | head -1 | cut -d'"' -f4) |
| 39 | + else |
| 40 | + resolved_version=$(echo "$releases" | grep -o '"tag_name": "nightly[^"]*"' | head -1 | cut -d'"' -f4) |
| 41 | + fi |
| 42 | + |
| 43 | + url="https://raw.githubusercontent.com/noir-lang/noir/${resolved_version}/scripts/install_bb.sh" |
| 44 | + else |
| 45 | + url="https://raw.githubusercontent.com/noir-lang/noir/v${noir_version}/scripts/install_bb.sh" |
| 46 | + fi |
| 47 | + |
| 48 | + # Extract BB version from install script |
| 49 | + local install_script=$(curl -s "$url") |
| 50 | + local bb_version=$(echo "$install_script" | grep 'VERSION=' | cut -d'"' -f2) |
| 51 | + echo "$bb_version" |
| 52 | +} |
| 53 | + |
| 54 | +install_bb() { |
| 55 | + local version=$1 |
| 56 | + local architecture=$(uname -m) |
| 57 | + local platform="" |
| 58 | + |
| 59 | + # Convert architecture names |
| 60 | + if [ "$architecture" = "arm64" ]; then |
| 61 | + architecture="aarch64" |
| 62 | + elif [ "$architecture" = "x86_64" ]; then |
| 63 | + architecture="x86_64" |
| 64 | + else |
| 65 | + printf "${RED}${ERROR} Unsupported architecture: ${architecture}${NC}\n" |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + |
| 69 | + # Determine platform |
| 70 | + if [ "$(uname)" = "Darwin" ]; then |
| 71 | + platform="apple-darwin" |
| 72 | + elif [ "$(uname)" = "Linux" ]; then |
| 73 | + platform="linux-gnu" |
| 74 | + else |
| 75 | + printf "${RED}${ERROR} Unsupported platform: $(uname)${NC}\n" |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + |
| 79 | + local home_dir=$HOME |
| 80 | + local bb_path="${home_dir}/.bb" |
| 81 | + |
| 82 | + printf "${BLUE}Installing to ${bb_path}${NC}\n" |
| 83 | + |
| 84 | + # Create temporary directory |
| 85 | + local temp_dir=$(mktemp -d) |
| 86 | + local temp_tar="${temp_dir}/temp.tar.gz" |
| 87 | + |
| 88 | + # Download and extract |
| 89 | + local release_url="https://github.com/AztecProtocol/aztec-packages/releases/download/aztec-packages-v${version}" |
| 90 | + local binary_url="${release_url}/barretenberg-${architecture}-${platform}.tar.gz" |
| 91 | + |
| 92 | + curl -L "$binary_url" -o "$temp_tar" |
| 93 | + mkdir -p "$bb_path" |
| 94 | + tar xzf "$temp_tar" -C "$bb_path" |
| 95 | + rm -rf "$temp_dir" |
| 96 | + |
| 97 | + # Update shell configuration |
| 98 | + update_shell_config "$bb_path" |
| 99 | + |
| 100 | + printf "${GREEN}${SUCCESS} Installed barretenberg to ${bb_path}${NC}\n" |
| 101 | +} |
| 102 | + |
| 103 | +update_shell_config() { |
| 104 | + local bb_bin_path=$1 |
| 105 | + local path_entry="export PATH=\"${bb_bin_path}:\$PATH\"" |
| 106 | + |
| 107 | + # Update various shell configs if they exist |
| 108 | + if [ -f "${HOME}/.bashrc" ]; then |
| 109 | + echo "$path_entry" >> "${HOME}/.bashrc" |
| 110 | + fi |
| 111 | + |
| 112 | + if [ -f "${HOME}/.zshrc" ]; then |
| 113 | + echo "$path_entry" >> "${HOME}/.zshrc" |
| 114 | + fi |
| 115 | + |
| 116 | + if [ -f "${HOME}/.config/fish/config.fish" ]; then |
| 117 | + echo "set -gx PATH ${bb_bin_path} \$PATH" >> "${HOME}/.config/fish/config.fish" |
| 118 | + fi |
| 119 | + |
| 120 | + # Update current session's PATH |
| 121 | + export PATH="${bb_bin_path}:$PATH" |
| 122 | +} |
| 123 | + |
| 124 | +# Main script |
| 125 | +main() { |
| 126 | + local version="" |
| 127 | + local noir_version="" |
| 128 | + |
| 129 | + # Parse arguments |
| 130 | + while [[ $# -gt 0 ]]; do |
| 131 | + case $1 in |
| 132 | + -v|--version) |
| 133 | + version="$2" |
| 134 | + shift 2 |
| 135 | + ;; |
| 136 | + -nv|--noir-version) |
| 137 | + noir_version="$2" |
| 138 | + shift 2 |
| 139 | + ;; |
| 140 | + *) |
| 141 | + printf "${RED}${ERROR} Unknown option: $1${NC}\n" |
| 142 | + exit 1 |
| 143 | + ;; |
| 144 | + esac |
| 145 | + done |
| 146 | + |
| 147 | + # If no version specified, try to get current noir version |
| 148 | + if [ -z "$version" ] && [ -z "$noir_version" ]; then |
| 149 | + noir_version="current" |
| 150 | + fi |
| 151 | + |
| 152 | + if [ "$noir_version" = "current" ]; then |
| 153 | + printf "${BLUE}Querying noir version from nargo${NC}\n" |
| 154 | + if ! command -v nargo &> /dev/null; then |
| 155 | + printf "${RED}${ERROR} Could not get noir version from nargo --version. Please specify a version.${NC}\n" |
| 156 | + exit 1 |
| 157 | + fi |
| 158 | + noir_version=$(nargo --version | grep -o 'nargo version = [0-9]\+\.[0-9]\+\.[0-9]\+\(-[a-zA-Z]\+\.[0-9]\+\)\?' | cut -d' ' -f4) |
| 159 | + printf "${GREEN}${SUCCESS} Resolved noir version ${noir_version} from nargo${NC}\n" |
| 160 | + fi |
| 161 | + |
| 162 | + if [ -n "$noir_version" ]; then |
| 163 | + printf "${BLUE}Getting compatible barretenberg version for noir version ${noir_version}${NC}\n" |
| 164 | + if [ "$noir_version" = "stable" ] || [ "$noir_version" = "nightly" ]; then |
| 165 | + printf "${BLUE}Resolving noir version ${noir_version}...${NC}\n" |
| 166 | + # Get releases from GitHub API to show the resolved version |
| 167 | + local releases=$(curl -s "https://api.github.com/repos/noir-lang/noir/releases") |
| 168 | + local resolved_version="" |
| 169 | + if [ "$noir_version" = "stable" ]; then |
| 170 | + resolved_version=$(echo "$releases" | grep -o '"tag_name": "[^"]*"' | grep -v "aztec\|nightly" | head -1 | cut -d'"' -f4) |
| 171 | + else |
| 172 | + resolved_version=$(echo "$releases" | grep -o '"tag_name": "nightly[^"]*"' | head -1 | cut -d'"' -f4) |
| 173 | + fi |
| 174 | + printf "${GREEN}${SUCCESS} Resolved noir version ${noir_version} to ${resolved_version}${NC}\n" |
| 175 | + fi |
| 176 | + version=$(get_bb_version_for_noir "$noir_version") |
| 177 | + printf "${GREEN}${SUCCESS} Resolved to barretenberg version ${version}${NC}\n" |
| 178 | + fi |
| 179 | + |
| 180 | + if [ -z "$version" ]; then |
| 181 | + printf "${RED}${ERROR} No version specified and couldn't determine version from noir${NC}\n" |
| 182 | + exit 1 |
| 183 | + fi |
| 184 | + |
| 185 | + install_bb "$version" |
| 186 | +} |
| 187 | + |
| 188 | +main "$@" |
0 commit comments