|
| 1 | +param( |
| 2 | + [string]$ExePath |
| 3 | +) |
| 4 | + |
| 5 | +$ErrorActionPreference = 'Stop' |
| 6 | + |
| 7 | +Write-Host "==== Run $(Get-Date -Format 'MM/dd/yyyy HH:mm:ss') ====" |
| 8 | + |
| 9 | +if (-not $ExePath) { |
| 10 | + Write-Error "ExePath parameter is required." |
| 11 | + exit 1 |
| 12 | +} |
| 13 | + |
| 14 | +if (-not (Test-Path -LiteralPath $ExePath)) { |
| 15 | + Write-Error "ExePath does not exist: $ExePath" |
| 16 | + exit 1 |
| 17 | +} |
| 18 | + |
| 19 | +# Resolve full path and output folder |
| 20 | +$ExePath = (Resolve-Path -LiteralPath $ExePath).ProviderPath |
| 21 | +$OutputDir = Split-Path -Path $ExePath -Parent # no -LiteralPath here |
| 22 | + |
| 23 | +Write-Host "ExePath = $ExePath" |
| 24 | +Write-Host "OutputDir = $OutputDir" |
| 25 | + |
| 26 | +# Read version info from the EXE |
| 27 | +$versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($ExePath) |
| 28 | +$rawVersion = $versionInfo.FileVersion |
| 29 | + |
| 30 | +Write-Host "Raw version = $rawVersion" |
| 31 | + |
| 32 | +# Try to parse the version string |
| 33 | +try { |
| 34 | + $parsed = [version]$rawVersion |
| 35 | +} catch { |
| 36 | + Write-Warning "Could not parse version '$rawVersion'. Falling back to 0.0.1." |
| 37 | + $parsed = [version]'0.0.1.0' |
| 38 | +} |
| 39 | + |
| 40 | +$major = $parsed.Major |
| 41 | +$minor = $parsed.Minor |
| 42 | +$revision = $parsed.Revision |
| 43 | +if ($revision -lt 0) { $revision = 0 } |
| 44 | + |
| 45 | +Write-Host "Parsed: Major=$major Minor=$minor Revision=$revision" |
| 46 | + |
| 47 | +# Format like 2.30.01 (Major.Minor.Revision(2 digits)) |
| 48 | +$formattedVersion = '{0}.{1}.{2:00}' -f $major, $minor, $revision |
| 49 | +Write-Host "Formatted = $formattedVersion" |
| 50 | + |
| 51 | +# Build zip path based on EXE name |
| 52 | +$exeNameNoExt = [System.IO.Path]::GetFileNameWithoutExtension($ExePath) |
| 53 | +$zipName = "{0}_{1}.zip" -f $exeNameNoExt, $formattedVersion |
| 54 | +$zipPath = Join-Path -Path $OutputDir -ChildPath $zipName |
| 55 | + |
| 56 | +Write-Host "ZipPath = $zipPath" |
| 57 | + |
| 58 | +# Exclusions |
| 59 | +$excludeNames = @( |
| 60 | + 'CompactJson.xml', |
| 61 | + 'CompactJSON.xml' |
| 62 | +) |
| 63 | + |
| 64 | +$excludeExts = @( |
| 65 | + '.zip', |
| 66 | + '.log' |
| 67 | +) |
| 68 | + |
| 69 | +# Collect files to include (publish folder + all subfolders) |
| 70 | +$files = Get-ChildItem -Path $OutputDir -Recurse -File | |
| 71 | + Where-Object { |
| 72 | + ($_.Extension -notin $excludeExts) -and |
| 73 | + ($excludeNames -notcontains $_.Name) |
| 74 | + } |
| 75 | + |
| 76 | +$filesCount = $files.Count |
| 77 | +Write-Host "Files to zip (after filter): $filesCount" |
| 78 | + |
| 79 | +if ($filesCount -eq 0) { |
| 80 | + Write-Warning "No files found to archive after filtering. Aborting." |
| 81 | + exit 1 |
| 82 | +} |
| 83 | + |
| 84 | +# Use .NET's System.IO.Compression instead of Compress-Archive |
| 85 | +Add-Type -AssemblyName System.IO.Compression.FileSystem |
| 86 | + |
| 87 | +# Pick maximum compression if available |
| 88 | +$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal |
| 89 | +try { |
| 90 | + $smallest = [System.IO.Compression.CompressionLevel]::SmallestSize |
| 91 | + if ($smallest -ne $null) { |
| 92 | + $compressionLevel = $smallest |
| 93 | + } |
| 94 | +} catch { |
| 95 | + # Older frameworks won't have SmallestSize; stay on Optimal |
| 96 | +} |
| 97 | + |
| 98 | +# Delete existing zip if present |
| 99 | +if (Test-Path -LiteralPath $zipPath) { |
| 100 | + Write-Host "Removing existing archive..." |
| 101 | + Remove-Item -LiteralPath $zipPath -Force |
| 102 | +} |
| 103 | + |
| 104 | +Write-Host "Creating archive via System.IO.Compression with compression level: $compressionLevel" |
| 105 | + |
| 106 | +$zip = $null |
| 107 | +$archiveSucceeded = $false |
| 108 | + |
| 109 | +try { |
| 110 | + $zip = [System.IO.Compression.ZipFile]::Open( |
| 111 | + $zipPath, |
| 112 | + [System.IO.Compression.ZipArchiveMode]::Create |
| 113 | + ) |
| 114 | + |
| 115 | + foreach ($file in $files) { |
| 116 | + # relative path inside zip |
| 117 | + $relativePath = [System.IO.Path]::GetRelativePath($OutputDir, $file.FullName) |
| 118 | + |
| 119 | + Write-Host " Adding $relativePath" |
| 120 | + |
| 121 | + [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile( |
| 122 | + $zip, |
| 123 | + $file.FullName, |
| 124 | + $relativePath, |
| 125 | + $compressionLevel |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + $archiveSucceeded = $true |
| 130 | + Write-Host "Archive created successfully." |
| 131 | +} |
| 132 | +catch { |
| 133 | + Write-Error "Failed to create archive: $($_.Exception.Message)" |
| 134 | + if (Test-Path -LiteralPath $zipPath) { |
| 135 | + Write-Host "Removing incomplete archive..." |
| 136 | + Remove-Item -LiteralPath $zipPath -Force |
| 137 | + } |
| 138 | + exit 1 |
| 139 | +} |
| 140 | +finally { |
| 141 | + if ($zip -ne $null) { |
| 142 | + $zip.Dispose() |
| 143 | + } |
| 144 | +} |
0 commit comments