2022-06-15 23:36:47 +02:00
|
|
|
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
package report
|
|
|
|
|
|
|
|
import (
|
2022-07-18 01:50:40 +02:00
|
|
|
_ "embed"
|
2022-06-15 23:36:47 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
2022-07-18 01:50:40 +02:00
|
|
|
"text/template"
|
|
|
|
"time"
|
2022-06-15 23:36:47 +02:00
|
|
|
)
|
|
|
|
|
2022-07-12 16:56:49 +02:00
|
|
|
const (
|
|
|
|
outPrefix = "out/"
|
2022-07-12 21:15:38 +02:00
|
|
|
texDir = "tex/"
|
2022-07-12 16:56:49 +02:00
|
|
|
picsDir = "pics/"
|
|
|
|
)
|
|
|
|
|
2022-07-18 01:50:40 +02:00
|
|
|
var (
|
2022-07-18 03:27:08 +02:00
|
|
|
//go:embed report_base.tmpl
|
|
|
|
tmplReportBaseFile []byte
|
2022-07-18 01:50:40 +02:00
|
|
|
//go:embed meta.sty
|
|
|
|
metaSty []byte
|
|
|
|
//go:embed report.tmpl
|
|
|
|
tmplReportFile []byte
|
|
|
|
)
|
|
|
|
|
2022-07-12 21:15:38 +02:00
|
|
|
// GetPicsDirPrefix returns the path to the folder meant for tex files.
|
|
|
|
func GetTexDir() string {
|
|
|
|
return outPrefix + texDir
|
|
|
|
}
|
|
|
|
|
2022-07-12 16:56:49 +02:00
|
|
|
// GetPicsDirPrefix returns the path to the folder meant for plot pics.
|
|
|
|
func GetPicsDir() string {
|
|
|
|
return outPrefix + picsDir
|
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:08 +02:00
|
|
|
func emitReportBaseTex() error {
|
|
|
|
fname := GetTexDir() + "report_base.tex"
|
|
|
|
|
|
|
|
f, err := os.Create(fname)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
tmplReportBase := template.New("report_base")
|
|
|
|
tmplReportBase = template.Must(tmplReportBase.Parse(string(tmplReportBaseFile)))
|
|
|
|
|
|
|
|
err = tmplReportBase.Execute(f, struct {
|
|
|
|
Timestamp time.Time
|
|
|
|
}{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2022-08-03 17:43:07 +02:00
|
|
|
return err
|
2022-07-18 03:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 01:50:40 +02:00
|
|
|
func emitReportTex(fname string) error {
|
|
|
|
f, err := os.Create(fname)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
tmplReport := template.New("report")
|
|
|
|
tmplReport = template.Must(tmplReport.Parse(string(tmplReportFile)))
|
2022-06-15 23:36:47 +02:00
|
|
|
|
2022-07-18 01:50:40 +02:00
|
|
|
err = tmplReport.Execute(f, struct {
|
2022-07-18 03:27:08 +02:00
|
|
|
Timestamp time.Time
|
2022-07-18 01:50:40 +02:00
|
|
|
}{
|
2022-07-18 03:27:08 +02:00
|
|
|
Timestamp: time.Now(),
|
2022-07-18 01:50:40 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveMetaSty() error {
|
|
|
|
// save meta.sty
|
|
|
|
fmeta, err := os.Create("meta.sty")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fmeta.Close()
|
|
|
|
|
|
|
|
_, err = fmeta.Write(metaSty)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveAndPrint emits the tex files necessary to compile the report in full
|
|
|
|
// (report.tex, meta.sty) and prints the report.tex file to console.
|
2022-07-18 01:58:56 +02:00
|
|
|
func SaveAndPrint(doPrint bool) {
|
2022-07-18 03:27:08 +02:00
|
|
|
err := emitReportBaseTex()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reportFname := "report.tex"
|
2022-07-18 01:50:40 +02:00
|
|
|
|
2022-07-18 03:27:08 +02:00
|
|
|
err = emitReportTex(reportFname)
|
2022-07-18 01:50:40 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = saveMetaSty()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-07-18 01:58:56 +02:00
|
|
|
if doPrint {
|
|
|
|
fmt.Fprint(os.Stderr, "\n* printing the report...\n\n")
|
2022-06-15 23:36:47 +02:00
|
|
|
|
2022-07-18 03:27:08 +02:00
|
|
|
fh, err := os.Open(reportFname)
|
2022-07-18 01:58:56 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-06-15 23:36:47 +02:00
|
|
|
|
2022-07-18 01:58:56 +02:00
|
|
|
_, err = io.Copy(os.Stdout, fh)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-06-15 23:36:47 +02:00
|
|
|
}
|
|
|
|
}
|