Files
xbcr/scripts/pack_zip.ps1
T

66 lines
3.5 KiB
PowerShell

# 源码分发包:只包含代码、模板、必要静态资源和项目说明。
# 用法: pwsh -File scripts/pack_zip.ps1
$ErrorActionPreference = 'Stop'
$src = [IO.Path]::GetFullPath((Split-Path -Parent $PSScriptRoot))
$zip = Join-Path $src '1.zip'
$pendingZip = Join-Path $src '1.zip.tmp'
$include = @(
'.gitignore','LICENSE','README.md','app.py','db.py','campus.py','alerts.py','subscribe.py',
'email_utils.py','email_verify.py','generate_icons.py','map_cache.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')
$extensions = @('.py','.js','.cjs','.mjs','.css','.html','.ps1','.json',
'.svg','.png','.ico','.woff','.woff2','.ttf')
$files = [Collections.Generic.List[object]]::new()
foreach ($name in $include) {
$file = Join-Path $src $name
if (Test-Path -LiteralPath $file -PathType Leaf) { $files.Add((Get-Item -LiteralPath $file)) }
}
foreach ($dir in $dirs) {
$path = Join-Path $src $dir
if (-not (Test-Path -LiteralPath $path -PathType Container)) { continue }
Get-ChildItem -LiteralPath $path -Recurse -File | ForEach-Object {
$relative = [IO.Path]::GetRelativePath($src, $_.FullName).Replace('\','/')
if ($relative -match '(^|/)(__pycache__|\.pytest_cache|\.reasonix|backups|node_modules)(/|$)') { return }
if ($_.Name.StartsWith('.') -or $_.Name.StartsWith('_preview') -or $_.Name -eq '_layout_preview.html') { return }
if ($extensions -notcontains $_.Extension.ToLowerInvariant()) { return }
$files.Add($_)
}
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
try {
$stream = [IO.File]::Open($pendingZip, [IO.FileMode]::Create)
$archive = [IO.Compression.ZipArchive]::new($stream, [IO.Compression.ZipArchiveMode]::Create)
try {
foreach ($file in $files) {
$relative = [IO.Path]::GetRelativePath($src, $file.FullName).Replace('\','/')
[IO.Compression.ZipFileExtensions]::CreateEntryFromFile(
$archive, $file.FullName, $relative, [IO.Compression.CompressionLevel]::Optimal) | Out-Null
}
} finally { $archive.Dispose(); $stream.Dispose() }
$check = [IO.Compression.ZipFile]::OpenRead($pendingZip)
try {
$names = @($check.Entries | Select-Object -ExpandProperty FullName)
$required = @('app.py','db.py','map_cache.py','campus.py','alerts.py','subscribe.py',
'routes/export.py','routes/subscribe.py','routes/rental.py','routes/admin.py',
'templates/map.html','templates/admin.html','static/js/map.js',
'static/js/bike-clusters.js','static/js/settings-pages.js','static/css/bike-clusters.css','static/css/map-settings.css','requirements.txt')
$missing = @($required | Where-Object { $names -notcontains $_ })
if ($missing.Count) { throw "打包缺文件: $($missing -join ', ')" }
$unexpected = @($names | Where-Object {
$_ -match '(^|/)(downloads|artifacts|backups|logs|temp|sessions|__pycache__|\.git)/|\.(db|pem|key|crt|exe|zip|log|pyc)$|(^|/)(token_config|smtp_config|admin_config|secret_key)\.'
})
if ($unexpected.Count) { throw "压缩包包含非源码文件: $($unexpected -join ', ')" }
} finally { $check.Dispose() }
Move-Item -LiteralPath $pendingZip -Destination $zip -Force
$sizeKB = [math]::Round((Get-Item -LiteralPath $zip).Length / 1KB, 1)
Write-Host "1.zip 完成:$($names.Count) 个文件,$sizeKB KB(仅源码与必要资源)"
} finally {
if (Test-Path -LiteralPath $pendingZip) { Remove-Item -LiteralPath $pendingZip -Force }
}