This commit is contained in:
Alex Yatskov 2015-12-18 13:21:49 +09:00
parent 7954a85a2a
commit 72347cf724
3 changed files with 55 additions and 89 deletions

View File

@ -31,6 +31,10 @@ import (
"time"
)
type stage struct {
input, output chan *file
}
type goldsmith struct {
srcDir, dstDir string
stages []*stage
@ -69,7 +73,7 @@ func (gs *goldsmith) queueFiles(target uint) {
panic(err)
}
s.CopyFile(relPath, path)
gs.CopyFile(relPath, path)
}
}()
}
@ -124,32 +128,12 @@ func (gs *goldsmith) exportFile(f *file) error {
return err
}
gs.refFile(f.path)
gs.RefFile(f.path)
return nil
}
func (gs *goldsmith) refFile(path string) {
gs.mtx.Lock()
defer gs.mtx.Unlock()
if gs.refs == nil {
gs.refs = make(map[string]bool)
}
path = cleanPath(path)
for {
gs.refs[path] = true
if path == "." {
break
}
path = filepath.Dir(path)
}
}
func (gs *goldsmith) newStage() *stage {
s := &stage{gs: gs, output: make(chan *file)}
s := &stage{output: make(chan *file)}
if len(gs.stages) > 0 {
s.input = gs.stages[len(gs.stages)-1].output
}
@ -180,12 +164,12 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
batch = append(batch, f)
mtx.Unlock()
atomic.AddInt64(&s.gs.stalled, 1)
atomic.AddInt64(&gs.stalled, 1)
}
}
if init != nil {
if err := init.Initialize(s); err != nil {
if err := init.Initialize(gs); err != nil {
gs.fault(s, nil, err)
return
}
@ -200,7 +184,7 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
wg.Add(1)
go func(f *file) {
defer wg.Done()
if err := proc.Process(s, f); err != nil {
if err := proc.Process(gs, f); err != nil {
gs.fault(s, f, err)
}
dispatch(f)
@ -211,12 +195,12 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
wg.Wait()
if fin != nil {
if err := fin.Finalize(s, batch); err != nil {
if err := fin.Finalize(gs, batch); err != nil {
gs.fault(s, nil, err)
}
for _, f := range batch {
atomic.AddInt64(&s.gs.stalled, -1)
atomic.AddInt64(&gs.stalled, -1)
s.output <- f.(*file)
}
}
@ -244,3 +228,41 @@ func (gs *goldsmith) Complete() bool {
return tainted
}
func (gs *goldsmith) NewFile(path string, data []byte) File {
atomic.AddInt64(&gs.active, 1)
return newFileFromData(path, data)
}
func (gs *goldsmith) CopyFile(dst, src string) File {
atomic.AddInt64(&gs.active, 1)
return newFileFromPath(dst, src)
}
func (gs *goldsmith) RefFile(path string) {
gs.mtx.Lock()
defer gs.mtx.Unlock()
if gs.refs == nil {
gs.refs = make(map[string]bool)
}
path = cleanPath(path)
for {
gs.refs[path] = true
if path == "." {
break
}
path = filepath.Dir(path)
}
}
func (gs *goldsmith) SrcDir() string {
return gs.srcDir
}
func (gs *goldsmith) DstDir() string {
return gs.dstDir
}

View File

@ -1,55 +0,0 @@
/*
* Copyright (c) 2015 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 (
"io"
"sync/atomic"
)
type stage struct {
gs *goldsmith
input, output chan *file
}
func (s *stage) NewFile(path string, r io.Reader) File {
atomic.AddInt64(&s.gs.active, 1)
return nil
}
func (s *stage) CopyFile(dst, src string) File {
atomic.AddInt64(&s.gs.active, 1)
return nil
}
func (s *stage) RefFile(path string) {
s.gs.refFile(path)
}
func (s *stage) SrcDir() string {
return s.gs.srcDir
}
func (s *stage) DstDir() string {
return s.gs.dstDir
}

View File

@ -22,10 +22,7 @@
package goldsmith
import (
"io"
"runtime"
)
import "runtime"
type Goldsmith interface {
Chain(p Plugin) Goldsmith
@ -53,7 +50,7 @@ type File interface {
}
type Context interface {
NewFile(path string, r io.Reader) File
NewFile(path string, data []byte) File
CopyFile(dst, src string) File
RefFile(path string)
@ -61,7 +58,9 @@ type Context interface {
DstDir() string
}
type Plugin interface{}
type Plugin interface {
Name() string
}
type Accepter interface {
Accept(file File) bool