From c5f6bae50e9e2d398abd6f002b37ff8992822be8 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 17 Dec 2015 13:06:44 +0900 Subject: [PATCH] Cleanup --- goldsmith.go | 31 +++++++++++++++---------------- stage.go | 17 ++++++----------- types.go | 12 +----------- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/goldsmith.go b/goldsmith.go index 464e0a6..0248821 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -39,17 +39,15 @@ type goldsmith struct { srcDir, dstDir string stages []*stage refs map[string]bool -} - -func newFile(path string) *File { - return &File{ - Path: cleanPath(path), - Meta: make(map[string]interface{}), - } + mtx sync.Mutex } func New(srcDir, dstDir string) Goldsmith { - gs := &goldsmith{srcDir: srcDir, dstDir: dstDir} + gs := &goldsmith{ + srcDir: srcDir, + dstDir: dstDir, + } + gs.queueFiles() return gs } @@ -83,8 +81,11 @@ func (gs *goldsmith) queueFiles() { } func (gs *goldsmith) cleanupFiles() { - files := make(chan string) - dirs := make(chan string) + var ( + files = make(chan string) + dirs = make(chan string) + ) + go scanDir(gs.dstDir, files, dirs) for files != nil || dirs != nil { @@ -126,11 +127,6 @@ func (gs *goldsmith) exportFile(file *File) { return } - if file.Type == FileReference { - gs.refFile(file.Path) - return - } - absPath := filepath.Join(gs.dstDir, file.Path) if file.Err = os.MkdirAll(path.Dir(absPath), 0755); file.Err != nil { return @@ -192,12 +188,15 @@ func (gs *goldsmith) chain(s *stage, p Plugin) { } func (gs *goldsmith) refFile(path string) { - path = cleanPath(path) + 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 == "." { diff --git a/stage.go b/stage.go index 425abe8..868f8cf 100644 --- a/stage.go +++ b/stage.go @@ -23,21 +23,16 @@ package goldsmith func (s *stage) NewFile(path string) *File { - file := newFile(path) + file := &File{ + Path: cleanPath(path), + Meta: make(map[string]interface{}), + } s.output <- file return file } -func (s *stage) NewFileStatic(path string) *File { - file := s.NewFile(path) - file.Type = FileStatic - return file -} - -func (s *stage) NewFileRef(path string) *File { - file := s.NewFile(path) - file.Type = FileReference - return file +func (s *stage) RefFile(path string) { + s.gs.refFile(path) } func (s *stage) SrcDir() string { diff --git a/types.go b/types.go index 6c6ce17..a664758 100644 --- a/types.go +++ b/types.go @@ -47,20 +47,11 @@ type Chainer interface { Chain(ctx Context, input, output chan *File) } -type FileType int - -const ( - FileNormal FileType = iota - FileStatic - FileReference -) - type File struct { Path string Meta map[string]interface{} Buff bytes.Buffer Err error - Type FileType } type Context interface { @@ -68,6 +59,5 @@ type Context interface { DstDir() string NewFile(path string) *File - NewFileStatic(path string) *File - NewFileRef(path string) *File + RefFile(path string) }