Structures output of the scores
This commit is contained in:
parent
f84ff44d47
commit
66fa2fd436
@ -46,14 +46,9 @@ func CompileDir(path string, opts *settings.Settings) {
|
||||
)
|
||||
switch filepath.Ext(scores[i].Path) {
|
||||
case ".ly":
|
||||
msg, err = Lilypond(scores[i].Path)
|
||||
msg, err = Lilypond(&scores[i])
|
||||
case ".pdf":
|
||||
if helpers.Exists(scores[i].OutputPath) {
|
||||
os.Remove(scores[i].OutputPath)
|
||||
}
|
||||
if err == nil {
|
||||
err = os.Link(scores[i].Path, scores[i].OutputPath)
|
||||
}
|
||||
err = linkPDF(&scores[i])
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@ -108,3 +103,15 @@ func generateScores() func(string, os.FileInfo) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func linkPDF(s *settings.Score) (err error) {
|
||||
err = helpers.ExistsOrCreate(filepath.Dir(s.OutputPath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if helpers.Exists(s.OutputPath) {
|
||||
os.Remove(s.OutputPath)
|
||||
}
|
||||
err = os.Link(s.Path, s.OutputPath)
|
||||
return
|
||||
}
|
||||
|
@ -15,8 +15,8 @@
|
||||
package compiler
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/jjdekker/ponder/helpers"
|
||||
@ -37,21 +37,21 @@ func PrepareLilypond(opts *settings.Settings) {
|
||||
}
|
||||
lilypondArgs = append(lilypondArgs, "--loglevel=ERROR")
|
||||
lilypondArgs = append(lilypondArgs, "--pdf")
|
||||
|
||||
lilypondArgs = append(lilypondArgs, "--output="+opts.OutputDir)
|
||||
if !helpers.Exists(opts.OutputDir) {
|
||||
log.WithFields(log.Fields{"path": opts.OutputDir}).Info("creating output directory")
|
||||
err := os.MkdirAll(opts.OutputDir, os.ModePerm)
|
||||
helpers.Check(err, "Could not create output directory")
|
||||
}
|
||||
}
|
||||
|
||||
// Lilypond runs the lilypond compiler on the given path
|
||||
// using the arguments prepared by the PrepareLilypond function
|
||||
func Lilypond(path string) (string, error) {
|
||||
cmd := exec.Command(lilypondCmd, append(lilypondArgs, path)...)
|
||||
func Lilypond(s *settings.Score) (string, error) {
|
||||
args := append(lilypondArgs, "--output="+filepath.Dir(s.OutputPath))
|
||||
err := helpers.ExistsOrCreate(filepath.Dir(s.OutputPath))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
args = append(args, s.Path)
|
||||
|
||||
cmd := exec.Command(lilypondCmd, args...)
|
||||
log.WithFields(log.Fields{
|
||||
"path": path,
|
||||
"path": s.Path,
|
||||
"cmd": cmd,
|
||||
}).Info("compiling file using lilypond")
|
||||
out, err := cmd.CombinedOutput()
|
||||
|
@ -55,6 +55,15 @@ func Exists(path string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ExistsOrCreate will create a directory unless it already Exists
|
||||
func ExistsOrCreate(path string) (err error) {
|
||||
if !Exists(path) {
|
||||
log.WithFields(log.Fields{"path": path}).Info("creating directory")
|
||||
err = os.MkdirAll(path, os.ModePerm)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// LastModified returns the time the file on the path was last modified,
|
||||
// if file lookup fails the current time is returned.
|
||||
func LastModified(path string) time.Time {
|
||||
|
@ -141,7 +141,11 @@ func (s *Score) GenerateOutputPath(opts *Settings) {
|
||||
return
|
||||
}
|
||||
file = file[:dot+1] + "pdf"
|
||||
s.OutputPath = filepath.Join(opts.OutputDir, file)
|
||||
s.OutputPath = opts.OutputDir
|
||||
if !opts.FlatOutputDir && len(s.Categories) > 0 {
|
||||
s.OutputPath = filepath.Join(s.OutputPath, s.Categories[0])
|
||||
}
|
||||
s.OutputPath = filepath.Join(s.OutputPath, file)
|
||||
}
|
||||
|
||||
// Scores aliases a slice of scores
|
||||
|
@ -38,6 +38,7 @@ type Settings struct {
|
||||
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
|
||||
FlatOutputDir bool // Keep all output file in a flat output directory
|
||||
}
|
||||
|
||||
// FromFile reads a settings file in json format and returns the Settings struct
|
||||
|
Reference in New Issue
Block a user