Files
xbcr/scripts/pack_update.ps1

117 lines
4.9 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
param([switch]$Full)
# 增量更新包生成(update.zip)
# 用法: pwsh -File scripts/pack_update.ps1 → 增量 update.zip(不含 160MB 安装包)
# pwsh -File scripts/pack_update.ps1 -Full → 全量 1.zip(含安装包)
# 只打包「相对上次基准后有变化/新增」的文件 —— 用于覆盖式部署,避免重传 60MB 安装包等未变动内容。
#
# 基准机制:
# scripts/.pack_baseline.json 记录上次打包时每个文件的 (路径, 大小, 哈希)。
# 没有基准 = 首次全量(等价 1.zip,但不包含 downloads/*.exe 大文件——安装包基本不变,
# 如需全量含安装包用 -Full 开关)。
# 删除处理:基准里有、现在没有的文件会写入 update.zip 根的 _deleted.txt(部署时按行删)。
# 部署方式:解压覆盖到项目根即可(zip 内保留相对路径)。
$ErrorActionPreference = 'Stop'
$src = Split-Path -Parent $PSScriptRoot
$outZip = if ($Full) { Join-Path $src "1.zip" } else { Join-Path $src "update.zip" }
$baselinePath = Join-Path $PSScriptRoot ".pack_baseline.json"
$stage = Join-Path $env:TEMP "xbniu_update_stage"
$include = @(
'.gitignore','LICENSE','README.md','app.py','db.py','campus.py','alerts.py','subscribe.py',
'email_utils.py','email_verify.py','generate_icons.py','pytest.ini','rate_limit.py',
'rental_mail.py','requirements.txt','server_config.json','timer.py',
'update_creds_from_capture.py','wechat.py'
)
$dirs = @('routes','templates','static','scripts','tests')
$bigDirs = @('downloads') # 大文件(160MB 安装包),仅 -Full 时纳入
function Get-PackFiles {
param([switch]$WithBig)
$files = @()
foreach ($f in $include) {
$p = Join-Path $src $f
if (Test-Path $p) { $files += (Get-Item $p).FullName }
}
$allDirs = $dirs + $(if ($WithBig) { $bigDirs } else { @() })
foreach ($d in $allDirs) {
$s = Join-Path $src $d
if (-not (Test-Path $s)) { continue }
Get-ChildItem $s -Recurse -File | ForEach-Object {
$rel = $_.FullName.Substring($src.Length).TrimStart('\')
if ($rel -match '(^|[\\/])(__pycache__|\.pytest_cache|\.reasonix|backups)([\\/]|$)') { return }
if ($rel -match '\.(pyc|pyo|log|tmp|bak|zip)$') { return }
if ((Split-Path $rel -Leaf) -in @('.gitignore', '.pack_baseline.json')) { return }
$files += $_.FullName
}
}
return $files
}
function Get-FileKey($file) {
$item = Get-Item $file
$hash = (Get-FileHash $file -Algorithm SHA256).Hash
return @{ path = $file.Substring($src.Length + 1).Replace('\', '/'); size = $item.Length; hash = $hash }
}
$hasBaseline = (Test-Path $baselinePath) -and (-not $Full)
$baseline = @{}
if ($hasBaseline) {
try { $baseline = @(Get-Content $baselinePath -Raw -Encoding UTF8 | ConvertFrom-Json) } catch { $hasBaseline = $false }
}
# 当前全部打包文件(update 不含大文件;-Full 全量含大文件)
$current = Get-PackFiles -WithBig:$Full
$changed = @()
foreach ($f in $current) {
$key = Get-FileKey $f
$old = $baseline | Where-Object { $_.path -eq $key.path }
if (-not $hasBaseline -or -not $old -or $old.hash -ne $key.hash -or $old.size -ne $key.size) {
$changed += $f
}
}
# 被删除的文件(基准有、现在没有)
$deleted = @()
if ($hasBaseline) {
$currentRel = @($current | ForEach-Object { $_.Substring($src.Length + 1).Replace('\', '/') })
foreach ($old in $baseline) {
if ($currentRel -notcontains $old.path) { $deleted += $old.path }
}
}
if ($changed.Count -eq 0 -and $deleted.Count -eq 0) {
Write-Host "✓ 无任何变化,不需要生成更新包"
exit 0
}
# 建 stage(保持相对路径)
Remove-Item $stage -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path $stage | Out-Null
foreach ($f in $changed) {
$rel = $f.Substring($src.Length + 1)
$target = Join-Path $stage $rel
$targetDir = Split-Path $target -Parent
if (-not (Test-Path $targetDir)) { New-Item -ItemType Directory -Path $targetDir -Force | Out-Null }
Copy-Item -LiteralPath $f -Destination $target -Force
}
if ($deleted.Count) {
Set-Content -Path (Join-Path $stage "_deleted.txt") -Value $deleted -Encoding UTF8
}
# 打包
Add-Type -AssemblyName System.IO.Compression.FileSystem
if (Test-Path $outZip) { Remove-Item $outZip -Force }
[System.IO.Compression.ZipFile]::CreateFromDirectory($stage, $outZip, [System.IO.Compression.CompressionLevel]::Optimal, $false)
# 更新基准
$allKeys = @()
foreach ($f in (Get-PackFiles -WithBig:$Full)) { $allKeys += Get-FileKey $f }
$allKeys | ConvertTo-Json -Depth 3 | Set-Content -Path $baselinePath -Encoding UTF8
$sizeKB = [math]::Round((Get-Item $outZip).Length/1KB, 0)
$mode = if ($Full) { '全量(1.zip)' } else { '增量(update.zip)' }
Write-Host "✓ $mode 打包完成: $($changed.Count) 个文件更新, $($deleted.Count) 个删除, $sizeKB KB"
Write-Host " 变更: $(($changed | ForEach-Object { $_.Substring($src.Length + 1) }) -join ', ')"
if ($deleted.Count) { Write-Host " 删除: $($deleted -join ', ')" }
Remove-Item $stage -Recurse -Force