pack-release.ps1 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # pack-release.ps1
  2. param(
  3. [switch]$NoBuild,
  4. [string]$OutputDir
  5. )
  6. $ErrorActionPreference = 'Stop'
  7. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  8. $ProjectRoot = Split-Path -Parent $ScriptDir
  9. $VersionsDir = Join-Path $ProjectRoot 'versions'
  10. $DefaultOutputDir = Join-Path $ProjectRoot 'nupkgs'
  11. function Write-ColorText {
  12. param([string]$Text, [string]$Color = 'White')
  13. Write-Host $Text -ForegroundColor $Color
  14. }
  15. # 读取 Y/N 确认,按Q立即退出
  16. function Read-Confirm {
  17. param([string]$Prompt)
  18. Write-Host $Prompt -NoNewline
  19. while ($true) {
  20. $key = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
  21. if ($key.Character -eq 'q' -or $key.Character -eq 'Q') {
  22. Write-Host ''
  23. Write-ColorText '已退出' 'Yellow'
  24. exit 0
  25. }
  26. if ($key.Character -eq 'y' -or $key.Character -eq 'Y' -or $key.VirtualKeyCode -eq 13) {
  27. Write-Host 'Y'
  28. return $true
  29. }
  30. if ($key.Character -eq 'n' -or $key.Character -eq 'N') {
  31. Write-Host 'N'
  32. return $false
  33. }
  34. }
  35. }
  36. Write-ColorText "`n========================================" 'Cyan'
  37. Write-ColorText ' Apq.ChangeBubbling NuGet 包生成工具' 'Cyan'
  38. Write-ColorText "========================================" 'Cyan'
  39. Write-ColorText ' 按 Q 随时退出' 'DarkGray'
  40. Write-ColorText "========================================`n" 'Cyan'
  41. if (-not (Test-Path $VersionsDir)) {
  42. Write-ColorText '错误: 找不到 versions 目录' 'Red'
  43. Write-ColorText "路径: $VersionsDir" 'Red'
  44. exit 1
  45. }
  46. # 从 versions 目录获取版本号(与 Directory.Build.props 保持一致)
  47. $versionFiles = @(Get-ChildItem -Path $VersionsDir -Filter 'v*.md' -ErrorAction SilentlyContinue)
  48. $versions = @($versionFiles | Where-Object { $_.BaseName -match '^v(\d+)\.(\d+)\.(\d+)' } | ForEach-Object {
  49. $fullVersion = $_.BaseName -replace '^v', ''
  50. $baseVersion = $_.BaseName -replace '^v(\d+\.\d+\.\d+).*', '$1'
  51. [PSCustomObject]@{
  52. Name = $fullVersion
  53. Version = [version]$baseVersion
  54. }
  55. } | Sort-Object Version -Descending)
  56. if ($versions.Count -eq 0) {
  57. Write-ColorText '错误: 无法从 versions 目录获取版本号' 'Red'
  58. Write-ColorText '请确保 versions 目录中存在 v*.*.*.md 格式的版本文件' 'Red'
  59. exit 1
  60. }
  61. $currentVersion = $versions[0].Name
  62. Write-ColorText "当前版本: $currentVersion" 'Yellow'
  63. # 设置输出目录
  64. if ([string]::IsNullOrWhiteSpace($OutputDir)) {
  65. $OutputDir = $DefaultOutputDir
  66. }
  67. Write-Host ''
  68. Write-ColorText '将要打包的项目:' 'Cyan'
  69. Write-ColorText ' - Apq.ChangeBubbling' 'White'
  70. Write-Host ''
  71. Write-ColorText "输出目录: $OutputDir" 'Gray'
  72. Write-Host ''
  73. if (-not (Read-Confirm '确认开始打包? ([Y]/N): ')) {
  74. Write-ColorText '已取消' 'Yellow'
  75. exit 0
  76. }
  77. # 创建输出目录
  78. if (-not (Test-Path $OutputDir)) {
  79. New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null
  80. Write-ColorText "已创建输出目录: $OutputDir" 'Gray'
  81. }
  82. Write-Host ''
  83. Write-ColorText '开始打包...' 'Cyan'
  84. Write-Host ''
  85. # 删除当前版本的旧包(避免 NuGet 缓存冲突)
  86. $oldPackages = Get-ChildItem -Path $OutputDir -Filter "Apq.ChangeBubbling*.$currentVersion.*pkg" -ErrorAction SilentlyContinue
  87. if ($oldPackages.Count -gt 0) {
  88. Write-ColorText "清理当前版本 ($currentVersion) 的旧包..." 'Gray'
  89. foreach ($pkg in $oldPackages) {
  90. Remove-Item $pkg.FullName -Force
  91. Write-ColorText " 已删除: $($pkg.Name)" 'DarkGray'
  92. }
  93. Write-Host ''
  94. }
  95. # 构建打包参数 - 只打包主项目
  96. $ProjectPath = Join-Path $ProjectRoot 'Apq.ChangeBubbling\Apq.ChangeBubbling.csproj'
  97. $packArgs = @(
  98. 'pack'
  99. $ProjectPath
  100. '-c', 'Release'
  101. '-o', $OutputDir
  102. )
  103. if ($NoBuild) {
  104. $packArgs += '--no-build'
  105. Write-ColorText '跳过构建 (使用 --no-build)' 'Gray'
  106. }
  107. try {
  108. # 执行打包
  109. & dotnet @packArgs
  110. if ($LASTEXITCODE -ne 0) {
  111. Write-Host ''
  112. Write-ColorText '错误: 打包失败' 'Red'
  113. exit 1
  114. }
  115. Write-Host ''
  116. Write-ColorText '打包完成!' 'Green'
  117. Write-Host ''
  118. # 列出生成的包
  119. $packages = Get-ChildItem -Path $OutputDir -Filter "*.nupkg" | Where-Object { $_.Name -match "Apq\.ChangeBubbling.*\.$([regex]::Escape($currentVersion))\.nupkg$" }
  120. $symbolPackages = Get-ChildItem -Path $OutputDir -Filter "*.snupkg" | Where-Object { $_.Name -match "Apq\.ChangeBubbling.*\.$([regex]::Escape($currentVersion))\.snupkg$" }
  121. if ($packages.Count -gt 0) {
  122. Write-ColorText '生成的 NuGet 包:' 'Cyan'
  123. foreach ($pkg in $packages) {
  124. $size = [math]::Round($pkg.Length / 1KB, 1)
  125. Write-ColorText " $($pkg.Name) ($size KB)" 'White'
  126. }
  127. Write-Host ''
  128. if ($symbolPackages.Count -gt 0) {
  129. Write-ColorText '生成的符号包:' 'Cyan'
  130. foreach ($pkg in $symbolPackages) {
  131. $size = [math]::Round($pkg.Length / 1KB, 1)
  132. Write-ColorText " $($pkg.Name) ($size KB)" 'White'
  133. }
  134. Write-Host ''
  135. }
  136. }
  137. Write-ColorText '下一步操作:' 'Yellow'
  138. Write-ColorText ' 运行 push-nuget.bat 发布到 NuGet' 'Gray'
  139. Write-Host ''
  140. } catch {
  141. Write-Host ''
  142. Write-ColorText '错误: 打包过程中发生异常' 'Red'
  143. Write-ColorText $_.Exception.Message 'Red'
  144. exit 1
  145. }