Files
latex-docker/build-seminar.ps1
T

139 lines
4.8 KiB
PowerShell

<#
.SYNOPSIS
Kompiliert die Seminararbeit mithilfe des LaTeX Docker Containers.
.DESCRIPTION
Skript zum bequemen Bauen, Säubern oder Überwachen der LaTeX-Seminararbeit via Docker.
.PARAMETER Clean
Führt ein 'clean' durch (latexmk -C), um temporäre Build-Dateien zu löschen.
.PARAMETER Watch
Startet den kontinuierlichen Überwachungsmodus (latexmk -pvc).
.PARAMETER RebuildImage
Erzwingt das erneute Erstellen des Docker-Images vor dem LaTeX-Build.
.PARAMETER ProjectPath
Pfad zum LaTeX-Projektverzeichnis (Standard: '../Seminar_Arbeit_BITI_Gruber').
.PARAMETER File
Spezifische LaTeX-Datei zum Kompilieren (Standard: 'seminararbeit.tex').
.EXAMPLE
.\build-seminar.ps1
.EXAMPLE
.\build-seminar.ps1 -Clean
.EXAMPLE
.\build-seminar.ps1 -Watch
#>
[CmdletBinding()]
param(
[switch]$Clean,
[switch]$Watch,
[switch]$RebuildImage,
[string]$ProjectPath = "",
[string]$File = ""
)
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
if ([string]::IsNullOrWhiteSpace($ProjectPath)) {
# 1. Wenn im aktuellen Verzeichnis (wo der User steht) .tex-Dateien existieren -> nimm dieses
if (Get-ChildItem -Path (Get-Location) -Filter "*.tex" -ErrorAction SilentlyContinue) {
$ProjectPath = (Get-Location).Path
}
# 2. Falls ../Seminar_Arbeit_BITI_Gruber existiert
elseif (Test-Path (Join-Path $ScriptDir "..\Seminar_Arbeit_BITI_Gruber")) {
$ProjectPath = Join-Path $ScriptDir "..\Seminar_Arbeit_BITI_Gruber"
}
else {
$ProjectPath = (Get-Location).Path
}
} else {
# Prüfen, ob relativ zum aktuellen Verzeichnis oder relativ zum Skriptverzeichnis
if (Test-Path $ProjectPath) {
$ProjectPath = (Resolve-Path $ProjectPath).Path
} elseif (Test-Path (Join-Path $ScriptDir $ProjectPath)) {
$ProjectPath = (Resolve-Path (Join-Path $ScriptDir $ProjectPath)).Path
}
}
$ErrorActionPreference = "Stop"
# Prüfen, ob Docker erreichbar ist
$dockerCmd = Get-Command docker -ErrorAction SilentlyContinue
if (-not $dockerCmd) {
Write-Error "Docker ist nicht in PATH verfügbar."
exit 1
}
$oldEAP = $ErrorActionPreference
$ErrorActionPreference = "Continue"
& docker info 2>&1 | Out-Null
$dockerOk = ($LASTEXITCODE -eq 0)
$ErrorActionPreference = $oldEAP
if (-not $dockerOk) {
Write-Error "Docker Desktop ist anscheinend nicht gestartet oder nicht erreichbar."
exit 1
}
$ImageName = "latex-docker:latest"
$FullProjectPath = [System.IO.Path]::GetFullPath($ProjectPath)
if (-not (Test-Path $FullProjectPath)) {
Write-Error "Projektverzeichnis existiert nicht: $FullProjectPath"
exit 1
}
# Image prüfen bzw. bauen
$imageExists = docker images -q $ImageName
if (-not $imageExists -or $RebuildImage) {
Write-Host "==> Erstelle Docker Image '$ImageName'..." -ForegroundColor Cyan
docker build -t $ImageName -t "git.djnizz.at/nizz05/latex-docker:latest" "$PSScriptRoot"
if ($LASTEXITCODE -ne 0) {
Write-Error "Docker Build fehlgeschlagen."
exit 1
}
}
# Befehl bestimmen
$cmdArgs = @()
if ($Clean) {
Write-Host "==> Bereinige Projektverzeichnis: $FullProjectPath" -ForegroundColor Yellow
$cmdArgs += "clean"
} elseif ($Watch) {
$targetName = if ($File) { $File } else { "Hauptdokument" }
Write-Host "==> Starte Watch-Modus für: $targetName in $FullProjectPath" -ForegroundColor Cyan
$cmdArgs += "watch"
if ($File) { $cmdArgs += $File }
} else {
$targetName = if ($File) { $File } else { "Dokument (Auto-Detection)" }
Write-Host "==> Kompiliere LaTeX-Projekt: $targetName in $FullProjectPath" -ForegroundColor Green
$cmdArgs += "build"
if ($File) { $cmdArgs += $File }
}
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$dockerRunArgs = @("--rm", "-v", "${FullProjectPath}:/workspace", "-w", "/workspace")
if ($Watch) {
# Interaktiv für sauberes Beenden per Strg+C
$dockerRunArgs += "-it"
}
docker run @dockerRunArgs $ImageName @cmdArgs
$stopwatch.Stop()
if ($LASTEXITCODE -eq 0) {
if (-not $Clean -and -not $Watch) {
Write-Host "`n[OK] Build erfolgreich abgeschlossen in $([math]::Round($stopwatch.Elapsed.TotalSeconds, 1))s!" -ForegroundColor Green
# Zuletzt geänderte PDF-Datei im Projekt/out-Verzeichnis suchen
$pdfFile = Get-ChildItem -Path $FullProjectPath -Filter "*.pdf" -Recurse -File -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
if ($pdfFile) {
Write-Host "PDF generiert: $($pdfFile.FullName) ($([math]::Round($pdfFile.Length / 1KB, 1)) KB)" -ForegroundColor Cyan
}
} else {
Write-Host "`n[OK] Aktion erfolgreich beendet!" -ForegroundColor Green
}
} else {
Write-Error "`n[FEHLER] LaTeX Build ist fehlgeschlagen mit Exit Code $LASTEXITCODE."
exit $LASTEXITCODE
}