regen-gallery-index.ps1 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # Regenerate gallery\index.json from every *.zip under gallery\packages\.
  2. #
  3. # Layout convention: gallery\packages\<host>\<id>\<id>-<version>.zip
  4. # Each zip must contain a manifest.json at its root. The manifest's "host"
  5. # field decides which host tab the package belongs to (missing => platform,
  6. # backward compatible with plain extension packages).
  7. #
  8. # Rules (do not relax):
  9. # - All plugin packages live in this single index.json.
  10. # - 0.1.0 skeleton packages are excluded (removed from the product).
  11. # - downloadUrl is stored relative (packages\...); GalleryClient rewrites it
  12. # against whatever source root is actually configured.
  13. #
  14. # Invoked by each project's post-build target: parallel builds launch several
  15. # copies at once, so the whole regeneration runs under a named mutex.
  16. $ErrorActionPreference = 'Stop'
  17. function Invoke-WithRetry
  18. {
  19. param([scriptblock]$Action, [int]$Attempts = 5)
  20. for ($i = 1; ; $i++)
  21. {
  22. try { return & $Action }
  23. catch
  24. {
  25. if ($i -ge $Attempts) { throw }
  26. Start-Sleep -Milliseconds (250 * $i)
  27. }
  28. }
  29. }
  30. function Invoke-Regenerate
  31. {
  32. $root = Split-Path -Parent $PSScriptRoot
  33. $galleryDir = Join-Path $root 'gallery'
  34. $packagesDir = Join-Path $galleryDir 'packages'
  35. $indexPath = Join-Path $galleryDir 'index.json'
  36. if (-not (Test-Path $packagesDir)) {
  37. throw "packages directory not found: $packagesDir"
  38. }
  39. Add-Type -AssemblyName System.IO.Compression.FileSystem
  40. $sha = [System.Security.Cryptography.SHA256]::Create()
  41. $entries = New-Object System.Collections.Generic.List[object]
  42. $seen = @{}
  43. $zipFiles = Invoke-WithRetry { Get-ChildItem -Path $packagesDir -Recurse -Filter '*.zip' | Sort-Object FullName }
  44. foreach ($zipFile in $zipFiles) {
  45. # The producing build may still be flushing the zip; retry the open.
  46. $zip = Invoke-WithRetry { [System.IO.Compression.ZipFile]::OpenRead($zipFile.FullName) }
  47. try {
  48. $manifestEntry = $zip.Entries |
  49. Where-Object { $_.FullName -replace '\\', '/' -eq 'manifest.json' } |
  50. Select-Object -First 1
  51. if (-not $manifestEntry) {
  52. Write-Warning "skip (no manifest.json at root): $($zipFile.Name)"
  53. continue
  54. }
  55. $reader = New-Object System.IO.StreamReader($manifestEntry.Open(), [System.Text.Encoding]::UTF8)
  56. try { $manifest = $reader.ReadToEnd() } finally { $reader.Dispose() }
  57. $j = $manifest | ConvertFrom-Json
  58. if (-not $j.id -or -not $j.version) {
  59. Write-Warning "skip (manifest missing id/version): $($zipFile.Name)"
  60. continue
  61. }
  62. if ($j.version -eq '0.1.0') {
  63. Write-Host "exclude 0.1.0 package: $($j.id)"
  64. continue
  65. }
  66. $dedupKey = "$($j.id)@$($j.version)"
  67. if ($seen.ContainsKey($dedupKey)) {
  68. Write-Warning "skip duplicate: $dedupKey ($($zipFile.Name))"
  69. continue
  70. }
  71. $seen[$dedupKey] = $true
  72. # Hash + size come from the packaged zip itself.
  73. $fs = Invoke-WithRetry { [System.IO.File]::OpenRead($zipFile.FullName) }
  74. try {
  75. $hashBytes = $sha.ComputeHash($fs)
  76. $size = $fs.Length
  77. } finally { $fs.Dispose() }
  78. $hashHex = ([System.BitConverter]::ToString($hashBytes) -replace '-', '').ToLowerInvariant()
  79. $relativeUrl = $zipFile.FullName.Substring($galleryDir.Length + 1) -replace '\\', '/'
  80. $entry = [ordered]@{
  81. id = [string]$j.id
  82. name = [string]$j.name
  83. version = [string]$j.version
  84. publisher = if ($j.publisher) { [string]$j.publisher } else { $null }
  85. description = if ($j.description) { [string]$j.description } else { $null }
  86. downloadUrl = $relativeUrl
  87. sha256 = $hashHex
  88. size = $size
  89. }
  90. if ($j.host) { $entry['host'] = [string]$j.host }
  91. if ($j.category) { $entry['category'] = [string]$j.category }
  92. if ($j.engine) { $entry['engine'] = [string]$j.engine }
  93. if ($j.kind) { $entry['kind'] = [string]$j.kind }
  94. # 构建形态(platform 包区分 Debug/Release 用;缺省不带该字段 → 消费方按 Release 处理)
  95. if ($j.flavor) { $entry['flavor'] = [string]$j.flavor }
  96. if ($j.guid) { $entry['guid'] = [string]$j.guid }
  97. # 本版特性:来自包内 manifest.json 的 notes 字段(扩展管理页"本版特性"区展示)
  98. if ($j.notes) { $entry['notes'] = [string]$j.notes }
  99. $tfLine = ($manifest -split "`r?`n") | Where-Object { $_ -match 'targetFramework' } | Select-Object -First 1
  100. if ($tfLine) { $entry['targetFramework'] = ($tfLine -replace '.*:\s*"([^"]+)".*', '$1') }
  101. if ($j.references) { $entry['references'] = @($j.references) }
  102. if ($j.dependencies -and @($j.dependencies).Count -gt 0) {
  103. $entry['dependencies'] = @($j.dependencies)
  104. }
  105. # 运行环境(包内 manifest.json 的 targetFramework:在线仓库尚未下载包时的唯一来源);
  106. # 取值直接按 JSON 文本提取,避免依赖 PS 对象成员访问(在本机环境下试过会静默取到空值)
  107. # 宿主要求:{ id, minVersion } → "TeamPLT>=2.1.0"
  108. if ($j.requiresHost) {
  109. $rhText = if ($j.requiresHost.minVersion) { "$($j.requiresHost.id)>=$($j.requiresHost.minVersion)" } else { [string]$j.requiresHost.id }
  110. $entry['requiresHost'] = $rhText
  111. }
  112. if ($j.arch -and @($j.arch).Count -gt 0) { $entry['arch'] = @($j.arch) }
  113. if ($j.nativeDependencies -and @($j.nativeDependencies).Count -gt 0) {
  114. $entry['nativeDependencies'] = @($j.nativeDependencies)
  115. }
  116. # versions[]:index.json 契约里"多版本"就装在这个数组里(SDK 的 GalleryPackage.AllVersions 按它读取),
  117. # 平台扩展管理页的"版本"下拉也只认这一个来源 —— 缺了它,界面上永远只显示顶层 version 那一版,
  118. # 旧版本即使 zip 就在仓库里也选不到、装不了、回滚不了。
  119. $entry['versions'] = @([ordered]@{
  120. version = [string]$j.version
  121. downloadUrl = $relativeUrl
  122. sha256 = $hashHex
  123. size = $size
  124. releasedAt = (Get-Date).ToString('yyyy-MM-dd')
  125. status = 'stable'
  126. notes = if ($j.notes) { [string]$j.notes } else { $null }
  127. })
  128. $entries.Add([PSCustomObject]$entry)
  129. Write-Host ("indexed {0} {1} [{2}]" -f $j.id, $j.version, $(if ($j.host) { $j.host } else { 'platform' }))
  130. } finally {
  131. $zip.Dispose()
  132. }
  133. }
  134. $sha.Dispose()
  135. # 合并同 id 的多条为一个条目:packages[] 里每个 id 只应出现一次,多版本装进各自的 versions[]。
  136. # 不做这步的话,同 id 的每个版本各占一条,平台按 id 取一条展示 —— 表现为"版本下拉只剩最新一版"。
  137. function Get-VersionKey([string]$v) {
  138. $parsed = $null
  139. if ([version]::TryParse($v, [ref]$parsed)) { return $parsed }
  140. return [version]'0.0'
  141. }
  142. $byId = [ordered]@{}
  143. foreach ($e in $entries) {
  144. $key = [string]$e.id
  145. if ($byId.Contains($key)) {
  146. $cur = $byId[$key]
  147. $mergedVersions = @($cur.versions) + @($e.versions)
  148. # 顶层字段必须对齐到"更高的版本",而不是沿用先遇到的那条。
  149. # dependencies / references / kind / description 等各版本可能不同 ——
  150. # 例如 1.0.0 依赖后来废弃的契约包,1.0.1 改依赖新契约包。
  151. # 若顶层留在旧版(文件序通常旧版在前),平台按 id 读到的就是旧依赖,
  152. # 表现为"明明装了新版,却提示旧依赖未安装"。故版本更高时整体替换,再挂回累计版本列表。
  153. if ((Get-VersionKey $e.version) -gt (Get-VersionKey $cur.version)) {
  154. $cur = $e.PSObject.Copy()
  155. }
  156. $cur.versions = $mergedVersions
  157. $byId[$key] = $cur
  158. } else {
  159. $byId[$key] = $e
  160. }
  161. }
  162. $merged = New-Object System.Collections.Generic.List[object]
  163. foreach ($key in $byId.Keys) {
  164. $p = $byId[$key]
  165. $versions = @($p.versions | Sort-Object -Property @{ Expression = {
  166. $v = $null
  167. if ([version]::TryParse([string]$_.version, [ref]$v)) { $v } else { [version]'0.0' }
  168. } } -Descending)
  169. if ($versions.Count -eq 0) { $merged.Add($p); continue }
  170. # 顶层字段对齐到最高版本,保证只读顶层字段的旧消费方也拿到最新版。
  171. $top = $versions[0]
  172. $p.versions = $versions
  173. $p.version = [string]$top.version
  174. $p.downloadUrl = $top.downloadUrl
  175. $p.sha256 = $top.sha256
  176. $p.size = $top.size
  177. if (-not $p.notes -and $top.notes) { $p.notes = $top.notes }
  178. $merged.Add($p)
  179. }
  180. # Stable order: host (platform first), then id.
  181. $sorted = @($merged | Sort-Object @{ Expression = { if ($_.host) { $_.host } else { 'platform' } } }, @{ Expression = { $_.id } })
  182. $index = [ordered]@{
  183. schemaVersion = 1
  184. generatedAt = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
  185. packages = $sorted
  186. }
  187. $json = $index | ConvertTo-Json -Depth 10
  188. Invoke-WithRetry { [System.IO.File]::WriteAllText($indexPath, $json, (New-Object System.Text.UTF8Encoding($false))) }
  189. Write-Host "REGEN DONE: $($sorted.Count) package(s) -> $indexPath"
  190. }
  191. # Serialize parallel post-build invocations within this session.
  192. $mutex = New-Object System.Threading.Mutex($false, 'Local\TeamAAS-regen-gallery-index')
  193. $hasHandle = $false
  194. try {
  195. if (-not $mutex.WaitOne([TimeSpan]::FromMinutes(5))) {
  196. throw 'timed out waiting for gallery index lock'
  197. }
  198. $hasHandle = $true
  199. Invoke-Regenerate
  200. } finally {
  201. if ($hasHandle) { $mutex.ReleaseMutex() }
  202. $mutex.Dispose()
  203. }