| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <#
- .SYNOPSIS
- 按【发布仓里真实存在的包】重算 index.json 的 size / sha256。
- .DESCRIPTION
- 为什么必须有这一步:
- 构建机上的 gallery 源(TeamAAS-2.1\gallery)与发布仓(本目录)是两个独立的目录,
- 长期各自打包、各自推送 —— 两边同名包常常不是同一份文件。
- 而打包时会按【本地源】全量 REGEN 索引,若把这份索引直接推到发布仓,
- 客户端就会拿本地源记录的 size/sha256 去校验发布仓里的另一个文件,
- 安装时报 "大小不匹配:期望 X 字节,实际 Y 字节"。
- 因此:每次往发布仓放包之后、提交索引之前,必须跑一次本脚本,
- 让索引里的 size/sha256 与发布仓里实际的文件严格一致。
- 用法:
- .\sync-index.ps1 # 校正并打印改动数量
- .\sync-index.ps1 -Check # 只检查,不写文件(适合在提交前做校验/输出不符条目数)
- .NOTES
- 只改 size / sha256 两个字段,其余元数据(名称/描述/依赖/版本历史)保持原样。
- 编码按原文件的 BOM 状态原样写回,避免把 UTF-8 内容写坏。
- #>
- param(
- [switch]$Check
- )
- $ErrorActionPreference = 'Stop'
- $root = $PSScriptRoot
- $idxPath = Join-Path $root 'index.json'
- if (-not (Test-Path $idxPath)) { throw "未找到索引文件:$idxPath" }
- # 原样保持 BOM 状态
- $bytes = [IO.File]::ReadAllBytes($idxPath)
- $hasBom = ($bytes.Length -ge 3 -and $bytes[0] -eq 239 -and $bytes[1] -eq 187 -and $bytes[2] -eq 191)
- $text = [IO.File]::ReadAllText($idxPath, [Text.Encoding]::UTF8)
- # 发布仓里真实存在的包
- $files = @{}
- foreach ($p in (Get-ChildItem (Join-Path $root 'packages') -Recurse -Filter '*.zip' -ErrorAction SilentlyContinue)) {
- $rel = $p.FullName.Substring($root.Length).TrimStart('\', '/') -replace '\\', '/'
- $files[$rel] = $p
- }
- # 索引里引用到的包路径
- $refs = [regex]::Matches($text, '"downloadUrl":\s*"([^"]+)"') |
- ForEach-Object { $_.Groups[1].Value } | Sort-Object -Unique
- $fixed = 0
- $missing = @()
- foreach ($rel in $refs) {
- if (-not $files.ContainsKey($rel)) { $missing += $rel; continue }
- $f = $files[$rel]
- $sha = (Get-FileHash $f.FullName -Algorithm SHA256).Hash.ToLower()
- $size = $f.Length
- $esc = [regex]::Escape($rel)
- # 结构固定为:downloadUrl → sha256 → size
- $pattern = '("downloadUrl":\s*"' + $esc + '",\s*"sha256":\s*")[0-9a-fA-F]*(",\s*"size":\s*)\d+'
- $new = [regex]::Replace($text, $pattern, ('${1}' + $sha + '${2}' + $size))
- if ($new -ne $text) { $fixed++ }
- $text = $new
- }
- if ($Check) {
- Write-Host ("不符条目: " + $fixed + " 处")
- } else {
- [IO.File]::WriteAllText($idxPath, $text, (New-Object Text.UTF8Encoding($hasBom)))
- Write-Host ("索引已按发布仓实际包校正: " + $fixed + " 处(BOM=" + $hasBom + ")")
- }
- if ($missing.Count -gt 0) {
- Write-Host "警告:索引中有 $($missing.Count) 个条目指向发布仓不存在的包(客户端安装会 404):" -ForegroundColor Yellow
- $missing | Sort-Object -Unique | ForEach-Object { Write-Host (" " + $_) -ForegroundColor Yellow }
- }
- if ($Check -and $fixed -gt 0) { exit 1 }
- exit 0
|