|
| 1 | +# Get the directory of the script |
| 2 | +$EXTENSION_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 3 | + |
| 4 | +# Change to the script directory |
| 5 | +Set-Location -Path $EXTENSION_DIR |
| 6 | + |
| 7 | +# Create a safe version of EXTENSION_ID replacing dots with dashes |
| 8 | +$EXTENSION_ID_SAFE = $env:EXTENSION_ID -replace '\.', '-' |
| 9 | + |
| 10 | +# Define output directory |
| 11 | +$OUTPUT_DIR = if ($env:OUTPUT_DIR) { $env:OUTPUT_DIR } else { Join-Path $EXTENSION_DIR "bin" } |
| 12 | + |
| 13 | +# Create output directory if it doesn't exist |
| 14 | +if (-not (Test-Path -Path $OUTPUT_DIR)) { |
| 15 | + New-Item -ItemType Directory -Path $OUTPUT_DIR | Out-Null |
| 16 | +} |
| 17 | + |
| 18 | +# Get Git commit hash and build date |
| 19 | +$COMMIT = git rev-parse HEAD |
| 20 | +$BUILD_DATE = (Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ") |
| 21 | + |
| 22 | +# List of OS and architecture combinations |
| 23 | +if ($env:EXTENSION_PLATFORM) { |
| 24 | + $PLATFORMS = @($env:EXTENSION_PLATFORM) |
| 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 = "github.com/azure/azure-dev/cli/azd/extensions/$env:EXTENSION_ID/internal/cmd" |
| 37 | + |
| 38 | +# Check if the build type is specified |
| 39 | +if (-not $env:EXTENSION_LANGUAGE) { |
| 40 | + Write-Host "Error: BUILD_TYPE environment variable is required (go or dotnet)" |
| 41 | + exit 1 |
| 42 | +} |
| 43 | + |
| 44 | +# Loop through platforms and build |
| 45 | +foreach ($PLATFORM in $PLATFORMS) { |
| 46 | + $OS, $ARCH = $PLATFORM -split '/' |
| 47 | + |
| 48 | + $OUTPUT_NAME = Join-Path $OUTPUT_DIR "$EXTENSION_ID_SAFE-$OS-$ARCH" |
| 49 | + |
| 50 | + if ($OS -eq "windows") { |
| 51 | + $OUTPUT_NAME += ".exe" |
| 52 | + } |
| 53 | + |
| 54 | + Write-Host "Building for $OS/$ARCH..." |
| 55 | + |
| 56 | + # Delete the output file if it already exists |
| 57 | + if (Test-Path -Path $OUTPUT_NAME) { |
| 58 | + Remove-Item -Path $OUTPUT_NAME -Force |
| 59 | + } |
| 60 | + |
| 61 | + if ($env:EXTENSION_LANGUAGE -eq "dotnet") { |
| 62 | + # Set runtime identifier for .NET |
| 63 | + $RUNTIME = if ($OS -eq "windows") { "win-$ARCH" } elseif ($OS -eq "darwin") { "osx-$ARCH" } else { "linux-$ARCH" } |
| 64 | + $PROJECT_FILE = "$EXTENSION_ID_SAFE.csproj" |
| 65 | + |
| 66 | + # Run dotnet publish for single file executable |
| 67 | + dotnet publish ` |
| 68 | + -c Release ` |
| 69 | + -r $RUNTIME ` |
| 70 | + -o $OUTPUT_DIR ` |
| 71 | + /p:PublishTrimmed=true ` |
| 72 | + $PROJECT_FILE |
| 73 | + |
| 74 | + if ($LASTEXITCODE -ne 0) { |
| 75 | + Write-Host "An error occurred while building for $OS/$ARCH" |
| 76 | + exit 1 |
| 77 | + } |
| 78 | + |
| 79 | + $EXPECTED_OUTPUT_NAME = $EXTENSION_ID_SAFE |
| 80 | + if ($OS -eq "windows") { |
| 81 | + $EXPECTED_OUTPUT_NAME += ".exe" |
| 82 | + } |
| 83 | + |
| 84 | + Rename-Item -Path "$OUTPUT_DIR/$EXPECTED_OUTPUT_NAME" -NewName $OUTPUT_NAME |
| 85 | + } elseif ($env:EXTENSION_LANGUAGE -eq "javascript") { |
| 86 | + $ENTRY_FILE = "pkg-entry.js" |
| 87 | + $TARGET = "node16-$OS-x64" |
| 88 | + $EXPECTED_OUTPUT_NAME = "$EXTENSION_ID_SAFE-$OS-$ARCH" |
| 89 | + if ($OS -eq "windows") { |
| 90 | + $EXPECTED_OUTPUT_NAME += ".exe" |
| 91 | + } |
| 92 | + |
| 93 | + Write-Host "Installing dependencies..." |
| 94 | + npm install |
| 95 | + |
| 96 | + Write-Host "Building JavaScript extension for $OS/$ARCH..." |
| 97 | + pkg $ENTRY_FILE -o $OUTPUT_DIR/$EXPECTED_OUTPUT_NAME --targets $TARGET --config package.json |
| 98 | + |
| 99 | + if ($LASTEXITCODE -ne 0) { |
| 100 | + Write-Host "An error occurred while building for $OS/$ARCH" |
| 101 | + exit 1 |
| 102 | + } |
| 103 | + } elseif ($env:EXTENSION_LANGUAGE -eq "go") { |
| 104 | + # Set environment variables for Go build |
| 105 | + $env:GOOS = $OS |
| 106 | + $env:GOARCH = $ARCH |
| 107 | + |
| 108 | + go build ` |
| 109 | + -ldflags="-X '$APP_PATH.Version=$env:EXTENSION_VERSION' -X '$APP_PATH.Commit=$COMMIT' -X '$APP_PATH.BuildDate=$BUILD_DATE'" ` |
| 110 | + -o $OUTPUT_NAME |
| 111 | + |
| 112 | + if ($LASTEXITCODE -ne 0) { |
| 113 | + Write-Host "An error occurred while building for $OS/$ARCH" |
| 114 | + exit 1 |
| 115 | + } |
| 116 | + } else { |
| 117 | + Write-Host "Error: Unsupported BUILD_TYPE '$env:BUILD_TYPE'. Use 'go' or 'dotnet'." |
| 118 | + exit 1 |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +Write-Host "Build completed successfully!" |
| 123 | +Write-Host "Binaries are located in the $OUTPUT_DIR directory." |
0 commit comments