Skip to content

Commit

Permalink
Clean also generated pdf files
Browse files Browse the repository at this point in the history
  • Loading branch information
laenzlinger committed May 20, 2024
1 parent 121c73a commit 2d58910
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test: ## run tests
go test ./...

test-integration: clean docker-build ## run integration tests
$(RUN) clean
$(RUN) generate sheet --all
$(RUN) generate sheet
$(RUN) generate list --landscape
Expand All @@ -35,10 +36,7 @@ lint: ## lint source code
clean: ## clean all output files
rm -f setlist
rm -rf dist
rm -f test/Repertoire/Band/Songs/Frankie\ and\ Johnnie.pdf
rm -f test/Repertoire/Band/Songs/Her\ Song.pdf
go clean -testcache
$(RUN) clean

docker-build: build
docker build -t $(DOCKER_IMAGE):latest .
Expand Down
2 changes: 2 additions & 0 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"

"github.com/laenzlinger/setlist/internal/config"
"github.com/laenzlinger/setlist/internal/sheet"
"github.com/spf13/cobra"
)

Expand All @@ -31,6 +32,7 @@ var cleanCmd = &cobra.Command{
`,
Run: func(_ *cobra.Command, _ []string) {
os.RemoveAll(config.Target())
sheet.Clean(config.NewBand())
},
}

Expand Down
46 changes: 33 additions & 13 deletions internal/sheet/sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"sort"
"strings"

Expand All @@ -32,34 +33,53 @@ type Sheet struct {
}

func AllForBand(band config.Band) error {
songs := map[string]bool{}
songNames, err := songNames(band, []string{".pdf", ".odt", ".md"})
if err != nil {
return err
}
sheets := []Sheet{}
for _, title := range songNames {
s := Sheet{band: band, name: title, content: title}
sheets = append(sheets, s)
}
return merge(sheets, fmt.Sprintf("for all %s songs", band.Name))
}

func Clean(band config.Band) error {
songNames, err := songNames(band, []string{".odt", ".md"})
if err != nil {
return err
}
for _, title := range songNames {
s := Sheet{band: band, name: title}
os.Remove(s.pdfFilePath())
}
return nil
}

func songNames(band config.Band, extensions []string) ([]string, error) {
songNames := []string{}
aSheet := Sheet{band: band}
files, err := os.ReadDir(aSheet.sourceDir())
if err != nil {
return fmt.Errorf("failed to list Band directory: %w", err)
return songNames, fmt.Errorf("failed to list Band directory: %w", err)
}

songs := map[string]bool{}
for _, file := range files {
extraw := filepath.Ext(file.Name())
ext := strings.ToLower(extraw)
if !file.IsDir() && (ext == ".pdf" || ext == ".odt" || ext == ".md") {
ext := filepath.Ext(file.Name())
if !file.IsDir() && (slices.Contains(extensions, ext)) {
songs[strings.TrimSuffix(filepath.Base(file.Name()), ext)] = true
}
}
if len(songs) == 0 {
return fmt.Errorf("no songs found in %s", aSheet.sourceDir())
return songNames, fmt.Errorf("no songs found in %s", aSheet.sourceDir())
}
songNames := []string{}
for song := range songs {
songNames = append(songNames, song)
}
sort.Strings(songNames)
sheets := []Sheet{}
for _, title := range songNames {
s := Sheet{band: band, name: title, content: title}
sheets = append(sheets, s)
}
return merge(sheets, fmt.Sprintf("for all %s songs", band.Name))
return songNames, nil
}

const SectionPrefix = "SECTION:"
Expand Down

0 comments on commit 2d58910

Please sign in to comment.