# Xianyan MSIX Packaging Script # Auto-syncs version from pubspec.yaml to msix_version # # Usage: # .\scripts\package_msix.ps1 # Store submission # .\scripts\package_msix.ps1 -LocalTest # Local test (sign + install) param( [switch]$LocalTest = $false ) $ErrorActionPreference = "Stop" $ProjectRoot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) $PubspecPath = Join-Path $ProjectRoot "pubspec.yaml" Write-Host "========================================" -ForegroundColor Cyan Write-Host " Xianyan MSIX Packaging Tool" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan # 1. Read version from pubspec.yaml $pubspecContent = Get-Content $PubspecPath -Raw -Encoding UTF8 if ($pubspecContent -match 'version:\s*(\d+)\.(\d+)\.(\d+)\+(\d+)') { $major = $Matches[1] $minor = $Matches[2] $patch = $Matches[3] $build = $Matches[4] $flutterVersion = "${major}.${minor}.${patch}+${build}" $msixVersion = "${major}.${minor}.${patch}.0" Write-Host "[1/5] Version: $flutterVersion -> MSIX: $msixVersion" -ForegroundColor Green } else { Write-Host "[1/5] ERROR: Cannot read version from pubspec.yaml" -ForegroundColor Red exit 1 } # 2. Sync msix_version in pubspec.yaml $updatedContent = $pubspecContent -replace 'msix_version:\s*\d+\.\d+\.\d+\.\d+', "msix_version: $msixVersion" [System.IO.File]::WriteAllText($PubspecPath, $updatedContent, [System.Text.UTF8Encoding]::new($false)) Write-Host "[2/5] msix_version synced: $msixVersion" -ForegroundColor Green # 3. Set store mode if ($LocalTest) { $updatedContent = $updatedContent -replace 'store:\s*true', 'store: false' [System.IO.File]::WriteAllText($PubspecPath, $updatedContent, [System.Text.UTF8Encoding]::new($false)) Write-Host "[3/5] Mode: Local Test (store: false)" -ForegroundColor Yellow } else { $updatedContent = $updatedContent -replace 'store:\s*false', 'store: true' [System.IO.File]::WriteAllText($PubspecPath, $updatedContent, [System.Text.UTF8Encoding]::new($false)) Write-Host "[3/5] Mode: Store Submit (store: true)" -ForegroundColor Green } # 4. Build MSIX Set-Location $ProjectRoot Write-Host "[4/5] Building MSIX..." -ForegroundColor Cyan dart run msix:create 2>&1 | ForEach-Object { Write-Host $_ } $msixPath = Join-Path $ProjectRoot "build\windows\x64\runner\Release\xianyan.msix" if (-not (Test-Path $msixPath)) { Write-Host "[4/5] ERROR: MSIX file not generated" -ForegroundColor Red exit 1 } $msixSize = [math]::Round((Get-Item $msixPath).Length / 1MB, 2) Write-Host "[4/5] MSIX generated: $msixPath ($msixSize MB)" -ForegroundColor Green # 5. Local test: sign + install if ($LocalTest) { $certPath = Join-Path $ProjectRoot "build\cert\test_certificate.pfx" $cerPath = Join-Path $ProjectRoot "build\cert\test.cer" $signtoolPath = "d:\Windows Kits\10\bin\10.0.22621.0\x64\signtool.exe" if (-not (Test-Path $certPath)) { Write-Host "[5/5] Generating test certificate..." -ForegroundColor Cyan New-Item -ItemType Directory -Force -Path (Split-Path $certPath) | Out-Null $cert = New-SelfSignedCertificate -Type Custom ` -Subject "CN=0334AD95-A5D7-4597-B71F-AA0696B7E9F7" ` -KeyUsage DigitalSignature ` -FriendlyName "Xianyan Test Cert" ` -CertStoreLocation "Cert:\CurrentUser\My" ` -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}") Export-PfxCertificate -Cert $cert -FilePath $certPath ` -Password (ConvertTo-SecureString -String "123456" -Force -AsPlainText) | Out-Null Export-Certificate -Cert $cert -FilePath $cerPath | Out-Null Write-Host " Certificate generated" -ForegroundColor Green } if (Test-Path $signtoolPath) { Write-Host "[5/5] Signing MSIX..." -ForegroundColor Cyan & $signtoolPath sign /fd SHA256 /f $certPath /p "123456" $msixPath 2>&1 | ForEach-Object { Write-Host $_ } Write-Host " Signed successfully" -ForegroundColor Green } else { Write-Host "[5/5] WARNING: signtool.exe not found, skip signing" -ForegroundColor Yellow } if (Test-Path $cerPath) { Write-Host "[5/5] Installing certificate to Trusted Root (requires admin)..." -ForegroundColor Cyan Start-Process powershell -Verb RunAs -Wait -ArgumentList @( '-Command', "Import-Certificate -FilePath '$cerPath' -CertStoreLocation 'Cert:\LocalMachine\Root'; Start-Sleep 2" ) } Write-Host "[5/5] Installing MSIX..." -ForegroundColor Cyan Add-AppxPackage -Path $msixPath -ForceUpdateFromAnyVersion 2>&1 | ForEach-Object { Write-Host $_ } Write-Host " Installed! Search 'xianyan' in Start Menu" -ForegroundColor Green # Restore store: true $finalContent = [System.IO.File]::ReadAllText($PubspecPath, [System.Text.UTF8Encoding]::new($false)) $finalContent = $finalContent -replace 'store:\s*false', 'store: true' [System.IO.File]::WriteAllText($PubspecPath, $finalContent, [System.Text.UTF8Encoding]::new($false)) Write-Host " Restored store: true" -ForegroundColor DarkGray } else { Write-Host "[5/5] Store package ready. Upload to Partner Center:" -ForegroundColor Green Write-Host " $msixPath" -ForegroundColor White } Write-Host "" Write-Host "========================================" -ForegroundColor Cyan Write-Host " Done!" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan