Adds clean command
This commit is contained in:
parent
1510422a59
commit
345b99cc0c
37
cmd/clean.go
Normal file
37
cmd/clean.go
Normal file
@ -0,0 +1,37 @@
|
||||
// Copyright © 2016 Jip J. Dekker <jip@dekker.li>
|
||||
//
|
||||
// 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)
|
||||
}
|
76
compiler/clean.go
Normal file
76
compiler/clean.go
Normal file
@ -0,0 +1,76 @@
|
||||
// Copyright © 2016 Jip J. Dekker <jip@dekker.li>
|
||||
//
|
||||
// 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")
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user