push-nuget.ps1 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # push-nuget.ps1
  2. param(
  3. [string]$ApiKey,
  4. [string]$Source = 'https://api.nuget.org/v3/index.json',
  5. [string]$PackageDir,
  6. [switch]$SkipConfirm
  7. )
  8. $ErrorActionPreference = 'Stop'
  9. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  10. $RootDir = Split-Path -Parent $ScriptDir
  11. $ApqNewDir = Split-Path -Parent $RootDir
  12. $PropsFile = Join-Path $RootDir 'Directory.Build.props'
  13. $DefaultPackageDir = Join-Path $RootDir 'nupkgs'
  14. $ApiKeyFile = Join-Path $ScriptDir 'NuGet_Apq_Key.txt'
  15. function Write-ColorText {
  16. param([string]$Text, [string]$Color = 'White')
  17. Write-Host $Text -ForegroundColor $Color
  18. }
  19. # 读取 Y/N 确认,按Q立即退出
  20. function Read-Confirm {
  21. param([string]$Prompt)
  22. Write-Host $Prompt -NoNewline
  23. while ($true) {
  24. $key = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
  25. if ($key.Character -eq 'q' -or $key.Character -eq 'Q') {
  26. Write-Host ''
  27. Write-ColorText '已退出' 'Yellow'
  28. exit 0
  29. }
  30. if ($key.Character -eq 'y' -or $key.Character -eq 'Y') {
  31. Write-Host 'Y'
  32. return $true
  33. }
  34. if ($key.Character -eq 'n' -or $key.Character -eq 'N') {
  35. Write-Host 'N'
  36. return $false
  37. }
  38. }
  39. }
  40. Write-ColorText "`n========================================" 'Cyan'
  41. Write-ColorText ' Apq.Cfg NuGet 发布工具' 'Cyan'
  42. Write-ColorText "========================================" 'Cyan'
  43. Write-ColorText ' 按 Q 随时退出' 'DarkGray'
  44. Write-ColorText "========================================`n" 'Cyan'
  45. if (-not (Test-Path $PropsFile)) {
  46. Write-ColorText '错误: 找不到 Directory.Build.props 文件' 'Red'
  47. Write-ColorText "路径: $PropsFile" 'Red'
  48. exit 1
  49. }
  50. # 读取当前版本
  51. $fileContent = Get-Content $PropsFile -Raw -Encoding UTF8
  52. if ($fileContent -match '<Version>([^<]+)</Version>') {
  53. $currentVersion = $Matches[1]
  54. Write-ColorText "当前版本: $currentVersion" 'Yellow'
  55. } else {
  56. Write-ColorText '错误: 无法读取当前版本号' 'Red'
  57. exit 1
  58. }
  59. # 设置包目录
  60. if ([string]::IsNullOrWhiteSpace($PackageDir)) {
  61. $PackageDir = $DefaultPackageDir
  62. }
  63. if (-not (Test-Path $PackageDir)) {
  64. Write-ColorText '错误: 包目录不存在' 'Red'
  65. Write-ColorText "路径: $PackageDir" 'Red'
  66. Write-ColorText '请先运行 pack-release.bat 生成包' 'Yellow'
  67. exit 1
  68. }
  69. # 查找当前版本的包
  70. $packages = Get-ChildItem -Path $PackageDir -Filter "*.nupkg" | Where-Object { $_.Name -match "Apq\.Cfg.*\.$([regex]::Escape($currentVersion))\.nupkg$" }
  71. $symbolPackages = Get-ChildItem -Path $PackageDir -Filter "*.snupkg" | Where-Object { $_.Name -match "Apq\.Cfg.*\.$([regex]::Escape($currentVersion))\.snupkg$" }
  72. if ($packages.Count -eq 0) {
  73. Write-ColorText "错误: 未找到版本 $currentVersion 的包" 'Red'
  74. Write-ColorText "路径: $PackageDir" 'Red'
  75. Write-ColorText '请先运行 pack-release.bat 生成包' 'Yellow'
  76. exit 1
  77. }
  78. Write-Host ''
  79. Write-ColorText '将要发布的包:' 'Cyan'
  80. foreach ($pkg in $packages) {
  81. $size = [math]::Round($pkg.Length / 1KB, 1)
  82. Write-ColorText " $($pkg.Name) ($size KB)" 'White'
  83. }
  84. if ($symbolPackages.Count -gt 0) {
  85. Write-Host ''
  86. Write-ColorText '符号包 (将自动上传):' 'Cyan'
  87. foreach ($pkg in $symbolPackages) {
  88. $size = [math]::Round($pkg.Length / 1KB, 1)
  89. Write-ColorText " $($pkg.Name) ($size KB)" 'Gray'
  90. }
  91. }
  92. Write-Host ''
  93. Write-ColorText "目标源: $Source" 'Gray'
  94. Write-Host ''
  95. # 获取 API Key
  96. if ([string]::IsNullOrWhiteSpace($ApiKey)) {
  97. # 从文件读取 API Key
  98. if (Test-Path $ApiKeyFile) {
  99. $ApiKey = (Get-Content $ApiKeyFile -Raw -Encoding UTF8).Trim()
  100. if ([string]::IsNullOrWhiteSpace($ApiKey)) {
  101. Write-ColorText '错误: API Key 文件内容为空' 'Red'
  102. Write-ColorText "路径: $ApiKeyFile" 'Red'
  103. exit 1
  104. }
  105. Write-ColorText "已从文件读取 API Key: $ApiKeyFile" 'Gray'
  106. } else {
  107. Write-ColorText '错误: 找不到 API Key 文件' 'Red'
  108. Write-ColorText "路径: $ApiKeyFile" 'Red'
  109. exit 1
  110. }
  111. }
  112. Write-Host ''
  113. if (-not $SkipConfirm) {
  114. if (-not (Read-Confirm '确认发布到 NuGet? (Y/N): ')) {
  115. Write-ColorText '已取消' 'Yellow'
  116. exit 0
  117. }
  118. }
  119. Write-Host ''
  120. Write-ColorText '开始发布...' 'Cyan'
  121. Write-Host ''
  122. $successCount = 0
  123. $failCount = 0
  124. foreach ($pkg in $packages) {
  125. Write-ColorText "发布: $($pkg.Name)" 'White'
  126. try {
  127. $result = & dotnet nuget push $pkg.FullName -s $Source -k $ApiKey --skip-duplicate 2>&1
  128. if ($LASTEXITCODE -eq 0) {
  129. Write-ColorText " 成功" 'Green'
  130. $successCount++
  131. } else {
  132. $errorMsg = $result | Out-String
  133. if ($errorMsg -match 'already exists') {
  134. Write-ColorText " 跳过 (已存在)" 'Yellow'
  135. $successCount++
  136. } else {
  137. Write-ColorText " 失败: $errorMsg" 'Red'
  138. $failCount++
  139. }
  140. }
  141. } catch {
  142. Write-ColorText " 失败: $($_.Exception.Message)" 'Red'
  143. $failCount++
  144. }
  145. }
  146. Write-Host ''
  147. Write-ColorText "========================================" 'Cyan'
  148. Write-ColorText "发布完成: 成功 $successCount, 失败 $failCount" $(if ($failCount -eq 0) { 'Green' } else { 'Yellow' })
  149. Write-ColorText "========================================" 'Cyan'
  150. Write-Host ''
  151. if ($failCount -eq 0) {
  152. Write-ColorText '所有包已成功发布!' 'Green'
  153. Write-Host ''
  154. Write-ColorText '查看已发布的包:' 'Yellow'
  155. Write-ColorText ' https://www.nuget.org/profiles/Apq' 'DarkGray'
  156. } else {
  157. Write-ColorText "有 $failCount 个包发布失败,请检查错误信息" 'Red'
  158. }
  159. Write-Host ''