mirror of https://github.com/markbates/pkger.git
added exclude functionality
This commit is contained in:
parent
6d3fc43fdb
commit
843759e98f
|
@ -31,6 +31,7 @@ type packCmd struct {
|
|||
*flag.FlagSet
|
||||
out string
|
||||
help bool
|
||||
exclude slice
|
||||
include slice
|
||||
subs []command
|
||||
}
|
||||
|
@ -48,6 +49,11 @@ func (e *packCmd) Exec(args []string) error {
|
|||
fp := filepath.Join(info.Dir, e.out, outName)
|
||||
os.RemoveAll(fp)
|
||||
|
||||
exclude := []string(e.exclude)
|
||||
if len(exclude) > 0 {
|
||||
parser.DefaultIgnoredFolders = append(parser.DefaultIgnoredFolders, exclude...)
|
||||
}
|
||||
|
||||
decls, err := parser.Parse(info, e.include...)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -104,6 +110,7 @@ func New() (*packCmd, error) {
|
|||
c.FlagSet = flag.NewFlagSet("pkger", flag.ExitOnError)
|
||||
c.BoolVar(&c.help, "h", false, "prints help information")
|
||||
c.StringVar(&c.out, "o", "", "output directory for pkged.go")
|
||||
c.Var(&c.exclude, "exclude", "folder prefix to ignore")
|
||||
c.Var(&c.include, "include", "packages the specified file or directory")
|
||||
c.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage:\n\n")
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
"github.com/markbates/pkger/here"
|
||||
)
|
||||
|
||||
var defaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "testdata"}
|
||||
var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "testdata"}
|
||||
|
||||
func New(her here.Info) (*Parser, error) {
|
||||
return &Parser{
|
||||
|
@ -31,16 +31,6 @@ type Parser struct {
|
|||
err error
|
||||
}
|
||||
|
||||
func ParseLazy(her here.Info, includes ...string) (Decls, error) {
|
||||
p, err := New(her)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.includes = includes
|
||||
|
||||
return p.DeclsLazy()
|
||||
}
|
||||
|
||||
func Parse(her here.Info, includes ...string) (Decls, error) {
|
||||
p, err := New(her)
|
||||
if err != nil {
|
||||
|
@ -120,7 +110,6 @@ func (p *Parser) ParseDir(abs string, mode parser.Mode) ([]*ParsedSource, error)
|
|||
|
||||
fset := token.NewFileSet()
|
||||
pkgs, err := parser.ParseDir(fset, abs, nil, 0)
|
||||
fmt.Printf("%v\n", pkgs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: ParseDir failed %s", err, abs)
|
||||
}
|
||||
|
@ -167,29 +156,6 @@ func (p *Parser) Decls() (Decls, error) {
|
|||
return decls, nil
|
||||
}
|
||||
|
||||
func (p *Parser) DeclsLazy() (Decls, error) {
|
||||
if err := p.parseLazy(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var decls Decls
|
||||
orderedNames := []string{
|
||||
"MkdirAll",
|
||||
"Create",
|
||||
"Include",
|
||||
"Stat",
|
||||
"Open",
|
||||
"Dir",
|
||||
"Walk",
|
||||
}
|
||||
|
||||
for _, n := range orderedNames {
|
||||
decls = append(decls, p.decls[n]...)
|
||||
}
|
||||
|
||||
return decls, nil
|
||||
}
|
||||
|
||||
func (p *Parser) DeclsMap() (map[string]Decls, error) {
|
||||
err := p.Parse()
|
||||
return p.decls, err
|
||||
|
@ -202,26 +168,6 @@ func (p *Parser) Parse() error {
|
|||
return p.err
|
||||
}
|
||||
|
||||
func (p *Parser) parseLazy() error {
|
||||
p.decls = map[string]Decls{}
|
||||
|
||||
root := p.Dir
|
||||
|
||||
if err := p.parseIncludes(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fi, err := os.Stat(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !fi.IsDir() {
|
||||
return fmt.Errorf("%q is not a directory", root)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Parser) parse() error {
|
||||
p.decls = map[string]Decls{}
|
||||
|
||||
|
@ -250,7 +196,7 @@ func (p *Parser) parse() error {
|
|||
}
|
||||
|
||||
base := filepath.Base(path)
|
||||
for _, x := range defaultIgnoredFolders {
|
||||
for _, x := range DefaultIgnoredFolders {
|
||||
if strings.HasPrefix(base, x) {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue