$ErrorActionPreference = "Stop" $root = Split-Path -Parent $MyInvocation.MyCommand.Path $srcDir = Join-Path $root "src" $installerDir = Join-Path $root "installer" $distDir = Join-Path $root "dist" New-Item -ItemType Directory -Force -Path $distDir | Out-Null # 1. Generate multi-resolution Windows .ico file (16, 32, 48, 256 px) if not already created $icoPath = Join-Path $srcDir "app.ico" if (-not (Test-Path $icoPath)) { Add-Type -AssemblyName System.Drawing function New-IconPngBytes([int]$size) { $bmp = New-Object System.Drawing.Bitmap($size, $size, [System.Drawing.Imaging.PixelFormat]::Format32bppArgb) $g = [System.Drawing.Graphics]::FromImage($bmp) $g.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias $g.Clear([System.Drawing.Color]::Transparent) $pad = [float]($size * 0.06) $rectSize = [float]($size - 2 * $pad) $radius = [float]($size * 0.22) $d = $radius * 2 $path = New-Object System.Drawing.Drawing2D.GraphicsPath $path.AddArc($pad, $pad, $d, $d, 180, 90) $path.AddArc($pad + $rectSize - $d, $pad, $d, $d, 270, 90) $path.AddArc($pad + $rectSize - $d, $pad + $rectSize - $d, $d, $d, 0, 90) $path.AddArc($pad, $pad + $rectSize - $d, $d, $d, 90, 90) $path.CloseFigure() $brush = New-Object System.Drawing.Drawing2D.LinearGradientBrush( (New-Object System.Drawing.PointF(0, 0)), (New-Object System.Drawing.PointF($size, $size)), [System.Drawing.Color]::FromArgb(255, 0, 114, 214), [System.Drawing.Color]::FromArgb(255, 0, 74, 152) ) $g.FillPath($brush, $path) $strokeW = [Math]::Max(1.6, [float]($size * 0.095)) $pen = New-Object System.Drawing.Pen([System.Drawing.Color]::White, $strokeW) $pen.StartCap = [System.Drawing.Drawing2D.LineCap]::Round $pen.EndCap = [System.Drawing.Drawing2D.LineCap]::Round $pen.LineJoin = [System.Drawing.Drawing2D.LineJoin]::Round $cx = [float]($size * 0.5) $topY = [float]($size * 0.24) $tipY = [float]($size * 0.62) $wingDx = [float]($size * 0.19) $wingY = [float]($size * 0.44) $trayY = [float]($size * 0.76) $trayHalf = [float]($size * 0.24) $g.DrawLine($pen, $cx, $topY, $cx, $tipY) $g.DrawLine($pen, ($cx - $wingDx), $wingY, $cx, $tipY) $g.DrawLine($pen, ($cx + $wingDx), $wingY, $cx, $tipY) $g.DrawLine($pen, ($cx - $trayHalf), $trayY, ($cx + $trayHalf), $trayY) $ms = New-Object System.IO.MemoryStream $bmp.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png) $pen.Dispose(); $brush.Dispose(); $path.Dispose(); $g.Dispose(); $bmp.Dispose() return ,$ms.ToArray() } $sizes = @(16, 32, 48, 256) $pngBlobs = @() foreach ($s in $sizes) { $pngBlobs += ,(New-IconPngBytes $s) } $fs = [System.IO.File]::Open($icoPath, [System.IO.FileMode]::Create) $bw = New-Object System.IO.BinaryWriter($fs) $bw.Write([uint16]0); $bw.Write([uint16]1); $bw.Write([uint16]$sizes.Count) $offset = 6 + (16 * $sizes.Count) for ($i = 0; $i -lt $sizes.Count; $i++) { $s = $sizes[$i]; $blob = $pngBlobs[$i] $dimByte = if ($s -ge 256) { [byte]0 } else { [byte]$s } $bw.Write($dimByte); $bw.Write($dimByte); $bw.Write([byte]0); $bw.Write([byte]0) $bw.Write([uint16]1); $bw.Write([uint16]32) $bw.Write([uint32]$blob.Length); $bw.Write([uint32]$offset) $offset += $blob.Length } for ($i = 0; $i -lt $sizes.Count; $i++) { $bw.Write($pngBlobs[$i]) } $bw.Flush(); $bw.Close(); $fs.Close() } # 2. Locate .NET SDK (system or portable scratch SDK) $dotnetExe = $null if (Get-Command dotnet -ErrorAction SilentlyContinue) { $sdks = & dotnet --list-sdks 2>$null if ($sdks) { $dotnetExe = (Get-Command dotnet).Source } } if (-not $dotnetExe) { $portableDotnet = Join-Path (Split-Path -Parent $root) ".dotnet\dotnet.exe" if (Test-Path $portableDotnet) { $dotnetExe = $portableDotnet } } if (-not $dotnetExe) { throw "No .NET SDK found. Install the .NET 9+ SDK and Visual Studio C++ Build Tools." } $env:DOTNET_CLI_TELEMETRY_OPTOUT = "1" $env:DOTNET_NOLOGO = "1" # 3. Publish Portable NativeAOT single-file executable (skip overwriting if currently running) $portableExe = Join-Path $distDir "M365IsoDownloader.exe" $isRunning = @(Get-Process -Name "M365IsoDownloader" -ErrorAction SilentlyContinue).Count -gt 0 if (-not $isRunning -or -not (Test-Path $portableExe)) { $projFile = Join-Path $root "M365IsoDownloader.csproj" & $dotnetExe publish $projFile -c Release -r win-x64 --nologo -o $distDir if ($LASTEXITCODE -ne 0) { throw "NativeAOT app publish failed with exit code $LASTEXITCODE" } } else { Write-Host "M365IsoDownloader.exe is currently running; preserving existing binary in dist\." } # 4. Compress M365IsoDownloader.exe into installer\payload.gz and publish NativeAOT Setup Installer $setupProj = Join-Path $installerDir "M365IsoDownloader.Setup.csproj" if (Test-Path $setupProj) { $payloadGz = Join-Path $installerDir "payload.gz" $inFs = New-Object System.IO.FileStream($portableExe, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite) $outFs = [System.IO.File]::Create($payloadGz) $gz = New-Object System.IO.Compression.GZipStream($outFs, [System.IO.Compression.CompressionLevel]::Optimal) $inFs.CopyTo($gz) $gz.Dispose() $outFs.Dispose() $inFs.Dispose() $setupOutDir = Join-Path $installerDir "bin\publish" & $dotnetExe publish $setupProj -c Release -r win-x64 --nologo -o $setupOutDir if ($LASTEXITCODE -ne 0) { throw "NativeAOT setup installer publish failed with exit code $LASTEXITCODE" } Copy-Item (Join-Path $setupOutDir "M365IsoDownloader-Setup.exe") (Join-Path $distDir "M365IsoDownloader-Setup.exe") -Force Remove-Item $payloadGz -Force -ErrorAction SilentlyContinue } # Clean up any non-exe artifacts in dist Get-ChildItem $distDir -File | Where-Object { $_.Extension -ne ".exe" } | Remove-Item -Force Write-Host "`nBuilt .NET NativeAOT deliverables in dist\:" Get-ChildItem $distDir -Filter "*.exe" | ForEach-Object { Write-Host (" {0,-32} {1,6:N2} MB ({2})" -f $_.Name, ($_.Length / 1MB), $_.FullName) }