askrobots / dbbasic-cli
Public
Make a complete, running app (a CRM, a shop, a blog) with one command, such as dbbasic crm.
bootstrap.py · 5.8 KB · 197 lines
| 1 | """ |
| 2 | Bootstrap DBBasic core for new installations |
| 3 | """ |
| 4 | |
| 5 | import os |
| 6 | import subprocess |
| 7 | import sys |
| 8 | from pathlib import Path |
| 9 | from rich.console import Console |
| 10 | from rich.progress import Progress, SpinnerColumn, TextColumn |
| 11 | |
| 12 | console = Console() |
| 13 | |
| 14 | def get_dbbasic_home(): |
| 15 | """Get or create DBBasic home directory""" |
| 16 | dbbasic_home = Path.home() / ".dbbasic" |
| 17 | dbbasic_home.mkdir(exist_ok=True) |
| 18 | return dbbasic_home |
| 19 | |
| 20 | def is_dbbasic_installed(): |
| 21 | """Check if DBBasic core is installed""" |
| 22 | dbbasic_home = get_dbbasic_home() |
| 23 | core_dir = dbbasic_home / "core" |
| 24 | return core_dir.exists() and (core_dir / "dbbasic_crud_engine.py").exists() |
| 25 | |
| 26 | def install_dbbasic(): |
| 27 | """Install DBBasic core from GitHub""" |
| 28 | dbbasic_home = get_dbbasic_home() |
| 29 | core_dir = dbbasic_home / "core" |
| 30 | |
| 31 | with Progress( |
| 32 | SpinnerColumn(), |
| 33 | TextColumn("[progress.description]{task.description}"), |
| 34 | console=console, |
| 35 | ) as progress: |
| 36 | |
| 37 | # Clone repository |
| 38 | task = progress.add_task("Installing DBBasic core...", total=4) |
| 39 | |
| 40 | if core_dir.exists(): |
| 41 | # Update existing installation |
| 42 | progress.update(task, description="Updating DBBasic core...") |
| 43 | subprocess.run( |
| 44 | ["git", "pull"], |
| 45 | cwd=str(core_dir), |
| 46 | capture_output=True, |
| 47 | check=False |
| 48 | ) |
| 49 | else: |
| 50 | # Fresh installation |
| 51 | progress.update(task, description="Downloading DBBasic core...") |
| 52 | result = subprocess.run( |
| 53 | ["git", "clone", "https://github.com/askrobots/dbbasic.git", str(core_dir)], |
| 54 | capture_output=True, |
| 55 | text=True |
| 56 | ) |
| 57 | if result.returncode != 0: |
| 58 | console.print(f"[red]Failed to download DBBasic: {result.stderr}[/red]") |
| 59 | raise SystemExit(1) |
| 60 | |
| 61 | progress.advance(task) |
| 62 | |
| 63 | # Install dependencies |
| 64 | progress.update(task, description="Installing dependencies...") |
| 65 | requirements_file = core_dir / "requirements.txt" |
| 66 | |
| 67 | # Create minimal requirements if file doesn't exist |
| 68 | if not requirements_file.exists(): |
| 69 | requirements = """ |
| 70 | fastapi>=0.100.0 |
| 71 | uvicorn>=0.24.0 |
| 72 | duckdb>=0.9.0 |
| 73 | pyyaml>=6.0 |
| 74 | httpx>=0.25.0 |
| 75 | websockets>=11.0 |
| 76 | jinja2>=3.1.0 |
| 77 | python-multipart>=0.0.5 |
| 78 | pystructui>=0.1.0 |
| 79 | """ |
| 80 | requirements_file.write_text(requirements.strip()) |
| 81 | |
| 82 | # Install requirements |
| 83 | subprocess.run( |
| 84 | [sys.executable, "-m", "pip", "install", "-q", "-r", str(requirements_file)], |
| 85 | capture_output=True |
| 86 | ) |
| 87 | progress.advance(task) |
| 88 | |
| 89 | # Create templates directory |
| 90 | progress.update(task, description="Setting up templates...") |
| 91 | templates_dir = core_dir / "templates" |
| 92 | templates_dir.mkdir(exist_ok=True) |
| 93 | |
| 94 | # Create a basic CRM template if it doesn't exist |
| 95 | crm_template_dir = templates_dir / "crm" |
| 96 | crm_template_dir.mkdir(exist_ok=True) |
| 97 | |
| 98 | crm_config = crm_template_dir / "config.yaml" |
| 99 | if not crm_config.exists(): |
| 100 | crm_config.write_text(""" |
| 101 | # CRM Template Configuration |
| 102 | name: CRM |
| 103 | description: Customer Relationship Management |
| 104 | |
| 105 | models: |
| 106 | contacts: |
| 107 | fields: |
| 108 | - {name: name, type: string, required: true} |
| 109 | - {name: email, type: email, unique: true} |
| 110 | - {name: phone, type: string} |
| 111 | - {name: company, type: string} |
| 112 | - {name: status, type: enum, values: [lead, prospect, customer]} |
| 113 | |
| 114 | deals: |
| 115 | fields: |
| 116 | - {name: title, type: string, required: true} |
| 117 | - {name: contact_id, type: reference, model: contacts} |
| 118 | - {name: amount, type: decimal} |
| 119 | - {name: stage, type: enum, values: [discovery, proposal, negotiation, closed]} |
| 120 | - {name: close_date, type: date} |
| 121 | |
| 122 | views: |
| 123 | - {name: dashboard, type: dashboard} |
| 124 | - {name: contacts, type: table, model: contacts} |
| 125 | - {name: pipeline, type: kanban, model: deals, group_by: stage} |
| 126 | """) |
| 127 | progress.advance(task) |
| 128 | |
| 129 | # The data service is what we need |
| 130 | progress.update(task, description="Finalizing installation...") |
| 131 | data_service = core_dir / "dbbasic_crud_engine.py" |
| 132 | if not data_service.exists(): |
| 133 | # Create a minimal working CRUD engine |
| 134 | crud_engine.write_text(''' |
| 135 | """ |
| 136 | DBBasic CRUD Engine - Minimal working version |
| 137 | """ |
| 138 | |
| 139 | from fastapi import FastAPI |
| 140 | import uvicorn |
| 141 | import yaml |
| 142 | from pathlib import Path |
| 143 | |
| 144 | def create_app(config_file="dbbasic.yaml"): |
| 145 | """Create FastAPI application from config""" |
| 146 | app = FastAPI(title="DBBasic App") |
| 147 | |
| 148 | # Load configuration |
| 149 | config_path = Path(config_file) |
| 150 | if config_path.exists(): |
| 151 | with open(config_path) as f: |
| 152 | config = yaml.safe_load(f) |
| 153 | else: |
| 154 | config = {"project": {"name": "DBBasic App"}} |
| 155 | |
| 156 | @app.get("/") |
| 157 | async def root(): |
| 158 | return { |
| 159 | "message": f"Welcome to {config.get('project', {}).get('name', 'DBBasic')}", |
| 160 | "status": "running", |
| 161 | "config": config_file |
| 162 | } |
| 163 | |
| 164 | @app.get("/health") |
| 165 | async def health(): |
| 166 | return {"status": "healthy"} |
| 167 | |
| 168 | return app |
| 169 | |
| 170 | if __name__ == "__main__": |
| 171 | app = create_app() |
| 172 | uvicorn.run(app, host="0.0.0.0", port=8000) |
| 173 | ''') |
| 174 | |
| 175 | progress.advance(task) |
| 176 | |
| 177 | console.print("\n[green]✅ DBBasic core installed successfully![/green]") |
| 178 | return core_dir |
| 179 | |
| 180 | def ensure_dbbasic(): |
| 181 | """Ensure DBBasic is installed, install if not""" |
| 182 | if not is_dbbasic_installed(): |
| 183 | console.print("[yellow]DBBasic core not found. Installing...[/yellow]") |
| 184 | return install_dbbasic() |
| 185 | else: |
| 186 | dbbasic_home = get_dbbasic_home() |
| 187 | return dbbasic_home / "core" |
| 188 | |
| 189 | def get_dbbasic_dir(): |
| 190 | """Get the DBBasic directory, installing if necessary""" |
| 191 | # First check if we're in development mode (local dbbasic exists) |
| 192 | local_dbbasic = Path("/Users/danq/websheet/dbbasic") |
| 193 | if local_dbbasic.exists(): |
| 194 | return local_dbbasic |
| 195 | |
| 196 | # Otherwise ensure it's installed |
| 197 | return ensure_dbbasic() |