$ErrorActionPreference = 'Stop' $projectDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path $dockerCommand = Get-Command docker -ErrorAction SilentlyContinue if ($dockerCommand) { $docker = $dockerCommand.Source } else { $candidates = @( (Join-Path $env:LOCALAPPDATA 'Programs\DockerDesktop\resources\bin\docker.exe'), (Join-Path $env:ProgramFiles 'Docker\Docker\resources\bin\docker.exe') ) $docker = $candidates | Where-Object { Test-Path -LiteralPath $_ } | Select-Object -First 1 } if (-not $docker) { throw 'Docker was not found. Start Docker Desktop and retry.' } $buildLog = Join-Path $projectDirectory 'docker-build.log' $runLog = Join-Path $projectDirectory 'docker-run.log' $pdfPath = Join-Path $projectDirectory 'sample.pdf' Push-Location $projectDirectory try { $engineVersion = (& $docker info --format '{{.ServerVersion}}' 2>$null).Trim() if ($LASTEXITCODE -ne 0) { throw 'Docker Desktop is not ready.' } Write-Host '> docker build --no-cache -t latex-docker .' $ErrorActionPreference = 'Continue' & $docker build --no-cache --progress=plain -t latex-docker . *> $buildLog $ErrorActionPreference = 'Stop' if ($LASTEXITCODE -ne 0) { Get-Content -LiteralPath $buildLog -Tail 25 throw "Image build failed. Full output: $buildLog" } $imageId = (& $docker image inspect --format '{{.Id}}' latex-docker).Trim() if ($LASTEXITCODE -ne 0 -or -not $imageId) { throw 'Built image could not be inspected.' } Write-Host '> docker run --rm -v "${PWD}:/work" latex-docker' $runStarted = Get-Date $ErrorActionPreference = 'Continue' & $docker run --rm -v "${PWD}:/work" latex-docker *> $runLog $ErrorActionPreference = 'Stop' if ($LASTEXITCODE -ne 0) { Get-Content -LiteralPath $runLog -Tail 25 throw "LaTeX compilation failed. Full output: $runLog" } $pdf = Get-Item -LiteralPath $pdfPath -ErrorAction Stop $runText = Get-Content -LiteralPath $runLog -Raw if ($pdf.LastWriteTime -lt $runStarted.AddSeconds(-2) -or $runText -notmatch 'Output written on sample\.pdf \((\d+) page') { throw "A fresh, successful sample.pdf was not confirmed. See $runLog" } $pages = $matches[1] Write-Host "Docker engine: $engineVersion" Write-Host "Built image: latex-docker ($($imageId.Substring(0, 19)))" Write-Host "Compiled PDF: sample.pdf ($pages page, $($pdf.Length) bytes)" Write-Host 'Full output saved: docker-build.log, docker-run.log' } finally { Pop-Location }