Archived
1
0

Move output path generation to the settings package

This commit is contained in:
Jip J. Dekker 2016-04-13 19:00:56 +02:00
parent 0f8b31f7a6
commit a72f9d02a6
4 changed files with 25 additions and 23 deletions

View File

@ -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()

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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)
}