1
0

MakeSourcesArchive.ps1 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Param(
  2. [Parameter(Mandatory=$true)] [string]$ProjectRoot,
  3. [Parameter(Mandatory=$true)] [string]$VersionsDir,
  4. [Parameter(Mandatory=$true)] [string]$Timestamp,
  5. [Parameter(Mandatory=$true)] [string]$DeployZip
  6. )
  7. Set-StrictMode -Version Latest
  8. $ErrorActionPreference = 'Stop'
  9. function Get-RelPath($base, $full){
  10. return Resolve-Path -LiteralPath $full | ForEach-Object { $_.Path.Substring((Resolve-Path $base).Path.Length).TrimStart('\','/') }
  11. }
  12. if(!(Test-Path $VersionsDir)){ New-Item -ItemType Directory -Force -Path $VersionsDir | Out-Null }
  13. $srcZip = Join-Path $VersionsDir ("waouw-deploy-$Timestamp-src.zip")
  14. $srcReport = Join-Path $VersionsDir ("waouw-deploy-$Timestamp-src.txt")
  15. # Build file list with exclusions
  16. $excludes = @(
  17. '(^|[\\/])Versions([\\/]|$)',
  18. 'version_counter.txt$',
  19. '\\.git(\\|$)',
  20. '\\.vscode(\\|$)',
  21. '\\.idea(\\|$)',
  22. '\\node_modules(\\|$)',
  23. '\\dist(\\|$)',
  24. '\\__pycache__(\\|$)',
  25. '\\.pytest_cache(\\|$)',
  26. '\\.mypy_cache(\\|$)',
  27. '\\backend\\secure(\\|$)',
  28. '\\backend\\logs(\\|$)',
  29. '\\tools(\\|$)',
  30. '\\.DS_Store$'
  31. )
  32. $allFiles = Get-ChildItem -LiteralPath $ProjectRoot -Recurse -File -Force
  33. $files = $allFiles | Where-Object {
  34. $rel = Get-RelPath $ProjectRoot $_.FullName
  35. if ($rel -match '(^|[\\/])Versions([\\/]|$)') { return $false }
  36. -not ($excludes | Where-Object { $rel -match $_ })
  37. }
  38. # Write zip
  39. if(Test-Path $srcZip){ Remove-Item -LiteralPath $srcZip -Force }
  40. Add-Type -AssemblyName System.IO.Compression.FileSystem
  41. $zip = [System.IO.Compression.ZipFile]::Open($srcZip, 'Create')
  42. foreach($f in $files){
  43. $rel = Get-RelPath $ProjectRoot $f.FullName
  44. $rel = $rel -replace '\\','/'
  45. [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $f.FullName, $rel, [System.IO.Compression.CompressionLevel]::Optimal) | Out-Null
  46. }
  47. $zip.Dispose()
  48. # Compute hashes for diff
  49. function HashMapFromZip($zipPath){
  50. $map = @{}
  51. if(!(Test-Path $zipPath)){ return $map }
  52. $tmp = New-Item -ItemType Directory -Force -Path ([System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetRandomFileName()))
  53. Add-Type -AssemblyName System.IO.Compression.FileSystem
  54. [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $tmp.FullName)
  55. $files = Get-ChildItem -LiteralPath $tmp.FullName -Recurse -File
  56. foreach($f in $files){
  57. $rel = Get-RelPath $tmp.FullName $f.FullName
  58. $hash = (Get-FileHash -Algorithm SHA256 -LiteralPath $f.FullName).Hash
  59. $map[$rel] = $hash
  60. }
  61. Remove-Item -Recurse -Force -LiteralPath $tmp.FullName
  62. return $map
  63. }
  64. $prevSrcZip = Get-ChildItem -LiteralPath $VersionsDir -Filter '*-src.zip' | Sort-Object LastWriteTime -Descending | Select-Object -Skip 1 -First 1
  65. $prevMap = @{}
  66. if($prevSrcZip){ $prevMap = HashMapFromZip $prevSrcZip.FullName }
  67. $curMap = @{}
  68. $tmpCur = New-Item -ItemType Directory -Force -Path ([System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetRandomFileName()))
  69. [System.IO.Compression.ZipFile]::ExtractToDirectory($srcZip, $tmpCur.FullName)
  70. (Get-ChildItem -LiteralPath $tmpCur.FullName -Recurse -File) | ForEach-Object {
  71. $rel = Get-RelPath $tmpCur.FullName $_.FullName
  72. $curMap[$rel] = (Get-FileHash -Algorithm SHA256 -LiteralPath $_.FullName).Hash
  73. }
  74. Remove-Item -Recurse -Force -LiteralPath $tmpCur.FullName
  75. $added = New-Object System.Collections.Generic.List[string]
  76. $removed = New-Object System.Collections.Generic.List[string]
  77. $modified = New-Object System.Collections.Generic.List[string]
  78. $allKeys = ($prevMap.Keys + $curMap.Keys) | Sort-Object -Unique
  79. foreach($k in $allKeys){
  80. if(-not $prevMap.ContainsKey($k) -and $curMap.ContainsKey($k)){ $added.Add($k) | Out-Null }
  81. elseif($prevMap.ContainsKey($k) -and -not $curMap.ContainsKey($k)){ $removed.Add($k) | Out-Null }
  82. else{ if($prevMap[$k] -ne $curMap[$k]){ $modified.Add($k) | Out-Null } }
  83. }
  84. $baseCompare = if ($null -ne $prevSrcZip) { $prevSrcZip.Name } else { '' }
  85. $header = @(
  86. "Version: $Timestamp",
  87. "DeployZip: $DeployZip",
  88. "SourcesZip: $srcZip",
  89. "GeneratedAt: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')",
  90. "BaseCompare: $baseCompare"
  91. ) -join "`r`n"
  92. $content = @()
  93. $content += $header
  94. $content += ""
  95. $content += "[Added]"
  96. $content += ($added | Sort-Object)
  97. $content += ""
  98. $content += "[Removed]"
  99. $content += ($removed | Sort-Object)
  100. $content += ""
  101. $content += "[Modified]"
  102. $content += ($modified | Sort-Object)
  103. Set-Content -LiteralPath $srcReport -Value $content -Encoding UTF8
  104. Write-Host "[OK] Sources archive: $srcZip"
  105. Write-Host "[OK] Report: $srcReport"