goldsmith/saver.go

50 lines
958 B
Go
Raw Normal View History

2021-04-25 06:03:12 +00:00
package goldsmith
import (
"os"
"path/filepath"
)
type saver struct {
2021-04-25 15:56:07 +00:00
clean bool
2021-04-25 06:03:12 +00:00
tokens map[string]bool
}
func (*saver) Name() string {
return "saver"
}
2021-04-25 15:56:07 +00:00
func (saver *saver) Initialize(context *Context) error {
saver.tokens = make(map[string]bool)
context.Threads(1)
return nil
}
2021-04-25 06:03:12 +00:00
2021-04-25 15:56:07 +00:00
func (saver *saver) Process(context *Context, file *File) error {
2021-04-25 06:03:12 +00:00
for token := cleanPath(file.sourcePath); token != "."; token = filepath.Dir(token) {
saver.tokens[token] = true
}
return file.export(context.goldsmith.targetDir)
}
func (saver *saver) Finalize(context *Context) error {
if !saver.clean {
return nil
}
infos := make(chan fileInfo)
go scanDir(context.goldsmith.targetDir, infos)
for info := range infos {
if info.path != context.goldsmith.targetDir {
relPath, _ := filepath.Rel(context.goldsmith.targetDir, info.path)
if contained, _ := saver.tokens[relPath]; !contained {
os.RemoveAll(info.path)
}
}
}
return nil
}