This commit is contained in:
Alex Yatskov 2015-10-30 22:05:45 +09:00
parent 512dfcb953
commit 5327d67eae
2 changed files with 21 additions and 30 deletions

View File

@ -35,61 +35,54 @@ type stage struct {
} }
type goldsmith struct { type goldsmith struct {
srcPath, dstPath string
stages []stage stages []stage
files chan File files chan File
wg sync.WaitGroup wg sync.WaitGroup
} }
func NewGoldsmith(srcPath, dstPath string) (Goldsmith, error) { func NewGoldsmith(path string) (Goldsmith, error) {
gs := &goldsmith{srcPath: srcPath, dstPath: dstPath} gs := new(goldsmith)
if err := gs.scan(); err != nil { if err := gs.scan(path); err != nil {
return nil, err return nil, err
} }
return gs, nil return gs, nil
} }
func (gs *goldsmith) scan() error { func (gs *goldsmith) scan(path string) error {
matches, err := doublestar.Glob(filepath.Join(gs.srcPath, "**")) matches, err := doublestar.Glob(filepath.Join(path, "**"))
if err != nil { if err != nil {
return err return err
} }
gs.files = make(chan File, len(matches)) s := stage{
input: nil,
output: make(chan File, len(matches)),
}
for _, match := range matches { for _, match := range matches {
path, err := filepath.Rel(gs.srcPath, match) path, err := filepath.Rel(path, match)
if err != nil { if err != nil {
return err return err
} }
gs.files <- gs.NewFile(path) s.output <- gs.NewFile(path)
} }
gs.stages = append(gs.stages, s)
return nil return nil
} }
func (gs *goldsmith) stage() stage { func (gs *goldsmith) stage() stage {
s := stage{output: make(chan File)} s := stage{
if len(gs.stages) == 0 { input: gs.stages[len(gs.stages)-1].output,
s.input = gs.files output: make(chan File),
} else {
s.input = gs.stages[len(gs.stages)-1].output
} }
gs.stages = append(gs.stages, s) gs.stages = append(gs.stages, s)
return s return s
} }
func (gs *goldsmith) SrcPath(path string) string {
return gs.srcPath
}
func (gs *goldsmith) DstPath(path string) string {
return gs.dstPath
}
func (gs *goldsmith) NewFile(path string) File { func (gs *goldsmith) NewFile(path string) File {
return &file{path, make(map[string]interface{}), nil} return &file{path, make(map[string]interface{}), nil}
} }
@ -106,6 +99,7 @@ func (gs *goldsmith) Apply(p Processor) Goldsmith {
return gs return gs
} }
func (gs *goldsmith) Complete() { func (gs *goldsmith) Complete(path string) error {
gs.wg.Wait() gs.wg.Wait()
return nil
} }

View File

@ -25,9 +25,6 @@ package goldsmith
import "bytes" import "bytes"
type Context interface { type Context interface {
SrcPath(path string) string
DstPath(path string) string
NewFile(path string) File NewFile(path string) File
} }
@ -47,5 +44,5 @@ type Processor interface {
type Goldsmith interface { type Goldsmith interface {
Apply(p Processor) Goldsmith Apply(p Processor) Goldsmith
Complete() Complete(path string) error
} }