From a72f9d02a6006842d0478094c74a389da7a37a93 Mon Sep 17 00:00:00 2001 From: "Jip J. Dekker" Date: Wed, 13 Apr 2016 19:00:56 +0200 Subject: [PATCH] Move output path generation to the settings package --- compiler/book.go | 8 +++----- compiler/compile.go | 12 ++++++------ compiler/file_discovery.go | 12 ------------ settings/score.go | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/compiler/book.go b/compiler/book.go index 1da7ba2..2b5fd0f 100644 --- a/compiler/book.go +++ b/compiler/book.go @@ -47,7 +47,7 @@ var bookTempl = ` {{range .Scores}} \phantomsection \addcontentsline{toc}{section}{{printf "{"}}{{ .Name }}{{printf "}"}} -\includepdf[pages=-]{{printf "{"}}{{call $.OutputPath .Path $.Settings}}{{printf "}"}} +\includepdf[pages=-]{{printf "{"}}{{.OutputPath}}{{printf "}"}} {{end}} \end{document} @@ -68,13 +68,11 @@ func MakeBook(path string, opts *settings.Settings) { f, err := os.Create(texPath) helpers.Check(err, "could not create songbook texfile") err = templ.Execute(f, &struct { - Scores []*settings.Score + Scores *[]settings.Score Settings *settings.Settings - OutputPath func(string, *settings.Settings) string }{ - Scores: scores, + Scores: &scores, Settings: opts, - OutputPath: outputPath, }) helpers.Check(err, "error executing book template") f.Close() diff --git a/compiler/compile.go b/compiler/compile.go index 058b90f..407fb3a 100644 --- a/compiler/compile.go +++ b/compiler/compile.go @@ -25,7 +25,7 @@ import ( ) var ( - scores []*settings.Score + scores []settings.Score ) // CompileDir compiles all lilypond files and makes all @@ -37,10 +37,10 @@ func CompileDir(path string, opts *settings.Settings) { PrepareLilypond(opts) for _, score := range scores { - output := outputPath(score.Path, opts) + score.GenerateOutputPath(opts) - if !helpers.Exists(output) || - score.LastModified.After(helpers.LastModified(output)) { + if !helpers.Exists(score.OutputPath) || + score.LastModified.After(helpers.LastModified(score.OutputPath)) { msg, err := Lilypond(score.Path) if err != nil { log.WithFields(log.Fields{ @@ -58,7 +58,7 @@ func generateScores() func(string, os.FileInfo) error { switch filepath.Ext(path) { case ".ly": log.WithFields(log.Fields{"path": path}).Info("adding lilypond file") - scores = append(scores, &settings.Score{ + scores = append(scores, settings.Score{ Name: filepath.Base(path)[:strings.LastIndex(filepath.Base(path), ".")], Path: path, LastModified: file.ModTime(), @@ -73,7 +73,7 @@ func generateScores() func(string, os.FileInfo) error { "path": path, }).Warning("unable to parse score settings, skipping...") } else { - scores = append(scores, score) + scores = append(scores, *score) } } diff --git a/compiler/file_discovery.go b/compiler/file_discovery.go index 61bf5ea..474b2ac 100644 --- a/compiler/file_discovery.go +++ b/compiler/file_discovery.go @@ -17,7 +17,6 @@ package compiler import ( "os" "path/filepath" - "strings" log "github.com/Sirupsen/logrus" "github.com/jjdekker/ponder/helpers" @@ -61,14 +60,3 @@ func compilePath(root string, opts *settings.Settings, return nil } } - -// outputPath returns the path that the compiled file will take -func outputPath(source string, opts *settings.Settings) string { - file := filepath.Base(source) - dot := strings.LastIndex(file, ".") - if dot == -1 { - log.WithFields(log.Fields{"path": source}).Error("Unable to compute output path") - } - file = file[:dot+1] + "pdf" - return filepath.Join(opts.OutputDir, file) -} diff --git a/settings/score.go b/settings/score.go index 837bef3..57c13f0 100644 --- a/settings/score.go +++ b/settings/score.go @@ -31,6 +31,7 @@ type Score struct { Categories []string `json:",omitempty"` // Categories to which the scores belong Path string // The path to the scores (uncompiled) file LastModified time.Time `json:"-"` // Time when the score source was last modified (will be set internally) + OutputPath string `json:",omitempty"` // The path on which the compiled version of the score will be placed } // FromJSON reads the settings of a score from a JSON file @@ -72,3 +73,18 @@ func CreateScore(path, workDir string) { err = ioutil.WriteFile(jsonPath+".json", data, 0644) helpers.Check(err, "Unable to save json to file") } + +// GenerateOutputPath fills path that the compiled score will take +func (s *Score) GenerateOutputPath(opts *Settings) { + if s.OutputPath != "" { + return + } + file := filepath.Base(s.Path) + dot := strings.LastIndex(file, ".") + if dot == -1 { + log.WithFields(log.Fields{"path": s.Path}).Error("Unable to compute output path") + return + } + file = file[:dot+1] + "pdf" + s.OutputPath = filepath.Join(opts.OutputDir, file) +}