|
| 1 | +# Ensure script fails on any error |
| 2 | +$ErrorActionPreference = 'Stop' |
| 3 | + |
| 4 | +# Get the directory of the script |
| 5 | +$EXTENSION_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 6 | + |
| 7 | +# Change to the script directory |
| 8 | +Set-Location -Path $EXTENSION_DIR |
| 9 | + |
| 10 | +# Create a safe version of EXTENSION_ID replacing dots with dashes |
| 11 | +$EXTENSION_ID_SAFE = $env:EXTENSION_ID -replace '\.', '-' |
| 12 | + |
| 13 | +# Define output directory |
| 14 | +$OUTPUT_DIR = if ($env:OUTPUT_DIR) { $env:OUTPUT_DIR } else { Join-Path $EXTENSION_DIR "bin" } |
| 15 | + |
| 16 | +# Create output directory if it doesn't exist |
| 17 | +if (-not (Test-Path -Path $OUTPUT_DIR)) { |
| 18 | + New-Item -ItemType Directory -Path $OUTPUT_DIR | Out-Null |
| 19 | +} |
| 20 | + |
| 21 | +# List of OS and architecture combinations |
| 22 | +if ($env:EXTENSION_PLATFORM) { |
| 23 | + $PLATFORMS = @($env:EXTENSION_PLATFORM) |
| 24 | +} |
| 25 | +else { |
| 26 | + $PLATFORMS = @( |
| 27 | + "windows/amd64", |
| 28 | + "windows/arm64", |
| 29 | + "darwin/amd64", |
| 30 | + "darwin/arm64", |
| 31 | + "linux/amd64", |
| 32 | + "linux/arm64" |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +$APP_PATH = "$env:EXTENSION_ID/internal/cmd" |
| 37 | + |
| 38 | +# Loop through platforms and build |
| 39 | +foreach ($PLATFORM in $PLATFORMS) { |
| 40 | + $OS, $ARCH = $PLATFORM -split '/' |
| 41 | + |
| 42 | + $OUTPUT_NAME = Join-Path $OUTPUT_DIR "$EXTENSION_ID_SAFE-$OS-$ARCH" |
| 43 | + |
| 44 | + if ($OS -eq "windows") { |
| 45 | + $OUTPUT_NAME += ".exe" |
| 46 | + } |
| 47 | + |
| 48 | + Write-Host "Building for $OS/$ARCH..." |
| 49 | + |
| 50 | + # Delete the output file if it already exists |
| 51 | + if (Test-Path -Path $OUTPUT_NAME) { |
| 52 | + Remove-Item -Path $OUTPUT_NAME -Force |
| 53 | + } |
| 54 | + |
| 55 | + # Set environment variables for Go build |
| 56 | + $env:GOOS = $OS |
| 57 | + $env:GOARCH = $ARCH |
| 58 | + |
| 59 | + go build ` |
| 60 | + -trimpath ` |
| 61 | + -buildmode=pie ` |
| 62 | + -tags=cfi,cfg,osusergo ` |
| 63 | + -ldflags="-s -w -X '$APP_PATH.Version=$env:EXTENSION_VERSION'" ` |
| 64 | + -o $OUTPUT_NAME |
| 65 | + |
| 66 | + if ($LASTEXITCODE -ne 0) { |
| 67 | + Write-Host "An error occurred while building for $OS/$ARCH" |
| 68 | + exit 1 |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +Write-Host "Build completed successfully!" |
| 73 | +Write-Host "Binaries are located in the $OUTPUT_DIR directory." |
0 commit comments