pack-release.ps1 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. $RootDir = Split-Path -Parent $ScriptDir
  9. $PropsFile = Join-Path $RootDir 'Directory.Build.props'
  10. $DefaultOutputDir = Join-Path $RootDir 'nupkgs'
  11. function Write-ColorText {
  12. param([string]$Text, [string]$Color = 'White')
  13. Write-Host $Text -ForegroundColor $Color
  14. }
  15. # 读取 Y/N 确认,按Q立即退出,回车默认Y
  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.Cfg NuGet 包生成工具' 'Cyan'
  38. Write-ColorText "========================================" 'Cyan'
  39. Write-ColorText ' 按 Q 随时退出' 'DarkGray'
  40. Write-ColorText "========================================`n" 'Cyan'
  41. if (-not (Test-Path $PropsFile)) {
  42. Write-ColorText '错误: 找不到 Directory.Build.props 文件' 'Red'
  43. Write-ColorText "路径: $PropsFile" 'Red'
  44. exit 1
  45. }
  46. # 从 versions 目录获取版本号(与 Directory.Build.props 保持一致)
  47. $VersionsDir = Join-Path $RootDir 'versions'
  48. if (-not (Test-Path $VersionsDir)) {
  49. Write-ColorText '错误: 找不到 versions 目录' 'Red'
  50. Write-ColorText "路径: $VersionsDir" 'Red'
  51. exit 1
  52. }
  53. $versionFiles = @(Get-ChildItem -Path $VersionsDir -Filter 'v*.md' -ErrorAction SilentlyContinue)
  54. $versions = @($versionFiles | Where-Object { $_.BaseName -match '^v(\d+)\.(\d+)\.(\d+)' } | ForEach-Object {
  55. $fullVersion = $_.BaseName -replace '^v', ''
  56. $baseVersion = $_.BaseName -replace '^v(\d+\.\d+\.\d+).*', '$1'
  57. [PSCustomObject]@{
  58. Name = $fullVersion
  59. Version = [version]$baseVersion
  60. }
  61. } | Sort-Object Version -Descending)
  62. if ($versions.Count -gt 0) {
  63. $currentVersion = $versions[0].Name
  64. Write-ColorText "当前版本: $currentVersion" 'Yellow'
  65. } else {
  66. Write-ColorText '错误: 无法从 versions 目录获取版本号' 'Red'
  67. Write-ColorText '请确保 versions 目录中存在 v*.*.*.md 格式的版本文件' 'Red'
  68. exit 1
  69. }
  70. # 设置输出目录
  71. if ([string]::IsNullOrWhiteSpace($OutputDir)) {
  72. $OutputDir = $DefaultOutputDir
  73. }
  74. Write-Host ''
  75. Write-ColorText '将要打包的项目:' 'Cyan'
  76. Write-ColorText ' - Apq.Cfg' 'White'
  77. Write-ColorText ' - Apq.Cfg.Ini' 'White'
  78. Write-ColorText ' - Apq.Cfg.Xml' 'White'
  79. Write-ColorText ' - Apq.Cfg.Yaml' 'White'
  80. Write-ColorText ' - Apq.Cfg.Toml' 'White'
  81. Write-ColorText ' - Apq.Cfg.Redis' 'White'
  82. Write-ColorText ' - Apq.Cfg.Database' 'White'
  83. Write-ColorText ' - Apq.Cfg.SourceGenerator' 'White'
  84. Write-Host ''
  85. Write-ColorText "输出目录: $OutputDir" 'Gray'
  86. Write-Host ''
  87. if (-not (Read-Confirm '确认开始打包? ([Y]/N): ')) {
  88. Write-ColorText '已取消' 'Yellow'
  89. exit 0
  90. }
  91. # 创建输出目录
  92. if (-not (Test-Path $OutputDir)) {
  93. New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null
  94. Write-ColorText "已创建输出目录: $OutputDir" 'Gray'
  95. }
  96. Write-Host ''
  97. Write-ColorText '开始打包...' 'Cyan'
  98. Write-Host ''
  99. # 删除当前版本的旧包(避免 NuGet 缓存冲突)
  100. $oldPackages = Get-ChildItem -Path $OutputDir -Filter "Apq.Cfg*.$currentVersion.*pkg" -ErrorAction SilentlyContinue
  101. if ($oldPackages.Count -gt 0) {
  102. Write-ColorText "清理当前版本 ($currentVersion) 的旧包..." 'Gray'
  103. foreach ($pkg in $oldPackages) {
  104. Remove-Item $pkg.FullName -Force
  105. Write-ColorText " 已删除: $($pkg.Name)" 'DarkGray'
  106. }
  107. Write-Host ''
  108. }
  109. # 构建打包参数
  110. $packArgs = @(
  111. 'pack'
  112. $RootDir
  113. '-c', 'Release'
  114. '-o', $OutputDir
  115. )
  116. if ($NoBuild) {
  117. $packArgs += '--no-build'
  118. Write-ColorText '跳过构建 (使用 --no-build)' 'Gray'
  119. }
  120. try {
  121. # 执行打包
  122. & dotnet @packArgs
  123. if ($LASTEXITCODE -ne 0) {
  124. Write-Host ''
  125. Write-ColorText '错误: 打包失败' 'Red'
  126. exit 1
  127. }
  128. Write-Host ''
  129. Write-ColorText '打包完成!' 'Green'
  130. Write-Host ''
  131. # 列出生成的包
  132. $packages = Get-ChildItem -Path $OutputDir -Filter "*.nupkg" | Where-Object { $_.Name -match "Apq\.Cfg.*\.$([regex]::Escape($currentVersion))\.nupkg$" }
  133. $symbolPackages = Get-ChildItem -Path $OutputDir -Filter "*.snupkg" | Where-Object { $_.Name -match "Apq\.Cfg.*\.$([regex]::Escape($currentVersion))\.snupkg$" }
  134. if ($packages.Count -gt 0) {
  135. Write-ColorText '生成的 NuGet 包:' 'Cyan'
  136. foreach ($pkg in $packages) {
  137. $size = [math]::Round($pkg.Length / 1KB, 1)
  138. Write-ColorText " $($pkg.Name) ($size KB)" 'White'
  139. }
  140. Write-Host ''
  141. if ($symbolPackages.Count -gt 0) {
  142. Write-ColorText '生成的符号包:' 'Cyan'
  143. foreach ($pkg in $symbolPackages) {
  144. $size = [math]::Round($pkg.Length / 1KB, 1)
  145. Write-ColorText " $($pkg.Name) ($size KB)" 'White'
  146. }
  147. Write-Host ''
  148. }
  149. }
  150. Write-ColorText '下一步操作:' 'Yellow'
  151. Write-ColorText ' 运行 push-nuget.bat 发布到 NuGet' 'Gray'
  152. Write-Host ''
  153. } catch {
  154. Write-Host ''
  155. Write-ColorText '错误: 打包过程中发生异常' 'Red'
  156. Write-ColorText $_.Exception.Message 'Red'
  157. exit 1
  158. }