diff --git a/scripts/apk-builder-nginx.conf b/scripts/apk-builder-nginx.conf
new file mode 100644
index 00000000..d4029bee
--- /dev/null
+++ b/scripts/apk-builder-nginx.conf
@@ -0,0 +1,166 @@
+# APK Build Service - Nginx Configuration
+#
+# Usage:
+# 1. Copy to /etc/nginx/sites-available/apk-builder
+# 2. Update server_name to your domain
+# 3. Update SSL certificate paths (or use certbot)
+# 4. sudo ln -s /etc/nginx/sites-available/apk-builder /etc/nginx/sites-enabled/
+# 5. sudo nginx -t && sudo systemctl reload nginx
+#
+# For Let's Encrypt SSL:
+# sudo certbot --nginx -d apk-builder.yourdomain.com
+#
+
+# Rate limiting zone (optional - uncomment to enable)
+# limit_req_zone $binary_remote_addr zone=apk_build_limit:10m rate=5r/m;
+
+upstream apk_builder {
+ server 127.0.0.1:3000;
+ keepalive 32;
+}
+
+# Redirect HTTP to HTTPS
+server {
+ listen 80;
+ listen [::]:80;
+ server_name apk-builder.yourdomain.com;
+
+ # Let's Encrypt challenge
+ location /.well-known/acme-challenge/ {
+ root /var/www/certbot;
+ }
+
+ location / {
+ return 301 https://$server_name$request_uri;
+ }
+}
+
+# HTTPS server
+server {
+ listen 443 ssl http2;
+ listen [::]:443 ssl http2;
+ server_name apk-builder.yourdomain.com;
+
+ # SSL Configuration
+ # Option 1: Let's Encrypt (recommended)
+ ssl_certificate /etc/letsencrypt/live/apk-builder.yourdomain.com/fullchain.pem;
+ ssl_certificate_key /etc/letsencrypt/live/apk-builder.yourdomain.com/privkey.pem;
+
+ # Option 2: Self-signed (for testing only)
+ # ssl_certificate /etc/nginx/ssl/apk-builder.crt;
+ # ssl_certificate_key /etc/nginx/ssl/apk-builder.key;
+
+ # SSL Security Settings
+ ssl_protocols TLSv1.2 TLSv1.3;
+ ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
+ ssl_prefer_server_ciphers off;
+ ssl_session_timeout 1d;
+ ssl_session_cache shared:SSL:50m;
+ ssl_session_tickets off;
+
+ # OCSP Stapling
+ ssl_stapling on;
+ ssl_stapling_verify on;
+ resolver 8.8.8.8 8.8.4.4 valid=300s;
+ resolver_timeout 5s;
+
+ # Security Headers
+ add_header X-Frame-Options "SAMEORIGIN" always;
+ add_header X-Content-Type-Options "nosniff" always;
+ add_header X-XSS-Protection "1; mode=block" always;
+ add_header Referrer-Policy "strict-origin-when-cross-origin" always;
+
+ # Allow large file uploads (for project ZIPs)
+ client_max_body_size 100M;
+ client_body_timeout 300s;
+ client_body_buffer_size 128k;
+
+ # Logging
+ access_log /var/log/nginx/apk-builder.access.log;
+ error_log /var/log/nginx/apk-builder.error.log;
+
+ # Health check endpoint (no auth, fast response)
+ location = /health {
+ proxy_pass http://apk_builder;
+ proxy_http_version 1.1;
+ proxy_set_header Connection "";
+ proxy_connect_timeout 5s;
+ proxy_read_timeout 10s;
+ }
+
+ # Build submission endpoint (longer timeouts)
+ location = /api/build {
+ # Optional rate limiting - uncomment to enable
+ # limit_req zone=apk_build_limit burst=2 nodelay;
+ # limit_req_status 429;
+
+ proxy_pass http://apk_builder;
+ proxy_http_version 1.1;
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Proto $scheme;
+ proxy_set_header Connection "";
+
+ # Long timeout for upload processing
+ proxy_connect_timeout 60s;
+ proxy_send_timeout 300s;
+ proxy_read_timeout 300s;
+ }
+
+ # Status polling endpoint
+ location ~ ^/api/build/[a-f0-9-]+/status$ {
+ proxy_pass http://apk_builder;
+ proxy_http_version 1.1;
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Proto $scheme;
+ proxy_set_header Connection "";
+
+ proxy_connect_timeout 10s;
+ proxy_read_timeout 30s;
+ }
+
+ # APK download endpoint (large response, long timeout)
+ location ~ ^/api/build/[a-f0-9-]+/download$ {
+ proxy_pass http://apk_builder;
+ proxy_http_version 1.1;
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Proto $scheme;
+ proxy_set_header Connection "";
+
+ # Long timeout for large APK downloads
+ proxy_connect_timeout 30s;
+ proxy_read_timeout 600s;
+ proxy_send_timeout 600s;
+
+ # Buffering for large files
+ proxy_buffering on;
+ proxy_buffer_size 128k;
+ proxy_buffers 4 256k;
+ proxy_busy_buffers_size 256k;
+ }
+
+ # All other API endpoints
+ location /api/ {
+ proxy_pass http://apk_builder;
+ proxy_http_version 1.1;
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Proto $scheme;
+ proxy_set_header Connection "";
+
+ proxy_connect_timeout 30s;
+ proxy_read_timeout 60s;
+ }
+
+ # Block all other paths
+ location / {
+ return 404 '{"error": "Not found"}';
+ add_header Content-Type application/json;
+ }
+}
diff --git a/scripts/setup-apk-build-service.sh b/scripts/setup-apk-build-service.sh
new file mode 100755
index 00000000..bff6485c
--- /dev/null
+++ b/scripts/setup-apk-build-service.sh
@@ -0,0 +1,1275 @@
+#!/bin/bash
+#
+# APK Build Service Setup Script
+# Sets up a self-hosted Capacitor APK build service for Shakespeare
+#
+# Usage:
+# curl -fsSL https://your-domain.com/setup-apk-build-service.sh | bash
+# # or
+# ./setup-apk-build-service.sh [options]
+#
+# Options:
+# --dir DIR Installation directory (default: ./apk-build-service)
+# --port PORT Service port (default: 3000)
+# --no-start Don't start the service after setup
+# --help Show this help message
+#
+
+set -e
+
+# Colors for output
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+YELLOW='\033[1;33m'
+BLUE='\033[0;34m'
+NC='\033[0m' # No Color
+
+# Default values
+INSTALL_DIR="./apk-build-service"
+PORT=3000
+START_SERVICE=true
+
+# Parse arguments
+while [[ $# -gt 0 ]]; do
+ case $1 in
+ --dir)
+ INSTALL_DIR="$2"
+ shift 2
+ ;;
+ --port)
+ PORT="$2"
+ shift 2
+ ;;
+ --no-start)
+ START_SERVICE=false
+ shift
+ ;;
+ --help)
+ head -20 "$0" | tail -18
+ exit 0
+ ;;
+ *)
+ echo -e "${RED}Unknown option: $1${NC}"
+ exit 1
+ ;;
+ esac
+done
+
+echo -e "${BLUE}"
+echo "╔═══════════════════════════════════════════════════════════╗"
+echo "║ APK Build Service Setup Script ║"
+echo "║ Capacitor + Android SDK + Docker ║"
+echo "╚═══════════════════════════════════════════════════════════╝"
+echo -e "${NC}"
+
+# Check prerequisites
+echo -e "${YELLOW}Checking prerequisites...${NC}"
+
+if ! command -v docker &> /dev/null; then
+ echo -e "${RED}Error: Docker is not installed${NC}"
+ echo "Install Docker: https://docs.docker.com/get-docker/"
+ exit 1
+fi
+
+if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
+ echo -e "${RED}Error: Docker Compose is not installed${NC}"
+ echo "Install Docker Compose: https://docs.docker.com/compose/install/"
+ exit 1
+fi
+
+# Use 'docker compose' if available, otherwise 'docker-compose'
+if docker compose version &> /dev/null 2>&1; then
+ COMPOSE_CMD="docker compose"
+else
+ COMPOSE_CMD="docker-compose"
+fi
+
+echo -e "${GREEN}Prerequisites OK${NC}"
+
+# Check for existing installation
+EXISTING_API_KEY=""
+UPGRADE_MODE=false
+
+if [ -f "$INSTALL_DIR/.env" ]; then
+ EXISTING_API_KEY=$(grep "^API_KEY=" "$INSTALL_DIR/.env" 2>/dev/null | cut -d= -f2)
+ if [ -n "$EXISTING_API_KEY" ]; then
+ UPGRADE_MODE=true
+ echo -e "${YELLOW}Existing installation found. Preserving API key...${NC}"
+ fi
+fi
+
+# Use existing API key or generate new one
+if [ -n "$EXISTING_API_KEY" ]; then
+ API_KEY="$EXISTING_API_KEY"
+ echo -e "${GREEN}Using existing API key${NC}"
+else
+ API_KEY=$(openssl rand -hex 32 2>/dev/null || head -c 64 /dev/urandom | xxd -p | tr -d '\n' | head -c 64)
+ echo -e "${GREEN}Generated new API key${NC}"
+fi
+
+# Create directory structure
+if [ "$UPGRADE_MODE" = true ]; then
+ echo -e "${YELLOW}Upgrading existing installation...${NC}"
+else
+ echo -e "${YELLOW}Creating directory structure...${NC}"
+fi
+mkdir -p "$INSTALL_DIR/lib"
+cd "$INSTALL_DIR"
+
+echo -e "${GREEN}Installing to: $(pwd)${NC}"
+
+# =============================================================================
+# Dockerfile
+# =============================================================================
+echo -e "${YELLOW}Creating Dockerfile...${NC}"
+cat > Dockerfile << 'DOCKERFILE_EOF'
+FROM node:20-bookworm
+
+# Prevent interactive prompts
+ENV DEBIAN_FRONTEND=noninteractive
+
+# Install system dependencies
+RUN apt-get update && apt-get install -y \
+ openjdk-17-jdk \
+ wget \
+ unzip \
+ git \
+ curl \
+ && rm -rf /var/lib/apt/lists/*
+
+# Set Java home
+ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
+ENV PATH=$PATH:$JAVA_HOME/bin
+
+# Android SDK setup
+ENV ANDROID_HOME=/opt/android-sdk
+ENV ANDROID_SDK_ROOT=/opt/android-sdk
+ENV PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools
+
+# Download and install Android command line tools
+RUN mkdir -p $ANDROID_HOME/cmdline-tools && \
+ cd $ANDROID_HOME/cmdline-tools && \
+ wget -q https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -O tools.zip && \
+ unzip -q tools.zip && \
+ rm tools.zip && \
+ mv cmdline-tools latest
+
+# Accept licenses and install SDK components
+RUN yes | sdkmanager --licenses > /dev/null 2>&1 && \
+ sdkmanager --install \
+ "platform-tools" \
+ "platforms;android-34" \
+ "build-tools;34.0.0" \
+ > /dev/null 2>&1
+
+# Create app directory
+WORKDIR /app
+
+# Install app dependencies
+COPY package.json ./
+RUN npm install --omit=dev
+
+# Copy app source
+COPY . .
+
+# Create directories for builds
+RUN mkdir -p /tmp/builds /tmp/output
+
+# Expose port
+EXPOSE 3000
+
+# Health check
+HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
+ CMD curl -f http://localhost:3000/health || exit 1
+
+# Start server
+CMD ["node", "server.js"]
+DOCKERFILE_EOF
+
+# =============================================================================
+# docker-compose.yml
+# =============================================================================
+echo -e "${YELLOW}Creating docker-compose.yml...${NC}"
+cat > docker-compose.yml << COMPOSE_EOF
+version: '3.8'
+
+services:
+ apk-builder:
+ build: .
+ container_name: apk-build-service
+ restart: unless-stopped
+ ports:
+ - "\${PORT:-3000}:3000"
+ volumes:
+ # Persist Gradle cache for faster builds
+ - gradle-cache:/root/.gradle
+ # Persist npm cache
+ - npm-cache:/root/.npm
+ # Persist Android debug keystore for consistent signing
+ - android-config:/root/.android
+ environment:
+ - NODE_ENV=production
+ - API_KEY=\${API_KEY}
+ - MAX_CONCURRENT_BUILDS=\${MAX_CONCURRENT_BUILDS:-2}
+ - BUILD_TIMEOUT_MS=\${BUILD_TIMEOUT_MS:-600000}
+ - CORS_ORIGINS=\${CORS_ORIGINS:-*}
+ deploy:
+ resources:
+ limits:
+ memory: 4G
+ reservations:
+ memory: 2G
+ logging:
+ driver: "json-file"
+ options:
+ max-size: "10m"
+ max-file: "3"
+
+volumes:
+ gradle-cache:
+ npm-cache:
+ android-config:
+COMPOSE_EOF
+
+# =============================================================================
+# .env
+# =============================================================================
+echo -e "${YELLOW}Creating .env file...${NC}"
+cat > .env << ENV_EOF
+# APK Build Service Configuration
+
+# Server port
+PORT=${PORT}
+
+# API key for authentication (keep this secret!)
+API_KEY=${API_KEY}
+
+# Build settings
+MAX_CONCURRENT_BUILDS=2
+BUILD_TIMEOUT_MS=600000
+
+# CORS origins (comma-separated, or * for all)
+# Example: https://shakespeare.dev,https://your-app.com
+CORS_ORIGINS=*
+ENV_EOF
+
+# =============================================================================
+# package.json
+# =============================================================================
+echo -e "${YELLOW}Creating package.json...${NC}"
+cat > package.json << 'PACKAGE_EOF'
+{
+ "name": "apk-build-service",
+ "version": "1.0.0",
+ "description": "Self-hosted Capacitor APK build service for Shakespeare",
+ "type": "module",
+ "scripts": {
+ "start": "node server.js",
+ "dev": "node --watch server.js"
+ },
+ "dependencies": {
+ "adm-zip": "^0.5.10",
+ "cors": "^2.8.5",
+ "express": "^4.18.2",
+ "multer": "^1.4.5-lts.1",
+ "sharp": "^0.33.2",
+ "uuid": "^9.0.1"
+ },
+ "engines": {
+ "node": ">=18"
+ }
+}
+PACKAGE_EOF
+
+# =============================================================================
+# server.js
+# =============================================================================
+echo -e "${YELLOW}Creating server.js...${NC}"
+cat > server.js << 'SERVER_EOF'
+import express from 'express';
+import cors from 'cors';
+import multer from 'multer';
+import { randomUUID } from 'crypto';
+import { BuildQueue } from './lib/queue.js';
+import { buildAPK } from './lib/builder.js';
+
+const app = express();
+const upload = multer({
+ storage: multer.memoryStorage(),
+ limits: { fileSize: 100 * 1024 * 1024 } // 100MB max
+});
+
+// Configuration
+const PORT = process.env.PORT || 3000;
+const API_KEY = process.env.API_KEY;
+const CORS_ORIGINS = process.env.CORS_ORIGINS?.split(',').map(s => s.trim()) || ['*'];
+const MAX_CONCURRENT = parseInt(process.env.MAX_CONCURRENT_BUILDS || '2');
+const BUILD_TIMEOUT = parseInt(process.env.BUILD_TIMEOUT_MS || '600000');
+
+// Build state
+const builds = new Map();
+const queue = new BuildQueue(MAX_CONCURRENT);
+
+// Middleware
+app.use(cors({
+ origin: CORS_ORIGINS.includes('*') ? true : CORS_ORIGINS,
+ credentials: true
+}));
+app.use(express.json());
+
+// API Key authentication
+const authenticate = (req, res, next) => {
+ if (!API_KEY) return next(); // Skip if no API key configured
+
+ const providedKey = req.headers['x-api-key'] || req.query.apiKey;
+ if (providedKey !== API_KEY) {
+ return res.status(401).json({ error: 'Invalid or missing API key' });
+ }
+ next();
+};
+
+// Health check (no auth required)
+app.get('/health', (req, res) => {
+ res.json({
+ status: 'ok',
+ version: '1.0.0',
+ queue: queue.getStatus(),
+ activeBuilds: builds.size,
+ uptime: process.uptime()
+ });
+});
+
+// Submit build
+app.post('/api/build', authenticate, upload.single('project'), async (req, res) => {
+ try {
+ if (!req.file) {
+ return res.status(400).json({ error: 'No project ZIP provided' });
+ }
+
+ let config;
+ try {
+ config = JSON.parse(req.body.config || '{}');
+ } catch {
+ return res.status(400).json({ error: 'Invalid config JSON' });
+ }
+
+ // Validate required fields
+ if (!config.appName || typeof config.appName !== 'string') {
+ return res.status(400).json({ error: 'appName is required' });
+ }
+
+ if (!config.packageId || !/^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$/i.test(config.packageId)) {
+ return res.status(400).json({
+ error: 'Valid packageId required (e.g., com.example.myapp)'
+ });
+ }
+
+ // Sanitize inputs
+ config.appName = config.appName.slice(0, 50).replace(/[<>:"/\\|?*]/g, '');
+ config.packageId = config.packageId.toLowerCase();
+
+ const buildId = randomUUID();
+ const buildState = {
+ id: buildId,
+ status: 'queued',
+ progress: 0,
+ config,
+ createdAt: new Date().toISOString(),
+ logs: []
+ };
+
+ builds.set(buildId, buildState);
+
+ // Add to queue
+ queue.add(async () => {
+ try {
+ await buildAPK(buildId, req.file.buffer, config, builds, BUILD_TIMEOUT);
+ } catch (error) {
+ const build = builds.get(buildId);
+ if (build && build.status !== 'failed') {
+ build.status = 'failed';
+ build.error = error.message;
+ build.logs.push(`[ERROR] ${error.message}`);
+ }
+ }
+ });
+
+ console.log(`Build ${buildId} queued for ${config.appName} (${config.packageId})`);
+
+ res.json({
+ buildId,
+ status: 'queued',
+ message: 'Build queued successfully'
+ });
+
+ } catch (error) {
+ console.error('Build submission error:', error);
+ res.status(500).json({ error: 'Failed to submit build' });
+ }
+});
+
+// Get build status
+app.get('/api/build/:buildId/status', authenticate, (req, res) => {
+ const build = builds.get(req.params.buildId);
+
+ if (!build) {
+ return res.status(404).json({ error: 'Build not found' });
+ }
+
+ res.json({
+ id: build.id,
+ status: build.status,
+ progress: build.progress,
+ error: build.error,
+ config: {
+ appName: build.config.appName,
+ packageId: build.config.packageId
+ },
+ createdAt: build.createdAt,
+ completedAt: build.completedAt,
+ apkSize: build.apkSize,
+ logs: build.logs.slice(-20) // Last 20 log entries
+ });
+});
+
+// Download APK
+app.get('/api/build/:buildId/download', authenticate, (req, res) => {
+ const build = builds.get(req.params.buildId);
+
+ if (!build) {
+ return res.status(404).json({ error: 'Build not found' });
+ }
+
+ if (build.status !== 'complete') {
+ return res.status(400).json({
+ error: 'Build not complete',
+ status: build.status
+ });
+ }
+
+ if (!build.apkPath) {
+ return res.status(404).json({ error: 'APK file not found' });
+ }
+
+ const filename = `${build.config.appName.replace(/[^a-zA-Z0-9]/g, '_')}.apk`;
+ res.download(build.apkPath, filename, (err) => {
+ if (err) {
+ console.error(`Download error for ${build.id}:`, err);
+ }
+ });
+});
+
+// Get build logs (full)
+app.get('/api/build/:buildId/logs', authenticate, (req, res) => {
+ const build = builds.get(req.params.buildId);
+
+ if (!build) {
+ return res.status(404).json({ error: 'Build not found' });
+ }
+
+ res.json({
+ id: build.id,
+ status: build.status,
+ logs: build.logs
+ });
+});
+
+// Cancel build (if still queued)
+app.delete('/api/build/:buildId', authenticate, (req, res) => {
+ const build = builds.get(req.params.buildId);
+
+ if (!build) {
+ return res.status(404).json({ error: 'Build not found' });
+ }
+
+ if (build.status === 'queued') {
+ build.status = 'cancelled';
+ console.log(`Build ${build.id} cancelled`);
+ res.json({ message: 'Build cancelled' });
+ } else {
+ res.status(400).json({
+ error: 'Cannot cancel build in progress',
+ status: build.status
+ });
+ }
+});
+
+// List recent builds (admin endpoint)
+app.get('/api/builds', authenticate, (req, res) => {
+ const buildList = Array.from(builds.values())
+ .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
+ .slice(0, 50)
+ .map(b => ({
+ id: b.id,
+ status: b.status,
+ appName: b.config.appName,
+ packageId: b.config.packageId,
+ progress: b.progress,
+ createdAt: b.createdAt,
+ completedAt: b.completedAt
+ }));
+
+ res.json({ builds: buildList });
+});
+
+// Cleanup old builds periodically (every 30 minutes)
+setInterval(() => {
+ const maxAge = 60 * 60 * 1000; // 1 hour
+ const now = Date.now();
+ let cleaned = 0;
+
+ for (const [id, build] of builds) {
+ const age = now - new Date(build.createdAt).getTime();
+ if (age > maxAge && ['complete', 'failed', 'cancelled'].includes(build.status)) {
+ builds.delete(id);
+ cleaned++;
+ }
+ }
+
+ if (cleaned > 0) {
+ console.log(`Cleaned up ${cleaned} old build records`);
+ }
+}, 30 * 60 * 1000);
+
+// Graceful shutdown
+process.on('SIGTERM', () => {
+ console.log('Received SIGTERM, shutting down gracefully...');
+ process.exit(0);
+});
+
+app.listen(PORT, () => {
+ console.log('');
+ console.log('╔═══════════════════════════════════════════════════════════╗');
+ console.log('║ APK Build Service Started ║');
+ console.log('╚═══════════════════════════════════════════════════════════╝');
+ console.log('');
+ console.log(` Port: ${PORT}`);
+ console.log(` Max concurrent: ${MAX_CONCURRENT}`);
+ console.log(` Build timeout: ${BUILD_TIMEOUT}ms`);
+ console.log(` API key required: ${!!API_KEY}`);
+ console.log(` CORS origins: ${CORS_ORIGINS.join(', ')}`);
+ console.log('');
+ console.log(` Health check: http://localhost:${PORT}/health`);
+ console.log('');
+});
+SERVER_EOF
+
+# =============================================================================
+# lib/queue.js
+# =============================================================================
+echo -e "${YELLOW}Creating lib/queue.js...${NC}"
+cat > lib/queue.js << 'QUEUE_EOF'
+export class BuildQueue {
+ constructor(maxConcurrent = 2) {
+ this.maxConcurrent = maxConcurrent;
+ this.running = 0;
+ this.queue = [];
+ }
+
+ add(task) {
+ return new Promise((resolve, reject) => {
+ this.queue.push({ task, resolve, reject });
+ this.process();
+ });
+ }
+
+ async process() {
+ if (this.running >= this.maxConcurrent || this.queue.length === 0) {
+ return;
+ }
+
+ this.running++;
+ const { task, resolve, reject } = this.queue.shift();
+
+ try {
+ const result = await task();
+ resolve(result);
+ } catch (error) {
+ reject(error);
+ } finally {
+ this.running--;
+ this.process();
+ }
+ }
+
+ getStatus() {
+ return {
+ running: this.running,
+ queued: this.queue.length,
+ maxConcurrent: this.maxConcurrent
+ };
+ }
+}
+QUEUE_EOF
+
+# =============================================================================
+# lib/builder.js
+# =============================================================================
+echo -e "${YELLOW}Creating lib/builder.js...${NC}"
+cat > lib/builder.js << 'BUILDER_EOF'
+import { exec } from 'child_process';
+import { promisify } from 'util';
+import fs from 'fs/promises';
+import path from 'path';
+import AdmZip from 'adm-zip';
+import sharp from 'sharp';
+
+const execAsync = promisify(exec);
+
+const BUILDS_DIR = '/tmp/builds';
+const OUTPUT_DIR = '/tmp/output';
+
+export async function buildAPK(buildId, zipBuffer, config, builds, timeout) {
+ const buildDir = path.join(BUILDS_DIR, buildId);
+ const outputPath = path.join(OUTPUT_DIR, `${buildId}.apk`);
+ const build = builds.get(buildId);
+
+ const log = (message) => {
+ const timestamp = new Date().toISOString().split('T')[1].split('.')[0];
+ console.log(`[${buildId.slice(0, 8)}] ${message}`);
+ build.logs.push(`[${timestamp}] ${message}`);
+ };
+
+ try {
+ build.status = 'building';
+ build.progress = 5;
+ log('Starting build...');
+
+ // Create directories
+ await fs.mkdir(buildDir, { recursive: true });
+ await fs.mkdir(OUTPUT_DIR, { recursive: true });
+
+ // Extract ZIP
+ build.progress = 10;
+ log('Extracting project files...');
+ const zip = new AdmZip(zipBuffer);
+ zip.extractAllTo(buildDir, true);
+
+ // Verify dist folder exists
+ const distPath = path.join(buildDir, 'dist');
+ try {
+ await fs.access(path.join(distPath, 'index.html'));
+ } catch {
+ throw new Error('No index.html found in dist folder. Build the web project first.');
+ }
+
+ // Count files
+ const fileCount = await countFiles(distPath);
+ log(`Found ${fileCount} files in dist folder`);
+
+ // Initialize npm project
+ build.progress = 15;
+ log('Initializing Capacitor project...');
+ await execAsync('npm init -y', { cwd: buildDir });
+
+ // Install Capacitor
+ build.progress = 20;
+ log('Installing Capacitor dependencies (this may take a minute)...');
+ // Use Capacitor 5.x for Java 17 compatibility (Capacitor 6+ requires Java 21)
+ await execAsync('npm install @capacitor/cli@5 @capacitor/core@5 @capacitor/android@5 --loglevel=error', {
+ cwd: buildDir,
+ timeout: 180000
+ });
+
+ // Create capacitor.config.json
+ build.progress = 30;
+ log('Creating Capacitor configuration...');
+ const capConfig = {
+ appId: config.packageId,
+ appName: config.appName,
+ webDir: 'dist',
+ android: {
+ allowMixedContent: true,
+ buildOptions: {
+ signingType: 'apksigner'
+ }
+ },
+ server: {
+ androidScheme: 'https'
+ }
+ };
+ await fs.writeFile(
+ path.join(buildDir, 'capacitor.config.json'),
+ JSON.stringify(capConfig, null, 2)
+ );
+
+ // Add Android platform
+ build.progress = 40;
+ log('Adding Android platform...');
+ await execAsync('npx cap add android', {
+ cwd: buildDir,
+ timeout: 180000
+ });
+
+ // Sync web assets
+ build.progress = 50;
+ log('Syncing web assets to Android project...');
+ await execAsync('npx cap sync android', {
+ cwd: buildDir,
+ timeout: 120000
+ });
+
+ // Update app icon - use provided icon or auto-detect from project
+ build.progress = 55;
+ let iconSource = null;
+
+ if (config.iconBase64) {
+ log('Using provided app icon...');
+ iconSource = { type: 'base64', data: config.iconBase64 };
+ } else {
+ log('Auto-detecting app icon from project...');
+ iconSource = await autoDetectIcon(distPath, log);
+ }
+
+ if (iconSource) {
+ try {
+ if (iconSource.type === 'base64') {
+ await updateAppIcon(buildDir, iconSource.data);
+ } else {
+ await updateAppIconFromFile(buildDir, iconSource.path);
+ }
+ log('App icon updated successfully');
+ } catch (iconError) {
+ log(`Warning: Failed to update icon: ${iconError.message}`);
+ }
+ } else {
+ log('No app icon found, using default Capacitor icon');
+ }
+
+ // Update app colors if provided
+ if (config.primaryColor) {
+ build.progress = 57;
+ log('Updating app theme colors...');
+ await updateAppColors(buildDir, config.primaryColor);
+ }
+
+ // Make gradlew executable
+ const gradlew = path.join(buildDir, 'android', 'gradlew');
+ await execAsync(`chmod +x ${gradlew}`);
+
+ // Build APK
+ build.progress = 60;
+ const buildType = config.buildType || 'debug';
+ const gradleTask = buildType === 'release' ? 'assembleRelease' : 'assembleDebug';
+ log(`Building APK (${buildType})... This may take several minutes on first run.`);
+
+ const { stdout, stderr } = await execAsync(`./gradlew ${gradleTask} --no-daemon -q`, {
+ cwd: path.join(buildDir, 'android'),
+ timeout: timeout,
+ env: {
+ ...process.env,
+ JAVA_HOME: '/usr/lib/jvm/java-17-openjdk-amd64',
+ ANDROID_HOME: '/opt/android-sdk',
+ ANDROID_SDK_ROOT: '/opt/android-sdk'
+ }
+ });
+
+ if (stderr && !stderr.includes('BUILD SUCCESSFUL')) {
+ log(`Gradle warnings: ${stderr.slice(0, 500)}`);
+ }
+
+ build.progress = 90;
+ log('Gradle build complete, locating APK...');
+
+ // Find and copy APK
+ const apkDir = path.join(buildDir, 'android/app/build/outputs/apk', buildType);
+ let apkFiles;
+ try {
+ apkFiles = await fs.readdir(apkDir);
+ } catch {
+ throw new Error(`APK output directory not found: ${apkDir}`);
+ }
+
+ const apkFile = apkFiles.find(f => f.endsWith('.apk'));
+ if (!apkFile) {
+ throw new Error('APK file not found after build');
+ }
+
+ await fs.copyFile(path.join(apkDir, apkFile), outputPath);
+
+ // Get APK size
+ const stats = await fs.stat(outputPath);
+ const sizeMB = (stats.size / 1024 / 1024).toFixed(2);
+
+ // Cleanup build directory
+ build.progress = 95;
+ log('Cleaning up build files...');
+ await fs.rm(buildDir, { recursive: true, force: true });
+
+ // Update build state
+ build.status = 'complete';
+ build.progress = 100;
+ build.apkPath = outputPath;
+ build.apkSize = stats.size;
+ build.completedAt = new Date().toISOString();
+ log(`Build complete! APK size: ${sizeMB} MB`);
+
+ // Schedule APK cleanup after 1 hour
+ setTimeout(async () => {
+ try {
+ await fs.unlink(outputPath);
+ console.log(`Cleaned up APK: ${buildId}`);
+ } catch {}
+ }, 60 * 60 * 1000);
+
+ } catch (error) {
+ build.status = 'failed';
+ build.error = error.message;
+ build.completedAt = new Date().toISOString();
+ log(`Build failed: ${error.message}`);
+
+ // Cleanup on failure
+ await fs.rm(buildDir, { recursive: true, force: true }).catch(() => {});
+
+ throw error;
+ }
+}
+
+async function countFiles(dir) {
+ let count = 0;
+ const entries = await fs.readdir(dir, { withFileTypes: true });
+ for (const entry of entries) {
+ if (entry.isDirectory()) {
+ count += await countFiles(path.join(dir, entry.name));
+ } else {
+ count++;
+ }
+ }
+ return count;
+}
+
+async function updateAppIcon(buildDir, iconBase64) {
+ const iconBuffer = Buffer.from(iconBase64, 'base64');
+ const resDir = path.join(buildDir, 'android/app/src/main/res');
+
+ const sizes = {
+ 'mipmap-mdpi': 48,
+ 'mipmap-hdpi': 72,
+ 'mipmap-xhdpi': 96,
+ 'mipmap-xxhdpi': 144,
+ 'mipmap-xxxhdpi': 192
+ };
+
+ for (const [folder, size] of Object.entries(sizes)) {
+ const folderPath = path.join(resDir, folder);
+
+ // Ensure folder exists
+ await fs.mkdir(folderPath, { recursive: true });
+
+ const iconPath = path.join(folderPath, 'ic_launcher.png');
+ const roundIconPath = path.join(folderPath, 'ic_launcher_round.png');
+ const foregroundPath = path.join(folderPath, 'ic_launcher_foreground.png');
+
+ // Resize icon
+ const resized = await sharp(iconBuffer)
+ .resize(size, size, { fit: 'contain', background: { r: 255, g: 255, b: 255, alpha: 0 } })
+ .png()
+ .toBuffer();
+
+ await fs.writeFile(iconPath, resized);
+ await fs.writeFile(roundIconPath, resized);
+ await fs.writeFile(foregroundPath, resized);
+ }
+}
+
+async function updateAppIconFromFile(buildDir, iconFilePath) {
+ const iconBuffer = await fs.readFile(iconFilePath);
+ const resDir = path.join(buildDir, 'android/app/src/main/res');
+
+ const sizes = {
+ 'mipmap-mdpi': 48,
+ 'mipmap-hdpi': 72,
+ 'mipmap-xhdpi': 96,
+ 'mipmap-xxhdpi': 144,
+ 'mipmap-xxxhdpi': 192
+ };
+
+ for (const [folder, size] of Object.entries(sizes)) {
+ const folderPath = path.join(resDir, folder);
+ await fs.mkdir(folderPath, { recursive: true });
+
+ const iconPath = path.join(folderPath, 'ic_launcher.png');
+ const roundIconPath = path.join(folderPath, 'ic_launcher_round.png');
+ const foregroundPath = path.join(folderPath, 'ic_launcher_foreground.png');
+
+ const resized = await sharp(iconBuffer)
+ .resize(size, size, { fit: 'contain', background: { r: 255, g: 255, b: 255, alpha: 0 } })
+ .png()
+ .toBuffer();
+
+ await fs.writeFile(iconPath, resized);
+ await fs.writeFile(roundIconPath, resized);
+ await fs.writeFile(foregroundPath, resized);
+ }
+}
+
+async function autoDetectIcon(distPath, log) {
+ // Common icon file patterns to search for (in priority order)
+ const iconPatterns = [
+ // PWA icons (highest priority - usually best quality)
+ 'icon-512x512.png',
+ 'icon-512.png',
+ 'icons/icon-512x512.png',
+ 'icons/512x512.png',
+ 'icon-384x384.png',
+ 'icon-256x256.png',
+ 'icon-192x192.png',
+ 'icon-192.png',
+ 'icons/icon-192x192.png',
+ 'icons/192x192.png',
+ // Apple touch icons (good quality)
+ 'apple-touch-icon.png',
+ 'apple-touch-icon-180x180.png',
+ 'apple-touch-icon-precomposed.png',
+ // Standard icons
+ 'icon.png',
+ 'logo.png',
+ 'app-icon.png',
+ 'favicon.png',
+ // In assets/images folders
+ 'assets/icon.png',
+ 'assets/logo.png',
+ 'assets/images/icon.png',
+ 'assets/images/logo.png',
+ 'images/icon.png',
+ 'images/logo.png',
+ 'img/icon.png',
+ 'img/logo.png',
+ // Favicon as last resort (usually small)
+ 'favicon.ico',
+ 'favicon-32x32.png',
+ 'favicon-16x16.png'
+ ];
+
+ // Try each pattern
+ for (const pattern of iconPatterns) {
+ const iconPath = path.join(distPath, pattern);
+ try {
+ const stat = await fs.stat(iconPath);
+ if (stat.isFile()) {
+ // Verify it's a valid image
+ const ext = path.extname(iconPath).toLowerCase();
+ if (['.png', '.jpg', '.jpeg', '.ico', '.webp'].includes(ext)) {
+ log(`Found icon: ${pattern}`);
+ return { type: 'file', path: iconPath };
+ }
+ }
+ } catch {
+ // File doesn't exist, try next pattern
+ }
+ }
+
+ // Try to parse index.html for icon links
+ try {
+ const indexPath = path.join(distPath, 'index.html');
+ const html = await fs.readFile(indexPath, 'utf8');
+
+ // Look for various icon link patterns
+ const iconLinkPatterns = [
+ /]*rel=["']apple-touch-icon["'][^>]*href=["']([^"']+)["']/i,
+ /]*rel=["']icon["'][^>]*href=["']([^"']+)["']/i,
+ /]*rel=["']shortcut icon["'][^>]*href=["']([^"']+)["']/i,
+ /]*href=["']([^"']+)["'][^>]*rel=["']apple-touch-icon["']/i,
+ /]*href=["']([^"']+)["'][^>]*rel=["']icon["']/i,
+ ];
+
+ for (const pattern of iconLinkPatterns) {
+ const match = html.match(pattern);
+ if (match && match[1]) {
+ let iconHref = match[1];
+ // Remove leading slash or ./
+ iconHref = iconHref.replace(/^\.?\//, '');
+ const iconPath = path.join(distPath, iconHref);
+
+ try {
+ const stat = await fs.stat(iconPath);
+ if (stat.isFile()) {
+ log(`Found icon from HTML: ${iconHref}`);
+ return { type: 'file', path: iconPath };
+ }
+ } catch {
+ // Referenced icon doesn't exist
+ }
+ }
+ }
+ } catch {
+ // Couldn't parse index.html
+ }
+
+ return null;
+}
+
+async function updateAppColors(buildDir, primaryColor) {
+ const colorsPath = path.join(
+ buildDir,
+ 'android/app/src/main/res/values/colors.xml'
+ );
+
+ const colorsXml = `
+
Built with Shakespeare APK Builder
+