Optimization

This commit is contained in:
Alex Yatskov 2015-11-11 17:32:09 +09:00
parent 187f25cddd
commit eee9607a81

View File

@ -51,7 +51,7 @@ func New(srcDir, dstDir string) Goldsmith {
refs: make(map[string]bool), refs: make(map[string]bool),
} }
gs.err = gs.scanFs() gs.scanFs()
return gs return gs
} }
@ -74,19 +74,17 @@ func NewFileRef(path string) *File {
return file return file
} }
func (gs *goldsmith) scanFs() error { func (gs *goldsmith) scanFs() {
fileMatches, _, err := scanDir(gs.srcDir) files := make(chan string)
if err != nil { go scanDir(gs.srcDir, files, nil)
return err
}
s := gs.makeStage() s := gs.makeStage()
go func() { go func() {
defer close(s.output) defer close(s.output)
for _, match := range fileMatches { for path := range files {
relPath, err := filepath.Rel(gs.srcDir, match) relPath, err := filepath.Rel(gs.srcDir, path)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -94,7 +92,7 @@ func (gs *goldsmith) scanFs() error {
file := NewFile(relPath) file := NewFile(relPath)
var f *os.File var f *os.File
if f, file.Err = os.Open(match); file.Err == nil { if f, file.Err = os.Open(path); file.Err == nil {
_, file.Err = file.Buff.ReadFrom(f) _, file.Err = file.Buff.ReadFrom(f)
f.Close() f.Close()
} }
@ -102,20 +100,35 @@ func (gs *goldsmith) scanFs() error {
s.output <- file s.output <- file
} }
}() }()
return nil
} }
func (gs *goldsmith) cleanupFiles() error { func (gs *goldsmith) cleanupFiles() {
fileMatches, dirMatches, err := scanDir(gs.dstDir) files := make(chan string)
if err != nil { dirs := make(chan string)
return err go scanDir(gs.dstDir, files, dirs)
for files != nil || dirs != nil {
var (
path string
ok bool
)
select {
case path, ok = <-files:
if !ok {
files = nil
continue
}
case path, ok = <-dirs:
if !ok {
dirs = nil
continue
}
default:
continue
} }
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 {
panic(err) panic(err)
} }
@ -124,12 +137,8 @@ func (gs *goldsmith) cleanupFiles() error {
continue continue
} }
if err := os.RemoveAll(match); err != nil { os.RemoveAll(path)
return err
} }
}
return nil
} }
func (gs *goldsmith) exportFile(file *File) { func (gs *goldsmith) exportFile(file *File) {
@ -253,7 +262,7 @@ func (gs *goldsmith) Complete() ([]*File, error) {
files = append(files, file) files = append(files, file)
} }
gs.err = gs.cleanupFiles() gs.cleanupFiles()
return files, gs.err return files, gs.err
} }
@ -268,20 +277,31 @@ func cleanPath(path string) string {
return filepath.Clean(path) return filepath.Clean(path)
} }
func scanDir(root string) (files, dirs []string, err error) { func scanDir(root string, files, dirs chan string) {
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { defer func() {
if files != nil {
close(files)
}
if dirs != nil {
close(dirs)
}
}()
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
if info.IsDir() { if info.IsDir() {
dirs = append(dirs, path) if dirs != nil {
dirs <- path
}
} else { } else {
files = append(files, path) if files != nil {
files <- path
}
} }
return nil return nil
}) })
return
} }