forked from jsnbuchanan/crowd-funder-for-time-pwa
migration: move to bash based build scripts
This commit is contained in:
285
scripts/README.md
Normal file
285
scripts/README.md
Normal file
@@ -0,0 +1,285 @@
|
||||
# TimeSafari Build Scripts
|
||||
|
||||
This directory contains unified build and test scripts for the TimeSafari application. All scripts use a common utilities library to eliminate redundancy and provide consistent logging, error handling, timing, and environment variable management.
|
||||
|
||||
## Architecture
|
||||
|
||||
### Common Utilities (`common.sh`)
|
||||
|
||||
The `common.sh` script provides shared functionality used by all build scripts:
|
||||
|
||||
- **Logging Functions**: `log_info`, `log_success`, `log_warn`, `log_error`, `log_debug`, `log_step`
|
||||
- **Timing**: `measure_time` for execution time tracking
|
||||
- **Headers/Footers**: `print_header`, `print_footer` for consistent output formatting
|
||||
- **Validation**: `check_command`, `check_directory`, `check_file`, `check_venv`
|
||||
- **Execution**: `safe_execute` for error-handled command execution
|
||||
- **Utilities**: `get_git_hash`, `clean_build_artifacts`, `validate_env_vars`
|
||||
- **Environment Management**: `setup_build_env`, `setup_app_directories`, `load_env_file`, `print_env_vars`
|
||||
- **CLI**: `parse_args`, `print_usage` for command-line argument handling
|
||||
|
||||
### Environment Variable Management
|
||||
|
||||
All scripts automatically handle environment variables for different build types:
|
||||
|
||||
#### Build Types and Environment Variables
|
||||
|
||||
| Platform | Mode | PWA Enabled | Native Features | Build Script |
|
||||
|----------|------|-------------|-----------------|--------------|
|
||||
| `web` | web | true | false | `build-web.sh` |
|
||||
| `capacitor` | capacitor | false | true | `build-capacitor.sh` |
|
||||
| `electron` | electron | false | true | `build-electron.sh` |
|
||||
|
||||
#### Automatic Environment Setup
|
||||
|
||||
Each script automatically:
|
||||
1. **Sets platform-specific variables** based on build type
|
||||
2. **Gets git hash** for versioning (`VITE_GIT_HASH`)
|
||||
3. **Creates application directories** (`~/.local/share/TimeSafari/timesafari`)
|
||||
4. **Loads .env file** if it exists
|
||||
5. **Validates required variables** when needed
|
||||
|
||||
#### Environment Functions
|
||||
|
||||
- `setup_build_env(build_type, production)` - Sets environment for specific build type
|
||||
- `setup_app_directories()` - Creates necessary application directories
|
||||
- `load_env_file(filename)` - Loads variables from .env file
|
||||
- `print_env_vars(prefix)` - Displays current environment variables
|
||||
- `validate_env_vars(var1, var2, ...)` - Validates required variables exist
|
||||
|
||||
### Script Structure
|
||||
|
||||
All scripts follow this unified pattern:
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
# script-name.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Brief description of what the script does
|
||||
#
|
||||
# Exit Codes: List of exit codes and their meanings
|
||||
# Usage: ./scripts/script-name.sh [options]
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Source common utilities
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args "$@"
|
||||
|
||||
# Print header
|
||||
print_header "Script Title"
|
||||
log_info "Starting process at $(date)"
|
||||
|
||||
# Setup environment (automatic)
|
||||
setup_build_env "build_type"
|
||||
setup_app_directories
|
||||
load_env_file ".env"
|
||||
|
||||
# Execute steps with safe_execute
|
||||
safe_execute "Step description" "command to execute" || exit 1
|
||||
|
||||
# Print footer
|
||||
print_footer "Script Title"
|
||||
exit 0
|
||||
```
|
||||
|
||||
## Available Scripts
|
||||
|
||||
### Test Scripts
|
||||
|
||||
- **`test-all.sh`**: Comprehensive test suite (prerequisites, build, web tests, mobile tests)
|
||||
- **`test-mobile.sh`**: Mobile test suite (Capacitor build, Android tests, iOS tests)
|
||||
- **`test-common.sh`**: Test script to verify common utilities work correctly
|
||||
- **`test-env.sh`**: Test script to verify environment variable handling
|
||||
|
||||
### Build Scripts
|
||||
|
||||
- **`build-electron.sh`**: Complete Electron build process
|
||||
- **`build-android.sh`**: Complete Android build process
|
||||
- **`build-electron-linux.sh`**: Linux Electron packaging (AppImage, .deb)
|
||||
- **`build-electron-mac.sh`**: macOS Electron packaging (standard, universal)
|
||||
|
||||
### Development Scripts
|
||||
|
||||
- **`electron-dev.sh`**: Electron development workflow
|
||||
|
||||
## Benefits of Unification
|
||||
|
||||
### Before (Redundant)
|
||||
```bash
|
||||
# Each script had 50+ lines of duplicate code:
|
||||
readonly RED='\033[0;31m'
|
||||
readonly GREEN='\033[0;32m'
|
||||
# ... 40+ more lines of duplicate logging functions
|
||||
log_info "Step 1/4: Doing something..."
|
||||
if ! measure_time some_command; then
|
||||
log_error "Step failed!"
|
||||
exit 1
|
||||
fi
|
||||
# Manual environment variable setup
|
||||
export VITE_PLATFORM=electron
|
||||
export VITE_PWA_ENABLED=false
|
||||
# ... more manual exports
|
||||
```
|
||||
|
||||
### After (Unified)
|
||||
```bash
|
||||
# Each script is now ~20 lines of focused logic:
|
||||
source "$(dirname "$0")/common.sh"
|
||||
print_header "Script Title"
|
||||
setup_build_env "electron" # Automatic environment setup
|
||||
safe_execute "Step description" "some_command" || exit 1
|
||||
print_footer "Script Title"
|
||||
```
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Running Tests
|
||||
```bash
|
||||
# Run all tests
|
||||
./scripts/test-all.sh
|
||||
|
||||
# Run mobile tests only
|
||||
./scripts/test-mobile.sh
|
||||
|
||||
# Run with verbose logging
|
||||
./scripts/test-all.sh --verbose
|
||||
|
||||
# Show environment variables
|
||||
./scripts/test-env.sh --env
|
||||
```
|
||||
|
||||
### Building Applications
|
||||
```bash
|
||||
# Build Electron
|
||||
./scripts/build-electron.sh
|
||||
|
||||
# Build Android
|
||||
./scripts/build-android.sh
|
||||
|
||||
# Build Linux package
|
||||
./scripts/build-electron-linux.sh deb
|
||||
|
||||
# Build universal Mac package
|
||||
./scripts/build-electron-mac.sh universal
|
||||
|
||||
# Show environment variables for build
|
||||
./scripts/build-electron.sh --env
|
||||
```
|
||||
|
||||
### Development Workflows
|
||||
```bash
|
||||
# Start Electron development
|
||||
./scripts/electron-dev.sh
|
||||
```
|
||||
|
||||
## Environment Variable Features
|
||||
|
||||
### Automatic Setup
|
||||
All scripts automatically configure the correct environment variables for their build type:
|
||||
|
||||
```bash
|
||||
# Electron builds automatically get:
|
||||
export VITE_PLATFORM=electron
|
||||
export VITE_PWA_ENABLED=false
|
||||
export VITE_DISABLE_PWA=true
|
||||
export DEBUG_MIGRATIONS=0
|
||||
export VITE_GIT_HASH=<git-hash>
|
||||
|
||||
# Production builds also get:
|
||||
export NODE_ENV=production
|
||||
```
|
||||
|
||||
### .env File Support
|
||||
Scripts automatically load variables from `.env` files if they exist:
|
||||
|
||||
```bash
|
||||
# .env file example:
|
||||
VITE_API_URL=https://api.example.com
|
||||
VITE_DEBUG=true
|
||||
CUSTOM_VAR=value
|
||||
```
|
||||
|
||||
### Environment Validation
|
||||
Required environment variables can be validated:
|
||||
|
||||
```bash
|
||||
# In your script
|
||||
validate_env_vars "VITE_API_URL" "VITE_DEBUG" || exit 1
|
||||
```
|
||||
|
||||
### Environment Inspection
|
||||
View current environment variables with the `--env` flag:
|
||||
|
||||
```bash
|
||||
./scripts/build-electron.sh --env
|
||||
./scripts/test-env.sh --env
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
All scripts use consistent error handling:
|
||||
|
||||
- **Exit Codes**: Each script documents specific exit codes
|
||||
- **Safe Execution**: `safe_execute` provides timing and error handling
|
||||
- **Graceful Failure**: Scripts stop on first error with clear messages
|
||||
- **Logging**: All operations are logged with timestamps and colors
|
||||
- **Environment Validation**: Required variables are checked before execution
|
||||
|
||||
## Testing
|
||||
|
||||
To verify the common utilities work correctly:
|
||||
|
||||
```bash
|
||||
# Test all common functions
|
||||
./scripts/test-common.sh
|
||||
|
||||
# Test environment variable handling
|
||||
./scripts/test-env.sh
|
||||
|
||||
# Test with verbose logging
|
||||
./scripts/test-env.sh --verbose
|
||||
```
|
||||
|
||||
## Maintenance
|
||||
|
||||
### Adding New Scripts
|
||||
|
||||
1. Create new script following the unified pattern
|
||||
2. Source `common.sh` at the top
|
||||
3. Use `setup_build_env()` for environment setup
|
||||
4. Use `safe_execute` for command execution
|
||||
5. Document exit codes and usage
|
||||
6. Make executable: `chmod +x scripts/new-script.sh`
|
||||
|
||||
### Modifying Common Utilities
|
||||
|
||||
1. Update `common.sh` with new functions
|
||||
2. Export new functions with `export -f function_name`
|
||||
3. Update this README if adding new categories
|
||||
4. Test with `test-common.sh` and `test-env.sh`
|
||||
|
||||
### Adding New Build Types
|
||||
|
||||
1. Add new case to `setup_build_env()` function
|
||||
2. Define appropriate environment variables
|
||||
3. Update this README with new build type
|
||||
4. Test with `test-env.sh`
|
||||
|
||||
## Security Considerations
|
||||
|
||||
- All scripts use `set -e` for immediate failure on errors
|
||||
- Commands are executed through `safe_execute` for consistent error handling
|
||||
- No direct execution of user input without validation
|
||||
- Environment variables are validated when required
|
||||
- .env files are loaded safely with proper parsing
|
||||
|
||||
## Performance
|
||||
|
||||
- Common utilities are sourced once per script execution
|
||||
- Timing information is automatically collected for all operations
|
||||
- Build artifacts are cleaned up automatically
|
||||
- No redundant command execution or file operations
|
||||
- Environment variables are set efficiently with minimal overhead
|
||||
71
scripts/build-android.sh
Executable file
71
scripts/build-android.sh
Executable file
@@ -0,0 +1,71 @@
|
||||
#!/bin/bash
|
||||
# build-android.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Android build script for TimeSafari application
|
||||
# This script handles the complete Android build process including cleanup,
|
||||
# web build, Capacitor build, Gradle build, and Android Studio launch.
|
||||
#
|
||||
# Exit Codes:
|
||||
# 1 - Android cleanup failed
|
||||
# 2 - Web build failed
|
||||
# 3 - Capacitor build failed
|
||||
# 4 - Gradle clean failed
|
||||
# 5 - Gradle assemble failed
|
||||
# 6 - Capacitor sync failed
|
||||
# 7 - Asset generation failed
|
||||
# 8 - Android Studio launch failed
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Source common utilities
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args "$@"
|
||||
|
||||
# Print build header
|
||||
print_header "TimeSafari Android Build Process"
|
||||
log_info "Starting Android build process at $(date)"
|
||||
|
||||
# Setup environment for Capacitor build
|
||||
setup_build_env "capacitor"
|
||||
|
||||
# Setup application directories
|
||||
setup_app_directories
|
||||
|
||||
# Load environment from .env file if it exists
|
||||
load_env_file ".env"
|
||||
|
||||
# Step 1: Clean Android app
|
||||
safe_execute "Cleaning Android app" "npm run clean:android" || exit 1
|
||||
|
||||
# Step 2: Clean dist directory
|
||||
log_info "Cleaning dist directory..."
|
||||
clean_build_artifacts "dist"
|
||||
|
||||
# Step 3: Build web assets
|
||||
safe_execute "Building web assets" "npm run build:web" || exit 2
|
||||
|
||||
# Step 4: Build Capacitor version
|
||||
safe_execute "Building Capacitor version" "npm run build:capacitor" || exit 3
|
||||
|
||||
# Step 5: Clean Gradle build
|
||||
safe_execute "Cleaning Gradle build" "cd android && ./gradlew clean && cd .." || exit 4
|
||||
|
||||
# Step 6: Assemble debug build
|
||||
safe_execute "Assembling debug build" "cd android && ./gradlew assembleDebug && cd .." || exit 5
|
||||
|
||||
# Step 7: Sync with Capacitor
|
||||
safe_execute "Syncing with Capacitor" "npx cap sync android" || exit 6
|
||||
|
||||
# Step 8: Generate assets and open Android Studio
|
||||
safe_execute "Generating assets" "npx capacitor-assets generate --android" || exit 7
|
||||
safe_execute "Opening Android Studio" "npx cap open android" || exit 8
|
||||
|
||||
# Print build summary
|
||||
log_success "Android build completed successfully!"
|
||||
print_footer "Android Build"
|
||||
|
||||
# Exit with success
|
||||
exit 0
|
||||
82
scripts/build-electron-linux.sh
Executable file
82
scripts/build-electron-linux.sh
Executable file
@@ -0,0 +1,82 @@
|
||||
#!/bin/bash
|
||||
# build-electron-linux.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Electron Linux build script for TimeSafari application
|
||||
# This script builds Electron packages for Linux with support for different formats.
|
||||
#
|
||||
# Usage: ./scripts/build-electron-linux.sh [deb|prod]
|
||||
# - No argument: Builds AppImage
|
||||
# - deb: Builds .deb package
|
||||
# - prod: Builds production AppImage
|
||||
#
|
||||
# Exit Codes:
|
||||
# 1 - Build failed
|
||||
# 2 - Invalid argument
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Source common utilities
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args "$@"
|
||||
|
||||
# Parse build type argument
|
||||
BUILD_TYPE=${1:-"appimage"}
|
||||
PRODUCTION=false
|
||||
|
||||
case $BUILD_TYPE in
|
||||
"deb")
|
||||
BUILD_TARGET="deb"
|
||||
log_info "Building .deb package"
|
||||
;;
|
||||
"prod")
|
||||
BUILD_TARGET="AppImage"
|
||||
PRODUCTION=true
|
||||
log_info "Building production AppImage"
|
||||
;;
|
||||
"appimage"|"")
|
||||
BUILD_TARGET="AppImage"
|
||||
log_info "Building AppImage"
|
||||
;;
|
||||
*)
|
||||
log_error "Invalid build type: $BUILD_TYPE"
|
||||
log_error "Usage: $0 [deb|prod]"
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
|
||||
# Print build header
|
||||
print_header "TimeSafari Electron Linux Build"
|
||||
log_info "Starting Linux build process at $(date)"
|
||||
log_info "Build type: $BUILD_TYPE"
|
||||
log_info "Build target: $BUILD_TARGET"
|
||||
log_info "Production mode: $PRODUCTION"
|
||||
|
||||
# Setup environment for Electron build
|
||||
setup_build_env "electron" "$PRODUCTION"
|
||||
|
||||
# Setup application directories
|
||||
setup_app_directories
|
||||
|
||||
# Load environment from .env file if it exists
|
||||
load_env_file ".env"
|
||||
|
||||
# Step 1: Build Electron application
|
||||
if [ "$PRODUCTION" = true ]; then
|
||||
safe_execute "Building production Electron application" "npm run build:electron-prod" || exit 1
|
||||
else
|
||||
safe_execute "Building Electron application" "npm run build:electron" || exit 1
|
||||
fi
|
||||
|
||||
# Step 2: Build package
|
||||
safe_execute "Building Linux package" "npx electron-builder --linux $BUILD_TARGET" || exit 1
|
||||
|
||||
# Print build summary
|
||||
log_success "Linux build completed successfully!"
|
||||
log_info "Package type: $BUILD_TARGET"
|
||||
print_footer "Linux Build"
|
||||
|
||||
# Exit with success
|
||||
exit 0
|
||||
74
scripts/build-electron-mac.sh
Normal file
74
scripts/build-electron-mac.sh
Normal file
@@ -0,0 +1,74 @@
|
||||
#!/bin/bash
|
||||
# build-electron-mac.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Electron Mac build script for TimeSafari application
|
||||
# This script builds Electron packages for macOS with support for universal builds.
|
||||
#
|
||||
# Usage: ./scripts/build-electron-mac.sh [universal]
|
||||
# - No argument: Builds standard Mac package
|
||||
# - universal: Builds universal Mac package (Intel + Apple Silicon)
|
||||
#
|
||||
# Exit Codes:
|
||||
# 1 - Build failed
|
||||
# 2 - Invalid argument
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Source common utilities
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args "$@"
|
||||
|
||||
# Parse build type argument
|
||||
BUILD_TYPE=${1:-"standard"}
|
||||
UNIVERSAL=false
|
||||
|
||||
case $BUILD_TYPE in
|
||||
"universal")
|
||||
UNIVERSAL=true
|
||||
log_info "Building universal Mac package (Intel + Apple Silicon)"
|
||||
;;
|
||||
"standard"|"")
|
||||
log_info "Building standard Mac package"
|
||||
;;
|
||||
*)
|
||||
log_error "Invalid build type: $BUILD_TYPE"
|
||||
log_error "Usage: $0 [universal]"
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
|
||||
# Print build header
|
||||
print_header "TimeSafari Electron Mac Build"
|
||||
log_info "Starting Mac build process at $(date)"
|
||||
log_info "Build type: $BUILD_TYPE"
|
||||
log_info "Universal build: $UNIVERSAL"
|
||||
|
||||
# Setup environment for Electron build (production mode for packaging)
|
||||
setup_build_env "electron" "true"
|
||||
|
||||
# Setup application directories
|
||||
setup_app_directories
|
||||
|
||||
# Load environment from .env file if it exists
|
||||
load_env_file ".env"
|
||||
|
||||
# Step 1: Build Electron application
|
||||
safe_execute "Building Electron application" "npm run build:electron-prod" || exit 1
|
||||
|
||||
# Step 2: Build package
|
||||
if [ "$UNIVERSAL" = true ]; then
|
||||
safe_execute "Building universal Mac package" "npx electron-builder --mac --universal" || exit 1
|
||||
else
|
||||
safe_execute "Building Mac package" "npx electron-builder --mac" || exit 1
|
||||
fi
|
||||
|
||||
# Print build summary
|
||||
log_success "Mac build completed successfully!"
|
||||
log_info "Package type: $([ "$UNIVERSAL" = true ] && echo "Universal" || echo "Standard")"
|
||||
print_footer "Mac Build"
|
||||
|
||||
# Exit with success
|
||||
exit 0
|
||||
53
scripts/build-electron.sh
Executable file
53
scripts/build-electron.sh
Executable file
@@ -0,0 +1,53 @@
|
||||
#!/bin/bash
|
||||
# build-electron.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Electron build script for TimeSafari application
|
||||
# This script handles the complete Electron build process including cleanup,
|
||||
# TypeScript compilation, Vite build, and Electron-specific setup.
|
||||
#
|
||||
# Exit Codes:
|
||||
# 1 - Cleanup failed
|
||||
# 2 - TypeScript compilation failed
|
||||
# 3 - Vite build failed
|
||||
# 4 - Electron build script failed
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Source common utilities
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args "$@"
|
||||
|
||||
# Print build header
|
||||
print_header "TimeSafari Electron Build Process"
|
||||
log_info "Starting Electron build process at $(date)"
|
||||
|
||||
# Setup environment for Electron build
|
||||
setup_build_env "electron"
|
||||
|
||||
# Setup application directories
|
||||
setup_app_directories
|
||||
|
||||
# Load environment from .env file if it exists
|
||||
load_env_file ".env"
|
||||
|
||||
# Step 1: Clean previous builds
|
||||
safe_execute "Cleaning previous builds" "npm run clean:electron" || exit 1
|
||||
|
||||
# Step 2: Compile TypeScript for Electron
|
||||
safe_execute "Compiling TypeScript for Electron" "npx tsc -p tsconfig.electron.json" || exit 2
|
||||
|
||||
# Step 3: Build with Vite
|
||||
safe_execute "Building with Vite" "npx vite build --config vite.config.electron.mts" || exit 3
|
||||
|
||||
# Step 4: Run Electron build script
|
||||
safe_execute "Running Electron build script" "node scripts/build-electron.js" || exit 4
|
||||
|
||||
# Print build summary
|
||||
log_success "Electron build completed successfully!"
|
||||
print_footer "Electron Build"
|
||||
|
||||
# Exit with success
|
||||
exit 0
|
||||
331
scripts/common.sh
Executable file
331
scripts/common.sh
Executable file
@@ -0,0 +1,331 @@
|
||||
#!/bin/bash
|
||||
# common.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Common utilities and functions for TimeSafari build scripts
|
||||
# This script provides shared logging, timing, and utility functions
|
||||
# that can be sourced by other build scripts to eliminate redundancy.
|
||||
#
|
||||
# Usage: source ./scripts/common.sh
|
||||
#
|
||||
# Provides:
|
||||
# - Color constants
|
||||
# - Logging functions (log_info, log_success, log_warn, log_error)
|
||||
# - Timing function (measure_time)
|
||||
# - Common utility functions
|
||||
# - Environment variable management
|
||||
|
||||
# ANSI color codes for better output formatting
|
||||
readonly RED='\033[0;31m'
|
||||
readonly GREEN='\033[0;32m'
|
||||
readonly YELLOW='\033[1;33m'
|
||||
readonly BLUE='\033[0;34m'
|
||||
readonly PURPLE='\033[0;35m'
|
||||
readonly CYAN='\033[0;36m'
|
||||
readonly NC='\033[0m' # No Color
|
||||
|
||||
# Logging functions
|
||||
log_info() {
|
||||
echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')] [INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_success() {
|
||||
echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] [SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
log_warn() {
|
||||
echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] [WARN]${NC} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
log_debug() {
|
||||
echo -e "${PURPLE}[$(date '+%Y-%m-%d %H:%M:%S')] [DEBUG]${NC} $1"
|
||||
}
|
||||
|
||||
log_step() {
|
||||
echo -e "${CYAN}[$(date '+%Y-%m-%d %H:%M:%S')] [STEP]${NC} $1"
|
||||
}
|
||||
|
||||
# Function to measure and log execution time
|
||||
measure_time() {
|
||||
local start_time=$(date +%s)
|
||||
"$@"
|
||||
local end_time=$(date +%s)
|
||||
local duration=$((end_time - start_time))
|
||||
log_success "Completed in ${duration} seconds"
|
||||
}
|
||||
|
||||
# Function to print section headers
|
||||
print_header() {
|
||||
local title="$1"
|
||||
echo -e "\n${BLUE}=== $title ===${NC}\n"
|
||||
}
|
||||
|
||||
print_footer() {
|
||||
local title="$1"
|
||||
echo -e "\n${GREEN}=== $title Complete ===${NC}\n"
|
||||
}
|
||||
|
||||
# Function to check if a command exists
|
||||
check_command() {
|
||||
if ! command -v "$1" &> /dev/null; then
|
||||
log_error "$1 is required but not installed."
|
||||
return 1
|
||||
fi
|
||||
log_debug "Found $1: $(command -v "$1")"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to check if a directory exists
|
||||
check_directory() {
|
||||
if [ ! -d "$1" ]; then
|
||||
log_error "Directory not found: $1"
|
||||
return 1
|
||||
fi
|
||||
log_debug "Directory exists: $1"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to check if a file exists
|
||||
check_file() {
|
||||
if [ ! -f "$1" ]; then
|
||||
log_error "File not found: $1"
|
||||
return 1
|
||||
fi
|
||||
log_debug "File exists: $1"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to safely execute a command with error handling
|
||||
safe_execute() {
|
||||
local step_name="$1"
|
||||
local command="$2"
|
||||
|
||||
log_step "$step_name"
|
||||
if ! measure_time eval "$command"; then
|
||||
log_error "$step_name failed!"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to check virtual environment for Python scripts
|
||||
check_venv() {
|
||||
if [ ! -d ".venv" ]; then
|
||||
log_error "Virtual environment not found. Please create it first:"
|
||||
log_error "python -m venv .venv"
|
||||
log_error "source .venv/bin/activate"
|
||||
log_error "pip install -r requirements.txt"
|
||||
return 1
|
||||
fi
|
||||
log_debug "Virtual environment found: .venv"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to get git hash for versioning
|
||||
get_git_hash() {
|
||||
if command -v git &> /dev/null; then
|
||||
git log -1 --pretty=format:%h 2>/dev/null || echo "unknown"
|
||||
else
|
||||
echo "unknown"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to clean build artifacts
|
||||
clean_build_artifacts() {
|
||||
local artifacts=("$@")
|
||||
for artifact in "${artifacts[@]}"; do
|
||||
if [ -e "$artifact" ]; then
|
||||
log_info "Cleaning $artifact"
|
||||
rm -rf "$artifact"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Function to validate environment variables
|
||||
validate_env_vars() {
|
||||
local required_vars=("$@")
|
||||
local missing_vars=()
|
||||
|
||||
for var in "${required_vars[@]}"; do
|
||||
if [ -z "${!var}" ]; then
|
||||
missing_vars+=("$var")
|
||||
fi
|
||||
done
|
||||
|
||||
if [ ${#missing_vars[@]} -gt 0 ]; then
|
||||
log_error "Missing required environment variables: ${missing_vars[*]}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to set environment variables for different build types
|
||||
setup_build_env() {
|
||||
local build_type="$1"
|
||||
local production="${2:-false}"
|
||||
|
||||
log_info "Setting up environment for $build_type build"
|
||||
|
||||
# Get git hash for versioning
|
||||
local git_hash=$(get_git_hash)
|
||||
export VITE_GIT_HASH="$git_hash"
|
||||
log_debug "Set VITE_GIT_HASH=$git_hash"
|
||||
|
||||
case $build_type in
|
||||
"electron")
|
||||
export VITE_PLATFORM=electron
|
||||
export VITE_PWA_ENABLED=false
|
||||
export VITE_DISABLE_PWA=true
|
||||
export DEBUG_MIGRATIONS=0
|
||||
if [ "$production" = true ]; then
|
||||
export NODE_ENV=production
|
||||
log_debug "Set production mode for Electron"
|
||||
fi
|
||||
;;
|
||||
"capacitor")
|
||||
export VITE_PLATFORM=capacitor
|
||||
export VITE_PWA_ENABLED=false
|
||||
export VITE_DISABLE_PWA=true
|
||||
export DEBUG_MIGRATIONS=0
|
||||
;;
|
||||
"web")
|
||||
export VITE_PLATFORM=web
|
||||
export VITE_PWA_ENABLED=true
|
||||
export VITE_DISABLE_PWA=false
|
||||
export DEBUG_MIGRATIONS=0
|
||||
;;
|
||||
*)
|
||||
log_warn "Unknown build type: $build_type, using default environment"
|
||||
export VITE_PLATFORM=web
|
||||
export VITE_PWA_ENABLED=true
|
||||
export VITE_DISABLE_PWA=false
|
||||
export DEBUG_MIGRATIONS=0
|
||||
;;
|
||||
esac
|
||||
|
||||
# Log environment setup
|
||||
log_debug "Environment variables set:"
|
||||
log_debug " VITE_PLATFORM=$VITE_PLATFORM"
|
||||
log_debug " VITE_PWA_ENABLED=$VITE_PWA_ENABLED"
|
||||
log_debug " VITE_DISABLE_PWA=$VITE_DISABLE_PWA"
|
||||
log_debug " DEBUG_MIGRATIONS=$DEBUG_MIGRATIONS"
|
||||
if [ -n "$NODE_ENV" ]; then
|
||||
log_debug " NODE_ENV=$NODE_ENV"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to create application directories
|
||||
setup_app_directories() {
|
||||
log_info "Setting up application directories..."
|
||||
|
||||
# Create TimeSafari data directory
|
||||
mkdir -p ~/.local/share/TimeSafari/timesafari
|
||||
|
||||
# Create build directories if they don't exist
|
||||
mkdir -p dist
|
||||
mkdir -p dist-electron
|
||||
|
||||
log_debug "Application directories created"
|
||||
}
|
||||
|
||||
# Function to load environment from .env file if it exists
|
||||
load_env_file() {
|
||||
local env_file="$1"
|
||||
|
||||
if [ -f "$env_file" ]; then
|
||||
log_info "Loading environment from $env_file"
|
||||
# Export variables from .env file (simple key=value format)
|
||||
while IFS='=' read -r key value; do
|
||||
# Skip comments and empty lines
|
||||
[[ $key =~ ^#.*$ ]] && continue
|
||||
[[ -z $key ]] && continue
|
||||
|
||||
# Remove quotes from value if present
|
||||
value=$(echo "$value" | sed 's/^["'\'']//;s/["'\'']$//')
|
||||
|
||||
export "$key=$value"
|
||||
log_debug "Loaded: $key=$value"
|
||||
done < "$env_file"
|
||||
else
|
||||
log_debug "No $env_file file found"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to print current environment variables
|
||||
print_env_vars() {
|
||||
local prefix="$1"
|
||||
|
||||
if [ -n "$prefix" ]; then
|
||||
log_info "Environment variables with prefix '$prefix':"
|
||||
env | grep "^$prefix" | sort | while read -r line; do
|
||||
log_debug " $line"
|
||||
done
|
||||
else
|
||||
log_info "Current environment variables:"
|
||||
env | sort | while read -r line; do
|
||||
log_debug " $line"
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to print script usage
|
||||
print_usage() {
|
||||
local script_name="$1"
|
||||
local usage_text="$2"
|
||||
|
||||
echo "Usage: $script_name $usage_text"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " -h, --help Show this help message"
|
||||
echo " -v, --verbose Enable verbose logging"
|
||||
echo " -e, --env Show environment variables"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Function to parse command line arguments
|
||||
parse_args() {
|
||||
local args=("$@")
|
||||
local verbose=false
|
||||
local show_env=false
|
||||
|
||||
for arg in "${args[@]}"; do
|
||||
case $arg in
|
||||
-h|--help)
|
||||
print_usage "$0" "[options]"
|
||||
exit 0
|
||||
;;
|
||||
-v|--verbose)
|
||||
verbose=true
|
||||
;;
|
||||
-e|--env)
|
||||
show_env=true
|
||||
;;
|
||||
*)
|
||||
# Handle other arguments in child scripts
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ "$verbose" = true ]; then
|
||||
# Enable debug logging
|
||||
set -x
|
||||
fi
|
||||
|
||||
if [ "$show_env" = true ]; then
|
||||
print_env_vars "VITE_"
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
|
||||
# Export functions for use in child scripts
|
||||
export -f log_info log_success log_warn log_error log_debug log_step
|
||||
export -f measure_time print_header print_footer
|
||||
export -f check_command check_directory check_file
|
||||
export -f safe_execute check_venv get_git_hash
|
||||
export -f clean_build_artifacts validate_env_vars
|
||||
export -f setup_build_env setup_app_directories load_env_file print_env_vars
|
||||
export -f print_usage parse_args
|
||||
44
scripts/electron-dev.sh
Executable file
44
scripts/electron-dev.sh
Executable file
@@ -0,0 +1,44 @@
|
||||
#!/bin/bash
|
||||
# electron-dev.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Electron development script for TimeSafari application
|
||||
# This script builds the application and starts Electron for development.
|
||||
#
|
||||
# Exit Codes:
|
||||
# 1 - Build failed
|
||||
# 2 - Electron start failed
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Source common utilities
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args "$@"
|
||||
|
||||
# Print dev header
|
||||
print_header "TimeSafari Electron Development"
|
||||
log_info "Starting Electron development at $(date)"
|
||||
|
||||
# Setup environment for Electron development
|
||||
setup_build_env "electron"
|
||||
|
||||
# Setup application directories
|
||||
setup_app_directories
|
||||
|
||||
# Load environment from .env file if it exists
|
||||
load_env_file ".env"
|
||||
|
||||
# Step 1: Build the application
|
||||
safe_execute "Building application" "npm run build" || exit 1
|
||||
|
||||
# Step 2: Start Electron
|
||||
safe_execute "Starting Electron" "electron ." || exit 2
|
||||
|
||||
# Print dev summary
|
||||
log_success "Electron development session ended"
|
||||
print_footer "Electron Development"
|
||||
|
||||
# Exit with success
|
||||
exit 0
|
||||
44
scripts/test-all.sh
Executable file
44
scripts/test-all.sh
Executable file
@@ -0,0 +1,44 @@
|
||||
#!/bin/bash
|
||||
# test-all.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Comprehensive test suite for TimeSafari application
|
||||
# This script runs all tests including prerequisites, web tests, and mobile tests
|
||||
# with proper error handling and logging.
|
||||
#
|
||||
# Exit Codes:
|
||||
# 1 - Prerequisites check failed
|
||||
# 2 - Build failed
|
||||
# 3 - Web tests failed
|
||||
# 4 - Mobile tests failed
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Source common utilities
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args "$@"
|
||||
|
||||
# Print test header
|
||||
print_header "TimeSafari Test Suite"
|
||||
log_info "Starting comprehensive test suite at $(date)"
|
||||
|
||||
# Step 1: Check prerequisites
|
||||
safe_execute "Checking prerequisites" "npm run test:prerequisites" || exit 1
|
||||
|
||||
# Step 2: Build the application
|
||||
safe_execute "Building application" "npm run build" || exit 2
|
||||
|
||||
# Step 3: Run web tests
|
||||
safe_execute "Running web tests" "npm run test:web" || exit 3
|
||||
|
||||
# Step 4: Run mobile tests
|
||||
safe_execute "Running mobile tests" "npm run test:mobile" || exit 4
|
||||
|
||||
# Print test summary
|
||||
log_success "All tests completed successfully!"
|
||||
print_footer "Test Suite"
|
||||
|
||||
# Exit with success
|
||||
exit 0
|
||||
74
scripts/test-common.sh
Executable file
74
scripts/test-common.sh
Executable file
@@ -0,0 +1,74 @@
|
||||
#!/bin/bash
|
||||
# test-common.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Test script to verify common utilities work correctly
|
||||
# This script tests the common.sh utilities to ensure they function properly.
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Source common utilities
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args "$@"
|
||||
|
||||
# Print test header
|
||||
print_header "Common Utilities Test"
|
||||
log_info "Testing common utilities at $(date)"
|
||||
|
||||
# Test logging functions
|
||||
log_info "Testing info logging"
|
||||
log_success "Testing success logging"
|
||||
log_warn "Testing warning logging"
|
||||
log_error "Testing error logging (this is expected)"
|
||||
log_debug "Testing debug logging"
|
||||
log_step "Testing step logging"
|
||||
|
||||
# Test timing function
|
||||
log_info "Testing timing function..."
|
||||
measure_time sleep 1
|
||||
|
||||
# Test command checking
|
||||
log_info "Testing command checking..."
|
||||
if check_command "echo"; then
|
||||
log_success "echo command found"
|
||||
else
|
||||
log_error "echo command not found"
|
||||
fi
|
||||
|
||||
# Test directory checking
|
||||
log_info "Testing directory checking..."
|
||||
if check_directory "scripts"; then
|
||||
log_success "scripts directory found"
|
||||
else
|
||||
log_error "scripts directory not found"
|
||||
fi
|
||||
|
||||
# Test file checking
|
||||
log_info "Testing file checking..."
|
||||
if check_file "scripts/common.sh"; then
|
||||
log_success "common.sh file found"
|
||||
else
|
||||
log_error "common.sh file not found"
|
||||
fi
|
||||
|
||||
# Test git hash function
|
||||
log_info "Testing git hash function..."
|
||||
GIT_HASH=$(get_git_hash)
|
||||
log_info "Git hash: $GIT_HASH"
|
||||
|
||||
# Test safe execute
|
||||
log_info "Testing safe execute..."
|
||||
safe_execute "Testing safe execute" "echo 'Hello from safe_execute'"
|
||||
|
||||
# Test build artifact cleaning
|
||||
log_info "Testing build artifact cleaning..."
|
||||
clean_build_artifacts "test-file-1" "test-file-2"
|
||||
|
||||
# Print test summary
|
||||
log_success "All common utilities tests completed successfully!"
|
||||
print_footer "Common Utilities Test"
|
||||
|
||||
# Exit with success
|
||||
exit 0
|
||||
59
scripts/test-env.sh
Executable file
59
scripts/test-env.sh
Executable file
@@ -0,0 +1,59 @@
|
||||
#!/bin/bash
|
||||
# test-env.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Test script to verify environment variable handling
|
||||
# This script tests the environment variable setup functions.
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Source common utilities
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args "$@"
|
||||
|
||||
# Print test header
|
||||
print_header "Environment Variable Test"
|
||||
log_info "Testing environment variable handling at $(date)"
|
||||
|
||||
# Test 1: Electron environment
|
||||
log_info "Test 1: Setting up Electron environment..."
|
||||
setup_build_env "electron"
|
||||
print_env_vars "VITE_"
|
||||
|
||||
# Test 2: Capacitor environment
|
||||
log_info "Test 2: Setting up Capacitor environment..."
|
||||
setup_build_env "capacitor"
|
||||
print_env_vars "VITE_"
|
||||
|
||||
# Test 3: Web environment
|
||||
log_info "Test 3: Setting up Web environment..."
|
||||
setup_build_env "web"
|
||||
print_env_vars "VITE_"
|
||||
|
||||
# Test 4: Production Electron environment
|
||||
log_info "Test 4: Setting up Production Electron environment..."
|
||||
setup_build_env "electron" "true"
|
||||
print_env_vars "VITE_"
|
||||
print_env_vars "NODE_ENV"
|
||||
|
||||
# Test 5: Application directories
|
||||
log_info "Test 5: Setting up application directories..."
|
||||
setup_app_directories
|
||||
|
||||
# Test 6: Load .env file (if it exists)
|
||||
log_info "Test 6: Loading .env file..."
|
||||
load_env_file ".env"
|
||||
|
||||
# Test 7: Git hash
|
||||
log_info "Test 7: Getting git hash..."
|
||||
GIT_HASH=$(get_git_hash)
|
||||
log_info "Git hash: $GIT_HASH"
|
||||
|
||||
# Print test summary
|
||||
log_success "All environment variable tests completed successfully!"
|
||||
print_footer "Environment Variable Test"
|
||||
|
||||
# Exit with success
|
||||
exit 0
|
||||
40
scripts/test-mobile.sh
Executable file
40
scripts/test-mobile.sh
Executable file
@@ -0,0 +1,40 @@
|
||||
#!/bin/bash
|
||||
# test-mobile.sh
|
||||
# Author: Matthew Raymer
|
||||
# Description: Mobile test suite for TimeSafari application
|
||||
# This script builds the Capacitor version and runs Android and iOS tests
|
||||
# with proper error handling and logging.
|
||||
#
|
||||
# Exit Codes:
|
||||
# 1 - Capacitor build failed
|
||||
# 2 - Android tests failed
|
||||
# 3 - iOS tests failed
|
||||
|
||||
# Exit on any error
|
||||
set -e
|
||||
|
||||
# Source common utilities
|
||||
source "$(dirname "$0")/common.sh"
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args "$@"
|
||||
|
||||
# Print test header
|
||||
print_header "TimeSafari Mobile Test Suite"
|
||||
log_info "Starting mobile test suite at $(date)"
|
||||
|
||||
# Step 1: Build Capacitor version
|
||||
safe_execute "Building Capacitor version" "npm run build:capacitor" || exit 1
|
||||
|
||||
# Step 2: Run Android tests
|
||||
safe_execute "Running Android tests" "npm run test:android" || exit 2
|
||||
|
||||
# Step 3: Run iOS tests
|
||||
safe_execute "Running iOS tests" "npm run test:ios" || exit 3
|
||||
|
||||
# Print test summary
|
||||
log_success "Mobile test suite completed successfully!"
|
||||
print_footer "Mobile Test Suite"
|
||||
|
||||
# Exit with success
|
||||
exit 0
|
||||
Reference in New Issue
Block a user