-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathBuildAndRun.ps1
More file actions
268 lines (234 loc) · 10.3 KB
/
Copy pathBuildAndRun.ps1
File metadata and controls
268 lines (234 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<#
.SYNOPSIS
Builds and optionally runs a WinUI 3 / .NET project.
.DESCRIPTION
One command to build and run: .\BuildAndRun.ps1 MyApp.csproj
- Checks Developer Mode is enabled (required for packaged WinUI apps)
- Auto-detects platform (x64/ARM64), defaults to Debug, auto-restores
- Builds with dotnet build by default; pass -UseMSBuild to build with Visual Studio's MSBuild instead
- After successful build, finds the output folder and runs with winapp run
- Pass -SkipRun to build without launching
- Pass -Symbols to add --symbols (optional Symbol Server fallback for non-WinUI native frames)
.EXAMPLE
.\BuildAndRun.ps1 MyApp.csproj # Build + run
.\BuildAndRun.ps1 MyApp.csproj -SkipRun # Build only
.\BuildAndRun.ps1 MyApp.csproj -UseMSBuild # Build with Visual Studio MSBuild instead of dotnet build
.\BuildAndRun.ps1 MyApp.csproj -Symbols # Build + run with --debug-output --symbols (optional Symbol Server fallback)
.\BuildAndRun.ps1 MyApp.csproj /p:Configuration=Release # Override config
#>
param(
[Parameter(Position = 0)]
[string]$Project,
[switch]$SkipRun,
[switch]$Detach,
[switch]$UseMSBuild,
[switch]$Symbols,
[Parameter(ValueFromRemainingArguments)]
[string[]]$ExtraArgs
)
$ErrorActionPreference = 'Stop'
# Accept --detach (CLI style) as an alias for -Detach (PS style)
if ($ExtraArgs -contains '--detach') {
$Detach = $true
$ExtraArgs = $ExtraArgs | Where-Object { $_ -ne '--detach' }
}
# Accept --use-msbuild (CLI style) as an alias for -UseMSBuild (PS style)
if ($ExtraArgs -contains '--use-msbuild') {
$UseMSBuild = $true
$ExtraArgs = $ExtraArgs | Where-Object { $_ -ne '--use-msbuild' }
}
# Accept --symbols (CLI style) as an alias for -Symbols (PS style)
if ($ExtraArgs -contains '--symbols') {
$Symbols = $true
$ExtraArgs = $ExtraArgs | Where-Object { $_ -ne '--symbols' }
}
# Extra args are MSBuild-style flags like /p:Platform=x64
$extraArgs = $ExtraArgs
# -- 0. Check Developer Mode --
$devMode = $false
try {
$regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
if (Test-Path $regPath) {
$val = Get-ItemProperty $regPath -Name AllowDevelopmentWithoutDevLicense -ErrorAction SilentlyContinue
if ($val.AllowDevelopmentWithoutDevLicense -eq 1) { $devMode = $true }
}
} catch {}
if (-not $devMode) {
Write-Host "ERROR: Developer Mode is not enabled." -ForegroundColor Red
Write-Host "WinUI 3 packaged apps require Developer Mode to deploy and run." -ForegroundColor Red
Write-Host "Enable it: Settings > System > For developers > Developer Mode" -ForegroundColor Yellow
exit 1
}
# -- 1. Find the .csproj if not specified --
if (-not $Project) {
$csprojFiles = Get-ChildItem -Path . -Filter "*.csproj" -Depth 0
if ($csprojFiles.Count -eq 1) {
$Project = $csprojFiles[0].Name
} elseif ($csprojFiles.Count -gt 1) {
Write-Error "Multiple .csproj files found. Specify which one: .\BuildAndRun.ps1 <name>.csproj"
exit 1
} else {
Write-Error "No .csproj file found in current directory."
exit 1
}
}
# -- 2. Auto-detect platform --
$detectedPlatform = if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") { "ARM64" } else { "x64" }
$detectedConfig = "Debug"
$hasPlatform = $extraArgs | Where-Object { $_ -match "^[/|-]p:Platform=" }
$hasConfig = $extraArgs | Where-Object { $_ -match "^[/|-]p:Configuration=" }
$hasRestore = $extraArgs | Where-Object { $_ -match "^[/|-]restore$|^[/|-]t:restore$|^--restore$" }
# Extract actual values if overridden
if ($hasPlatform -and $hasPlatform -match "Platform=(\w+)") { $detectedPlatform = $Matches[1] }
if ($hasConfig -and $hasConfig -match "Configuration=(\w+)") { $detectedConfig = $Matches[1] }
$autoArgs = @()
if (-not $hasPlatform) { $autoArgs += "/p:Platform=$detectedPlatform" }
if (-not $hasConfig) { $autoArgs += "/p:Configuration=$detectedConfig" }
if (-not $hasRestore) { $autoArgs += "/restore" }
# -- 3. Find build tool --
# dotnet build is the default. Current Windows App SDK releases (>= 2.1.3 on the
# 2.x line, >= 1.8 on the 1.x line) surface XAML compiler errors under dotnet
# build, so MSBuild is only used when explicitly requested via -UseMSBuild.
$msbuild = $null
if ($UseMSBuild) {
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
if (Test-Path $vswhere) {
$vsPath = & $vswhere -latest -requires Microsoft.Component.MSBuild -property installationPath 2>$null
if ($vsPath) {
$candidate = Join-Path $vsPath "MSBuild\Current\Bin\MSBuild.exe"
if (Test-Path $candidate) { $msbuild = $candidate }
}
}
if (-not $msbuild) {
Write-Host "--> -UseMSBuild requested but Visual Studio MSBuild was not found; using dotnet build." -ForegroundColor Yellow
}
}
# -- 4. Build --
$defaultArgs = @("/nologo")
$hasVerbosity = $extraArgs | Where-Object { $_ -match "^[/|-]v(erbosity)?:" }
if (-not $hasVerbosity) { $defaultArgs += "/v:m" }
# -- 4a. Inject Microsoft.WindowsAppSDK.Analyzers if available --
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
# Look for pre-built analyzer DLL in the skill folder first, then fall back to source tree
$analyzerDll = Join-Path $scriptDir "analyzer\Microsoft.WindowsAppSDK.Analyzers.dll"
$analyzerTargets = Join-Path $scriptDir "analyzer\Microsoft.WindowsAppSDK.Analyzers.targets"
if (-not (Test-Path $analyzerDll)) {
$analyzerDll = Join-Path $scriptDir "..\..\tools\winui-analyzer\Microsoft.WindowsAppSDK.Analyzers\bin\Release\netstandard2.0\Microsoft.WindowsAppSDK.Analyzers.dll"
$analyzerTargets = Join-Path $scriptDir "..\..\tools\winui-analyzer\Microsoft.WindowsAppSDK.Analyzers\Microsoft.WindowsAppSDK.Analyzers.targets"
}
$analyzerArgs = @()
$tempBuildProps = $null
if (Test-Path $analyzerDll) {
$analyzerDll = (Resolve-Path $analyzerDll).Path
$analyzerTargets = (Resolve-Path $analyzerTargets).Path
# Inject via temporary Directory.Build.props (works with both MSBuild and dotnet build)
$projectDir = Split-Path (Resolve-Path $Project) -Parent
if (-not $projectDir) { $projectDir = "." }
$tempBuildProps = Join-Path $projectDir "Directory.Build.props"
$existingProps = $null
if (Test-Path $tempBuildProps) {
$existingProps = Get-Content $tempBuildProps -Raw
}
# Only create if one doesn't already exist (don't overwrite user's file)
if (-not $existingProps) {
@"
<Project>
<ItemGroup>
<Analyzer Include="$analyzerDll" />
</ItemGroup>
<Import Project="$analyzerTargets" />
</Project>
"@ | Set-Content $tempBuildProps
Write-Host "--> Microsoft.WindowsAppSDK.Analyzers: enabled" -ForegroundColor DarkGray
} else {
$tempBuildProps = $null # Don't clean up a pre-existing file
Write-Host "--> Microsoft.WindowsAppSDK.Analyzers: skipped (existing Directory.Build.props)" -ForegroundColor DarkGray
}
}
Write-Host ""
try {
if ($msbuild) {
Write-Host "--> Building with MSBuild (Platform: $detectedPlatform, Config: $detectedConfig)" -ForegroundColor Cyan
Write-Host "--> MSBuild: $msbuild" -ForegroundColor DarkGray
$allArgs = $defaultArgs + $autoArgs + @($Project) + $extraArgs
& $msbuild $allArgs
$buildExit = $LASTEXITCODE
} else {
Write-Host "--> Building with dotnet build (Platform: $detectedPlatform, Config: $detectedConfig)" -ForegroundColor Cyan
$dotnetArgs = @($Project)
foreach ($a in ($autoArgs + $extraArgs)) {
if ($a -match "^[/|-]restore$|^[/|-]t:restore$") {
# dotnet build restores by default
} elseif ($a -match "^[/|-]p:(.+)$") {
$dotnetArgs += "-p:$($Matches[1])"
} elseif ($a -notmatch "\.(csproj|sln)$") {
$dotnetArgs += $a
}
}
& dotnet build @dotnetArgs
$buildExit = $LASTEXITCODE
}
}
finally {
# Always clean up the temp Directory.Build.props we created — even on
# Ctrl-C, throws, or unexpected exits. Otherwise the user's project
# gets a stray file pointing at our analyzer that subsequent vanilla
# `dotnet build` invocations will fail to resolve.
if ($tempBuildProps -and (Test-Path $tempBuildProps)) {
Remove-Item $tempBuildProps -Force -ErrorAction SilentlyContinue
}
}
if ($buildExit -ne 0) {
Write-Host ""
Write-Host "BUILD FAILED (exit code $buildExit)" -ForegroundColor Red
exit $buildExit
}
Write-Host ""
Write-Host "BUILD SUCCEEDED" -ForegroundColor Green
# -- 5. Run with winapp --
if ($SkipRun) {
Write-Host "--> Skipping run (-SkipRun)" -ForegroundColor DarkGray
exit 0
}
# Find the build output directory
$rid = $detectedPlatform.ToLower()
$projectDir = Split-Path (Resolve-Path $Project) -Parent
if (-not $projectDir) { $projectDir = "." }
# Search for the output folder pattern: bin\<Platform>\<Config>\<tfm>\win-<rid>\
$binDir = Join-Path $projectDir "bin\$detectedPlatform\$detectedConfig"
if (-not (Test-Path $binDir)) {
Write-Host "WARNING: Build output not found at $binDir -- skipping run" -ForegroundColor Yellow
exit 0
}
# Find the TFM folder (e.g., net10.0-windows10.0.26100.0)
$tfmDirs = Get-ChildItem $binDir -Directory | Where-Object { $_.Name -match "^net\d" }
if (-not $tfmDirs) {
Write-Host "WARNING: No TFM folder found in $binDir -- skipping run" -ForegroundColor Yellow
exit 0
}
$tfmDir = $tfmDirs | Sort-Object Name -Descending | Select-Object -First 1
$outputDir = Join-Path $tfmDir.FullName "win-$rid"
if (-not (Test-Path $outputDir)) {
# Try without RID subfolder
$outputDir = $tfmDir.FullName
}
# Check winapp is available
$winapp = Get-Command winapp -ErrorAction SilentlyContinue
if (-not $winapp) {
Write-Host "WARNING: winapp CLI not found in PATH -- skipping run" -ForegroundColor Yellow
Write-Host "Build output at: $outputDir"
exit 0
}
Write-Host ""
if ($Detach) {
Write-Host "--> Launching app in background..." -ForegroundColor Cyan
& winapp run $outputDir --detach --json
} else {
$runArgs = @($outputDir, '--debug-output')
if ($Symbols) { $runArgs += '--symbols' }
Write-Host "--> Launching app: winapp run $($runArgs -join ' ')" -ForegroundColor Cyan
Write-Host " The script will stay running while the app is open." -ForegroundColor DarkGray
Write-Host " Debug output and exceptions will appear below." -ForegroundColor DarkGray
Write-Host ""
& winapp run @runArgs
}