|
| 1 | +param( |
| 2 | + [ValidateSet("Release", "Canary")] |
| 3 | + [string]$Mode = "Release", |
| 4 | + |
| 5 | + # Canary 模式下的证书配置 |
| 6 | + [string]$CanaryPublisher = "CN=凌莞" |
| 7 | +) |
| 8 | + |
| 9 | +$ErrorActionPreference = "Stop" |
| 10 | +$ProjectRoot = Resolve-Path "$PSScriptRoot\.." |
| 11 | + |
| 12 | +# ========================================== |
| 13 | +# 1. 版本号处理 |
| 14 | +# ========================================== |
| 15 | +Write-Host "Calculating version..." -ForegroundColor Cyan |
| 16 | +$BuildVersion = "1.0.0.0" |
| 17 | + |
| 18 | +try { |
| 19 | + Push-Location $ProjectRoot |
| 20 | + $gitDescribe = git describe --tags --long |
| 21 | + Pop-Location |
| 22 | + |
| 23 | + if ($gitDescribe -match "v?(\d+\.\d+\.\d+)-(\d+)-g[0-9a-f]+") { |
| 24 | + $baseVer = $Matches[1] |
| 25 | + $commitCount = $Matches[2] |
| 26 | + |
| 27 | + if ($Mode -eq "Canary") { |
| 28 | + $BuildVersion = "$baseVer.$commitCount" |
| 29 | + } else { |
| 30 | + # Release 模式保留三位 (补0) |
| 31 | + $BuildVersion = "$baseVer.0" |
| 32 | + } |
| 33 | + } else { |
| 34 | + Write-Warning "Git describe format mismatch: $gitDescribe. Fallback to $BuildVersion" |
| 35 | + } |
| 36 | +} catch { |
| 37 | + Write-Warning "Git describe failed. Fallback to $BuildVersion" |
| 38 | +} |
| 39 | + |
| 40 | +Write-Host "Target Version: $BuildVersion" -ForegroundColor Green |
| 41 | + |
| 42 | +# 修改 C# 源码版本号 |
| 43 | +# Canary 模式使用完整的 4 段版本号 ($BuildVersion) |
| 44 | +# Release 模式使用 3 段版本号 ($baseVer) |
| 45 | +Write-Host "Updating C# source version..." -ForegroundColor Cyan |
| 46 | +$AppMainPath = "$ProjectRoot\MaiChartManager\AppMain.cs" |
| 47 | +$AppMainContent = Get-Content $AppMainPath -Raw |
| 48 | + |
| 49 | +if ($Mode -eq "Canary") { |
| 50 | + $SourceVersion = $BuildVersion |
| 51 | +} else { |
| 52 | + $SourceVersion = $baseVer |
| 53 | +} |
| 54 | + |
| 55 | +$AppMainContent = $AppMainContent -replace 'public const string Version = "[^"]+"', "public const string Version = ""$SourceVersion""" |
| 56 | +Set-Content $AppMainPath $AppMainContent -Encoding UTF8 |
| 57 | + |
| 58 | + |
| 59 | +# ========================================== |
| 60 | +# 2. 清理环境 |
| 61 | +# ========================================== |
| 62 | +Write-Host "Cleaning up..." -ForegroundColor Cyan |
| 63 | +$PackDir = "$PSScriptRoot\Pack" |
| 64 | +if (Test-Path $PackDir) { Remove-Item $PackDir -Recurse -Force } |
| 65 | +Remove-Item "$PSScriptRoot\*.appx" -ErrorAction SilentlyContinue |
| 66 | +Remove-Item "$PSScriptRoot\*.msix" -ErrorAction SilentlyContinue |
| 67 | + |
| 68 | +# ========================================== |
| 69 | +# 3. 构建 AquaMai |
| 70 | +# ========================================== |
| 71 | +Write-Host "Building AquaMai..." -ForegroundColor Cyan |
| 72 | +Push-Location "$ProjectRoot\AquaMai" |
| 73 | +try { |
| 74 | + Stop-Process -Name "dotnet" -Force -ErrorAction SilentlyContinue |
| 75 | + dotnet cake |
| 76 | + |
| 77 | + $TargetResDir = "$ProjectRoot\MaiChartManager\Resources" |
| 78 | + if (-not (Test-Path $TargetResDir)) { New-Item -ItemType Directory -Path $TargetResDir } |
| 79 | + Copy-Item "Output\AquaMai.dll" $TargetResDir -Force |
| 80 | + |
| 81 | + # AquaMai 签名 |
| 82 | + if (Get-Command "AquaMaiLocalBuild.exe" -ErrorAction SilentlyContinue) { |
| 83 | + Write-Host "Signing AquaMai.dll..." -ForegroundColor Cyan |
| 84 | + AquaMaiLocalBuild.exe "$TargetResDir\AquaMai.dll" |
| 85 | + } else { |
| 86 | + Write-Host "AquaMaiLocalBuild.exe not found, skipping AquaMai signing." -ForegroundColor Yellow |
| 87 | + } |
| 88 | + |
| 89 | +} finally { |
| 90 | + Pop-Location |
| 91 | +} |
| 92 | + |
| 93 | +# ========================================== |
| 94 | +# 4. 构建前端 |
| 95 | +# ========================================== |
| 96 | +Write-Host "Building Frontend..." -ForegroundColor Cyan |
| 97 | +Push-Location "$ProjectRoot\MaiChartManager\Front" |
| 98 | +try { |
| 99 | + cmd /c pnpm build |
| 100 | +} finally { |
| 101 | + Pop-Location |
| 102 | +} |
| 103 | + |
| 104 | +# ========================================== |
| 105 | +# 5. 发布主程序 |
| 106 | +# ========================================== |
| 107 | +Write-Host "Publishing MaiChartManager..." -ForegroundColor Cyan |
| 108 | +Push-Location "$ProjectRoot" |
| 109 | +try { |
| 110 | + # Determine Configuration |
| 111 | + $ConfigName = if ($Mode -eq "Canary") { "Crack" } else { "Release" } |
| 112 | + Write-Host "Using Configuration: $ConfigName" -ForegroundColor Yellow |
| 113 | + |
| 114 | + dotnet publish -p:Configuration=$ConfigName |
| 115 | +} finally { |
| 116 | + Pop-Location |
| 117 | +} |
| 118 | + |
| 119 | +# ========================================== |
| 120 | +# 6. 准备打包目录 |
| 121 | +# ========================================== |
| 122 | +Write-Host "Preparing Package Directory..." -ForegroundColor Cyan |
| 123 | + |
| 124 | +# 复制 Base 资源 |
| 125 | +Copy-Item "$PSScriptRoot\Base\*" $PackDir -Recurse -Force |
| 126 | + |
| 127 | +# 复制 Canary Base 资源 (如果存在且是 Canary 模式) |
| 128 | +if ($Mode -eq "Canary") { |
| 129 | + $BaseCanary = "$PSScriptRoot\Base-Canary" |
| 130 | + if (Test-Path $BaseCanary) { |
| 131 | + Write-Host "Copying Canary assets..." -ForegroundColor Cyan |
| 132 | + Copy-Item "$BaseCanary\*" $PackDir -Recurse -Force |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +# ========================================== |
| 137 | +# 7. 修改 Manifest (仅 Canary) |
| 138 | +# ========================================== |
| 139 | +if ($Mode -eq "Canary") { |
| 140 | + Write-Host "Patching Manifest for Canary..." -ForegroundColor Cyan |
| 141 | + $ManifestPath = "$PackDir\AppxManifest.xml" |
| 142 | + |
| 143 | + [xml]$xml = Get-Content $ManifestPath |
| 144 | + |
| 145 | + # 命名空间管理器 |
| 146 | + $ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable) |
| 147 | + $ns.AddNamespace("x", "http://schemas.microsoft.com/appx/manifest/foundation/windows10") |
| 148 | + $ns.AddNamespace("uap", "http://schemas.microsoft.com/appx/manifest/uap/windows10") |
| 149 | + $ns.AddNamespace("desktop", "http://schemas.microsoft.com/appx/manifest/desktop/windows10") |
| 150 | + $ns.AddNamespace("uap3", "http://schemas.microsoft.com/appx/manifest/uap/windows10/3") |
| 151 | + |
| 152 | + # 修改 Identity |
| 153 | + $xml.Package.Identity.Name = $xml.Package.Identity.Name + ".Canary" |
| 154 | + $xml.Package.Identity.Publisher = $CanaryPublisher |
| 155 | + $xml.Package.Identity.Version = $BuildVersion |
| 156 | + |
| 157 | + # 修改 Properties |
| 158 | + $xml.Package.Properties.DisplayName = "MaiChartManager (Canary)" |
| 159 | + $xml.Package.Properties.PublisherDisplayName = "凌莞" |
| 160 | + |
| 161 | + # 修改 Applications |
| 162 | + foreach ($app in $xml.Package.Applications.Application) { |
| 163 | + if ($app.VisualElements) { |
| 164 | + $app.VisualElements.DisplayName = $app.VisualElements.DisplayName + " (Canary)" |
| 165 | + } |
| 166 | + |
| 167 | + # 修改 CLI Alias: mcm.exe -> mcmc.exe |
| 168 | + $aliasNode = $app.SelectSingleNode(".//desktop:ExecutionAlias", $ns) |
| 169 | + if ($aliasNode) { |
| 170 | + $aliasNode.Alias = "mcmc.exe" |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + $xml.Save($ManifestPath) |
| 175 | +} else { |
| 176 | + # Release 模式也要更新 Version |
| 177 | + $ManifestPath = "$PackDir\AppxManifest.xml" |
| 178 | + [xml]$xml = Get-Content $ManifestPath |
| 179 | + $xml.Package.Identity.Version = $BuildVersion |
| 180 | + $xml.Save($ManifestPath) |
| 181 | +} |
| 182 | + |
| 183 | +# ========================================== |
| 184 | +# 8. 生成 PRI 并打包 |
| 185 | +# ========================================== |
| 186 | +Write-Host "Generating PRI and Packing..." -ForegroundColor Cyan |
| 187 | +Push-Location $PackDir |
| 188 | +try { |
| 189 | + Remove-Item "priconfig.xml" -ErrorAction SilentlyContinue |
| 190 | + Remove-Item "*.pri" -ErrorAction SilentlyContinue |
| 191 | + |
| 192 | + makepri.exe createconfig /cf priconfig.xml /dq zh-CN |
| 193 | + makepri.exe new /pr . /cf .\priconfig.xml |
| 194 | + Remove-Item "priconfig.xml" |
| 195 | + |
| 196 | + $OutputName = if ($Mode -eq "Canary") { "MaiChartManager_Canary_$BuildVersion.appx" } else { "Store64.appx" } |
| 197 | + $OutputAppx = "$PSScriptRoot\$OutputName" |
| 198 | + |
| 199 | + makeappx pack /d . /p $OutputAppx |
| 200 | +} finally { |
| 201 | + Pop-Location |
| 202 | +} |
| 203 | + |
| 204 | +# ========================================== |
| 205 | +# 9. 签名 (仅 Canary) |
| 206 | +# ========================================== |
| 207 | +if ($Mode -eq "Canary") { |
| 208 | + Write-Host "Signing Appx..." -ForegroundColor Cyan |
| 209 | + |
| 210 | + $SignCmd = "D:\Sign\signcode.cmd" |
| 211 | + if (Test-Path $SignCmd) { |
| 212 | + # 直接调用 cmd 脚本 |
| 213 | + & $SignCmd $OutputAppx |
| 214 | + Write-Host "Build & Sign Complete: $OutputAppx" -ForegroundColor Green |
| 215 | + } else { |
| 216 | + Write-Warning "Sign script not found at $SignCmd. Skipping signing." |
| 217 | + } |
| 218 | +} else { |
| 219 | + Write-Host "Build Complete: $OutputAppx" -ForegroundColor Green |
| 220 | +} |
0 commit comments