Files
LanMountainDesktop/scripts/Generate-DeltaPackage.ps1

230 lines
6.5 KiB
PowerShell
Raw Normal View History

2026-04-16 01:59:21 +08:00
param(
[Parameter(Mandatory = $true)]
2026-04-16 01:59:21 +08:00
[string]$PreviousVersion,
[Parameter(Mandatory = $true)]
2026-04-16 01:59:21 +08:00
[string]$CurrentVersion,
[Parameter(Mandatory = $true)]
2026-04-16 01:59:21 +08:00
[string]$PreviousDir,
[Parameter(Mandatory = $true)]
2026-04-16 01:59:21 +08:00
[string]$CurrentDir,
[Parameter(Mandatory = $true)]
2026-04-16 01:59:21 +08:00
[string]$OutputDir
)
$ErrorActionPreference = "Stop"
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Get-NormalizedRelativePath {
param(
[Parameter(Mandatory = $true)]
[string]$RootDir,
[Parameter(Mandatory = $true)]
[string]$FullPath
)
$separator = [System.IO.Path]::DirectorySeparatorChar
$altSeparator = [System.IO.Path]::AltDirectorySeparatorChar
$root = [System.IO.Path]::GetFullPath($RootDir).Replace($altSeparator, $separator).TrimEnd($separator)
$path = [System.IO.Path]::GetFullPath($FullPath).Replace($altSeparator, $separator)
$comparison = if ($separator -eq '\') {
[System.StringComparison]::OrdinalIgnoreCase
}
else {
[System.StringComparison]::Ordinal
}
$rootWithSeparator = "$root$separator"
if ($path.StartsWith($rootWithSeparator, $comparison)) {
$relative = $path.Substring($rootWithSeparator.Length)
}
elseif ($path.Equals($root, $comparison)) {
$relative = ""
}
else {
throw "File path '$path' is not under root '$root'."
}
return $relative.Replace('\', '/')
}
2026-04-16 01:59:21 +08:00
function Get-FileSha256Hex {
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
2026-04-16 01:59:21 +08:00
return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()
2026-04-16 01:59:21 +08:00
}
function Get-FileManifest {
param(
[Parameter(Mandatory = $true)]
[string]$RootDir
)
if (-not (Test-Path -LiteralPath $RootDir)) {
throw "Directory does not exist: $RootDir"
}
$resolvedRoot = (Resolve-Path -LiteralPath $RootDir).Path
2026-04-16 01:59:21 +08:00
$manifest = @{}
$files = Get-ChildItem -LiteralPath $resolvedRoot -Recurse -File
2026-04-16 01:59:21 +08:00
foreach ($file in $files) {
$relativePath = Get-NormalizedRelativePath -RootDir $resolvedRoot -FullPath $file.FullName
$manifest[$relativePath] = [ordered]@{
2026-04-16 01:59:21 +08:00
Path = $relativePath
Sha256 = Get-FileSha256Hex -Path $file.FullName
Size = [long]$file.Length
2026-04-16 01:59:21 +08:00
}
}
2026-04-16 01:59:21 +08:00
return $manifest
}
function New-DeltaArchive {
param(
[Parameter(Mandatory = $true)]
[string]$ZipPath,
[Parameter(Mandatory = $true)]
[string]$CurrentRoot,
[Parameter(Mandatory = $false)]
[AllowEmptyCollection()]
[object[]]$ChangedFiles = @()
)
if (Test-Path -LiteralPath $ZipPath) {
Remove-Item -LiteralPath $ZipPath -Force
}
$zip = [System.IO.Compression.ZipFile]::Open($ZipPath, [System.IO.Compression.ZipArchiveMode]::Create)
try {
foreach ($file in $ChangedFiles) {
$sourcePath = Join-Path $CurrentRoot $file.Path
if (-not (Test-Path -LiteralPath $sourcePath)) {
throw "Changed file was not found while building archive: $sourcePath"
}
2026-04-16 01:59:21 +08:00
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile(
$zip,
$sourcePath,
$file.Path,
[System.IO.Compression.CompressionLevel]::Optimal
) | Out-Null
}
}
finally {
$zip.Dispose()
}
2026-04-18 00:49:03 +08:00
}
Write-Host "Generating incremental package..."
Write-Host "From: $PreviousVersion"
Write-Host "To: $CurrentVersion"
Write-Host "Prev: $PreviousDir"
Write-Host "Curr: $CurrentDir"
Write-Host "Out: $OutputDir"
New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null
$previousManifest = Get-FileManifest -RootDir $PreviousDir
2026-04-16 01:59:21 +08:00
$currentManifest = Get-FileManifest -RootDir $CurrentDir
$changedFiles = @()
$reusedFiles = @()
$deletedFiles = @()
foreach ($path in ($currentManifest.Keys | Sort-Object)) {
2026-04-16 01:59:21 +08:00
$currentFile = $currentManifest[$path]
2026-04-16 01:59:21 +08:00
if ($previousManifest.ContainsKey($path)) {
$previousFile = $previousManifest[$path]
if ($currentFile.Sha256 -eq $previousFile.Sha256) {
$reusedFiles += [ordered]@{
2026-04-16 01:59:21 +08:00
Path = $path
Action = "reuse"
Sha256 = $currentFile.Sha256
Size = $currentFile.Size
}
}
else {
$changedFiles += [ordered]@{
2026-04-16 01:59:21 +08:00
Path = $path
Action = "replace"
Sha256 = $currentFile.Sha256
Size = $currentFile.Size
ArchivePath = $path
}
}
}
else {
$changedFiles += [ordered]@{
2026-04-16 01:59:21 +08:00
Path = $path
Action = "add"
Sha256 = $currentFile.Sha256
Size = $currentFile.Size
ArchivePath = $path
}
}
}
foreach ($path in ($previousManifest.Keys | Sort-Object)) {
2026-04-16 01:59:21 +08:00
if (-not $currentManifest.ContainsKey($path)) {
$deletedFiles += [ordered]@{
2026-04-16 01:59:21 +08:00
Path = $path
Action = "delete"
}
}
}
Write-Host "Changed: $($changedFiles.Count)"
Write-Host "Reused: $($reusedFiles.Count)"
Write-Host "Deleted: $($deletedFiles.Count)"
2026-04-16 01:59:21 +08:00
$resolvedCurrentDir = (Resolve-Path -LiteralPath $CurrentDir).Path
2026-04-18 00:49:03 +08:00
$updateZipPath = Join-Path $OutputDir "update.zip"
New-DeltaArchive -ZipPath $updateZipPath -CurrentRoot $resolvedCurrentDir -ChangedFiles $changedFiles
2026-04-16 01:59:21 +08:00
$deltaZipPath = Join-Path $OutputDir ("delta-{0}-to-{1}.zip" -f $PreviousVersion, $CurrentVersion)
Copy-Item -LiteralPath $updateZipPath -Destination $deltaZipPath -Force
2026-04-18 00:49:03 +08:00
$allEntries = @($changedFiles + $reusedFiles + $deletedFiles)
$filesJson = [ordered]@{
2026-04-16 01:59:21 +08:00
FromVersion = $PreviousVersion
ToVersion = $CurrentVersion
GeneratedAt = [DateTimeOffset]::UtcNow.ToString("o")
Files = $allEntries
2026-04-16 01:59:21 +08:00
}
$jsonText = $filesJson | ConvertTo-Json -Depth 10
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
2026-04-16 01:59:21 +08:00
$filesJsonPath = Join-Path $OutputDir "files.json"
[System.IO.File]::WriteAllText($filesJsonPath, $jsonText, $utf8NoBom)
2026-04-16 01:59:21 +08:00
$versionedFilesJsonPath = Join-Path $OutputDir ("files-{0}.json" -f $CurrentVersion)
Copy-Item -LiteralPath $filesJsonPath -Destination $versionedFilesJsonPath -Force
2026-04-18 00:49:03 +08:00
$updateSizeBytes = (Get-Item -LiteralPath $updateZipPath).Length
$updateSizeMb = [Math]::Round($updateSizeBytes / 1MB, 2)
2026-04-16 01:59:21 +08:00
Write-Host ""
Write-Host "Done."
Write-Host "update.zip size: $updateSizeMb MB"
Write-Host "Generated:"
Write-Host " $updateZipPath"
Write-Host " $filesJsonPath"
Write-Host " $deltaZipPath"
Write-Host " $versionedFilesJsonPath"