Refactoring

This commit is contained in:
Alex Yatskov 2015-12-20 17:58:50 +09:00
parent 20cd5d48ed
commit c0a6e3de95
2 changed files with 34 additions and 34 deletions

View File

@ -27,23 +27,23 @@ import (
"sync" "sync"
) )
type stage struct { type context struct {
gs *goldsmith gs *goldsmith
input, output chan *file input, output chan *file
} }
func newStage(gs *goldsmith) *stage { func newContext(gs *goldsmith) *context {
s := &stage{gs: gs, output: make(chan *file)} ctx := &context{gs: gs, output: make(chan *file)}
if len(gs.stages) > 0 { if len(gs.contexts) > 0 {
s.input = gs.stages[len(gs.stages)-1].output ctx.input = gs.contexts[len(gs.contexts)-1].output
} }
gs.stages = append(gs.stages, s) gs.contexts = append(gs.contexts, ctx)
return s return ctx
} }
func (s *stage) chain(p Plugin) { func (ctx *context) chain(p Plugin) {
defer close(s.output) defer close(ctx.output)
init, _ := p.(Initializer) init, _ := p.(Initializer)
accept, _ := p.(Accepter) accept, _ := p.(Accepter)
@ -51,8 +51,8 @@ func (s *stage) chain(p Plugin) {
fin, _ := p.(Finalizer) fin, _ := p.(Finalizer)
if init != nil { if init != nil {
if err := init.Initialize(s); err != nil { if err := init.Initialize(ctx); err != nil {
s.gs.fault(nil, err) ctx.gs.fault(nil, err)
return return
} }
} }
@ -62,13 +62,13 @@ func (s *stage) chain(p Plugin) {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
for f := range s.input { for f := range ctx.input {
if proc == nil || accept != nil && !accept.Accept(s, f) { if proc == nil || accept != nil && !accept.Accept(ctx, f) {
s.output <- f ctx.output <- f
} else { } else {
f.rewind() f.rewind()
if err := proc.Process(s, f); err != nil { if err := proc.Process(ctx, f); err != nil {
s.gs.fault(f, err) ctx.gs.fault(f, err)
} }
} }
} }
@ -77,8 +77,8 @@ func (s *stage) chain(p Plugin) {
wg.Wait() wg.Wait()
if fin != nil { if fin != nil {
if err := fin.Finalize(s); err != nil { if err := fin.Finalize(ctx); err != nil {
s.gs.fault(nil, err) ctx.gs.fault(nil, err)
} }
} }
} }
@ -87,18 +87,18 @@ func (s *stage) chain(p Plugin) {
// Context Implementation // Context Implementation
// //
func (s *stage) DispatchFile(f File) { func (ctx *context) DispatchFile(f File) {
s.output <- f.(*file) ctx.output <- f.(*file)
} }
func (s *stage) ReferenceFile(path string) { func (ctx *context) ReferenceFile(path string) {
s.gs.referenceFile(path) ctx.gs.referenceFile(path)
} }
func (s *stage) SrcDir() string { func (ctx *context) SrcDir() string {
return s.gs.srcDir return ctx.gs.srcDir
} }
func (s *stage) DstDir() string { func (ctx *context) DstDir() string {
return s.gs.dstDir return ctx.gs.dstDir
} }

View File

@ -30,7 +30,7 @@ import (
type goldsmith struct { type goldsmith struct {
srcDir, dstDir string srcDir, dstDir string
stages []*stage contexts []*context
refs map[string]bool refs map[string]bool
refMtx sync.Mutex refMtx sync.Mutex
@ -43,10 +43,10 @@ func (gs *goldsmith) queueFiles() {
files := make(chan string) files := make(chan string)
go scanDir(gs.srcDir, files, nil) go scanDir(gs.srcDir, files, nil)
s := newStage(gs) ctx := newContext(gs)
go func() { go func() {
defer close(s.output) defer close(ctx.output)
for path := range files { for path := range files {
relPath, err := filepath.Rel(gs.srcDir, path) relPath, err := filepath.Rel(gs.srcDir, path)
if err != nil { if err != nil {
@ -54,7 +54,7 @@ func (gs *goldsmith) queueFiles() {
} }
f := NewFileFromAsset(relPath, path) f := NewFileFromAsset(relPath, path)
s.DispatchFile(f) ctx.DispatchFile(f)
} }
}() }()
} }
@ -135,14 +135,14 @@ func (gs *goldsmith) fault(f *file, err error) {
// //
func (gs *goldsmith) Chain(p Plugin) Goldsmith { func (gs *goldsmith) Chain(p Plugin) Goldsmith {
s := newStage(gs) ctx := newContext(gs)
go s.chain(p) go ctx.chain(p)
return gs return gs
} }
func (gs *goldsmith) Complete() []error { func (gs *goldsmith) Complete() []error {
s := gs.stages[len(gs.stages)-1] ctx := gs.contexts[len(gs.contexts)-1]
for f := range s.output { for f := range ctx.output {
gs.exportFile(f) gs.exportFile(f)
} }