2015-12-18 08:03:30 +00:00
|
|
|
package goldsmith
|
|
|
|
|
|
|
|
import (
|
2018-12-08 19:18:51 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-01-09 02:08:36 +00:00
|
|
|
"io"
|
2015-12-20 14:18:58 +00:00
|
|
|
"os"
|
2022-05-16 03:26:09 +00:00
|
|
|
"path/filepath"
|
2015-12-20 08:38:53 +00:00
|
|
|
"runtime"
|
2015-12-18 08:03:30 +00:00
|
|
|
"sync"
|
2018-12-08 19:18:51 +00:00
|
|
|
"time"
|
2015-12-18 08:03:30 +00:00
|
|
|
)
|
|
|
|
|
2019-04-08 01:02:57 +00:00
|
|
|
// Context corresponds to the current link in the chain and provides methods
|
|
|
|
// that enable plugins to inject new files into the chain.
|
2024-03-04 01:46:38 +00:00
|
|
|
type Context struct {
|
2024-03-04 02:13:42 +00:00
|
|
|
chain *chainState
|
2022-01-09 02:08:36 +00:00
|
|
|
plugin Plugin
|
2018-12-08 19:18:51 +00:00
|
|
|
|
2021-05-01 22:12:50 +00:00
|
|
|
filtersExt filterStack
|
|
|
|
filtersInt filterStack
|
2021-04-12 02:13:59 +00:00
|
|
|
|
2021-05-01 22:12:50 +00:00
|
|
|
threads int
|
2022-01-09 02:08:36 +00:00
|
|
|
index int
|
2021-05-01 22:12:50 +00:00
|
|
|
|
2024-03-04 02:09:21 +00:00
|
|
|
filesIn chan *File
|
|
|
|
filesOut chan *File
|
2018-12-08 19:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 01:02:57 +00:00
|
|
|
// CreateFileFrom data creates a new file instance from the provided data buffer.
|
2024-03-04 05:30:32 +00:00
|
|
|
func (self *Context) CreateFileFromReader(relPath string, reader io.Reader) (*File, error) {
|
|
|
|
if filepath.IsAbs(relPath) {
|
|
|
|
return nil, errors.New("file paths must be relative")
|
|
|
|
}
|
|
|
|
|
2023-11-11 19:04:37 +00:00
|
|
|
data, err := io.ReadAll(reader)
|
2022-01-09 02:08:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-04 02:09:21 +00:00
|
|
|
file := &File{
|
2024-03-04 05:30:32 +00:00
|
|
|
relPath: relPath,
|
2024-03-04 02:09:21 +00:00
|
|
|
props: make(FileProps),
|
|
|
|
modTime: time.Now(),
|
|
|
|
size: int64(len(data)),
|
|
|
|
reader: bytes.NewReader(data),
|
|
|
|
index: self.index,
|
2024-02-20 06:20:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 02:09:21 +00:00
|
|
|
return file, nil
|
2018-12-08 19:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 01:02:57 +00:00
|
|
|
// CreateFileFromAsset creates a new file instance from the provided file path.
|
2024-03-04 05:30:32 +00:00
|
|
|
func (self *Context) CreateFileFromAsset(relPath, dataPath string) (*File, error) {
|
|
|
|
if filepath.IsAbs(relPath) {
|
|
|
|
return nil, errors.New("file paths must be relative")
|
2022-05-16 03:26:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:51 +00:00
|
|
|
info, err := os.Stat(dataPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
2024-03-04 05:30:32 +00:00
|
|
|
return nil, errors.New("file paths cannot be directories")
|
2018-12-08 19:18:51 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 02:09:21 +00:00
|
|
|
file := &File{
|
2024-03-04 05:30:32 +00:00
|
|
|
relPath: relPath,
|
2024-03-04 02:09:21 +00:00
|
|
|
props: make(FileProps),
|
|
|
|
modTime: info.ModTime(),
|
|
|
|
size: info.Size(),
|
|
|
|
dataPath: dataPath,
|
|
|
|
index: self.index,
|
2024-02-20 06:20:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 02:09:21 +00:00
|
|
|
return file, nil
|
2018-12-08 19:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 01:02:57 +00:00
|
|
|
// DispatchFile causes the file to get passed to the next link in the chain.
|
2024-03-04 02:09:21 +00:00
|
|
|
func (self *Context) DispatchFile(file *File) {
|
2022-01-09 02:08:36 +00:00
|
|
|
self.filesOut <- file
|
2018-12-08 19:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 01:02:57 +00:00
|
|
|
// DispatchAndCacheFile caches the file data (excluding the metadata), taking
|
|
|
|
// dependencies on any input files that are needed to generate it, and then
|
|
|
|
// passes it to the next link in the chain.
|
2024-03-04 02:09:21 +00:00
|
|
|
func (self *Context) DispatchAndCacheFile(outputFile *File, inputFiles ...*File) {
|
2024-03-04 02:13:42 +00:00
|
|
|
if self.chain.cache != nil {
|
2024-03-04 05:30:32 +00:00
|
|
|
self.chain.cache.storeFile(outputFile, inputFiles)
|
2021-05-01 22:12:50 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 06:20:10 +00:00
|
|
|
self.DispatchFile(outputFile)
|
2015-12-18 08:03:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 01:02:57 +00:00
|
|
|
// RetrieveCachedFile looks up file data (excluding the metadata), given an
|
|
|
|
// output path and any input files that are needed to generate it. The function
|
|
|
|
// will return nil if the desired file is not found in the cache.
|
2024-03-04 02:09:21 +00:00
|
|
|
func (self *Context) RetrieveCachedFile(outputPath string, inputFiles ...*File) *File {
|
|
|
|
var outputFile *File
|
2024-03-04 02:13:42 +00:00
|
|
|
if self.chain.cache != nil {
|
|
|
|
outputFile, _ = self.chain.cache.retrieveFile(self, outputPath, inputFiles)
|
2021-05-01 22:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return outputFile
|
2018-12-08 19:18:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 02:13:59 +00:00
|
|
|
// Specify internal filter(s) that exclude files from being processed.
|
2024-03-04 01:46:38 +00:00
|
|
|
func (self *Context) Filter(filters ...Filter) *Context {
|
2022-01-09 02:08:36 +00:00
|
|
|
for _, filter := range filters {
|
|
|
|
self.filtersInt.push(filter, self.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
return self
|
2021-05-01 22:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Specify the maximum number of threads used for processing.
|
2024-03-04 01:46:38 +00:00
|
|
|
func (self *Context) Threads(threads int) *Context {
|
2022-01-09 02:08:36 +00:00
|
|
|
self.threads = threads
|
|
|
|
return self
|
2021-04-12 02:13:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 01:46:38 +00:00
|
|
|
func (self *Context) step() {
|
2022-01-09 02:08:36 +00:00
|
|
|
defer close(self.filesOut)
|
2015-12-18 08:03:30 +00:00
|
|
|
|
2022-01-09 02:08:36 +00:00
|
|
|
if initializer, ok := self.plugin.(Initializer); ok {
|
|
|
|
if err := initializer.Initialize(self); err != nil {
|
2024-03-04 02:13:42 +00:00
|
|
|
self.chain.fault(self.plugin.Name(), nil, err)
|
2015-12-20 08:38:53 +00:00
|
|
|
return
|
2015-12-18 08:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 02:08:36 +00:00
|
|
|
if self.filesIn != nil {
|
|
|
|
processor, _ := self.plugin.(Processor)
|
2016-07-10 20:05:23 +00:00
|
|
|
|
2022-01-09 02:08:36 +00:00
|
|
|
threads := self.threads
|
2021-05-01 22:12:50 +00:00
|
|
|
if threads < 1 {
|
|
|
|
threads = runtime.NumCPU()
|
|
|
|
}
|
|
|
|
|
2016-01-10 11:17:35 +00:00
|
|
|
var wg sync.WaitGroup
|
2021-05-01 22:12:50 +00:00
|
|
|
for i := 0; i < threads; i++ {
|
2016-01-10 11:17:35 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2022-01-09 02:08:36 +00:00
|
|
|
for inputFile := range self.filesIn {
|
|
|
|
if processor != nil && self.filtersInt.accept(inputFile) && self.filtersExt.accept(inputFile) {
|
2023-11-11 19:04:37 +00:00
|
|
|
if _, err := inputFile.Seek(0, io.SeekStart); err != nil {
|
2024-03-04 02:13:42 +00:00
|
|
|
self.chain.fault("core", inputFile, err)
|
2016-01-10 11:17:35 +00:00
|
|
|
}
|
2022-01-09 02:08:36 +00:00
|
|
|
if err := processor.Process(self, inputFile); err != nil {
|
2024-03-04 02:13:42 +00:00
|
|
|
self.chain.fault(self.plugin.Name(), inputFile, err)
|
2016-01-10 11:17:35 +00:00
|
|
|
}
|
2016-07-04 03:08:31 +00:00
|
|
|
} else {
|
2024-02-22 02:53:17 +00:00
|
|
|
self.DispatchFile(inputFile)
|
2015-12-20 08:38:53 +00:00
|
|
|
}
|
2015-12-18 08:03:30 +00:00
|
|
|
}
|
2016-01-10 11:17:35 +00:00
|
|
|
}()
|
|
|
|
}
|
2022-01-09 02:08:36 +00:00
|
|
|
|
2016-01-10 11:17:35 +00:00
|
|
|
wg.Wait()
|
2015-12-18 08:03:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 02:08:36 +00:00
|
|
|
if finalizer, ok := self.plugin.(Finalizer); ok {
|
|
|
|
if err := finalizer.Finalize(self); err != nil {
|
2024-03-04 02:13:42 +00:00
|
|
|
self.chain.fault(self.plugin.Name(), nil, err)
|
2015-12-18 08:03:30 +00:00
|
|
|
}
|
2015-12-19 11:51:23 +00:00
|
|
|
}
|
2015-12-18 08:03:30 +00:00
|
|
|
}
|