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

View File

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