Add optional doublestar pattern matching

This commit is contained in:
Alex Yatskov 2016-07-03 20:08:31 -07:00
parent 7044e21201
commit 341e1c5bd1
3 changed files with 36 additions and 21 deletions

View File

@ -26,24 +26,27 @@ import (
"os" "os"
"runtime" "runtime"
"sync" "sync"
"github.com/bmatcuk/doublestar"
) )
type context struct { type context struct {
gs *goldsmith gs *goldsmith
plug Plugin plug Plugin
filters []string
input, output chan *file input, output chan *file
} }
func (ctx *context) chain() { func (ctx *context) chain() {
defer close(ctx.output) defer close(ctx.output)
init, _ := ctx.plug.(Initializer) initializer, _ := ctx.plug.(Initializer)
accept, _ := ctx.plug.(Accepter) accepter, _ := ctx.plug.(Accepter)
proc, _ := ctx.plug.(Processor) processor, _ := ctx.plug.(Processor)
fin, _ := ctx.plug.(Finalizer) finalizer, _ := ctx.plug.(Finalizer)
if init != nil { if initializer != nil {
if err := init.Initialize(ctx); err != nil { if err := initializer.Initialize(ctx); err != nil {
ctx.gs.fault(nil, err) ctx.gs.fault(nil, err)
return return
} }
@ -56,15 +59,26 @@ func (ctx *context) chain() {
go func() { go func() {
defer wg.Done() defer wg.Done()
for f := range ctx.input { for f := range ctx.input {
if proc == nil || accept != nil && !accept.Accept(ctx, f) { accept := processor != nil && (accepter == nil || accepter.Accept(ctx, f))
ctx.output <- f if accept && len(ctx.filters) > 0 {
} else { accept = false
for _, filter := range ctx.filters {
if match, _ := doublestar.PathMatch(filter, f.Path()); match {
accept = true
break
}
}
}
if accept {
if _, err := f.Seek(0, os.SEEK_SET); err != nil { if _, err := f.Seek(0, os.SEEK_SET); err != nil {
ctx.gs.fault(f, err) ctx.gs.fault(f, err)
} }
if err := proc.Process(ctx, f); err != nil { if err := processor.Process(ctx, f); err != nil {
ctx.gs.fault(f, err) ctx.gs.fault(f, err)
} }
} else {
ctx.output <- f
} }
} }
}() }()
@ -72,8 +86,8 @@ func (ctx *context) chain() {
wg.Wait() wg.Wait()
} }
if fin != nil { if finalizer != nil {
if err := fin.Finalize(ctx); err != nil { if err := finalizer.Finalize(ctx); err != nil {
ctx.gs.fault(nil, err) ctx.gs.fault(nil, err)
} }
} }

13
core.go
View File

@ -37,11 +37,12 @@ type goldsmith struct {
errorMtx sync.Mutex errorMtx sync.Mutex
} }
func (gs *goldsmith) pushContext(plug Plugin) *context { func (gs *goldsmith) pushContext(plug Plugin, filters []string) *context {
ctx := &context{ ctx := &context{
gs: gs, gs: gs,
plug: plug, plug: plug,
output: make(chan *file), filters: filters,
output: make(chan *file),
} }
if len(gs.contexts) > 0 { if len(gs.contexts) > 0 {
@ -100,8 +101,8 @@ func (gs *goldsmith) fault(f *file, err error) {
// Goldsmith Implementation // Goldsmith Implementation
// //
func (gs *goldsmith) Chain(p Plugin) Goldsmith { func (gs *goldsmith) Chain(p Plugin, filters ...string) Goldsmith {
gs.pushContext(p) gs.pushContext(p, filters)
return gs return gs
} }

View File

@ -31,13 +31,13 @@ import (
) )
type Goldsmith interface { type Goldsmith interface {
Chain(p Plugin) Goldsmith Chain(p Plugin, filters ...string) Goldsmith
End(dstDir string) []error End(dstDir string) []error
} }
func Begin(srcDir string) Goldsmith { func Begin(srcDir string, filters ...string) Goldsmith {
gs := &goldsmith{srcDir: srcDir, refs: make(map[string]bool)} gs := &goldsmith{srcDir: srcDir, refs: make(map[string]bool)}
gs.Chain(new(loader)) gs.Chain(new(loader), filters...)
return gs return gs
} }