General improvements

This commit is contained in:
Alex Yatskov 2015-11-07 16:40:13 +09:00
parent b08542715a
commit 0eaccdee1d

View File

@ -53,15 +53,14 @@ func New(srcDir, dstDir string) Goldsmith {
refs: make(map[string]bool), refs: make(map[string]bool),
} }
gs.scanFs() gs.err = gs.scanFs()
return gs return gs
} }
func (gs *goldsmith) scanFs() { func (gs *goldsmith) scanFs() error {
fileMatches, _, err := scanDir(gs.srcDir) fileMatches, _, err := scanDir(gs.srcDir)
if err != nil { if err != nil {
gs.err = err return err
return
} }
s := gs.makeStage() s := gs.makeStage()
@ -86,35 +85,38 @@ func (gs *goldsmith) scanFs() {
s.output <- file s.output <- file
} }
}() }()
return nil
} }
func (gs *goldsmith) cleanupFiles() { func (gs *goldsmith) cleanupFiles() error {
fileMatches, _, err := scanDir(gs.dstDir) fileMatches, dirMatches, err := scanDir(gs.dstDir)
if err != nil { if err != nil {
gs.err = err return err
return
} }
for _, path := range fileMatches { matches := append(fileMatches, dirMatches...)
relPath, err := filepath.Rel(gs.dstDir, path)
for _, match := range matches {
relPath, err := filepath.Rel(gs.dstDir, match)
if err != nil { if err != nil {
gs.err = err panic(err)
return
} }
if contained, _ := gs.refs[relPath]; !contained { if contained, _ := gs.refs[relPath]; contained {
if err := os.Remove(path); err != nil { continue
gs.err = err }
return
} if err := os.RemoveAll(match); err != nil {
return err
} }
} }
return nil
} }
func (gs *goldsmith) exportFile(file *File) { func (gs *goldsmith) exportFile(file *File) {
defer func() { defer func() { file.Buff = nil }()
file.Buff = nil
}()
if file.Err != nil { if file.Err != nil {
return return
@ -129,7 +131,7 @@ func (gs *goldsmith) exportFile(file *File) {
if f, file.Err = os.Create(absPath); file.Err == nil { if f, file.Err = os.Create(absPath); file.Err == nil {
defer f.Close() defer f.Close()
if _, file.Err = f.Write(file.Buff.Bytes()); file.Err == nil { if _, file.Err = f.Write(file.Buff.Bytes()); file.Err == nil {
gs.refs[file.Path] = true gs.RefFile(file.Path)
} }
} }
} }
@ -190,8 +192,14 @@ func (gs *goldsmith) RefFile(path string) error {
return fmt.Errorf("absolute paths are not supported: %s", path) return fmt.Errorf("absolute paths are not supported: %s", path)
} }
path = filepath.Clean(path)
for {
gs.refs[path] = true gs.refs[path] = true
if path == "." {
return nil return nil
}
path = filepath.Dir(path)
}
} }
func (gs *goldsmith) SrcDir() string { func (gs *goldsmith) SrcDir() string {
@ -215,6 +223,10 @@ func (gs *goldsmith) Chain(c Chainer, err error) Goldsmith {
} }
func (gs *goldsmith) Complete() ([]*File, error) { func (gs *goldsmith) Complete() ([]*File, error) {
if gs.err != nil {
return nil, gs.err
}
s := gs.stages[len(gs.stages)-1] s := gs.stages[len(gs.stages)-1]
var files []*File var files []*File
@ -223,6 +235,6 @@ func (gs *goldsmith) Complete() ([]*File, error) {
files = append(files, file) files = append(files, file)
} }
gs.cleanupFiles() gs.err = gs.cleanupFiles()
return files, gs.err return files, gs.err
} }