This commit is contained in:
Alex Yatskov 2015-10-29 18:51:17 +09:00
parent 835535b0f0
commit ceca011023
2 changed files with 44 additions and 19 deletions

56
core.go
View File

@ -24,14 +24,22 @@ package goldsmith
import ( import (
"path/filepath" "path/filepath"
"sync"
"github.com/bmatcuk/doublestar" "github.com/bmatcuk/doublestar"
) )
type stage struct {
input chan File
output chan File
}
type goldsmith struct { type goldsmith struct {
Context Context
files []File stages []stage
files chan File
wg sync.WaitGroup
} }
func NewGoldsmith(srcPath, dstPath string) (Applier, error) { func NewGoldsmith(srcPath, dstPath string) (Applier, error) {
@ -49,33 +57,49 @@ func (gs *goldsmith) scan() error {
return err return err
} }
gs.files = make(chan File, len(matches))
for _, match := range matches { for _, match := range matches {
path, err := filepath.Rel(gs.srcPath, match) path, err := filepath.Rel(gs.srcPath, match)
if err != nil { if err != nil {
return err return err
} }
file := File{path, make(map[string]interface{})} gs.files <- File{path, make(map[string]interface{})}
gs.files = append(gs.files, file)
} }
return nil return nil
} }
func (gs *goldsmith) ApplyAll(p Processor) Applier { func (gs *goldsmith) stage() stage {
return gs.Apply(p, "*") s := stage{output: make(chan File)}
} if len(gs.stages) == 0 {
s.input = gs.files
func (gs *goldsmith) Apply(p Processor, pattern string) Applier { } else {
inputFiles := make(chan File) s.input = gs.stages[len(gs.stages)-1].output
outputFiles := make(chan File)
for _, file := range gs.files {
if matched, _ := doublestar.Match(pattern, file.Path); matched {
inputFiles <- file
}
} }
p.ProcessFiles(inputFiles, outputFiles) gs.stages = append(gs.stages, s)
return s
}
func (gs *goldsmith) Apply(p Processor) Applier {
return gs.ApplyTo(p, "*")
}
func (gs *goldsmith) ApplyTo(p Processor, pattern string) Applier {
s := gs.stage()
gs.wg.Add(1)
go func() {
p.ProcessFiles(s.input, s.output)
gs.wg.Done()
}()
return gs
}
func (gs *goldsmith) Wait() Applier {
gs.wg.Wait()
return gs return gs
} }

View File

@ -32,10 +32,11 @@ type File struct {
} }
type Processor interface { type Processor interface {
ProcessFiles(inputFiles chan File, outputFiles chan File) error ProcessFiles(input chan File, output chan File) error
} }
type Applier interface { type Applier interface {
ApplyAll(p Processor) Applier Apply(p Processor) Applier
Apply(p Processor, pattern string) Applier ApplyTo(p Processor, pattern string) Applier
Wait() Applier
} }