From 7742642e14b0ba90e81310a43a5d3f73430ac773 Mon Sep 17 00:00:00 2001 From: surtur Date: Sun, 17 Jul 2022 11:36:59 +0200 Subject: [PATCH] go(report): embed all templates --- report/allPics.go | 20 ++++++++------------ report/pic.go | 17 ++++++----------- report/table.go | 17 ++++++----------- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/report/allPics.go b/report/allPics.go index 3cc70ca..099955e 100644 --- a/report/allPics.go +++ b/report/allPics.go @@ -4,6 +4,7 @@ package report import ( + _ "embed" "log" "os" "text/template" @@ -22,7 +23,11 @@ type allThePics struct { TexFiles []picTexFiles } -var allPics = &allThePics{} +var ( + allPics = &allThePics{} + //go:embed allpics.tmpl + tmplAllPicsFile []byte +) // SaveTexAllPics feeds all paths of generated pics to a template that creates // `allpics.tex` file, which then aggregates all tex files tracking plot pics @@ -30,18 +35,9 @@ var allPics = &allThePics{} func SaveTexAllPics() { a := allPics texAllPicsFile := GetTexDir() + "allpics" + ".tex" - tmplAllPicsFile := "report/allpics.tmpl" - if _, err := os.Stat(tmplAllPicsFile); err != nil { - // TODO(me): fix this. - // this block is relevant for the unit test path, somehow the file is - // not found as defined above. - log.Println(err, `, weird test behaviour , prepending "../"`) - - tmplAllPicsFile = "../" + tmplAllPicsFile - } - - tmplAllPics := template.Must(template.ParseFiles(tmplAllPicsFile)) + tmplAllPics := template.New("allpics") + tmplAllPics = template.Must(tmplAllPics.Parse(string(tmplAllPicsFile))) f, err := os.Create(texAllPicsFile) if err != nil { diff --git a/report/pic.go b/report/pic.go index 48814ab..ed1beb6 100644 --- a/report/pic.go +++ b/report/pic.go @@ -4,6 +4,7 @@ package report import ( + _ "embed" "log" "os" "sort" @@ -26,6 +27,9 @@ type PicList struct { Pics []Pic } +//go:embed pics.tmpl +var tmplPicsFile []byte + // NewPic returns a new copy of Pic. func NewPic() *Pic { return &Pic{} @@ -46,18 +50,9 @@ func SavePicsToFile(pls []PicList, algoName string) { for _, p := range pls { safeName := util.SanitiseFName(p.Algo + "-" + p.Bench) texPicsFile := GetTexDir() + "pics-" + safeName + ".tex" - tmplPicsFile := "report/pics.tmpl" + tmplPics := template.New("pics") - if _, err := os.Stat(tmplPicsFile); err != nil { - // TODO(me): fix this, same as in table.go. - // this block is relevant for the unit test path, somehow the file is - // not found as defined above. - log.Println(err, `, weird test behaviour , prepending "../"`) - - tmplPicsFile = "../" + tmplPicsFile - } - - tmplPics := template.Must(template.ParseFiles(tmplPicsFile)) + tmplPics = template.Must(tmplPics.Parse(string(tmplPicsFile))) paths = append(paths, texPicsFile) // keep the slice sorted. diff --git a/report/table.go b/report/table.go index 15efe4d..830a13d 100644 --- a/report/table.go +++ b/report/table.go @@ -4,6 +4,7 @@ package report import ( + _ "embed" "log" "os" "text/template" @@ -25,6 +26,9 @@ type Row struct { Values []float64 } +//go:embed table.tmpl +var tmplTableFile []byte + func NewTable() *Table { return &Table{} } @@ -36,18 +40,9 @@ func NewRow() *Row { func SaveTableToFile(t Table) { safeName := util.SanitiseFName(t.Algo) texTableFile := GetTexDir() + "table-" + safeName + ".tex" - tmplTableFile := "report/table.tmpl" + tmplTable := template.New("table") - if _, err := os.Stat(tmplTableFile); err != nil { - // TODO(me): fix this. - // this block is relevant for the unit test path, somehow the file is - // not found as defined above. - log.Println(err, `, weird test behaviour , prepending "../"`) - - tmplTableFile = "../" + tmplTableFile - } - - tmplTable := template.Must(template.ParseFiles(tmplTableFile)) + tmplTable = template.Must(tmplTable.Parse(string(tmplTableFile))) ttf := tableTexFiles{Algo: t.Algo, FilePaths: []string{texTableFile}} allTables.TexFiles = append(allTables.TexFiles, ttf)