summaryrefslogtreecommitdiffstats
path: root/deps/deps.go
blob: 8f3d81632c7c4cfdef6501cd5f612907257c5aac (plain)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package deps

import (
	"sync"
	"time"

	"github.com/pkg/errors"
	"go.uber.org/atomic"

	"github.com/gohugoio/hugo/cache/filecache"
	"github.com/gohugoio/hugo/common/loggers"
	"github.com/gohugoio/hugo/config"
	"github.com/gohugoio/hugo/helpers"
	"github.com/gohugoio/hugo/hugofs"
	"github.com/gohugoio/hugo/langs"
	"github.com/gohugoio/hugo/media"
	"github.com/gohugoio/hugo/resources/page"

	"github.com/gohugoio/hugo/metrics"
	"github.com/gohugoio/hugo/output"
	"github.com/gohugoio/hugo/resources"
	"github.com/gohugoio/hugo/source"
	"github.com/gohugoio/hugo/tpl"
	jww "github.com/spf13/jwalterweatherman"
)

// Deps holds dependencies used by many.
// There will be normally only one instance of deps in play
// at a given time, i.e. one per Site built.
type Deps struct {

	// The logger to use.
	Log *loggers.Logger `json:"-"`

	// Used to log errors that may repeat itself many times.
	DistinctErrorLog *helpers.DistinctLogger

	// Used to log warnings that may repeat itself many times.
	DistinctWarningLog *helpers.DistinctLogger

	// The templates to use. This will usually implement the full tpl.TemplateManager.
	tmpl tpl.TemplateHandler

	// We use this to parse and execute ad-hoc text templates.
	textTmpl tpl.TemplateParseFinder

	// The file systems to use.
	Fs *hugofs.Fs `json:"-"`

	// The PathSpec to use
	*helpers.PathSpec `json:"-"`

	// The ContentSpec to use
	*helpers.ContentSpec `json:"-"`

	// The SourceSpec to use
	SourceSpec *source.SourceSpec `json:"-"`

	// The Resource Spec to use
	ResourceSpec *resources.Spec

	// The configuration to use
	Cfg config.Provider `json:"-"`

	// The file cache to use.
	FileCaches filecache.Caches

	// The translation func to use
	Translate func(translationID string, args ...interface{}) string `json:"-"`

	// The language in use. TODO(bep) consolidate with site
	Language *langs.Language

	// The site building.
	Site page.Site

	// All the output formats available for the current site.
	OutputFormatsConfig output.Formats

	templateProvider ResourceProvider
	WithTemplate     func(templ tpl.TemplateManager) error `json:"-"`

	// Used in tests
	OverloadedTemplateFuncs map[string]interface{}

	translationProvider ResourceProvider

	Metrics metrics.Provider

	// Timeout is configurable in site config.
	Timeout time.Duration

	// BuildStartListeners will be notified before a build starts.
	BuildStartListeners *Listeners

	// Atomic flags set during a build.
	BuildFlags *BuildFlags

	*globalErrHandler
}

type globalErrHandler struct {
	// Channel for some "hard to get to" build errors
	buildErrors chan error
}

// SendErr sends the error on a channel to be handled later.
// This can be used in situations where returning and aborting the current
// operation isn't practical.
func (e *globalErrHandler) SendError(err error) {
	if e.buildErrors != nil {
		select {
		case e.buildErrors <- err:
		default:
		}
		return
	}

	jww.ERROR.Println(err)
}

func (e *globalErrHandler) StartErrorCollector() chan error {
	e.buildErrors = make(chan error, 10)
	return e.buildErrors
}

// Listeners represents an event listener.
type Listeners struct {
	sync.Mutex

	// A list of funcs to be notified about an event.
	listeners []func()
}

// Add adds a function to a Listeners instance.
func (b *Listeners) Add(f func()) {
	if b == nil {
		return
	}
	b.Lock()
	defer b.Unlock()
	b.listeners = append(b.listeners, f)
}

// Notify executes all listener functions.
func (b *Listeners) Notify() {
	b.Lock()
	defer b.Unlock()
	for _, notify := range b.listeners {
		notify()
	}
}

// ResourceProvider is used to create and refresh, and clone resources needed.
type ResourceProvider interface {
	Update(deps *Deps) error
	Clone(deps *Deps) error
}

func (d *Deps) Tmpl() tpl.TemplateHandler {
	return d.tmpl
}

func (d *Deps) TextTmpl() tpl.TemplateParseFinder {
	return d.textTmpl
}

func (d *Deps) SetTmpl(tmpl tpl.TemplateHandler) {
	d.tmpl = tmpl
}

func (d *Deps) SetTextTmpl(tmpl tpl.TemplateParseFinder) {
	d.textTmpl = tmpl
}

// LoadResources loads translations and templates.
func (d *Deps) LoadResources() error {
	// Note that the translations need to be loaded before the templates.
	if err := d.translationProvider.Update(d); err != nil {
		return errors.Wrap(err, "loading translations")
	}

	if err := d.templateProvider.Update(d); err != nil {
		return errors.Wrap(err, "loading templates")
	}

	return nil
}

// New initializes a Dep struct.
// Defaults are set for nil values,
// but TemplateProvider, TranslationProvider and Language are always required.
func New(cfg DepsCfg) (*Deps, error) {
	var (
		logger = cfg.Logger
		fs     = cfg.Fs
	)

	if cfg.TemplateProvider == nil {
		panic("Must have a TemplateProvider")
	}

	if cfg.TranslationProvider == nil {
		panic("Must have a TranslationProvider")
	}

	if cfg.Language == nil {
		panic("Must have a Language")
	}

	if logger == nil {
		logger = loggers.NewErrorLogger()
	}

	if fs == nil {
		// Default to the production file system.
		fs = hugofs.NewDefault(cfg.Language)
	}

	if cfg.MediaTypes == nil {
		cfg.MediaTypes = media.DefaultTypes
	}

	if cfg.OutputFormats == nil {
		cfg.OutputFormats = output.DefaultFormats
	}

	ps, err := helpers.NewPathSpec(fs, cfg.Language, logger)

	if err != nil {
		return nil, errors.Wrap(err, "create PathSpec")
	}

	fileCaches, err := filecache.NewCaches(ps)
	if err != nil {
		return nil, errors.WithMessage(err, "failed to create file caches from configuration")
	}

	resourceSpec, err := resources.NewSpec(ps, fileCaches, logger, cfg.OutputFormats, cfg.MediaTypes)
	if err != nil {
		return nil, err
	}

	contentSpec, err := helpers.NewContentSpec(cfg.Language, logger, ps.BaseFs.Content.Fs)
	if err != nil {
		return nil, err
	}

	sp := source.NewSourceSpec(ps, fs.Source)

	timeoutms := cfg.Language.GetInt("timeout")
	if timeoutms <= 0 {
		timeoutms = 3000
	}

	distinctErrorLogger := helpers.NewDistinctLogger(logger.ERROR)
	distinctWarnLogger := helpers.NewDistinctLogger(logger.WARN)

	d := &Deps{
		Fs:                      fs