goldsmith/goldsmith.go

178 lines
4.2 KiB
Go
Raw Normal View History

2019-04-07 20:43:25 +00:00
// Package goldsmith generates static websites.
2015-10-29 09:24:47 +00:00
package goldsmith
2015-10-29 14:26:43 +00:00
import (
2018-12-08 19:18:51 +00:00
"hash"
"hash/crc32"
2016-06-11 23:24:06 +00:00
"os"
2018-12-08 19:18:51 +00:00
"path/filepath"
"sync"
2015-10-29 14:26:43 +00:00
)
2019-04-07 20:43:25 +00:00
// Goldsmith chainable context.
2018-12-08 19:18:51 +00:00
type Goldsmith struct {
sourceDir string
targetDir string
contexts []*Context
contextHasher hash.Hash32
fileRefs map[string]bool
fileFilters []Filter
2018-12-10 01:00:46 +00:00
fileCache *cache
2018-12-08 19:18:51 +00:00
clean bool
2018-12-10 01:00:46 +00:00
errors []error
mutex sync.Mutex
2016-01-13 03:21:30 +00:00
}
2015-12-18 11:30:14 +00:00
// Begin starts a chain, reading the files located in the source directory as input.
2018-12-08 19:18:51 +00:00
func Begin(sourceDir string) *Goldsmith {
2018-12-10 01:00:46 +00:00
goldsmith := &Goldsmith{
2018-12-08 19:18:51 +00:00
sourceDir: sourceDir,
contextHasher: crc32.NewIEEE(),
fileRefs: make(map[string]bool),
}
2018-12-10 01:00:46 +00:00
goldsmith.Chain(new(loader))
return goldsmith
2015-12-18 04:14:39 +00:00
}
2019-04-07 20:43:25 +00:00
// Cache enables caching in cacheDir for the remainder of the chain.
2018-12-10 01:00:46 +00:00
func (goldsmith *Goldsmith) Cache(cacheDir string) *Goldsmith {
goldsmith.fileCache = &cache{cacheDir}
return goldsmith
2015-10-29 14:26:43 +00:00
}
// Clean enables or disables removal of leftover files in the target directory.
func (goldsmith *Goldsmith) Clean(clean bool) *Goldsmith {
goldsmith.clean = clean
return goldsmith
}
2019-04-07 20:43:25 +00:00
// Chain links a plugin instance into the chain.
2018-12-10 01:00:46 +00:00
func (goldsmith *Goldsmith) Chain(plugin Plugin) *Goldsmith {
goldsmith.contextHasher.Write([]byte(plugin.Name()))
2015-11-02 09:07:34 +00:00
2018-12-08 19:18:51 +00:00
context := &Context{
2018-12-10 01:00:46 +00:00
goldsmith: goldsmith,
2018-12-08 19:18:51 +00:00
plugin: plugin,
2018-12-10 01:00:46 +00:00
hash: goldsmith.contextHasher.Sum32(),
2018-12-08 19:18:51 +00:00
outputFiles: make(chan *File),
2016-06-11 23:24:06 +00:00
}
2018-12-10 01:00:46 +00:00
context.fileFilters = append(context.fileFilters, goldsmith.fileFilters...)
2016-06-12 02:38:17 +00:00
2018-12-10 01:00:46 +00:00
if len(goldsmith.contexts) > 0 {
context.inputFiles = goldsmith.contexts[len(goldsmith.contexts)-1].outputFiles
2015-11-02 09:23:13 +00:00
}
2016-06-11 23:24:06 +00:00
2018-12-10 01:00:46 +00:00
goldsmith.contexts = append(goldsmith.contexts, context)
return goldsmith
2015-11-02 09:23:13 +00:00
}
2019-04-07 20:43:25 +00:00
// FilterPush pushes a filter instance on the chain's filter stack.
2018-12-10 01:00:46 +00:00
func (goldsmith *Goldsmith) FilterPush(filter Filter) *Goldsmith {
goldsmith.fileFilters = append(goldsmith.fileFilters, filter)
return goldsmith
2015-10-31 05:12:03 +00:00
}
2019-04-07 20:43:25 +00:00
// FilterPop pops a filter instance from the chain's filter stack.
2018-12-10 01:00:46 +00:00
func (goldsmith *Goldsmith) FilterPop() *Goldsmith {
count := len(goldsmith.fileFilters)
2018-12-08 19:18:51 +00:00
if count == 0 {
panic("attempted to pop empty filter stack")
}
2018-12-10 01:00:46 +00:00
goldsmith.fileFilters = goldsmith.fileFilters[:count-1]
return goldsmith
2015-12-18 04:37:32 +00:00
}
2019-04-07 20:43:25 +00:00
// End stops a chain, writing all recieved files to targetDir as output.
2018-12-10 01:00:46 +00:00
func (goldsmith *Goldsmith) End(targetDir string) []error {
goldsmith.targetDir = targetDir
2018-12-08 19:18:51 +00:00
2018-12-10 01:00:46 +00:00
for _, context := range goldsmith.contexts {
2018-12-08 19:18:51 +00:00
go context.step()
2016-08-21 19:54:44 +00:00
}
2018-12-10 01:00:46 +00:00
context := goldsmith.contexts[len(goldsmith.contexts)-1]
2019-04-03 02:01:36 +00:00
export:
2018-12-08 19:18:51 +00:00
for file := range context.outputFiles {
2019-04-03 02:01:36 +00:00
for _, fileFilter := range goldsmith.fileFilters {
accept, err := fileFilter.Accept(file)
if err != nil {
goldsmith.fault(fileFilter.Name(), file, err)
continue export
}
if !accept {
continue export
}
}
2018-12-10 01:00:46 +00:00
goldsmith.exportFile(file)
2018-12-08 19:18:51 +00:00
}
2015-10-29 09:24:47 +00:00
if goldsmith.clean {
goldsmith.removeUnreferencedFiles()
}
2018-12-10 01:00:46 +00:00
return goldsmith.errors
2016-01-13 03:21:30 +00:00
}
2016-01-10 11:17:35 +00:00
2018-12-10 01:00:46 +00:00
func (goldsmith *Goldsmith) retrieveFile(context *Context, outputPath string, inputFiles []*File) *File {
if goldsmith.fileCache != nil {
outputFile, _ := goldsmith.fileCache.retrieveFile(context, outputPath, inputFiles)
2018-12-08 19:18:51 +00:00
return outputFile
}
return nil
2016-01-13 03:21:30 +00:00
}
2015-10-31 03:30:41 +00:00
2018-12-10 01:00:46 +00:00
func (goldsmith *Goldsmith) storeFile(context *Context, outputFile *File, inputFiles []*File) {
if goldsmith.fileCache != nil {
goldsmith.fileCache.storeFile(context, outputFile, inputFiles)
2018-12-08 19:18:51 +00:00
}
2019-04-03 02:01:36 +00:00
2015-10-29 09:24:47 +00:00
}
2016-01-13 03:21:30 +00:00
2018-12-10 01:00:46 +00:00
func (goldsmith *Goldsmith) removeUnreferencedFiles() {
2018-12-08 19:18:51 +00:00
infos := make(chan fileInfo)
2018-12-10 01:00:46 +00:00
go scanDir(goldsmith.targetDir, infos)
2018-12-08 19:18:51 +00:00
for info := range infos {
2018-12-10 01:00:46 +00:00
if info.path != goldsmith.targetDir {
relPath, _ := filepath.Rel(goldsmith.targetDir, info.path)
if contained, _ := goldsmith.fileRefs[relPath]; !contained {
2018-12-08 19:18:51 +00:00
os.RemoveAll(info.path)
}
}
}
2016-08-21 19:54:44 +00:00
}
2018-01-07 23:42:32 +00:00
2018-12-10 01:00:46 +00:00
func (goldsmith *Goldsmith) exportFile(file *File) error {
if err := file.export(goldsmith.targetDir); err != nil {
2018-12-08 19:18:51 +00:00
return err
}
for pathSeg := cleanPath(file.sourcePath); pathSeg != "."; pathSeg = filepath.Dir(pathSeg) {
2018-12-10 01:00:46 +00:00
goldsmith.fileRefs[pathSeg] = true
2018-12-08 19:18:51 +00:00
}
return nil
2018-01-07 23:42:32 +00:00
}
2018-12-10 01:00:46 +00:00
func (goldsmith *Goldsmith) fault(pluginName string, file *File, err error) {
goldsmith.mutex.Lock()
defer goldsmith.mutex.Unlock()
2018-12-08 19:18:51 +00:00
faultError := &Error{Name: pluginName, Err: err}
if file != nil {
faultError.Path = file.sourcePath
}
2018-12-10 01:00:46 +00:00
goldsmith.errors = append(goldsmith.errors, faultError)
2018-01-07 23:42:32 +00:00
}