param( [ValidateSet('pdf', 'clean')] [string]$Action = 'pdf' ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' $projectDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path $documentName = 'devopsAssignment' function Invoke-BuildTool { param( [Parameter(Mandatory)][string]$Name, [Parameter(Mandatory)][string[]]$ToolArguments ) Write-Host "> $Name $($ToolArguments -join ' ')" & $Name @ToolArguments if ($LASTEXITCODE -ne 0) { throw "'$Name' failed with exit code $LASTEXITCODE. Review $documentName.log." } } Push-Location $projectDirectory try { if ($Action -eq 'clean') { $patterns = @( '*.acn', '*.acr', '*.alg', '*.aux', '*.bbl', '*.blg', '*.glg', '*.glo', '*.gls', '*.idx', '*.ilg', '*.ind', '*.ist', '*.lof', '*.log', '*.lot', '*.out', '*.toc' ) foreach ($pattern in $patterns) { Get-ChildItem -LiteralPath $projectDirectory -Filter $pattern -File | Remove-Item -Force } Write-Host "Removed generated files; the PDF in the repository root is retained." exit 0 } $repoDirectory = Split-Path -Parent $projectDirectory $projectsDirectory = Split-Path -Parent $repoDirectory $toolDirectories = @() if ($env:DEVOPS_LATEX_BIN) { $toolDirectories += $env:DEVOPS_LATEX_BIN } $toolDirectories += @( (Join-Path $env:APPDATA 'TinyTeX\bin\windows'), (Join-Path $projectsDirectory 'Tools\LaTeX\TinyTeX\bin\windows'), (Join-Path $env:LOCALAPPDATA 'Programs\MiKTeX\miktex\bin\x64'), 'C:\Program Files\MiKTeX\miktex\bin\x64' ) $selectedDirectory = $toolDirectories | Where-Object { Test-Path -LiteralPath $_ } | Select-Object -First 1 if ($selectedDirectory -and -not (($env:Path -split ';') -contains $selectedDirectory)) { $env:Path = "$selectedDirectory;$env:Path" } foreach ($tool in @('pdflatex', 'bibtex', 'makeindex')) { if ($null -eq (Get-Command $tool -ErrorAction SilentlyContinue)) { throw "Required command '$tool' was not found. Install TinyTeX." } } # -shell-escape lets minted call Pygments (pip install Pygments). $pdfArgs = @('-shell-escape', '-interaction=nonstopmode', '-halt-on-error', '-file-line-error', "$documentName.tex") $pdfLaTeXPath = (Get-Command pdflatex).Source if ($pdfLaTeXPath -match '[\\/]MiKTeX[\\/]') { $pdfArgs = @('--disable-installer') + $pdfArgs } Invoke-BuildTool 'pdflatex' $pdfArgs # bibtex errors when no chapter cites anything. if (Select-String -LiteralPath "$documentName.aux" -Pattern '\\bibdata' -Quiet) { Invoke-BuildTool 'bibtex' @($documentName) } Invoke-BuildTool 'makeindex' @( '-s', "$documentName.ist", '-t', "$documentName.glg", '-o', "$documentName.gls", "$documentName.glo" ) Invoke-BuildTool 'makeindex' @( '-s', "$documentName.ist", '-t', "$documentName.alg", '-o', "$documentName.acr", "$documentName.acn" ) Invoke-BuildTool 'makeindex' @("$documentName.idx") Invoke-BuildTool 'pdflatex' $pdfArgs Invoke-BuildTool 'pdflatex' $pdfArgs Invoke-BuildTool 'pdflatex' $pdfArgs # Initial page numbers can move while the contents and references settle. # Regenerate glossary/index entries from the stabilized layout as well. Invoke-BuildTool 'makeindex' @( '-s', "$documentName.ist", '-t', "$documentName.glg", '-o', "$documentName.gls", "$documentName.glo" ) Invoke-BuildTool 'makeindex' @( '-s', "$documentName.ist", '-t', "$documentName.alg", '-o', "$documentName.acr", "$documentName.acn" ) Invoke-BuildTool 'makeindex' @("$documentName.idx") Invoke-BuildTool 'pdflatex' $pdfArgs Invoke-BuildTool 'pdflatex' $pdfArgs if (-not (Test-Path -LiteralPath "$documentName.pdf")) { throw "$documentName.pdf was not created." } $outputPdf = Join-Path (Split-Path -Parent $projectDirectory) '2026F-SSW-590-DevOps-Group-1.pdf' Move-Item -LiteralPath "$documentName.pdf" -Destination $outputPdf -Force Write-Host "Build complete: $outputPdf" } finally { Pop-Location }