# AOT Build and Test Script for Windows PowerShell # This script builds the Htmx.ApiDemo application with AOT compilation enabled # and runs the application to check for trimming issues param( [switch]$RunOnly, [switch]$BuildOnly ) # Set error handling $ErrorActionPreference = "Stop" # Define paths $ProjectRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) $ApiDemoProject = Join-Path $ProjectRoot "Htmx.ApiDemo" $PublishPath = Join-Path $PSScriptRoot "Publish" Write-Host "========================================" -ForegroundColor Cyan Write-Host "AOT Build and Test Script" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" if (-not $RunOnly) { Write-Host "Starting AOT Build Process..." -ForegroundColor Yellow Write-Host "Project Path: $ApiDemoProject" -ForegroundColor Gray Write-Host "Publish Path: $PublishPath" -ForegroundColor Gray Write-Host "" # Kill any running instance of the app before cleaning (it locks the publish folder) $AppName = "Htmx.ApiDemo" $Running = Get-Process -Name $AppName -ErrorAction SilentlyContinue if ($Running) { Write-Host "Stopping running instance of $AppName..." -ForegroundColor Yellow $Running | Stop-Process -Force Start-Sleep -Milliseconds 500 } # Clean previous publish folder if it exists if (Test-Path $PublishPath) { Write-Host "Cleaning previous publish folder..." -ForegroundColor Yellow Remove-Item -Path $PublishPath -Recurse -Force } # Create publish folder New-Item -Path $PublishPath -ItemType Directory -Force | Out-Null try { # Run dotnet publish with AOT enabled # PublishAot=true is already set in the .csproj # A Runtime Identifier (RID) is required for AOT compilation Write-Host "Publishing with AOT compilation enabled (win-x64)..." -ForegroundColor Yellow Push-Location $ApiDemoProject dotnet publish -c Release -r win-x64 -o $PublishPath ` -p:DebugSymbols=false ` -p:DebugType=none if ($LASTEXITCODE -ne 0) { Write-Host "Build failed!" -ForegroundColor Red Pop-Location exit 1 } Pop-Location Write-Host "AOT build completed successfully!" -ForegroundColor Green Write-Host "" # Copy appsettings.Development.json over appsettings.json in the publish folder # so the AOT executable has the correct connection strings for local testing $DevSettings = Join-Path $ApiDemoProject "appsettings.Development.json" $PublishedSettings = Join-Path $PublishPath "appsettings.json" if (Test-Path $DevSettings) { Write-Host "Copying appsettings.Development.json -> appsettings.json in publish folder..." -ForegroundColor Yellow Copy-Item -Path $DevSettings -Destination $PublishedSettings -Force Write-Host "Done." -ForegroundColor Green Write-Host "" } else { Write-Host "Warning: appsettings.Development.json not found, skipping copy." -ForegroundColor DarkYellow } } catch { Write-Host "Error during build: $_" -ForegroundColor Red Pop-Location exit 1 } } if (-not $BuildOnly) { Write-Host "Starting Application..." -ForegroundColor Yellow Write-Host "" # Find the executable $Executable = Get-ChildItem -Path $PublishPath -Filter "*.exe" -Recurse | Select-Object -First 1 if (-not $Executable) { Write-Host "Executable not found in publish directory!" -ForegroundColor Red exit 1 } Write-Host "Running: $($Executable.FullName)" -ForegroundColor Yellow Write-Host "Press Ctrl+C to stop the application" -ForegroundColor Gray Write-Host "" try { # Use Start-Process with WorkingDirectory so appsettings.json is found, # and the shell CWD is never changed (safe against Ctrl+C) $proc = Start-Process -FilePath $Executable.FullName ` -WorkingDirectory $PublishPath ` -NoNewWindow ` -PassThru $proc.WaitForExit() } catch { Write-Host "Error running application: $_" -ForegroundColor Red exit 1 } } Write-Host "" Write-Host "========================================" -ForegroundColor Cyan Write-Host "AOT Test Complete" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan