dev/dev

81 lines
1.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# Simple dev tool launcher - builds if needed
set -euo pipefail
# Change to script's directory
cd "$(dirname "${BASH_SOURCE[0]}")"
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
log() { echo -e "${GREEN}[DEV]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
# Simple platform detection
ARCH=$(uname -m)
case $ARCH in
x86_64) ARCH="amd64" ;;
arm64 | aarch64) ARCH="arm64" ;;
*)
error "Unsupported arch: $ARCH"
exit 1
;;
esac
BINARY="bin/dev-linux-$ARCH"
# Build if binary doesn't exist or source is newer
if [[ ! -f "$BINARY" ]] || [[ "main.go" -nt "$BINARY" ]]; then
# Check Go version
if ! command -v go &>/dev/null; then
error "Go not installed. Install with: sudo pacman -S go"
exit 1
fi
GO_VERSION=$(go version | cut -d' ' -f3)
if [[ "$GO_VERSION" < "go1.18" ]]; then
error "Go version 1.18 or higher required. Current version: $GO_VERSION"
exit 1
fi
# Check for required dependencies
if ! command -v git &>/dev/null; then
error "Git not installed. Install with: sudo pacman -S git"
exit 1
fi
# Create build cache directory
mkdir -p .build-cache
log "Building dev tool..."
mkdir -p bin
# Initialize Go modules if needed
if [[ ! -f "go.mod" ]]; then
log "Initializing Go modules..."
go mod init dev
fi
go mod tidy
# Build with cache
if go build -ldflags="-s -w" -o "$BINARY" -gcflags="all=-trimpath=$PWD" -asmflags="all=-trimpath=$PWD"; then
log "✅ Built: $BINARY"
find bin -type f -name "dev-linux-*" -not -name "$(basename "$BINARY")" -delete
else
error "Build failed"
exit 1
fi
fi
if [[ ${1:-} == "clean" ]]; then
log "Cleaning up build artifacts..."
rm -rf .build-cache
rm -rf bin/*.old
exit 0
fi
# Run the binary with all arguments
exec "$BINARY" "$@"