dev 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env bash
  2. # Simple dev tool launcher - builds if needed
  3. set -euo pipefail
  4. # Change to script's directory
  5. cd "$(dirname "${BASH_SOURCE[0]}")"
  6. # Colors
  7. GREEN='\033[0;32m'
  8. RED='\033[0;31m'
  9. NC='\033[0m'
  10. log() { echo -e "${GREEN}[DEV]${NC} $*"; }
  11. error() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
  12. # Simple platform detection
  13. ARCH=$(uname -m)
  14. case $ARCH in
  15. x86_64) ARCH="amd64" ;;
  16. arm64 | aarch64) ARCH="arm64" ;;
  17. *)
  18. error "Unsupported arch: $ARCH"
  19. exit 1
  20. ;;
  21. esac
  22. BINARY="bin/dev-linux-$ARCH"
  23. # Build if binary doesn't exist or source is newer
  24. if [[ ! -f "$BINARY" ]] || [[ "main.go" -nt "$BINARY" ]]; then
  25. # Check Go version
  26. if ! command -v go &>/dev/null; then
  27. error "Go not installed. Install with: sudo pacman -S go"
  28. exit 1
  29. fi
  30. GO_VERSION=$(go version | cut -d' ' -f3)
  31. if [[ "$GO_VERSION" < "go1.18" ]]; then
  32. error "Go version 1.18 or higher required. Current version: $GO_VERSION"
  33. exit 1
  34. fi
  35. # Check for required dependencies
  36. if ! command -v git &>/dev/null; then
  37. error "Git not installed. Install with: sudo pacman -S git"
  38. exit 1
  39. fi
  40. # Create build cache directory
  41. mkdir -p .build-cache
  42. log "Building dev tool..."
  43. mkdir -p bin
  44. # Initialize Go modules if needed
  45. if [[ ! -f "go.mod" ]]; then
  46. log "Initializing Go modules..."
  47. go mod init dev
  48. fi
  49. go mod tidy
  50. # Build with cache
  51. if go build -ldflags="-s -w" -o "$BINARY" -gcflags="all=-trimpath=$PWD" -asmflags="all=-trimpath=$PWD"; then
  52. log "✅ Built: $BINARY"
  53. find bin -type f -name "dev-linux-*" -not -name "$(basename "$BINARY")" -delete
  54. else
  55. error "Build failed"
  56. exit 1
  57. fi
  58. fi
  59. if [[ ${1:-} == "clean" ]]; then
  60. log "Cleaning up build artifacts..."
  61. rm -rf .build-cache
  62. rm -rf bin/*.old
  63. exit 0
  64. fi
  65. # Run the binary with all arguments
  66. exec "$BINARY" "$@"