-
Notifications
You must be signed in to change notification settings - Fork 55
feat: add one-command environment setup script #781
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
Closed
alexsuperpremium-collab
wants to merge
5
commits into
SolFoundry:main
from
alexsuperpremium-collab:fix/issue-491
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
237cc19
feat(devops): add one-command setup script for local dev environment
Xinyan0906 82d428c
fix(devops): address AI review feedback for setup.sh
Xinyan0906 e4f5605
fix(devops): further robustify setup script and mock tests for AI review
Xinyan0906 9b0f01e
fix(devops): further robustify setup script and mock tests for AI review
Xinyan0906 bcb7c1b
fix(devops): address AI review v3 feedback on test stability and edge…
Xinyan0906 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| #!/bin/bash | ||
|
|
||
| # SolFoundry Environment Setup Script | ||
| # This script sets up the local development environment from scratch. | ||
| # It checks for dependencies, installs packages, sets up .env, and starts services. | ||
|
|
||
| set -euo pipefail # Strict error handling | ||
|
|
||
| # Text formatting | ||
| GREEN='\033[0;32m' | ||
| RED='\033[0;31m' | ||
| YELLOW='\033[1;33m' | ||
| BLUE='\033[0;34m' | ||
| NC='\033[0m' # No Color | ||
| BOLD='\033[1m' | ||
|
|
||
| echo -e "${BLUE}${BOLD}🚀 Starting SolFoundry Environment Setup...${NC}\n" | ||
|
|
||
| # Helper functions | ||
| print_step() { | ||
| echo -e "${YELLOW}➤ $1${NC}" | ||
| } | ||
|
|
||
| print_success() { | ||
| echo -e "${GREEN}✓ $1${NC}" | ||
| } | ||
|
|
||
| print_error() { | ||
| echo -e "${RED}✗ $1${NC}" | ||
| exit 1 | ||
| } | ||
|
|
||
| print_warning() { | ||
| echo -e "${YELLOW}⚠️ $1${NC}" | ||
| } | ||
|
|
||
| version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; } | ||
|
|
||
| # Skip actual execution during tests (dry run mode) | ||
| DRY_RUN=${DRY_RUN:-0} | ||
| run_cmd() { | ||
| if [ "$DRY_RUN" -eq 1 ]; then | ||
| echo "[DRY RUN] Would execute: $*" | ||
| return 0 | ||
| else | ||
| "$@" | ||
| fi | ||
| } | ||
|
|
||
| # 1. Dependency Checks | ||
| print_step "Checking required tools..." | ||
|
|
||
| if ! command -v node &> /dev/null; then | ||
| print_error "Node.js is not installed. Please install Node.js v18+." | ||
| fi | ||
| NODE_VERSION=$(node -v | sed 's/v//') | ||
| if ! version_gt "$NODE_VERSION" "17.9.9"; then | ||
| print_error "Node.js version must be 18+. Found $NODE_VERSION." | ||
| fi | ||
| print_success "Node.js is installed (v$NODE_VERSION)." | ||
|
|
||
| if ! command -v npm &> /dev/null; then | ||
| print_error "npm is not installed. Please install npm." | ||
| fi | ||
| print_success "npm is installed." | ||
|
|
||
| if ! command -v python3 &> /dev/null; then | ||
| print_error "Python 3 is not installed. Please install Python v3.10+." | ||
| fi | ||
| PYTHON_VERSION=$(python3 -c 'import platform; print(platform.python_version())') | ||
| if ! version_gt "$PYTHON_VERSION" "3.9.9"; then | ||
| print_error "Python version must be 3.10+. Found $PYTHON_VERSION." | ||
| fi | ||
| print_success "Python is installed (v$PYTHON_VERSION)." | ||
|
|
||
| if ! python3 -c 'import venv' &> /dev/null; then | ||
| print_error "python3-venv is not installed. Please install it (e.g. sudo apt install python3-venv)." | ||
| fi | ||
| print_success "python3-venv is available." | ||
|
|
||
| if ! command -v pip3 &> /dev/null; then | ||
| print_error "pip3 is not installed. Please install pip for Python 3." | ||
| fi | ||
| print_success "pip3 is installed." | ||
|
|
||
| if ! command -v rustc &> /dev/null; then | ||
| print_error "Rust is not installed. Please install Rust (https://rustup.rs/)." | ||
| fi | ||
| RUST_VERSION=$(rustc --version | awk '{print $2}') | ||
| print_success "Rust is installed ($RUST_VERSION)." | ||
|
|
||
| if ! command -v anchor &> /dev/null; then | ||
| print_warning "Anchor is not installed. Smart contract development might fail." | ||
| else | ||
| ANCHOR_VERSION=$(anchor --version | awk '{print $2}') | ||
| print_success "Anchor is installed ($ANCHOR_VERSION)." | ||
| fi | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if ! command -v docker &> /dev/null; then | ||
| print_warning "Docker is not installed. You will need it to run Postgres/Redis via compose." | ||
| else | ||
| print_success "Docker is installed." | ||
| fi | ||
|
|
||
| # 2. Setup .env Files | ||
| print_step "Setting up environment variables..." | ||
|
|
||
| if [ ! -f .env ]; then | ||
| if [ -f .env.example ]; then | ||
| cp .env.example .env | ||
| print_success "Created Root .env from .env.example." | ||
| else | ||
| print_warning "Root .env.example not found. Skipping..." | ||
| fi | ||
| else | ||
| print_success "Root .env already exists." | ||
| fi | ||
|
|
||
| if [ -d "backend" ] && [ ! -f backend/.env ]; then | ||
| if [ -f backend/.env.example ]; then | ||
| cp backend/.env.example backend/.env | ||
| print_success "Created backend/.env from backend/.env.example." | ||
| else | ||
| print_warning "backend/.env.example not found. Skipping..." | ||
| fi | ||
| elif [ -f backend/.env ]; then | ||
| print_success "backend/.env already exists." | ||
| fi | ||
|
|
||
| # Track global setup status | ||
| SETUP_FAILED=0 | ||
|
|
||
| # 3. Install Backend Dependencies | ||
| if [ -d "backend" ]; then | ||
| print_step "Installing backend dependencies (Python)..." | ||
| cd backend | ||
| if [ ! -d "venv" ]; then | ||
| run_cmd python3 -m venv venv | ||
| fi | ||
|
|
||
| if [ -f "requirements.txt" ]; then | ||
| if run_cmd venv/bin/pip install -r requirements.txt; then | ||
| print_success "Backend dependencies installed." | ||
| else | ||
| print_error "Backend dependencies failed to install." | ||
| fi | ||
| else | ||
| print_warning "backend/requirements.txt not found." | ||
| fi | ||
| cd .. | ||
| else | ||
| print_warning "backend/ directory not found. Skipping backend setup." | ||
| fi | ||
|
|
||
| # 4. Install Frontend Dependencies | ||
| if [ -d "frontend" ]; then | ||
| print_step "Installing frontend dependencies (Node.js)..." | ||
| cd frontend | ||
| if [ -f "package.json" ]; then | ||
| if run_cmd npm install; then | ||
| print_success "Frontend dependencies installed." | ||
| else | ||
| print_error "Frontend dependencies failed to install." | ||
| fi | ||
| else | ||
| print_warning "frontend/package.json not found." | ||
| fi | ||
| cd .. | ||
| else | ||
| print_warning "frontend/ directory not found. Skipping frontend setup." | ||
| fi | ||
|
|
||
| # 5. Install SDK Dependencies (if applicable) | ||
| if [ -d "sdk" ]; then | ||
| print_step "Installing SDK dependencies..." | ||
| cd sdk | ||
| if [ -f "package.json" ]; then | ||
| if run_cmd npm install; then | ||
| print_success "SDK dependencies installed successfully." | ||
| else | ||
| print_warning "npm install in sdk failed. Continuing anyway." | ||
| SETUP_FAILED=1 | ||
| fi | ||
| else | ||
| print_warning "sdk/package.json not found." | ||
| fi | ||
| cd .. | ||
| fi | ||
|
|
||
| # 6. Start Local Services | ||
| print_step "Starting local services..." | ||
|
|
||
| DOCKER_SUCCESS=0 | ||
| if [ -f "docker-compose.yml" ]; then | ||
| if command -v docker-compose &> /dev/null; then | ||
| if run_cmd docker-compose up -d db redis; then | ||
| print_success "Database and Redis started via docker-compose." | ||
| DOCKER_SUCCESS=1 | ||
| else | ||
| print_warning "Failed to start services via docker-compose. Is Docker daemon running?" | ||
| SETUP_FAILED=1 | ||
| fi | ||
| elif command -v docker &> /dev/null && docker compose version &> /dev/null; then | ||
| if run_cmd docker compose up -d db redis; then | ||
| print_success "Database and Redis started via docker compose." | ||
| DOCKER_SUCCESS=1 | ||
| else | ||
| print_warning "Failed to start services via docker compose. Is Docker daemon running?" | ||
| SETUP_FAILED=1 | ||
| fi | ||
| else | ||
| print_warning "Docker Compose not found. Please ensure Postgres and Redis are running locally." | ||
| SETUP_FAILED=1 | ||
| fi | ||
| else | ||
| print_warning "docker-compose.yml not found. Skipping local service startup." | ||
| fi | ||
|
|
||
| # Final Output | ||
| if [ "$SETUP_FAILED" -eq 0 ]; then | ||
| echo -e "\n${GREEN}${BOLD}🎉 Setup Complete! All components successfully installed.${NC}\n" | ||
| else | ||
| echo -e "\n${YELLOW}${BOLD}⚠️ Setup Completed with Warnings. Some components may need manual attention.${NC}\n" | ||
| fi | ||
|
|
||
| echo -e "To start the development servers, open separate terminals and run:" | ||
| echo -e " ${BOLD}Backend:${NC} cd backend && source venv/bin/activate && uvicorn app.main:app --reload" | ||
| echo -e " ${BOLD}Frontend:${NC} cd frontend && npm run dev\n" | ||
|
|
||
| if [ "$DOCKER_SUCCESS" -eq 1 ]; then | ||
| echo -e "${BLUE}${BOLD}Local Services URLs:${NC}" | ||
| echo -e " - Frontend: http://localhost:5173 (or port shown by Vite)" | ||
| echo -e " - Backend API: http://localhost:8000" | ||
| echo -e " - Backend Docs: http://localhost:8000/docs" | ||
| fi | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #!/usr/bin/env bats | ||
|
|
||
| setup() { | ||
| # Create a temporary test directory structure mimicking the repo | ||
| export TEST_DIR="$(mktemp -d)" | ||
| cp ./scripts/setup.sh "$TEST_DIR/setup.sh" | ||
| chmod +x "$TEST_DIR/setup.sh" | ||
|
|
||
| # We use DRY_RUN=1 so it skips executing actual slow/side-effect commands | ||
| # like npm install, pip install, and docker compose | ||
| export DRY_RUN=1 | ||
|
|
||
| cd "$TEST_DIR" | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| teardown() { | ||
| rm -rf "$TEST_DIR" | ||
| } | ||
|
|
||
| @test "creates .env from .env.example correctly" { | ||
| touch .env.example | ||
| mkdir -p backend | ||
| touch backend/.env.example | ||
|
|
||
| run ./setup.sh | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [ -f .env ] | ||
| [ -f backend/.env ] | ||
| echo "$output" | grep "Created Root .env from .env.example" | ||
| echo "$output" | grep "Created backend/.env from backend/.env.example" | ||
| } | ||
|
|
||
| @test "skips backend setup gracefully when missing" { | ||
| mkdir -p frontend | ||
| touch frontend/package.json | ||
|
|
||
| run ./setup.sh | ||
|
|
||
| echo "$output" | grep "backend/ directory not found. Skipping backend setup." | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @test "skips frontend setup gracefully when missing" { | ||
| mkdir -p backend | ||
| touch backend/requirements.txt | ||
|
|
||
| run ./setup.sh | ||
|
|
||
| echo "$output" | grep "frontend/ directory not found. Skipping frontend setup." | ||
| } | ||
|
|
||
| @test "skips docker compose if docker-compose.yml is missing" { | ||
| mkdir -p backend frontend sdk | ||
| touch backend/requirements.txt frontend/package.json sdk/package.json | ||
|
|
||
| run ./setup.sh | ||
|
|
||
| echo "$output" | grep "docker-compose.yml not found. Skipping local service startup." | ||
| echo "$output" | grep "Setup Completed with Warnings" | ||
| } | ||
|
|
||
| @test "reports global success when all components are present and mock execution succeeds" { | ||
| mkdir -p backend frontend sdk | ||
| touch backend/requirements.txt frontend/package.json sdk/package.json | ||
| touch docker-compose.yml | ||
|
|
||
| run ./setup.sh | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| echo "$output" | grep "Setup Complete! All components successfully installed." | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @test "creates Python virtual environment using proper paths" { | ||
| mkdir -p backend | ||
| touch backend/requirements.txt | ||
|
|
||
| run ./setup.sh | ||
|
|
||
| echo "$output" | grep "\[DRY RUN\] Would execute: python3 -m venv venv" | ||
| echo "$output" | grep "\[DRY RUN\] Would execute: venv/bin/pip install -r requirements.txt" | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.