345 lines
8.2 KiB
Bash
Executable File
345 lines
8.2 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Database Render Application - Build Script
|
|
# This script provides comprehensive build functionality for the application
|
|
|
|
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
|
|
|
|
# Configuration
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
|
|
BINARY_NAME="database-render"
|
|
DIST_DIR="$PROJECT_DIR/dist"
|
|
BIN_DIR="$PROJECT_DIR/bin"
|
|
|
|
# Functions
|
|
log_info() {
|
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|
}
|
|
|
|
log_success() {
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
}
|
|
|
|
log_warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# Check if Go is installed
|
|
check_go() {
|
|
if ! command -v go >/dev/null 2>&1; then
|
|
log_error "Go is not installed. Please install Go first."
|
|
exit 1
|
|
fi
|
|
|
|
local go_version=$(go version | awk '{print $3}')
|
|
log_info "Go version: $go_version"
|
|
}
|
|
|
|
# Check if required tools are installed
|
|
check_tools() {
|
|
local tools=("go")
|
|
|
|
for tool in "${tools[@]}"; do
|
|
if command -v "$tool" >/dev/null 2>&1; then
|
|
log_success "$tool is available"
|
|
else
|
|
log_error "$tool is not installed"
|
|
exit 1
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Clean build artifacts
|
|
clean() {
|
|
log_info "Cleaning build artifacts..."
|
|
|
|
if [ -d "$BIN_DIR" ]; then
|
|
rm -rf "$BIN_DIR"
|
|
log_success "Removed $BIN_DIR"
|
|
fi
|
|
|
|
if [ -d "$DIST_DIR" ]; then
|
|
rm -rf "$DIST_DIR"
|
|
log_success "Removed $DIST_DIR"
|
|
fi
|
|
|
|
go clean
|
|
log_success "Clean completed"
|
|
}
|
|
|
|
# Install dependencies
|
|
install_deps() {
|
|
log_info "Installing dependencies..."
|
|
|
|
cd "$PROJECT_DIR"
|
|
go mod tidy
|
|
go mod download
|
|
|
|
log_success "Dependencies installed"
|
|
}
|
|
|
|
# Build for current platform
|
|
build_current() {
|
|
log_info "Building for current platform..."
|
|
|
|
mkdir -p "$BIN_DIR"
|
|
cd "$PROJECT_DIR"
|
|
|
|
local build_time=$(date -u '+%Y-%m-%d_%H:%M:%S')
|
|
local git_commit=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
|
|
|
|
go build \
|
|
-ldflags "-X main.Version=dev -X main.BuildTime=$build_time -X main.GitCommit=$git_commit" \
|
|
-o "$BIN_DIR/$BINARY_NAME" \
|
|
./cmd/server
|
|
|
|
log_success "Built for current platform: $BIN_DIR/$BINARY_NAME"
|
|
}
|
|
|
|
# Build for multiple platforms
|
|
build_all() {
|
|
log_info "Building for multiple platforms..."
|
|
|
|
mkdir -p "$DIST_DIR"
|
|
cd "$PROJECT_DIR"
|
|
|
|
local build_time=$(date -u '+%Y-%m-%d_%H:%M:%S')
|
|
local git_commit=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
|
|
local ldflags="-X main.Version=dev -X main.BuildTime=$build_time -X main.GitCommit=$git_commit"
|
|
|
|
# Build targets
|
|
local targets=(
|
|
"linux/amd64"
|
|
"linux/arm64"
|
|
"darwin/amd64"
|
|
"darwin/arm64"
|
|
"windows/amd64"
|
|
)
|
|
|
|
for target in "${targets[@]}"; do
|
|
local os=$(echo "$target" | cut -d'/' -f1)
|
|
local arch=$(echo "$target" | cut -d'/' -f2)
|
|
local ext=""
|
|
|
|
if [ "$os" = "windows" ]; then
|
|
ext=".exe"
|
|
fi
|
|
|
|
log_info "Building for $os/$arch..."
|
|
|
|
GOOS=$os GOARCH=$arch go build \
|
|
-ldflags "$ldflags" \
|
|
-o "$DIST_DIR/${BINARY_NAME}-${os}-${arch}${ext}" \
|
|
./cmd/server
|
|
|
|
log_success "Built: ${BINARY_NAME}-${os}-${arch}${ext}"
|
|
done
|
|
|
|
log_success "Multi-platform build completed in $DIST_DIR/"
|
|
}
|
|
|
|
# Run tests
|
|
run_tests() {
|
|
log_info "Running tests..."
|
|
|
|
cd "$PROJECT_DIR"
|
|
go test -v ./...
|
|
|
|
log_success "Tests completed"
|
|
}
|
|
|
|
# Run tests with coverage
|
|
run_tests_coverage() {
|
|
log_info "Running tests with coverage..."
|
|
|
|
cd "$PROJECT_DIR"
|
|
go test -v -coverprofile=coverage.out ./...
|
|
go tool cover -html=coverage.out -o coverage.html
|
|
|
|
log_success "Tests completed with coverage report: coverage.html"
|
|
}
|
|
|
|
# Format code
|
|
format_code() {
|
|
log_info "Formatting code..."
|
|
|
|
cd "$PROJECT_DIR"
|
|
go fmt ./...
|
|
|
|
log_success "Code formatted"
|
|
}
|
|
|
|
# Check code formatting
|
|
check_format() {
|
|
log_info "Checking code formatting..."
|
|
|
|
cd "$PROJECT_DIR"
|
|
local unformatted=$(gofmt -l .)
|
|
|
|
if [ -n "$unformatted" ]; then
|
|
log_error "Code is not properly formatted. Run 'go fmt ./...' to fix."
|
|
echo "$unformatted"
|
|
exit 1
|
|
else
|
|
log_success "Code formatting is correct"
|
|
fi
|
|
}
|
|
|
|
# Run linter
|
|
run_linter() {
|
|
if command -v golangci-lint >/dev/null 2>&1; then
|
|
log_info "Running linter..."
|
|
cd "$PROJECT_DIR"
|
|
golangci-lint run
|
|
log_success "Linter completed"
|
|
else
|
|
log_warning "golangci-lint not found. Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"
|
|
fi
|
|
}
|
|
|
|
# Security scan
|
|
security_scan() {
|
|
if command -v gosec >/dev/null 2>&1; then
|
|
log_info "Running security scan..."
|
|
cd "$PROJECT_DIR"
|
|
gosec ./...
|
|
log_success "Security scan completed"
|
|
else
|
|
log_warning "gosec not found. Install with: go install github.com/securecodewarrior/gosec/v2/cmd/gosec@latest"
|
|
fi
|
|
}
|
|
|
|
# Initialize project
|
|
init_project() {
|
|
log_info "Initializing project..."
|
|
|
|
# Create directories
|
|
mkdir -p "$PROJECT_DIR/config"
|
|
mkdir -p "$PROJECT_DIR/data"
|
|
mkdir -p "$PROJECT_DIR/logs"
|
|
|
|
# Copy example configuration if it doesn't exist
|
|
if [ ! -f "$PROJECT_DIR/config/config.yaml" ]; then
|
|
cp "$PROJECT_DIR/config/config.example.yaml" "$PROJECT_DIR/config/config.yaml"
|
|
log_success "Created config.yaml from example"
|
|
fi
|
|
|
|
# Copy schema if it doesn't exist
|
|
if [ ! -f "$PROJECT_DIR/config/schema.sql" ]; then
|
|
cp "$PROJECT_DIR/config/schema.example.sql" "$PROJECT_DIR/config/schema.sql"
|
|
log_success "Created schema.sql from example"
|
|
fi
|
|
|
|
install_deps
|
|
|
|
log_success "Project initialized successfully!"
|
|
log_info "Please edit config/config.yaml to configure your database connection"
|
|
log_info "Then run: $0 build-current"
|
|
}
|
|
|
|
# Show help
|
|
show_help() {
|
|
echo "Database Render Application - Build Script"
|
|
echo ""
|
|
echo "Usage: $0 [command]"
|
|
echo ""
|
|
echo "Commands:"
|
|
echo " init - Initialize project with sample configuration"
|
|
echo " deps - Install dependencies"
|
|
echo " clean - Clean build artifacts"
|
|
echo " build - Build for current platform"
|
|
echo " build-all - Build for multiple platforms"
|
|
echo " test - Run tests"
|
|
echo " test-cov - Run tests with coverage"
|
|
echo " fmt - Format code"
|
|
echo " fmt-check - Check code formatting"
|
|
echo " lint - Run linter"
|
|
echo " security - Run security scan"
|
|
echo " all - Run full build pipeline (clean, deps, test, build)"
|
|
echo " help - Show this help message"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " $0 init"
|
|
echo " $0 build"
|
|
echo " $0 test-cov"
|
|
}
|
|
|
|
# Main function
|
|
main() {
|
|
case "${1:-help}" in
|
|
"init")
|
|
check_go
|
|
init_project
|
|
;;
|
|
"deps")
|
|
check_go
|
|
install_deps
|
|
;;
|
|
"clean")
|
|
clean
|
|
;;
|
|
"build")
|
|
check_go
|
|
check_tools
|
|
install_deps
|
|
build_current
|
|
;;
|
|
"build-all")
|
|
check_go
|
|
check_tools
|
|
install_deps
|
|
build_all
|
|
;;
|
|
"test")
|
|
check_go
|
|
run_tests
|
|
;;
|
|
"test-cov")
|
|
check_go
|
|
run_tests_coverage
|
|
;;
|
|
"fmt")
|
|
check_go
|
|
format_code
|
|
;;
|
|
"fmt-check")
|
|
check_go
|
|
check_format
|
|
;;
|
|
"lint")
|
|
check_go
|
|
run_linter
|
|
;;
|
|
"security")
|
|
security_scan
|
|
;;
|
|
"all")
|
|
check_go
|
|
check_tools
|
|
clean
|
|
install_deps
|
|
check_format
|
|
run_tests
|
|
build_current
|
|
log_success "Full build pipeline completed!"
|
|
;;
|
|
"help"|*)
|
|
show_help
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# Run main function with all arguments
|
|
main "$@" |