2023-12-10 17:41:50 +00:00
|
|
|
package goldsmith
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fileExporter struct {
|
|
|
|
targetDir string
|
|
|
|
clean bool
|
|
|
|
tokens map[string]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*fileExporter) Name() string {
|
|
|
|
return "exporter"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *fileExporter) Initialize(context *Context) error {
|
|
|
|
self.tokens = make(map[string]bool)
|
|
|
|
context.Threads(1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *fileExporter) Process(context *Context, file *File) error {
|
|
|
|
slicePath := func(path string) string {
|
|
|
|
if filepath.IsAbs(path) {
|
|
|
|
var err error
|
|
|
|
if path, err = filepath.Rel("/", path); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Clean(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
for token := slicePath(file.relPath); token != "."; token = filepath.Dir(token) {
|
|
|
|
self.tokens[token] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.export(self.targetDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *fileExporter) Finalize(context *Context) error {
|
|
|
|
if !self.clean {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-10 17:51:46 +00:00
|
|
|
return filepath.Walk(self.targetDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if path == self.targetDir {
|
|
|
|
return nil
|
2023-12-10 17:41:50 +00:00
|
|
|
}
|
|
|
|
|
2023-12-10 17:51:46 +00:00
|
|
|
relPath, err := filepath.Rel(self.targetDir, path)
|
2023-12-10 17:41:50 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tokenized, _ := self.tokens[relPath]; !tokenized {
|
2023-12-10 17:51:46 +00:00
|
|
|
if err := os.RemoveAll(path); err != nil {
|
2023-12-10 17:41:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-10 17:51:46 +00:00
|
|
|
return nil
|
|
|
|
})
|
2023-12-10 17:41:50 +00:00
|
|
|
}
|