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

View File

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