pack-release.ps1 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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立即退出
  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') {
  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. # 读取当前版本
  47. $fileContent = Get-Content $PropsFile -Raw -Encoding UTF8
  48. if ($fileContent -match '<Version>([^<]+)</Version>') {
  49. $currentVersion = $Matches[1]
  50. Write-ColorText "当前版本: $currentVersion" 'Yellow'
  51. } else {
  52. Write-ColorText '错误: 无法读取当前版本号' 'Red'
  53. exit 1
  54. }
  55. # 设置输出目录
  56. if ([string]::IsNullOrWhiteSpace($OutputDir)) {
  57. $OutputDir = $DefaultOutputDir
  58. }
  59. Write-Host ''
  60. Write-ColorText '将要打包的项目:' 'Cyan'
  61. Write-ColorText ' - Apq.Cfg' 'White'
  62. Write-ColorText ' - Apq.Cfg.Ini' 'White'
  63. Write-ColorText ' - Apq.Cfg.Xml' 'White'
  64. Write-ColorText ' - Apq.Cfg.Yaml' 'White'
  65. Write-ColorText ' - Apq.Cfg.Toml' 'White'
  66. Write-ColorText ' - Apq.Cfg.Redis' 'White'
  67. Write-ColorText ' - Apq.Cfg.Database' 'White'
  68. Write-Host ''
  69. Write-ColorText "输出目录: $OutputDir" 'Gray'
  70. Write-Host ''
  71. if (-not (Read-Confirm '确认开始打包? (Y/N): ')) {
  72. Write-ColorText '已取消' 'Yellow'
  73. exit 0
  74. }
  75. # 创建输出目录
  76. if (-not (Test-Path $OutputDir)) {
  77. New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null
  78. Write-ColorText "已创建输出目录: $OutputDir" 'Gray'
  79. }
  80. Write-Host ''
  81. Write-ColorText '开始打包...' 'Cyan'
  82. Write-Host ''
  83. # 删除当前版本的旧包(避免 NuGet 缓存冲突)
  84. $oldPackages = Get-ChildItem -Path $OutputDir -Filter "Apq.Cfg*.$currentVersion.*pkg" -ErrorAction SilentlyContinue
  85. if ($oldPackages.Count -gt 0) {
  86. Write-ColorText "清理当前版本 ($currentVersion) 的旧包..." 'Gray'
  87. foreach ($pkg in $oldPackages) {
  88. Remove-Item $pkg.FullName -Force
  89. Write-ColorText " 已删除: $($pkg.Name)" 'DarkGray'
  90. }
  91. Write-Host ''
  92. }
  93. # 构建打包参数
  94. $packArgs = @(
  95. 'pack'
  96. $RootDir
  97. '-c', 'Release'
  98. '-o', $OutputDir
  99. )
  100. if ($NoBuild) {
  101. $packArgs += '--no-build'
  102. Write-ColorText '跳过构建 (使用 --no-build)' 'Gray'
  103. }
  104. try {
  105. # 执行打包
  106. & dotnet @packArgs
  107. if ($LASTEXITCODE -ne 0) {
  108. Write-Host ''
  109. Write-ColorText '错误: 打包失败' 'Red'
  110. exit 1
  111. }
  112. Write-Host ''
  113. Write-ColorText '打包完成!' 'Green'
  114. Write-Host ''
  115. # 列出生成的包
  116. $packages = Get-ChildItem -Path $OutputDir -Filter "*.nupkg" | Where-Object { $_.Name -match "Apq\.Cfg.*\.$([regex]::Escape($currentVersion))\.nupkg$" }
  117. $symbolPackages = Get-ChildItem -Path $OutputDir -Filter "*.snupkg" | Where-Object { $_.Name -match "Apq\.Cfg.*\.$([regex]::Escape($currentVersion))\.snupkg$" }
  118. if ($packages.Count -gt 0) {
  119. Write-ColorText '生成的 NuGet 包:' 'Cyan'
  120. foreach ($pkg in $packages) {
  121. $size = [math]::Round($pkg.Length / 1KB, 1)
  122. Write-ColorText " $($pkg.Name) ($size KB)" 'White'
  123. }
  124. Write-Host ''
  125. if ($symbolPackages.Count -gt 0) {
  126. Write-ColorText '生成的符号包:' 'Cyan'
  127. foreach ($pkg in $symbolPackages) {
  128. $size = [math]::Round($pkg.Length / 1KB, 1)
  129. Write-ColorText " $($pkg.Name) ($size KB)" 'White'
  130. }
  131. Write-Host ''
  132. }
  133. }
  134. Write-ColorText '下一步操作:' 'Yellow'
  135. Write-ColorText ' 运行 push-nuget.bat 发布到 NuGet' 'Gray'
  136. Write-Host ''
  137. } catch {
  138. Write-Host ''
  139. Write-ColorText '错误: 打包过程中发生异常' 'Red'
  140. Write-ColorText $_.Exception.Message 'Red'
  141. exit 1
  142. }