Files
PSK-Proxy-Tunnel/build.sh
2025-08-31 21:40:17 +08:00

182 lines
4.5 KiB
Bash
Executable File

#!/bin/bash
# PSK-Proxy-Tunnel Build Script
# This script builds single executable binaries for different platforms
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
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to detect current platform
detect_platform() {
case "$(uname -s)" in
Darwin*) echo "macos" ;;
Linux*) echo "linux" ;;
CYGWIN*|MINGW*|MSYS*) echo "windows" ;;
*) echo "unknown" ;;
esac
}
# Function to build for specific platform
build_for_platform() {
local platform=$1
print_status "Building for $platform..."
# Ensure dist initialized and clean for single-platform builds
npm run prebuild
case $platform in
"macos")
npm run build:macos
;;
"linux")
npm run build:linux
;;
"windows")
npm run build:windows
;;
*)
print_error "Unknown platform: $platform"
exit 1
;;
esac
print_success "Build completed for $platform"
}
# Function to build for all platforms
build_all() {
print_status "Building for all platforms..."
npm run build
print_success "Build completed for all platforms"
}
# Function to show help
show_help() {
echo "PSK-Proxy-Tunnel Build Script"
echo ""
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -p, --platform PLATFORM Build for specific platform (macos|linux|windows)"
echo " -a, --all Build for all platforms"
echo " -c, --clean Clean build artifacts"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 --platform macos Build for macOS only"
echo " $0 --all Build for all platforms"
echo " $0 --clean Clean build artifacts"
echo ""
echo "Current platform: $(detect_platform)"
}
# Function to clean build artifacts
clean_build() {
print_status "Cleaning build artifacts..."
npm run clean
print_success "Build artifacts cleaned"
}
# Main script logic
main() {
# Check if we're in the right directory
if [ ! -f "package.json" ]; then
print_error "package.json not found. Please run this script from the project root directory."
exit 1
fi
# Check if Node.js is installed
if ! command_exists node; then
print_error "Node.js is not installed. Please install Node.js first."
exit 1
fi
# Check if npm is installed
if ! command_exists npm; then
print_error "npm is not installed. Please install npm first."
exit 1
fi
# Install dependencies if node_modules doesn't exist
if [ ! -d "node_modules" ]; then
print_status "Installing dependencies..."
npm install
print_success "Dependencies installed"
fi
# Parse command line arguments
if [ $# -eq 0 ]; then
# No arguments provided, build for current platform
local current_platform=$(detect_platform)
if [ "$current_platform" = "unknown" ]; then
print_warning "Could not detect platform, building for all platforms"
build_all
else
print_status "Building for detected platform: $current_platform"
build_for_platform "$current_platform"
fi
return
fi
while [[ $# -gt 0 ]]; do
case $1 in
-p|--platform)
if [ -z "$2" ]; then
print_error "Platform not specified"
exit 1
fi
build_for_platform "$2"
shift 2
;;
-a|--all)
build_all
shift
;;
-c|--clean)
clean_build
shift
;;
-h|--help)
show_help
exit 0
;;
*)
print_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
}
# Run main function with all arguments
main "$@"