Move output path generation to the settings package
This commit is contained in:
parent
0f8b31f7a6
commit
a72f9d02a6
@ -47,7 +47,7 @@ var bookTempl = `
|
|||||||
{{range .Scores}}
|
{{range .Scores}}
|
||||||
\phantomsection
|
\phantomsection
|
||||||
\addcontentsline{toc}{section}{{printf "{"}}{{ .Name }}{{printf "}"}}
|
\addcontentsline{toc}{section}{{printf "{"}}{{ .Name }}{{printf "}"}}
|
||||||
\includepdf[pages=-]{{printf "{"}}{{call $.OutputPath .Path $.Settings}}{{printf "}"}}
|
\includepdf[pages=-]{{printf "{"}}{{.OutputPath}}{{printf "}"}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
\end{document}
|
\end{document}
|
||||||
@ -68,13 +68,11 @@ func MakeBook(path string, opts *settings.Settings) {
|
|||||||
f, err := os.Create(texPath)
|
f, err := os.Create(texPath)
|
||||||
helpers.Check(err, "could not create songbook texfile")
|
helpers.Check(err, "could not create songbook texfile")
|
||||||
err = templ.Execute(f, &struct {
|
err = templ.Execute(f, &struct {
|
||||||
Scores []*settings.Score
|
Scores *[]settings.Score
|
||||||
Settings *settings.Settings
|
Settings *settings.Settings
|
||||||
OutputPath func(string, *settings.Settings) string
|
|
||||||
}{
|
}{
|
||||||
Scores: scores,
|
Scores: &scores,
|
||||||
Settings: opts,
|
Settings: opts,
|
||||||
OutputPath: outputPath,
|
|
||||||
})
|
})
|
||||||
helpers.Check(err, "error executing book template")
|
helpers.Check(err, "error executing book template")
|
||||||
f.Close()
|
f.Close()
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
scores []*settings.Score
|
scores []settings.Score
|
||||||
)
|
)
|
||||||
|
|
||||||
// CompileDir compiles all lilypond files and makes all
|
// CompileDir compiles all lilypond files and makes all
|
||||||
@ -37,10 +37,10 @@ func CompileDir(path string, opts *settings.Settings) {
|
|||||||
|
|
||||||
PrepareLilypond(opts)
|
PrepareLilypond(opts)
|
||||||
for _, score := range scores {
|
for _, score := range scores {
|
||||||
output := outputPath(score.Path, opts)
|
score.GenerateOutputPath(opts)
|
||||||
|
|
||||||
if !helpers.Exists(output) ||
|
if !helpers.Exists(score.OutputPath) ||
|
||||||
score.LastModified.After(helpers.LastModified(output)) {
|
score.LastModified.After(helpers.LastModified(score.OutputPath)) {
|
||||||
msg, err := Lilypond(score.Path)
|
msg, err := Lilypond(score.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
@ -58,7 +58,7 @@ func generateScores() func(string, os.FileInfo) error {
|
|||||||
switch filepath.Ext(path) {
|
switch filepath.Ext(path) {
|
||||||
case ".ly":
|
case ".ly":
|
||||||
log.WithFields(log.Fields{"path": path}).Info("adding lilypond file")
|
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), ".")],
|
Name: filepath.Base(path)[:strings.LastIndex(filepath.Base(path), ".")],
|
||||||
Path: path,
|
Path: path,
|
||||||
LastModified: file.ModTime(),
|
LastModified: file.ModTime(),
|
||||||
@ -73,7 +73,7 @@ func generateScores() func(string, os.FileInfo) error {
|
|||||||
"path": path,
|
"path": path,
|
||||||
}).Warning("unable to parse score settings, skipping...")
|
}).Warning("unable to parse score settings, skipping...")
|
||||||
} else {
|
} else {
|
||||||
scores = append(scores, score)
|
scores = append(scores, *score)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ package compiler
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/jjdekker/ponder/helpers"
|
"github.com/jjdekker/ponder/helpers"
|
||||||
@ -61,14 +60,3 @@ func compilePath(root string, opts *settings.Settings,
|
|||||||
return nil
|
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)
|
|
||||||
}
|
|
||||||
|
@ -31,6 +31,7 @@ type Score struct {
|
|||||||
Categories []string `json:",omitempty"` // Categories to which the scores belong
|
Categories []string `json:",omitempty"` // Categories to which the scores belong
|
||||||
Path string // The path to the scores (uncompiled) file
|
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)
|
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
|
// 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)
|
err = ioutil.WriteFile(jsonPath+".json", data, 0644)
|
||||||
helpers.Check(err, "Unable to save json to file")
|
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)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user