From 345b99cc0cf96b2cd9dc2952acbd8d5729763a87 Mon Sep 17 00:00:00 2001 From: "Jip J. Dekker" Date: Sun, 2 Oct 2016 02:25:11 +0200 Subject: [PATCH] Adds clean command --- cmd/clean.go | 37 +++++++++++++++++++++++ compiler/clean.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 cmd/clean.go create mode 100644 compiler/clean.go diff --git a/cmd/clean.go b/cmd/clean.go new file mode 100644 index 0000000..21ae8f3 --- /dev/null +++ b/cmd/clean.go @@ -0,0 +1,37 @@ +// Copyright © 2016 Jip J. Dekker +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "github.com/jjdekker/ponder/compiler" + "github.com/spf13/cobra" +) + +// cleanCommand represents the cleaning command +var cleanCommand = &cobra.Command{ + Use: "clean", + Short: "Remove all ponder generated files.", + Long: `The cleaning command removes all item generated by Ponder according to +the target definition. Items removed by the cleaning command will be removed +from the file system, use with care.`, + Run: func(cmd *cobra.Command, args []string) { + path, opts := getSettings() + compiler.Clean(path, opts) + }, +} + +func init() { + RootCmd.AddCommand(cleanCommand) +} diff --git a/compiler/clean.go b/compiler/clean.go new file mode 100644 index 0000000..187f641 --- /dev/null +++ b/compiler/clean.go @@ -0,0 +1,76 @@ +// Copyright © 2016 Jip J. Dekker +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package compiler + +import ( + "os" + "path/filepath" + + log "github.com/Sirupsen/logrus" + "github.com/jjdekker/ponder/helpers" + "github.com/jjdekker/ponder/settings" +) + +// Clean removes all files generated by the CompileDir and MakeBook commands. +func Clean(path string, opts *settings.Settings) { + // Find all scores + collector := generateScores() + filepath.Walk(path, compilePath(path, opts, collector)) + + // Remove score files + for i := range scores { + scores[i].GenerateOutputPath(opts) + if helpers.Exists(scores[i].OutputPath) { + if err := os.Remove(scores[i].OutputPath); err != nil { + log.WithFields(log.Fields{ + "error": err, + "score": scores[i], + }).Error("unable to delete file") + } + } + } + + // Remove LaTeX resources + texPath := filepath.Join(opts.OutputDir, opts.Name+".tex") + if helpers.Exists(texPath) { + if err := os.Remove(texPath); err != nil { + log.WithFields(log.Fields{ + "error": err, + "file": texPath, + }).Error("unable to delete file") + } + } + for i := range opts.LatexResources { + path := filepath.Join(opts.OutputDir, filepath.Base(opts.LatexResources[i])) + err := os.RemoveAll(path) + if err != nil { + log.WithFields(log.Fields{ + "error": err, + "resource": path, + }).Error("unable to delete file") + } + } + + // Remove target songbook + songbookPath := filepath.Join(opts.OutputDir, opts.Name+".pdf") + if helpers.Exists(songbookPath) { + if err := os.Remove(songbookPath); err != nil { + log.WithFields(log.Fields{ + "error": err, + "songbook": songbookPath, + }).Error("unable to delete file") + } + } +}