| param( |
| [Parameter(Position = 0)] |
| [string]$Target = "hott-online.pdf", |
| [switch]$All, |
| [switch]$Clean, |
| [switch]$DryRun |
| ) |
|
|
| $ErrorActionPreference = "Stop" |
|
|
| function HasCommand([string]$Name) { |
| return [bool](Get-Command $Name -ErrorAction SilentlyContinue) |
| } |
|
|
| function RunStep([string]$Cmd) { |
| if ($DryRun) { |
| Write-Output $Cmd |
| return |
| } |
| & powershell.exe -NoProfile -ExecutionPolicy Bypass -Command $Cmd |
| if ($LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0) { |
| exit $LASTEXITCODE |
| } |
| } |
|
|
| function Ensure-VersionTex { |
| $ver = "local" |
| if (HasCommand "git") { |
| try { |
| $ver = (git describe --always --long).Trim() |
| } catch { |
| $ver = "local" |
| } |
| } |
| $line = "\newcommand{\OPTversion}{$ver}" |
| if ($DryRun) { |
| Write-Output ('Set-Content -Encoding ascii -Path version.tex -Value "{0}"' -f $line.Replace('"','`"')) |
| return |
| } |
| Set-Content -Encoding ascii -Path version.tex -Value $line |
| } |
|
|
| function LatexmkPdf([string]$TexFile) { |
| RunStep ('latexmk -interaction=batchmode -g -pdf "{0}"' -f $TexFile) |
| } |
|
|
| function LatexmkDvi([string]$TexFile) { |
| RunStep ('latexmk -interaction=batchmode -dvi "{0}"' -f $TexFile) |
| } |
|
|
| function PdflatexCycle([string]$TexFile) { |
| $base = [System.IO.Path]::GetFileNameWithoutExtension($TexFile) |
| RunStep ('pdflatex -halt-on-error -interaction=batchmode "{0}" *> $null' -f $TexFile) |
| RunStep ('bibtex "{0}"' -f $base) |
| RunStep ('makeindex "{0}"' -f $base) |
| RunStep ('pdflatex -halt-on-error -interaction=batchmode "{0}" *> $null' -f $TexFile) |
| RunStep ('pdflatex -halt-on-error "{0}"' -f $TexFile) |
| } |
|
|
| function LatexCycle([string]$TexFile) { |
| $base = [System.IO.Path]::GetFileNameWithoutExtension($TexFile) |
| RunStep ('latex -halt-on-error -interaction=batchmode "{0}" *> $null' -f $TexFile) |
| RunStep ('bibtex "{0}"' -f $base) |
| RunStep ('makeindex "{0}"' -f $base) |
| RunStep ('latex -halt-on-error -interaction=batchmode "{0}" *> $null' -f $TexFile) |
| RunStep ('latex -halt-on-error "{0}"' -f $TexFile) |
| } |
|
|
| function Compile([string]$TargetFile) { |
| Ensure-VersionTex |
|
|
| $ext = [System.IO.Path]::GetExtension($TargetFile) |
| $name = [System.IO.Path]::GetFileNameWithoutExtension($TargetFile) |
| $tex = "$name.tex" |
|
|
| if (-not (Test-Path $tex)) { |
| throw "Missing tex file: $tex" |
| } |
|
|
| if ($ext -ieq ".pdf") { |
| if (HasCommand "latexmk") { |
| LatexmkPdf $tex |
| } else { |
| PdflatexCycle $tex |
| } |
| return |
| } |
|
|
| if ($ext -ieq ".dvi") { |
| if (HasCommand "latexmk") { |
| LatexmkDvi $tex |
| } else { |
| LatexCycle $tex |
| } |
| return |
| } |
|
|
| throw "Unsupported target: $TargetFile" |
| } |
|
|
| function LogCheck { |
| if (-not (Test-Path "hott-online.ilg")) { |
| return |
| } |
| $hit = Select-String -Path "hott-online.ilg" -SimpleMatch "!! Input index error" -ErrorAction SilentlyContinue |
| if ($hit) { |
| throw "Indexing error detected in hott-online.ilg" |
| } |
| } |
|
|
| function CleanFiles { |
| $exts = @("out","log","pdf","dvi","fls","fdb_latexmk","aux","brf","bbl","idx","ilg","ind","toc","sed") |
| Remove-Item -Force -ErrorAction SilentlyContinue "*~" |
| Remove-Item -Force -ErrorAction SilentlyContinue "*.aux" |
| foreach ($e in $exts) { |
| Remove-Item -Force -ErrorAction SilentlyContinue ("exercise_solutions.{0}" -f $e) |
| Remove-Item -Force -ErrorAction SilentlyContinue ("errata.{0}" -f $e) |
| Remove-Item -Force -ErrorAction SilentlyContinue ("hott-*.{0}" -f $e) |
| } |
| if (HasCommand "latexmk") { |
| RunStep 'latexmk -interaction=batchmode -C hott-*.tex' |
| } |
| } |
|
|
| $targets = @( |
| "hott-online.pdf", |
| "hott-bilingual.pdf", |
| "hott-ustrade.pdf", |
| "hott-letter.pdf", |
| "hott-letter-exercises.pdf", |
| "hott-a4.pdf", |
| "hott-a4-exercises.pdf", |
| "hott-ebook.pdf", |
| "hott-ebook-wide.pdf", |
| "hott-ebook-narrow.pdf", |
| "hott-arxiv.pdf", |
| "exercise_solutions.pdf", |
| "errata.pdf", |
| "cover-lulu-hardcover.pdf", |
| "cover-lulu-paperback.pdf", |
| "cover-letter.pdf", |
| "cover-a4.pdf" |
| ) |
|
|
| if ($Clean) { |
| CleanFiles |
| exit 0 |
| } |
|
|
| if ($All) { |
| foreach ($t in $targets) { |
| Compile $t |
| } |
| LogCheck |
| exit 0 |
| } |
|
|
| Compile $Target |
| if ($Target -ieq "hott-online.pdf") { |
| LogCheck |
| } |
|
|