-
-
Save nick-bull/80d5f18740b7089e5749ef11096bbe17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script will: | |
# - Install all required dependencies (Homebrew, Node.js, Yarn, AWS CLI, Docker) | |
# - Configure GitHub authentication | |
# - Set up AWS SSO | |
# - Clone all submodules | |
# - Install dependencies | |
# Colors for output | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[1;33m' | |
NC='\033[0m' | |
BORDERLESS_REPO="[email protected]:getborderless/borderless.git" | |
BORDERLESS_PATH="$HOME/Documents/borderless" | |
print_message() { echo -e "${2}${1}${NC}"; } | |
print_error() { echo -e "${RED}${1}${NC}"; } | |
print_success() { echo -e "${GREEN}${1}${NC}"; } | |
print_warning() { echo -e "${YELLOW}${1}${NC}"; } | |
command_exists() { command -v "$1" >/dev/null 2>&1; } | |
log() { | |
setopt localoptions no_monitor no_notify | |
local spin=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏') | |
local i=1 | |
local name=$1 | |
shift | |
cat << EOF >> ~/setup.log | |
### | |
### step='$name' | |
### | |
EOF | |
( "$@" ) >> ~/setup.log 2>&1 & | |
local cmd_pid=$! | |
while kill -0 $cmd_pid 2>/dev/null; do | |
printf "\r\e[1;37m[%s] %s\e[0m" $spin[i] "$name" | |
sleep 0.1 | |
(( i = (i % ${#spin[@]}) + 1 )) | |
done | |
wait $cmd_pid | |
__status=$? | |
printf "\r\033[K" | |
if test $__status -eq 0; then | |
printf "\e[1;32m[✔] %s\e[0m\n" "$name" | |
else | |
printf "\e[1;31m[✖] %s (exit $__status)\e[0m\n" "$name" | |
fi | |
} | |
get_sudo() { sudo -v; } | |
install_homebrew() { | |
if command_exists brew; then | |
print_success "Homebrew is already installed" | |
return | |
fi | |
log "Homebrew installation" \ | |
env NONINTERACTIVE=1 bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | |
# Add Homebrew to PATH for the current session | |
eval "$(/opt/homebrew/bin/brew shellenv)" | |
# Setup in profile | |
echo >> ~/.zprofile | |
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile | |
} | |
install_github_cli() { | |
if command_exists gh; then | |
print_success "GitHub CLI is already installed" | |
return | |
fi | |
log "GitHub CLI installation" \ | |
brew install gh | |
} | |
auth_github_keys() { | |
if test -n "$GP_READ_PAT"; then | |
print_message "GitHub Packages authentication is already configured" "$GREEN" | |
return | |
fi | |
print_warning "Setting up GitHub Packages authentication..." | |
echo "You'll need a GitHub classic token to install dependencies." | |
echo "The classic token must have these scopes:" | |
echo " - read:packages (to read packages)" | |
echo " - admin:public_key (to add auth keys)" | |
echo " - read:org (to validate tokens)" | |
echo " - repo (to access private repositories)" | |
echo | |
echo "Click here to generate: https://github.com/settings/tokens/new?scopes=read:packages,repo,admin:public_key,read:org&description=borderless" | |
echo | |
read -p "Once generated, paste your GitHub token: " github_token | |
if test -z "$github_token"; then | |
print_error "GitHub token cannot be empty. Please try again." | |
exit 1 | |
fi | |
# Create or update .npmrc | |
echo "//npm.pkg.github.com/:_authToken=$github_token" > ~/.npmrc | |
# Add to shell rc file | |
echo "export GP_READ_PAT=\"$github_token\"" >> ~/.zshrc | |
export GP_READ_PAT="$github_token" | |
# Create key, add to github, configure ssh | |
log "ssh key generation"\ | |
ssh-keygen -t ed25519 -f ~/.ssh/borderless -N "" | |
cat << EOF > $HOME/.ssh/config | |
Host github.com | |
User git | |
Hostname github.com | |
PreferredAuthentications publickey | |
IdentityFile ~/.ssh/borderless | |
EOF | |
chmod 600 ~/.ssh/config | |
log " - Restart ssh agent" \ | |
eval "$(ssh-agent -s)" | |
log " - Add ssh key" \ | |
ssh-add ~/.ssh/borderless | |
# Prevent caching issues by refreshing scopes before adding - this won't be | |
# necessary for usual use, but it was a PITA to debug this script without this | |
log " - Refresh github scopes" \ | |
gh auth refresh -h github.com -s admin:public_key | |
log " - Add ssh key to github" \ | |
gh ssh-key add ~/.ssh/borderless.pub --title "borderless" | |
# Auto-accept github.com as a trusted host | |
log " - Host github.com" \ | |
ssh-keyscan github.com >> ~/.ssh/known_hosts | |
} | |
auth_github_cli() { | |
if gh auth token > /dev/null 2>&1; then | |
print_success "GitHub SSH authentication is already configured" | |
return | |
fi | |
# print_warning "Please complete GitHub authentication in your browser" | |
echo "$GP_READ_PAT" | | |
gh auth login \ | |
--with-token \ | |
--git-protocol ssh \ | |
--hostname github.com | |
} | |
clone_borderless() { | |
log "Borderless monorepo" \ | |
git clone "$BORDERLESS_REPO" | |
} | |
install_node() { | |
if command_exists "nvm" || command_exists "node"; then | |
print_success "nvm or node.js already installed" | |
return | |
fi | |
log "nvm installation" \ | |
brew install nvm | |
# Create NVM directory | |
mkdir -p ~/.nvm | |
# Add NVM to shell config | |
cat << EOF >> ~/.zshrc | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" | |
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" | |
EOF | |
# Source NVM for current session | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" | |
log "- use node v20" nvm install 20 | |
} | |
install_yarn() { | |
if command_exists yarn; then | |
print_success "Yarn is already installed" | |
return | |
fi | |
log "yarn installation" \ | |
npm install -g yarn | |
} | |
install_aws_cli() { | |
if command_exists aws; then | |
print_success "AWS CLI is already installed" | |
return | |
fi | |
log "awscli installation" \ | |
brew install awscli | |
} | |
configure_aws_cli() { | |
if [ -f ~/.aws/config ]; then | |
print_success "AWS CLI is already configured" | |
return | |
fi | |
mkdir -p ~/.aws | |
cp "$BORDERLESS_PATH/config/aws" ~/.aws/config | |
} | |
auth_aws_cli() { | |
if ! aws sts get-caller-identity --profile borderless-cdk > /dev/null 2&>1; then | |
print_warning "Please log in to AWS SSO in your browser" | |
aws sso login --profile borderless-cdk | |
fi | |
} | |
clone_submodules() { | |
auth_github_cli | |
( | |
cd "$BORDERLESS_PATH" && | |
log "submodule cloning: install" yarn && | |
log "submodule cloning: clone" yarn clone:all | |
) | |
} | |
install_dependencies() { | |
( | |
cd "$BORDERLESS_PATH" && | |
log "- monorepo dependency installation" yarn | |
) | |
} | |
install_docker() { | |
if command_exists docker; then | |
print_success "Docker is already installed" | |
return | |
fi | |
log "docker installation" \ | |
brew install --cask docker > /dev/null | |
log "docker-compose installation" \ | |
brew install docker-compose | |
echo | |
} | |
print_success "###" | |
print_success "# Starting Borderless local environment setup..." | |
print_success "###" | |
echo | |
get_sudo | |
install_homebrew | |
install_github_cli | |
auth_github_keys | |
auth_github_cli | |
clone_borderless | |
install_node | |
install_yarn | |
install_aws_cli | |
auth_aws_cli | |
configure_aws_cli | |
clone_submodules | |
install_dependencies | |
install_docker | |
echo | |
print_success "###" | |
print_success "# Setup completed!" | |
print_success "# You can now run './bin/local.sh' to start the local environment" | |
print_success "###" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment