-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuilder.go
102 lines (86 loc) · 3.17 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package builder
import (
"time"
"github.com/darklab8/fl-darkstat/darkstat/common/static_common"
"github.com/darklab8/fl-darkstat/darkstat/common/types"
"github.com/darklab8/fl-darkstat/darkstat/front/static_front"
"github.com/darklab8/fl-darkstat/darkstat/settings"
"github.com/darklab8/go-utils/utils/timeit"
"github.com/darklab8/go-utils/utils/utils_filepath"
"github.com/darklab8/go-utils/utils/utils_types"
)
type Builder struct {
components []*Component
darkPages []*Component
TractorTabName string
}
type BuilderOption func(b *Builder)
func NewBuilder(opts ...BuilderOption) *Builder {
b := &Builder{
TractorTabName: settings.Env.TractorTabName,
}
for _, opt := range opts {
opt(b)
}
return b
}
func WithTractorTabName(tractor_tab_name string) BuilderOption {
return func(b *Builder) {
b.TractorTabName = tractor_tab_name
}
}
func (b *Builder) RegComps(components ...*Component) {
b.components = append(b.components, components...)
}
func (b *Builder) RegDark(components ...*Component) {
b.darkPages = append(b.darkPages, components...)
}
func (b *Builder) build(components []*Component, params types.GlobalParams, filesystem *Filesystem) {
timeit.NewTimerF(func(m *timeit.Timer) {
results := make(chan WriteResult)
for _, comp := range components {
go func(comp *Component) {
results <- comp.Write(params)
}(comp)
}
for range components {
result := <-results
filesystem.WriteToMem(result.realpath, result.bytes)
}
}, timeit.WithMsg("wrote components"))
timeit.NewTimerF(func(m *timeit.Timer) {
target_folder := utils_filepath.Join(utils_types.FilePath(params.Buildpath.ToString()), "static")
filesystem.WriteToMem(utils_filepath.Join(target_folder, "htmx.js"), []byte(static_front.HtmxJs))
filesystem.WriteToMem(utils_filepath.Join(target_folder, "preload.js"), []byte(static_front.PreloadJs))
filesystem.WriteToMem(utils_filepath.Join(target_folder, "sortable.js"), []byte(static_front.SortableJs))
filesystem.WriteToMem(utils_filepath.Join(target_folder, "custom.js"), []byte(static_front.CustomJS))
filesystem.WriteToMem(utils_filepath.Join(target_folder, "common", "favicon.ico"), []byte(static_common.FaviconIco))
}, timeit.WithMsg("gathered static assets"))
}
func (b *Builder) BuildAll() *Filesystem {
build_root := utils_types.FilePath("build")
filesystem := NewFileystem(build_root)
staticPrefix := "static/"
siteRoot := settings.Env.SiteRoot
b.build(b.components, types.GlobalParams{
Buildpath: "",
Theme: types.ThemeLight,
TractorTabName: b.TractorTabName,
SiteRoot: siteRoot,
StaticRoot: siteRoot + staticPrefix,
OppositeThemeRoot: siteRoot + "dark.html",
Heading: settings.Env.AppHeading,
Timestamp: time.Now().UTC(),
}, filesystem)
// // Implement dark theme later
// // u need only Index page rebuilded, not all of them ^_^
// b.build(b.dark_pages, types.GlobalParams{
// Buildpath: "",
// Theme: types.ThemeDark,
// SiteRoot: siteRoot,
// StaticRoot: siteRoot + staticPrefix,
// OppositeThemeRoot: siteRoot,
// Heading: settings.Conf.AppHeading,
// }, filesystem)
return filesystem
}