Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
added a clean command
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Sep 9, 2018
1 parent b12366b commit d405e2c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 12 deletions.
58 changes: 56 additions & 2 deletions costello/store/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package store

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/gobuffalo/packr/costello/parser"
"github.com/karrick/godirwalk"
Expand All @@ -14,6 +18,21 @@ var _ Store = &Disk{}
type Disk struct {
DBPath string
DBPackage string
global map[string]*parser.File
}

func NewDisk(path string, pkg string) *Disk {
if len(path) == 0 {
path = filepath.Join("internal", "packr-packed")
}
if len(pkg) == 0 {
pkg = "packed"
}
return &Disk{
DBPath: path,
DBPackage: pkg,
global: map[string]*parser.File{},
}
}

func (d *Disk) FileNames(box *parser.Box) ([]string, error) {
Expand Down Expand Up @@ -57,11 +76,46 @@ func (d *Disk) Pack(box *parser.Box) error {
}

func (d *Disk) Clean(box *parser.Box) error {
panic("not implemented")
root := box.PackageDir
if len(root) == 0 {
return errors.New("can't clean an empty box.PackageDir")
}
return Clean(root)
}

func (d *Disk) Close() error {
panic("not implemented")
fmt.Println("not implemented")
return nil
}

func Clean(root string) error {
if len(root) == 0 {
pwd, err := os.Getwd()
if err != nil {
return errors.WithStack(err)
}
root = pwd
}
callback := func(path string, info *godirwalk.Dirent) error {
base := filepath.Base(path)
if base == ".git" || base == "vendor" || base == "node_modules" {
return filepath.SkipDir
}
if info == nil || info.IsDir() {
return nil
}
if strings.Contains(base, "-packr.go") {
err := os.Remove(path)
if err != nil {
return errors.WithStack(err)
}
}
return nil
}
return godirwalk.Walk(root, &godirwalk.Options{
FollowSymbolicLinks: true,
Callback: callback,
})
}

// resolve file paths (only) for the boxes
Expand Down
31 changes: 31 additions & 0 deletions packr/cmd/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"os"

"github.com/gobuffalo/packr/costello/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var cleanCmd = &cobra.Command{
Use: "clean",
Short: "removes any *-packr.go files",
RunE: func(cmd *cobra.Command, args []string) error {
pwd, err := os.Getwd()
if err != nil {
return errors.WithStack(err)
}
args = append(args, pwd)
for _, root := range args {
if err := store.Clean(root); err != nil {
return errors.WithStack(err)
}
}
return nil
},
}

func init() {
rootCmd.AddCommand(cleanCmd)
}
11 changes: 1 addition & 10 deletions packr/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,13 @@ var rootCmd = &cobra.Command{
fmt.Printf("Found %d boxes\n", len(boxes))

// "pack" boxes
d := &store.Disk{
DBPath: "./internal/packed",
DBPackage: "./internal/packed",
}
d := store.NewDisk("", "")
for _, b := range boxes {
if err := d.Pack(b); err != nil {
return errors.WithStack(err)
}
}
return d.Close()

// resolve file paths (only) for the boxes
// compile "global" db
// resolve files for boxes to point at global db
// write global db to disk (default internal/packed)
// write -packr.go files in each package (1 per package)
},
}

Expand Down

0 comments on commit d405e2c

Please sign in to comment.