Created
July 14, 2018 18:27
-
-
Save Vindaar/9ed80ee38faff8b6f3d3ce174d729f70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils | |
import os | |
import osproc | |
import streams | |
import regex | |
import docopt | |
const doc = """ | |
Compile your TeX file with pdflatex --shell-escape and install missing | |
packages automatically! | |
Note: It can only deal with missing LaTeX packages. On other compile errors | |
it may get stuck! | |
Note2: Well, this is maybe not a very efficient way to install your | |
missing packages. But hey, it's fun! | |
Usage: | |
installTexPackages <TexFile> [--quiet] | |
Options: | |
--quiet If set won't echo any pdflatex output. | |
-h, --help Show this help. | |
""" | |
const packageReg = re(r"^!.*\sFile\s`(\w+)\.sty'.*") | |
const pdflatex = staticExec("which pdflatex") | |
const cmdInstall = "sudo tlmgr install " | |
proc parseTexOutputLine(data: string): string = | |
## parses the given line, which may contain | |
## File `<name>.sty` not found, return `<name>` | |
let matches = findAll(data, packageReg) | |
if matches.len > 0: | |
let slices = matches[0].group(0) | |
result = data[slices[0]] | |
echo "Found package ", result | |
proc parseTexCommand(process: Process, quiet: bool): bool = | |
let stream = process.outputStream | |
var data = "" | |
while stream.readLine(data) == true: | |
let package = parseTexOutputLine(data) | |
if not quiet: | |
echo data | |
if package.len > 0: | |
# in this case install the package | |
echo "Installing package ", package | |
let errC = execCmd(cmdInstall & package) | |
if errC != 0: | |
echo "Could not install package? ", errC | |
process.terminate | |
# return early | |
return true | |
result = false | |
proc main() = | |
let docArgs = docopt(doc) | |
let quiet = ($docArgs["--quiet"]).parseBool | |
let file = $docArgs["<TexFile>"] | |
let args = ["--shell-escape", file] | |
while true: | |
let process = startProcess(pdflatex, args = args) | |
let again = process.parseTexCommand(quiet) | |
if not again: | |
break | |
echo "All packages installed!" | |
when isMainModule: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment