|
| 1 | +<#! |
| 2 | +.SYNOPSIS |
| 3 | + Builds WindowsAppSDK Samples solutions (or local solution(s)). |
| 4 | +.DESCRIPTION |
| 5 | + PowerShell port of the original build.cmd logic. |
| 6 | + * Ensures Visual Studio Developer environment (DevShell) is initialized. |
| 7 | + * Downloads a local nuget.exe if not present and restores packages. |
| 8 | + * Builds one or more .sln files with MSBuild producing a .binlog per solution. |
| 9 | +
|
| 10 | + Solution discovery order: |
| 11 | + 1. If -Sample is specified: build all .sln files under Samples/<Sample>. |
| 12 | + 2. Else if the current working directory (where you invoked the script) contains one or more .sln files: build those only (no recursion). |
| 13 | + 3. Else build all .sln files under Samples (recursive). |
| 14 | +
|
| 15 | +.PARAMETER Platform |
| 16 | + Target platform (x86, x64, arm64, auto). Default: auto (arm64 on ARM64 OS, else x64). |
| 17 | +.PARAMETER Configuration |
| 18 | + Build configuration (Debug or Release). Default: Release. |
| 19 | +.PARAMETER Sample |
| 20 | + Optional sample folder name (child of Samples) to restrict the build scope. |
| 21 | +.PARAMETER Help |
| 22 | + Show usage information. |
| 23 | +
|
| 24 | +.EXAMPLE |
| 25 | + ./build.ps1 |
| 26 | + (Auto-detect platform, build local solutions or all samples.) |
| 27 | +
|
| 28 | +.EXAMPLE |
| 29 | + ./build.ps1 -Platform arm64 -Configuration Debug -Sample AppLifecycle |
| 30 | + (Build only the AppLifecycle sample solutions for arm64 Debug.) |
| 31 | +#> |
| 32 | +#[CmdletBinding()] parameters |
| 33 | +[CmdletBinding()] param( |
| 34 | + [Parameter(Position=0)] [ValidateSet('x86','x64','arm64','auto')] [string]$Platform = 'auto', |
| 35 | + [Parameter(Position=1)] [ValidateSet('Debug','Release')] [string]$Configuration = 'Release', |
| 36 | + [Parameter(Position=2)] [string]$Sample = '', |
| 37 | + [switch]$Help |
| 38 | +) |
| 39 | + |
| 40 | +# --- Functions (grouped at top for clarity) --- |
| 41 | + |
| 42 | +# Show-Usage: Display help/usage information. |
| 43 | +function Show-Usage { |
| 44 | + Write-Host 'Usage:' |
| 45 | + Write-Host ' build.ps1 [-Platform x86|x64|arm64|auto] [-Configuration Debug|Release] [-Sample <SampleFolder>]' |
| 46 | + Write-Host '' |
| 47 | + Write-Host 'Platform:' |
| 48 | + Write-Host ' auto (default) Detects host OS arch: arm64 on ARM64, else x64.' |
| 49 | + Write-Host '' |
| 50 | + Write-Host 'Solution selection:' |
| 51 | + Write-Host ' -Sample <Name> Build solutions under Samples/<Name>' |
| 52 | + Write-Host ' (no Sample) If current directory has .sln file(s), build only those; otherwise build all Samples solutions.' |
| 53 | + Write-Host '' |
| 54 | + Write-Host 'Examples:' |
| 55 | + Write-Host ' ./build.ps1' |
| 56 | + Write-Host ' ./build.ps1 -Platform arm64 -Configuration Debug' |
| 57 | + Write-Host ' ./build.ps1 -Sample AppLifecycle' |
| 58 | +} |
| 59 | + |
| 60 | +# Initialize-VSEnvironment: Ensure VS dev environment & MSBuild are available. |
| 61 | +function Initialize-VSEnvironment { |
| 62 | + if (Get-Command msbuild -ErrorAction SilentlyContinue) { Write-Host 'MSBuild already on PATH; assuming VS env initialized.'; return } |
| 63 | + if ($env:VSINSTALLDIR) { Write-Host "VS environment already initialized: $env:VSINSTALLDIR"; return } |
| 64 | + $vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe' |
| 65 | + if (-not (Test-Path $vswhere)) { Write-Error 'vswhere.exe not found. Install Visual Studio 2022 with MSBuild.'; exit 1 } |
| 66 | + $installationPath = & $vswhere -latest -prerelease -products * -requires Microsoft.Component.MSBuild -property installationPath | Select-Object -First 1 |
| 67 | + if (-not $installationPath) { Write-Error 'Unable to locate a suitable Visual Studio installation.'; exit 1 } |
| 68 | + $devShellModule = Join-Path $installationPath 'Common7\Tools\Microsoft.VisualStudio.DevShell.dll' |
| 69 | + if (Test-Path $devShellModule) { |
| 70 | + Write-Host "Initializing VS DevShell from: $devShellModule" -ForegroundColor Yellow |
| 71 | + try { Import-Module $devShellModule -ErrorAction Stop; Enter-VsDevShell -VsInstallPath $installationPath -SkipAutomaticLocation | Out-Null } |
| 72 | + catch { Write-Warning "DevShell initialization failed: $($_.Exception.Message)" } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +# Resolve-Platform: Turn 'auto' into a concrete platform value. |
| 77 | +function Resolve-Platform { param([string]$PlatformValue) |
| 78 | + if ($PlatformValue -ne 'auto') { return $PlatformValue } |
| 79 | + $detected = 'x64' |
| 80 | + try { |
| 81 | + $osArch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLowerInvariant() |
| 82 | + switch ($osArch) { 'arm64' { $detected = 'arm64' } 'x86' { $detected = 'x86' } default { $detected = 'x64' } } |
| 83 | + if ($detected -eq 'x86' -and $env:PROCESSOR_ARCHITEW6432) { $detected = 'x64' } |
| 84 | + } catch {} |
| 85 | + Write-Host "Auto-detected platform: $detected" -ForegroundColor Yellow |
| 86 | + return $detected |
| 87 | +} |
| 88 | + |
| 89 | +# Initialize-NuGetEnvironment: Create folders/download nuget.exe if needed. |
| 90 | +function Initialize-NuGetEnvironment { |
| 91 | + if (-not (Test-Path $nugetDir)) { New-Item -ItemType Directory -Path $nugetDir | Out-Null } |
| 92 | + if (-not (Test-Path $nugetExe)) { Write-Host 'Downloading nuget.exe...'; Invoke-WebRequest -UseBasicParsing https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile $nugetExe } |
| 93 | + if (-not (Test-Path $packagesDir)) { New-Item -ItemType Directory -Path $packagesDir | Out-Null } |
| 94 | +} |
| 95 | + |
| 96 | +# Get-Solutions: Discover which solutions to build. |
| 97 | +function Get-Solutions { param([string]$SampleFilter) |
| 98 | + $targetRoot = $null; $solutions = @() |
| 99 | + if (-not [string]::IsNullOrWhiteSpace($SampleFilter)) { |
| 100 | + $targetRoot = Join-Path $samplesRoot $SampleFilter |
| 101 | + if (-not (Test-Path $targetRoot)) { Write-Error "Sample path not found: $targetRoot"; exit 1 } |
| 102 | + $solutions = Get-ChildItem -Path $targetRoot -Filter *.sln -Recurse | Sort-Object FullName |
| 103 | + } else { |
| 104 | + $currentDir = Get-Location |
| 105 | + $localSolutions = Get-ChildItem -Path $currentDir -Filter *.sln -File -ErrorAction SilentlyContinue | Sort-Object FullName |
| 106 | + if ($localSolutions) { Write-Host "Detected $($localSolutions.Count) solution(s) in current directory: $currentDir" -ForegroundColor Yellow; $solutions = $localSolutions } |
| 107 | + else { $targetRoot = $samplesRoot; $solutions = Get-ChildItem -Path $targetRoot -Filter *.sln -Recurse | Sort-Object FullName } |
| 108 | + } |
| 109 | + return $solutions |
| 110 | +} |
| 111 | + |
| 112 | + # Restore-Solution: Perform NuGet restore for a solution (.sln) using repo nuget.exe. |
| 113 | + function Restore-Solution { param([string]$SolutionPath) |
| 114 | + Write-Host "Restoring: $SolutionPath" -ForegroundColor Cyan |
| 115 | + & $nugetExe restore $SolutionPath -ConfigFile (Join-Path $samplesRoot 'nuget.config') -PackagesDirectory $packagesDir |
| 116 | + if ($LASTEXITCODE -ne 0) { Write-Error 'NuGet restore failed.'; exit $LASTEXITCODE } |
| 117 | + } |
| 118 | + |
| 119 | + # Build-Solution: Invoke MSBuild on a solution with platform/config and emit binlog. |
| 120 | + function Build-Solution { param([string]$SolutionPath,[string]$Platform,[string]$Configuration) |
| 121 | + $binlog = Join-Path (Split-Path $SolutionPath -Parent) ("{0}.binlog" -f ([IO.Path]::GetFileNameWithoutExtension($SolutionPath))) |
| 122 | + Write-Host "Building: $SolutionPath" -ForegroundColor Cyan |
| 123 | + & msbuild /warnaserror /p:Platform=$Platform /p:Configuration=$Configuration /p:NugetPackageDirectory=$packagesDir /bl:"$binlog" "$SolutionPath" |
| 124 | + if ($LASTEXITCODE -ne 0) { Write-Error 'MSBuild failed.'; exit $LASTEXITCODE } |
| 125 | + } |
| 126 | + |
| 127 | +# --- Main Execution Flow --- |
| 128 | +if ($Help -or $PSBoundParameters.ContainsKey('?')) { Show-Usage; return } |
| 129 | +Initialize-VSEnvironment |
| 130 | + |
| 131 | +if (-not $env:VSINSTALLDIR) { Write-Warning 'VSINSTALLDIR environment variable not found. Run this from a VS Developer PowerShell prompt.'; Show-Usage; exit 1 } |
| 132 | + |
| 133 | +$Platform = Resolve-Platform -PlatformValue $Platform |
| 134 | +$repoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 135 | +$nugetDir = Join-Path $repoRoot '.nuget' |
| 136 | +$nugetExe = Join-Path $nugetDir 'nuget.exe' |
| 137 | +$packagesDir = Join-Path $repoRoot 'packages' |
| 138 | +$samplesRoot = Join-Path $repoRoot 'Samples' |
| 139 | + |
| 140 | +Initialize-NuGetEnvironment |
| 141 | +$solutions = Get-Solutions -SampleFilter $Sample |
| 142 | + |
| 143 | +if (-not $solutions -or $solutions.Count -eq 0) { |
| 144 | + Write-Warning 'No solution files found to build.' |
| 145 | + exit 0 |
| 146 | +} |
| 147 | + |
| 148 | +$stopwatch = [System.Diagnostics.Stopwatch]::StartNew() |
| 149 | +$startTime = Get-Date |
| 150 | + |
| 151 | +Write-Host "Building $($solutions.Count) solution(s) for Platform=$Platform Configuration=$Configuration" -ForegroundColor Green |
| 152 | +foreach ($sln in $solutions) { |
| 153 | + Write-Host '---' -ForegroundColor Cyan |
| 154 | + Restore-Solution -SolutionPath $sln.FullName |
| 155 | + Build-Solution -SolutionPath $sln.FullName -Platform $Platform -Configuration $Configuration |
| 156 | +} |
| 157 | + |
| 158 | +$stopwatch.Stop() |
| 159 | +$endTime = Get-Date |
| 160 | + |
| 161 | +Write-Host '---' |
| 162 | +Write-Host ("Start time: {0}. End time: {1}" -f $startTime.ToLongTimeString(), $endTime.ToLongTimeString()) |
| 163 | +Write-Host (" Elapsed: {0}" -f [System.String]::Format('{0:hh\\:mm\\:ss\.ff}', $stopwatch.Elapsed)) |
0 commit comments