Files
design-patterns/run-structural.ps1

62 lines
2.0 KiB
PowerShell

$JAVAC = 'C:\Program Files\Eclipse Adoptium\jdk-17.0.7.7-hotspot\bin\javac.exe'
$JAVA = 'C:\Program Files\Eclipse Adoptium\jdk-17.0.7.7-hotspot\bin\java.exe'
$BASE = 'C:\Users\Ankur\Claude\Projects\@ankurm Blog\design-patterns'
$OUTDIR = "$BASE\screenshots"
New-Item -Force -ItemType Directory $OUTDIR | Out-Null
$patterns = @(
@{dir="adapter"; main="adapter.Main"},
@{dir="bridge"; main="bridge.Main"},
@{dir="composite"; main="composite.Main"},
@{dir="decorator"; main="decorator.Main"},
@{dir="facade"; main="facade.Main"},
@{dir="flyweight"; main="flyweight.Main"},
@{dir="proxy"; main="proxy.Main"}
)
$allOk = $true
foreach ($p in $patterns) {
$label = $p.dir.ToUpper()
Write-Host "`n=== $label ===" -ForegroundColor Cyan
$srcDir = "$BASE\02-structural\$($p.dir)"
$outDir = "$BASE\out\$($p.dir)"
New-Item -Force -ItemType Directory $outDir | Out-Null
$files = @(Get-ChildItem "$srcDir\*.java" | Select-Object -ExpandProperty FullName)
if ($files.Count -eq 0) {
Write-Host "[SKIP] No .java files in $srcDir" -ForegroundColor Yellow
continue
}
# Compile
$compileOut = & $JAVAC -d $outDir @files 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] $label compile error:" -ForegroundColor Red
$compileOut | ForEach-Object { Write-Host " $_" -ForegroundColor Red }
$allOk = $false
continue
}
Write-Host "[OK] Compiled $($p.dir)" -ForegroundColor Green
# Run and save to per-pattern file
$logFile = "$OUTDIR\$($p.dir)-output.txt"
$runOut = & $JAVA -cp $outDir $($p.main) 2>&1
$runOut | Out-File -FilePath $logFile -Encoding utf8
$runOut | ForEach-Object { Write-Host $_ }
Write-Host "[SAVED] $logFile" -ForegroundColor DarkGray
}
Write-Host "`n============================================" -ForegroundColor Yellow
if ($allOk) {
Write-Host "All 7 structural patterns compiled and ran OK" -ForegroundColor Green
} else {
Write-Host "Some patterns had errors - check output above" -ForegroundColor Red
}
Write-Host "Output files: $OUTDIR" -ForegroundColor Yellow
Read-Host "`nPress Enter to close"