Simplify API

This commit is contained in:
Alex Yatskov 2015-11-07 13:36:20 +09:00
parent 0ec5a5381d
commit ce668ab5ad
3 changed files with 16 additions and 77 deletions

View File

@ -28,7 +28,6 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"sync"
) )
type stage struct { type stage struct {
@ -124,11 +123,10 @@ func (gs *goldsmith) exportFile(file *File) {
var f *os.File var f *os.File
if f, file.Err = os.Create(absPath); file.Err == nil { if f, file.Err = os.Create(absPath); file.Err == nil {
defer f.Close()
if _, file.Err = f.Write(file.Buff.Bytes()); file.Err == nil { if _, file.Err = f.Write(file.Buff.Bytes()); file.Err == nil {
gs.refs[file.Path] = true gs.refs[file.Path] = true
} }
f.Close()
} }
} }
@ -142,36 +140,19 @@ func (gs *goldsmith) makeStage() stage {
return s return s
} }
func (gs *goldsmith) chainSingle(s stage, cs ChainerSingle, globs []string) { func (gs *goldsmith) chain(s stage, c Chainer) {
defer close(s.output) f, _ := c.(Filterer)
var wg sync.WaitGroup allowed := make(chan *File)
for file := range s.input { defer close(allowed)
wg.Add(1)
go func(f *File) {
defer wg.Done()
if skipFile(f, globs) {
s.output <- f
} else {
s.output <- cs.ChainSingle(gs, f)
}
}(file)
}
wg.Wait() go c.Chain(gs, allowed, s.output)
}
func (gs *goldsmith) chainMultiple(s stage, cm ChainerMultiple, globs []string) {
filtered := make(chan *File)
defer close(filtered)
go cm.ChainMultiple(gs, filtered, s.output)
for file := range s.input { for file := range s.input {
if skipFile(file, globs) { if f.Filter(file.Path) {
s.output <- file s.output <- file
} else { } else {
filtered <- file allowed <- file
} }
} }
} }
@ -207,18 +188,13 @@ func (gs *goldsmith) DstDir() string {
return gs.dstDir return gs.dstDir
} }
func (gs *goldsmith) Chain(ctx Config) Goldsmith { func (gs *goldsmith) Chain(c Chainer, err error) Goldsmith {
if gs.err != nil { if gs.err != nil {
return gs return gs
} }
if gs.err = ctx.Err; gs.err == nil { if gs.err = err; gs.err == nil {
switch c := ctx.Chainer.(type) { go gs.chain(gs.makeStage(), c)
case ChainerSingle:
go gs.chainSingle(gs.makeStage(), c, ctx.Globs)
case ChainerMultiple:
go gs.chainMultiple(gs.makeStage(), c, ctx.Globs)
}
} }
return gs return gs
@ -234,6 +210,5 @@ func (gs *goldsmith) Complete() ([]*File, error) {
} }
gs.cleanupFiles() gs.cleanupFiles()
return files, gs.err return files, gs.err
} }

View File

@ -25,16 +25,16 @@ package goldsmith
import "bytes" import "bytes"
type Goldsmith interface { type Goldsmith interface {
Chain(cfg Config) Goldsmith Chain(c Chainer, err error) Goldsmith
Complete() ([]*File, error) Complete() ([]*File, error)
} }
type ChainerSingle interface { type Chainer interface {
ChainSingle(ctx Context, file *File) *File Chain(ctx Context, input, output chan *File)
} }
type ChainerMultiple interface { type Filterer interface {
ChainMultiple(ctx Context, input, output chan *File) Filter(path string) bool
} }
type File struct { type File struct {
@ -51,9 +51,3 @@ type Context interface {
SrcDir() string SrcDir() string
DstDir() string DstDir() string
} }
type Config struct {
Chainer interface{}
Globs []string
Err error
}

30
util.go
View File

@ -27,36 +27,6 @@ import (
"path/filepath" "path/filepath"
) )
func globMatch(globs []string, name string) (bool, error) {
var (
match bool
err error
)
base := filepath.Base(name)
for _, glob := range globs {
match, err = filepath.Match(glob, base)
if err != nil || match {
break
}
}
return match, err
}
func skipFile(file *File, globs []string) bool {
if file.Err != nil {
return true
}
matched := true
if len(globs) > 0 {
matched, file.Err = globMatch(globs, file.Path)
}
return !matched
}
func scanDir(root string) (files, dirs []string, err error) { func scanDir(root string) (files, dirs []string, err error) {
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {