$ErrorActionPreference = "Stop" # Plugin folder name (slug) $pluginSlug = Split-Path -Leaf (Get-Location) # Try to read Version: from the main plugin file (best guess: the file that contains "Plugin Name:") $mainFile = Get-ChildItem -Filter "*.php" | Where-Object { (Get-Content $_.FullName -TotalCount 40 | Select-String -SimpleMatch "Plugin Name:") -ne $null } | Select-Object -First 1 if (-not $mainFile) { # Fallback: first php file in root $mainFile = Get-ChildItem -Filter "*.php" | Select-Object -First 1 } $version = "dev" if ($mainFile) { $match = Select-String -Path $mainFile.FullName -Pattern "Version:\s*(.+)" -ErrorAction SilentlyContinue | Select-Object -First 1 if ($match) { $version = $match.Matches.Groups[1].Value.Trim() } } $zipName = "$pluginSlug-$version.zip" # Temp staging directory $stagingRoot = Join-Path $env:TEMP "$pluginSlug-build" $stagingPlugin = Join-Path $stagingRoot $pluginSlug if (Test-Path $stagingRoot) { Remove-Item $stagingRoot -Recurse -Force } New-Item -ItemType Directory -Path $stagingPlugin | Out-Null # Excludes $excludeDirs = @( ".git", ".cursor", ".curson", "node_modules", "vendor", "tests", "build", "dist" ) $excludeFiles = @( "*.zip", "*.log", "build.ps1", ".gitlab-ci.yml", "composer.json", "composer.lock", "phpcs.xml", ".gitignore", "phpcs.xml.dist", ".gitattributes" ) Write-Host "Staging files into: $stagingPlugin" # Use robocopy to stage files (folder structure preserved) robocopy . $stagingPlugin /E ` /XD $excludeDirs ` /XF $excludeFiles ` /NFL /NDL /NJH /NJS /NP | Out-Null # Build zip with forward slashes in entry names Add-Type -AssemblyName System.IO.Compression Add-Type -AssemblyName System.IO.Compression.FileSystem if (Test-Path $zipName) { Remove-Item $zipName -Force } $zipPath = Join-Path (Get-Location) $zipName $zipStream = [System.IO.File]::Open($zipPath, [System.IO.FileMode]::Create) $zip = New-Object System.IO.Compression.ZipArchive($zipStream, [System.IO.Compression.ZipArchiveMode]::Create, $false) try { $files = Get-ChildItem -Path $stagingRoot -Recurse -File foreach ($f in $files) { # Compute relative path inside zip, and normalize to forward slashes $rel = $f.FullName.Substring($stagingRoot.Length).TrimStart('\','/') $entryName = $rel -replace '\\','/' $entry = $zip.CreateEntry($entryName, [System.IO.Compression.CompressionLevel]::Optimal) $entryStream = $entry.Open() try { $src = [System.IO.File]::OpenRead($f.FullName) try { $src.CopyTo($entryStream) } finally { $src.Dispose() } } finally { $entryStream.Dispose() } } } finally { $zip.Dispose() $zipStream.Dispose() } Remove-Item $stagingRoot -Recurse -Force Write-Host "Build complete: $zipName"