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

View File

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

30
util.go
View File

@ -27,36 +27,6 @@ import (
"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) {
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {