Repository navigation
Expand file tree
/
Copy pathbuild.go
More file actions
337 lines (281 loc) · 7.7 KB
/
Copy pathbuild.go
File metadata and controls
337 lines (281 loc) · 7.7 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package core
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/verless/verless/builder"
"github.com/verless/verless/config"
"github.com/verless/verless/fs"
"github.com/verless/verless/model"
"github.com/verless/verless/parser"
"github.com/verless/verless/plugin"
"github.com/verless/verless/theme"
"github.com/verless/verless/writer"
)
const (
// parallelism specifies the number of parallel workers
parallelism int = 4
)
var (
// ErrCannotOverwrite states that verless isn't allowed to delete or
// overwrite the output directory.
ErrCannotOverwrite = errors.New(`cannot overwrite the output directory.
Consider using the --overwrite flag or enable build.overwrite in verless.yml`)
// ErrMissingVersionKey states that the top-level `version` key is
// empty or missing in verless.yml.
ErrMissingVersionKey = errors.New("missing `version` key in verless.yml")
)
// Parser represents a parser that processes Markdown files and converts
// them into a model instance.
type Parser interface {
// ParsePage must be safe for concurrent usage.
ParsePage(src []byte) (model.Page, error)
}
// Builder represents a model builder that maintains a Site instance and
// registers all parsed pages in that instance.
type Builder interface {
// RegisterPage must be safe for concurrent usage.
RegisterPage(page model.Page) error
Dispatch() (model.Site, error)
}
// Writer represents a model writer that renders the site model as HTML
// using the corresponding templates.
type Writer interface {
Write(site model.Site) error
}
// BuildOptions represents options for running a verless build.
type BuildOptions struct {
// OutputDir sets the output directory. If this field is empty, config.OutputDir
// will be used.
OutputDir string
// Overwrite specifies that the output folder can be overwritten.
Overwrite bool
// RecompileTemplates forces a recompilation of all templates.
RecompileTemplates bool
}
// Build provides methods for building a static site.
type Build struct {
Path string
Parser Parser
Builder Builder
Writer Writer
Plugins []plugin.Plugin
Types map[string]*model.Type
Options BuildOptions
}
// New initializes a new Build instance.
func NewBuild(targetFs afero.Fs, path string, options BuildOptions) (*Build, error) {
cfg, err := config.FromFile(path, config.Filename)
if err != nil {
return nil, err
}
if cfg.Version == "" {
return nil, ErrMissingVersionKey
}
outputDir := outputDir(path, &options)
if !fs.IsSafeToRemove(targetFs, outputDir, options.Overwrite || cfg.Build.Overwrite) {
return nil, ErrCannotOverwrite
}
writerCtx := writer.Context{
Fs: targetFs,
Path: path,
OutputDir: outputDir,
Theme: cfg.Theme,
RecompileTemplates: options.RecompileTemplates,
}
themeCfg, err := theme.GetConfig(path, cfg.Theme)
if err != nil {
return nil, err
}
b := Build{
Path: path,
Parser: parser.NewMarkdown(),
Builder: builder.New(&cfg),
Writer: writer.New(writerCtx),
Types: theme.GetTypes(&themeCfg, cfg.Types),
Options: options,
}
plugins := plugin.LoadAll(&cfg, targetFs, outputDir)
for _, key := range cfg.Plugins {
if _, exists := plugins[key]; !exists {
return nil, fmt.Errorf("plugin %s not found", key)
}
b.Plugins = append(b.Plugins, plugins[key]())
}
for _, beforeHook := range cfg.Build.Before {
cmdParts := strings.Split(beforeHook, " ")
cmd := exec.Command(cmdParts[0], cmdParts[1:]...)
cmd.Dir = path
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return nil, err
}
}
if err := theme.RunBeforeHooks(path, cfg.Theme, &themeCfg); err != nil {
return nil, err
}
return &b, nil
}
// Run executes the build using the provided build context.
//
// The current build implementation runs the following steps:
// 1. Read all files in the content directory and send them through a channel.
// 2. Spawn workers reading from that channel.
// 3. Process each received file:
// 3.1. Read the file as a []byte
// 3.2. Parse the []byte and convert it to a model.Page.
// 3.3. Register the page in the builder's site model.
// 3.4. Let each plugin process the page.
// 4. Get the site model from the builder and render it as a website.
// 5. Let each plugin finish its work, e.g. by writing a file.
func (b *Build) Run() error {
var (
files = make(chan string)
errorCh = make(chan error)
collectedErrors = make([]error, 0)
contentDir = filepath.Join(b.Path, config.ContentDir)
)
go func() {
if err := fs.StreamFiles(contentDir, files, fs.MarkdownOnly, fs.NoUnderscores); err != nil {
errorCh <- err
}
}()
if err := b.preProcessing(); err != nil {
return err
}
wg := sync.WaitGroup{}
wg.Add(parallelism)
for i := 0; i < parallelism; i++ {
go func() {
// Process the files received via the files channel.
for file := range files {
if err := b.processFile(contentDir, file); err != nil {
errorCh <- err
}
}
wg.Done()
}()
}
// Close the error channel as soon as all workers have finished.
go func() {
for {
wg.Wait()
close(errorCh)
break
}
}()
for err := range errorCh {
if errors.Is(err, fs.ErrStreaming) {
return err
}
collectedErrors = append(collectedErrors, err)
}
if len(collectedErrors) > 0 {
return fmt.Errorf("errors while processing files: %v", collectedErrors)
}
if err := b.postProcessing(); err != nil {
return err
}
if err := b.render(); err != nil {
return err
}
return nil
}
func (b *Build) render() error {
site, err := b.Builder.Dispatch()
if err != nil {
return err
}
for _, p := range b.Plugins {
if err := p.PreWrite(&site); err != nil {
return err
}
}
if err := b.Writer.Write(site); err != nil {
return err
}
for _, p := range b.Plugins {
if err := p.PostWrite(); err != nil {
return err
}
}
return nil
}
func (b *Build) preProcessing() error {
for _, p := range b.Plugins {
prePostPlugin, ok := p.(plugin.PrePostProcessPlugin)
if !ok {
continue
}
if err := prePostPlugin.PreProcessPages(); err != nil {
return err
}
}
return nil
}
func (b *Build) postProcessing() error {
for _, p := range b.Plugins {
prePostPlugin, ok := p.(plugin.PrePostProcessPlugin)
if !ok {
continue
}
if err := prePostPlugin.PostProcessPages(); err != nil {
return err
}
}
return nil
}
func (b *Build) processFile(contentDir, file string) error {
src, err := ioutil.ReadFile(filepath.Join(contentDir, file))
if err != nil {
return err
}
page, err := b.Parser.ParsePage(src)
if err != nil {
return err
}
// A page like /blog/coffee/making-espresso.md will have /blog/coffee as
// route and making-espresso as ID.
page.Route = filepath.ToSlash(filepath.Dir(file))
page.ID = strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
page.Href = filepath.ToSlash(filepath.Join(page.Route, page.ID))
if err := b.setPageType(&page); err != nil {
return err
}
if err := b.Builder.RegisterPage(page); err != nil {
return err
}
for _, p := range b.Plugins {
if err := p.ProcessPage(&page); err != nil {
return err
}
}
return nil
}
// setPageType sets the Type field of a page if a page type has been
// provided by the user.
func (b *Build) setPageType(page *model.Page) error {
providedType := page.ProvidedType()
if providedType == "" {
return nil
}
if _, exists := b.Types[providedType]; !exists {
return fmt.
Errorf("%s: type %s has not been declared", page.ID, providedType)
}
page.Type = b.Types[providedType]
return nil
}
func outputDir(path string, options *BuildOptions) string {
if options.OutputDir != "" {
return options.OutputDir
}
return filepath.Join(path, config.OutputDir)
}