delete aux file if new package got installed

better error message if no latex compiler available
use latexmk as default compiler - if not available use pdflatex
This commit is contained in:
lukas 2021-08-26 17:31:20 +02:00
parent 2a9e4c1117
commit 9be91accb3

52
Main.go
View File

@ -3,36 +3,46 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io"
"log" "log"
"os"
"os/exec" "os/exec"
"regexp" "regexp"
"runtime" "runtime"
"strconv" "strconv"
) )
const (
ErrNoCompiler = "none of the following latex compilers available: [latexmk, pdflatex]"
)
func main() { func main() {
fmt.Printf("Pdflatex command exists: %t\n", commandExists("pdflatex")) fmt.Printf("Pdflatex command exists: %t\n", commandExists("pdflatex"))
fmt.Printf("%s/%s\n", runtime.GOOS, runtime.GOARCH) fmt.Printf("LatexMk command exists: %t\n", commandExists("latexmk"))
fmt.Printf("Operation System: %s/%s\n", runtime.GOOS, runtime.GOARCH)
compileAndInstall() compileAndInstall()
} }
func compileAndInstall() { func compileAndInstall() {
out, err := compileLatex("main.tex") out, err := compileLatex("main.tex")
//fmt.Println(*out)
if err != nil { if err != nil {
fmt.Println("An error occured while compiling the document!") fmt.Println("An error occured while compiling the document!")
filename := parseMissingFile(out) if err.Error() == ErrNoCompiler {
if filename != "" { log.Fatal(err.Error())
}
if filename := parseMissingFile(out); filename != "" {
fmt.Printf("We need to download: %s\n", filename) fmt.Printf("We need to download: %s\n", filename)
// now we neet to perform a root check // now we need to perform a root check
if rootCheck() { if rootCheck() {
log.Println("Awesome! You are now running this program with root permissions!") log.Println("Awesome! You are now running this program with root permissions!")
if installFile(filename) { if installFile(filename) {
// we remove the main aux file to really trigger a rebuild!
os.Remove("main.aux")
// if successfully installed we try to compile again // if successfully installed we try to compile again
compileAndInstall() compileAndInstall()
} }
@ -91,7 +101,14 @@ func commandExists(cmd string) bool {
// parse the thumbail picture from video file // parse the thumbail picture from video file
func compileLatex(filename string) (*string, error) { func compileLatex(filename string) (*string, error) {
app := "pdflatex" app := ""
if commandExists("latexmk") {
app = "latexmk"
} else if commandExists("pdflatex") {
app = "pdflatex"
} else {
return nil, fmt.Errorf(ErrNoCompiler)
}
cmd := exec.Command(app, cmd := exec.Command(app,
"-file-line-error", "-file-line-error",
@ -124,14 +141,11 @@ func rootCheck() bool {
i, err := strconv.Atoi(string(output[:len(output)-1])) i, err := strconv.Atoi(string(output[:len(output)-1]))
if err != nil { if err != nil {
// maybe no unix system?
log.Fatal(err) log.Fatal(err)
} }
if i == 0 { return i == 0
return true
} else {
return false
}
} }
func installFile(filename string) bool { func installFile(filename string) bool {
@ -147,17 +161,25 @@ func installFile(filename string) bool {
stderr, _ := cmd.StderrPipe() stderr, _ := cmd.StderrPipe()
fmt.Println("running dnf install now!") fmt.Println("running dnf install now!")
cmd.Start()
go func() { go func() {
merged := io.MultiReader(stderr, stdout) scanner := bufio.NewScanner(stdout)
scanner := bufio.NewScanner(merged)
for scanner.Scan() { for scanner.Scan() {
m := scanner.Text() m := scanner.Text()
fmt.Println(m) fmt.Println(m)
} }
}() }()
err := cmd.Run() go func() {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
}
}()
err := cmd.Wait()
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return false return false