More cleanup

This commit is contained in:
Alex Yatskov 2015-12-17 13:20:41 +09:00
parent c5f6bae50e
commit fc04c86d86
3 changed files with 69 additions and 60 deletions

View File

@ -27,6 +27,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"sync" "sync"
"sync/atomic"
) )
type stage struct { type stage struct {
@ -40,16 +41,7 @@ type goldsmith struct {
stages []*stage stages []*stage
refs map[string]bool refs map[string]bool
mtx sync.Mutex mtx sync.Mutex
} count int32
func New(srcDir, dstDir string) Goldsmith {
gs := &goldsmith{
srcDir: srcDir,
dstDir: dstDir,
}
gs.queueFiles()
return gs
} }
func (gs *goldsmith) queueFiles() { func (gs *goldsmith) queueFiles() {
@ -67,7 +59,7 @@ func (gs *goldsmith) queueFiles() {
panic(err) panic(err)
} }
file := s.NewFile(relPath) file := NewFile(relPath)
var f *os.File var f *os.File
if f, file.Err = os.Open(path); file.Err == nil { if f, file.Err = os.Open(path); file.Err == nil {
@ -123,6 +115,11 @@ func (gs *goldsmith) cleanupFiles() {
} }
func (gs *goldsmith) exportFile(file *File) { func (gs *goldsmith) exportFile(file *File) {
defer func() {
file.Buff.Reset()
gs.decFiles()
}()
if file.Err != nil { if file.Err != nil {
return return
} }
@ -141,6 +138,34 @@ func (gs *goldsmith) exportFile(file *File) {
} }
} }
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) incFiles() {
atomic.AddInt32(&gs.count, 1)
}
func (gs *goldsmith) decFiles() {
atomic.AddInt32(&gs.count, -1)
}
func (gs *goldsmith) newStage() *stage { func (gs *goldsmith) newStage() *stage {
s := &stage{ s := &stage{
gs: gs, gs: gs,
@ -187,26 +212,6 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
} }
} }
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) Chain(p Plugin) Goldsmith { func (gs *goldsmith) Chain(p Plugin) Goldsmith {
go gs.chain(gs.newStage(), p) go gs.chain(gs.newStage(), p)
return gs return gs
@ -218,7 +223,6 @@ func (gs *goldsmith) Complete() ([]*File, []error) {
var files []*File var files []*File
for file := range s.output { for file := range s.output {
gs.exportFile(file) gs.exportFile(file)
file.Buff.Reset()
files = append(files, file) files = append(files, file)
} }

View File

@ -22,19 +22,15 @@
package goldsmith package goldsmith
func (s *stage) NewFile(path string) *File {
file := &File{
Path: cleanPath(path),
Meta: make(map[string]interface{}),
}
s.output <- file
return file
}
func (s *stage) RefFile(path string) { func (s *stage) RefFile(path string) {
s.gs.refFile(path) s.gs.refFile(path)
} }
func (s *stage) AddFile(file *File) {
s.gs.incFiles()
s.output <- file
}
func (s *stage) SrcDir() string { func (s *stage) SrcDir() string {
return s.gs.srcDir return s.gs.srcDir
} }

View File

@ -29,6 +29,34 @@ type Goldsmith interface {
Complete() ([]*File, []error) Complete() ([]*File, []error)
} }
func New(srcDir, dstDir string) Goldsmith {
gs := &goldsmith{srcDir: srcDir, dstDir: dstDir}
gs.queueFiles()
return gs
}
type File struct {
Path string
Meta map[string]interface{}
Buff bytes.Buffer
Err error
}
func NewFile(path string) *File {
return &File{
Path: cleanPath(path),
Meta: make(map[string]interface{}),
}
}
type Context interface {
SrcDir() string
DstDir() string
AddFile(file *File)
RefFile(path string)
}
type Plugin interface{} type Plugin interface{}
type Initializer interface { type Initializer interface {
@ -42,22 +70,3 @@ type Finalizer interface {
type Processor interface { type Processor interface {
Process(ctx Context, file *File) bool Process(ctx Context, file *File) bool
} }
type Chainer interface {
Chain(ctx Context, input, output chan *File)
}
type File struct {
Path string
Meta map[string]interface{}
Buff bytes.Buffer
Err error
}
type Context interface {
SrcDir() string
DstDir() string
NewFile(path string) *File
RefFile(path string)
}