diff --git a/compiler/book.go b/compiler/book.go index 3f065d7..2bb12a3 100644 --- a/compiler/book.go +++ b/compiler/book.go @@ -33,6 +33,8 @@ func MakeBook(path string, opts *settings.Settings) { // Sort scores sort.Sort(settings.ScoresByName{scores}) + getBookResources(opts) + templ, err := parseBookTemplate(opts) texPath := filepath.Join(opts.OutputDir, opts.Name+".tex") @@ -62,18 +64,7 @@ func MakeBook(path string, opts *settings.Settings) { }).Fatal("songbook failed to compile") } - cmd = exec.Command("latexmk", "-c", "-cd", texPath) - out, err = cmd.CombinedOutput() - if err != nil { - log.WithFields(log.Fields{ - "message": string(out), - "error": err, - }).Error("failed to clean songbook latex files") - } - if !opts.KeepBookTemplate { - err = os.Remove(texPath) - } - helpers.Check(err, "could not remove songbook latex template") + cleanBookResources(texPath, opts) } // scoreCategories returns a sorted slice of all categories used @@ -103,3 +94,44 @@ func unknownCategories(scores *[]settings.Score) bool { } return false } + +// getBookResources copies the LaTeX resources to the compile directory +func getBookResources(opts *settings.Settings) { + for i := range opts.LatexResources { + log.WithFields(log.Fields{"path": opts.LatexResources[i]}).Debug("copying latex resource") + + err := os.Link(opts.LatexResources[i], filepath.Join(opts.OutputDir, filepath.Base(opts.LatexResources[i]))) + + if err != nil { + log.WithError(err).Warning("could not link LaTeX resource") + } + } +} + +// cleanBookResources removes both LaTeX generated files and copied resources +func cleanBookResources(bookpath string, opts *settings.Settings) { + var err error + log.Debug("removing LaTeX resources") + for i := range opts.LatexResources { + err = os.RemoveAll(filepath.Join(opts.OutputDir, filepath.Base(opts.LatexResources[i]))) + if err != nil { + helpers.Check(err, "could not remove latex resource") + } + } + + log.Debug("removing LaTeX generated files") + cmd := exec.Command("latexmk", "-c", "-cd", bookpath) + out, err := cmd.CombinedOutput() + if err != nil { + log.WithFields(log.Fields{ + "message": string(out), + "error": err, + }).Error("failed to clean songbook latex files") + } + + if !opts.KeepBookTemplate { + log.Debug("removing LaTeX template") + err = os.Remove(bookpath) + } + helpers.Check(err, "could not remove songbook latex template") +} diff --git a/helpers/error_utils.go b/helpers/error_utils.go index fc6a820..42c96a5 100644 --- a/helpers/error_utils.go +++ b/helpers/error_utils.go @@ -18,9 +18,7 @@ import log "github.com/Sirupsen/logrus" // Crash outputs it's arguments to the log and stops the program func Crash(err error, msg string) { - log.WithFields(log.Fields{ - "error": err, - }).Fatal(msg) + log.WithError(err).Fatal(msg) } // Check calls Crash if the error is not nil diff --git a/settings/settings.go b/settings/settings.go index 49f0a0a..a7d7a5f 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -36,6 +36,7 @@ type Settings struct { BookTitleTempl string // Override for the partial book template creating the title page BookCategoryTempl string // Override for the partial book template creating category pages BookScoreTempl string // Override for the partial book template placing scores + LatexResources []string // Files to be copied to compile the book template KeepBookTemplate bool // Leave the LaTeX source for the book in the output directory } @@ -64,5 +65,8 @@ func (s *Settings) AbsolutePaths(root string) { for i := range s.LilypondIncludes { s.LilypondIncludes[i] = helpers.AbsolutePath(s.LilypondIncludes[i], root) } + for i := range s.LatexResources { + s.LatexResources[i] = helpers.AbsolutePath(s.LatexResources[i], root) + } s.OutputDir = helpers.AbsolutePath(s.OutputDir, root) }