diff --git a/compiler/compile.go b/compiler/compile.go index 131cbd2..3bbaf12 100644 --- a/compiler/compile.go +++ b/compiler/compile.go @@ -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 +} diff --git a/compiler/lilypond.go b/compiler/lilypond.go index 6caf1c5..e505777 100644 --- a/compiler/lilypond.go +++ b/compiler/lilypond.go @@ -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() diff --git a/helpers/file_utils.go b/helpers/file_utils.go index a584e33..b830c57 100644 --- a/helpers/file_utils.go +++ b/helpers/file_utils.go @@ -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 { diff --git a/settings/score.go b/settings/score.go index 3b50d5c..cc9b5c8 100644 --- a/settings/score.go +++ b/settings/score.go @@ -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 diff --git a/settings/settings.go b/settings/settings.go index a7d7a5f..cc7a4d1 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -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