forked from go-xiaohei/pugo
/
builder.go
86 lines (76 loc) · 1.58 KB
/
builder.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
package builder
import (
"time"
"github.com/inconshreveable/log15"
)
type (
// Builder is object of Builder handlers
Builder struct {
IsBuilding bool
IsWatching bool
Counter int
handlers []Handler
}
// Handler define a step in building process
Handler func(ctx *Context)
)
var (
b = &Builder{
IsBuilding: false,
IsWatching: false,
handlers: []Handler{
ReadSource,
ReadTheme,
AssembleSource,
Compile,
Sync,
},
}
b2 = &Builder{
IsBuilding: false,
IsWatching: false,
handlers: []Handler{
ReadSource,
},
}
)
// Before add handler before building
func Before(fn Handler) {
b.handlers = append([]Handler{fn}, b.handlers...)
}
// After add handler after building
func After(fn Handler) {
b.handlers = append(b.handlers, fn)
}
// Build do a process with Context.
// the context should be prepared before building.
func Build(ctx *Context) {
b.IsBuilding = true
t := time.Now()
for i, h := range b.handlers {
if h(ctx); ctx.Err != nil {
log15.Crit("Build|Fail|%s", ctx.Err.Error())
break
}
log15.Debug("-----|Step|%d|%.3fms", i+1, time.Since(t).Seconds()*1e3)
}
b.IsBuilding = false
b.Counter++
if ctx.Err == nil {
log15.Info("Done|%d Pages|%.1fms", ctx.counter, ctx.Duration()*1e3)
}
}
// Counter return the times of building process ran
func Counter() int {
return b.Counter
}
// Read do a process to read Source with Context.
// It does not build any thing, just read source data.
func Read(ctx *Context) {
for _, h := range b2.handlers {
if h(ctx); ctx.Err != nil {
log15.Crit("Read|Fail|%s", ctx.Err.Error())
break
}
}
}