This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
169 lines (140 loc) · 4.05 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package goldsmith
import (
"bytes"
"errors"
"io"
"os"
"path/filepath"
"runtime"
"sync"
"time"
)
// Context corresponds to the current link in the chain and provides methods
// that enable plugins to inject new files into the chain.
type Context struct {
goldsmith *Goldsmith
plugin Plugin
filtersExt filterStack
filtersInt filterStack
threads int
index int
filesIn chan *File
filesOut chan *File
}
// CreateFileFrom data creates a new file instance from the provided data buffer.
func (self *Context) CreateFileFromReader(sourcePath string, reader io.Reader) (*File, error) {
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
file := &File{
relPath: sourcePath,
props: make(map[string]Prop),
modTime: time.Now(),
size: int64(len(data)),
reader: bytes.NewReader(data),
index: self.index,
}
return file, nil
}
// CreateFileFromAsset creates a new file instance from the provided file path.
func (self *Context) CreateFileFromAsset(sourcePath, dataPath string) (*File, error) {
if filepath.IsAbs(sourcePath) {
return nil, errors.New("source paths must be relative")
}
if filepath.IsAbs(dataPath) {
return nil, errors.New("data paths must be relative")
}
info, err := os.Stat(dataPath)
if err != nil {
return nil, err
}
if info.IsDir() {
return nil, errors.New("assets must be files")
}
file := &File{
relPath: sourcePath,
props: make(map[string]Prop),
modTime: info.ModTime(),
size: info.Size(),
dataPath: dataPath,
index: self.index,
}
return file, nil
}
// DispatchFile causes the file to get passed to the next link in the chain.
func (self *Context) DispatchFile(file *File) {
self.filesOut <- file
}
// 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.
func (self *Context) DispatchAndCacheFile(outputFile *File, inputFiles ...*File) {
if self.goldsmith.cache != nil {
self.goldsmith.cache.storeFile(self, outputFile, inputFiles)
}
self.filesOut <- outputFile
}
// 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.
func (self *Context) RetrieveCachedFile(outputPath string, inputFiles ...*File) *File {
var outputFile *File
if self.goldsmith.cache != nil {
outputFile, _ = self.goldsmith.cache.retrieveFile(self, outputPath, inputFiles)
}
return outputFile
}
// Specify internal filter(s) that exclude files from being processed.
func (self *Context) Filter(filters ...Filter) *Context {
for _, filter := range filters {
self.filtersInt.push(filter, self.index)
}
return self
}
// Specify the maximum number of threads used for processing.
func (self *Context) Threads(threads int) *Context {
self.threads = threads
return self
}
func (self *Context) step() {
defer close(self.filesOut)
if initializer, ok := self.plugin.(Initializer); ok {
if err := initializer.Initialize(self); err != nil {
self.goldsmith.fault(self.plugin.Name(), nil, err)
return
}
}
if self.filesIn != nil {
processor, _ := self.plugin.(Processor)
threads := self.threads
if threads < 1 {
threads = runtime.NumCPU()
}
var wg sync.WaitGroup
for i := 0; i < threads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for inputFile := range self.filesIn {
if processor != nil && self.filtersInt.accept(inputFile) && self.filtersExt.accept(inputFile) {
if _, err := inputFile.Seek(0, io.SeekStart); err != nil {
self.goldsmith.fault("core", inputFile, err)
}
if err := processor.Process(self, inputFile); err != nil {
self.goldsmith.fault(self.plugin.Name(), inputFile, err)
}
} else {
self.filesOut <- inputFile
}
}
}()
}
wg.Wait()
}
if finalizer, ok := self.plugin.(Finalizer); ok {
if err := finalizer.Finalize(self); err != nil {
self.goldsmith.fault(self.plugin.Name(), nil, err)
}
}
}