Updating API

This commit is contained in:
Alex Yatskov 2016-01-10 20:17:35 +09:00
parent bf2bc2c180
commit da057ec544
4 changed files with 88 additions and 61 deletions

View File

@ -30,26 +30,17 @@ import (
type context struct { type context struct {
gs *goldsmith gs *goldsmith
plug Plugin
input, output chan *file input, output chan *file
} }
func newContext(gs *goldsmith) *context { func (ctx *context) chain() {
ctx := &context{gs: gs, output: make(chan *file)}
if len(gs.contexts) > 0 {
ctx.input = gs.contexts[len(gs.contexts)-1].output
}
gs.contexts = append(gs.contexts, ctx)
return ctx
}
func (ctx *context) chain(p Plugin) {
defer close(ctx.output) defer close(ctx.output)
init, _ := p.(Initializer) init, _ := ctx.plug.(Initializer)
accept, _ := p.(Accepter) accept, _ := ctx.plug.(Accepter)
proc, _ := p.(Processor) proc, _ := ctx.plug.(Processor)
fin, _ := p.(Finalizer) fin, _ := ctx.plug.(Finalizer)
if init != nil { if init != nil {
if err := init.Initialize(ctx); err != nil { if err := init.Initialize(ctx); err != nil {
@ -58,26 +49,28 @@ func (ctx *context) chain(p Plugin) {
} }
} }
var wg sync.WaitGroup if ctx.input != nil {
for i := 0; i < runtime.NumCPU(); i++ { var wg sync.WaitGroup
wg.Add(1) for i := 0; i < runtime.NumCPU(); i++ {
go func() { wg.Add(1)
defer wg.Done() go func() {
for f := range ctx.input { defer wg.Done()
if proc == nil || accept != nil && !accept.Accept(ctx, f) { for f := range ctx.input {
ctx.output <- f if proc == nil || accept != nil && !accept.Accept(ctx, f) {
} else { ctx.output <- f
if _, err := f.Seek(0, os.SEEK_SET); err != nil { } else {
ctx.gs.fault(f, err) if _, err := f.Seek(0, os.SEEK_SET); err != nil {
} ctx.gs.fault(f, err)
if err := proc.Process(ctx, f); err != nil { }
ctx.gs.fault(f, err) if err := proc.Process(ctx, f); err != nil {
ctx.gs.fault(f, err)
}
} }
} }
} }()
}() }
wg.Wait()
} }
wg.Wait()
if fin != nil { if fin != nil {
if err := fin.Finalize(ctx); err != nil { if err := fin.Finalize(ctx); err != nil {

View File

@ -39,24 +39,14 @@ type goldsmith struct {
errorMtx sync.Mutex errorMtx sync.Mutex
} }
func (gs *goldsmith) queueFiles() { func (gs *goldsmith) pushContext(plug Plugin) *context {
files := make(chan string) ctx := &context{gs: gs, plug: plug, output: make(chan *file)}
go scanDir(gs.srcDir, files, nil) if len(gs.contexts) > 0 {
ctx.input = gs.contexts[len(gs.contexts)-1].output
}
ctx := newContext(gs) gs.contexts = append(gs.contexts, ctx)
return ctx
go func() {
defer close(ctx.output)
for path := range files {
relPath, err := filepath.Rel(gs.srcDir, path)
if err != nil {
panic(err)
}
f := NewFileFromAsset(relPath, path)
ctx.DispatchFile(f)
}
}()
} }
func (gs *goldsmith) cleanupFiles() { func (gs *goldsmith) cleanupFiles() {
@ -139,12 +129,17 @@ func (gs *goldsmith) fault(f *file, err error) {
// //
func (gs *goldsmith) Chain(p Plugin) Goldsmith { func (gs *goldsmith) Chain(p Plugin) Goldsmith {
ctx := newContext(gs) gs.pushContext(p)
go ctx.chain(p)
return gs return gs
} }
func (gs *goldsmith) Complete() []error { func (gs *goldsmith) End(dstDir string) []error {
gs.dstDir = dstDir
for _, ctx := range gs.contexts {
go ctx.chain()
}
ctx := gs.contexts[len(gs.contexts)-1] ctx := gs.contexts[len(gs.contexts)-1]
for f := range ctx.output { for f := range ctx.output {
gs.exportFile(f) gs.exportFile(f)

44
loader.go Normal file
View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package goldsmith
import "path/filepath"
type loader struct{}
func (*loader) Initialize(ctx Context) error {
files := make(chan string)
go scanDir(ctx.SrcDir(), files, nil)
for path := range files {
relPath, err := filepath.Rel(ctx.SrcDir(), path)
if err != nil {
return err
}
f := NewFileFromAsset(relPath, path)
ctx.DispatchFile(f)
}
return nil
}

View File

@ -29,17 +29,12 @@ import (
type Goldsmith interface { type Goldsmith interface {
Chain(p Plugin) Goldsmith Chain(p Plugin) Goldsmith
Complete() []error End(dstDir string) []error
} }
func New(srcDir, dstDir string) Goldsmith { func Begin(srcDir string) Goldsmith {
gs := &goldsmith{ gs := &goldsmith{srcDir: srcDir, refs: make(map[string]bool)}
srcDir: srcDir, gs.Chain(new(loader))
dstDir: dstDir,
refs: make(map[string]bool),
}
gs.queueFiles()
return gs return gs
} }