This commit is contained in:
Alex Yatskov 2015-12-17 13:06:44 +09:00
parent ac600d147e
commit c5f6bae50e
3 changed files with 22 additions and 38 deletions

View File

@ -39,17 +39,15 @@ type goldsmith struct {
srcDir, dstDir string srcDir, dstDir string
stages []*stage stages []*stage
refs map[string]bool refs map[string]bool
} mtx sync.Mutex
func newFile(path string) *File {
return &File{
Path: cleanPath(path),
Meta: make(map[string]interface{}),
}
} }
func New(srcDir, dstDir string) Goldsmith { func New(srcDir, dstDir string) Goldsmith {
gs := &goldsmith{srcDir: srcDir, dstDir: dstDir} gs := &goldsmith{
srcDir: srcDir,
dstDir: dstDir,
}
gs.queueFiles() gs.queueFiles()
return gs return gs
} }
@ -83,8 +81,11 @@ func (gs *goldsmith) queueFiles() {
} }
func (gs *goldsmith) cleanupFiles() { func (gs *goldsmith) cleanupFiles() {
files := make(chan string) var (
dirs := make(chan string) files = make(chan string)
dirs = make(chan string)
)
go scanDir(gs.dstDir, files, dirs) go scanDir(gs.dstDir, files, dirs)
for files != nil || dirs != nil { for files != nil || dirs != nil {
@ -126,11 +127,6 @@ func (gs *goldsmith) exportFile(file *File) {
return return
} }
if file.Type == FileReference {
gs.refFile(file.Path)
return
}
absPath := filepath.Join(gs.dstDir, file.Path) absPath := filepath.Join(gs.dstDir, file.Path)
if file.Err = os.MkdirAll(path.Dir(absPath), 0755); file.Err != nil { if file.Err = os.MkdirAll(path.Dir(absPath), 0755); file.Err != nil {
return return
@ -192,12 +188,15 @@ func (gs *goldsmith) chain(s *stage, p Plugin) {
} }
func (gs *goldsmith) refFile(path string) { func (gs *goldsmith) refFile(path string) {
path = cleanPath(path) gs.mtx.Lock()
defer gs.mtx.Unlock()
if gs.refs == nil { if gs.refs == nil {
gs.refs = make(map[string]bool) gs.refs = make(map[string]bool)
} }
path = cleanPath(path)
for { for {
gs.refs[path] = true gs.refs[path] = true
if path == "." { if path == "." {

View File

@ -23,21 +23,16 @@
package goldsmith package goldsmith
func (s *stage) NewFile(path string) *File { func (s *stage) NewFile(path string) *File {
file := newFile(path) file := &File{
Path: cleanPath(path),
Meta: make(map[string]interface{}),
}
s.output <- file s.output <- file
return file return file
} }
func (s *stage) NewFileStatic(path string) *File { func (s *stage) RefFile(path string) {
file := s.NewFile(path) s.gs.refFile(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) SrcDir() string { func (s *stage) SrcDir() string {

View File

@ -47,20 +47,11 @@ type Chainer interface {
Chain(ctx Context, input, output chan *File) Chain(ctx Context, input, output chan *File)
} }
type FileType int
const (
FileNormal FileType = iota
FileStatic
FileReference
)
type File struct { type File struct {
Path string Path string
Meta map[string]interface{} Meta map[string]interface{}
Buff bytes.Buffer Buff bytes.Buffer
Err error Err error
Type FileType
} }
type Context interface { type Context interface {
@ -68,6 +59,5 @@ type Context interface {
DstDir() string DstDir() string
NewFile(path string) *File NewFile(path string) *File
NewFileStatic(path string) *File RefFile(path string)
NewFileRef(path string) *File
} }