-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
136 lines (107 loc) · 3.22 KB
/
build.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
package main
import (
"bytes"
"errors"
"io/fs"
"os"
"path"
"text/template"
"github.com/yosssi/gohtml"
)
type buildConfig struct {
*Config
*template.Template
Data map[string]map[string]any
TemplateName string
}
func BuildTemplated(config *Config) {
dir, err := os.ReadDir(config.TemplatedSrcDir)
check(err)
data := GetData(config.DataFile)
for _, srcDirMeta := range dir {
if !srcDirMeta.IsDir() {
logger.SetOutput(os.Stderr)
logger.Errorf("Template content is not a directory (%v)", path.Join(config.TemplatedSrcDir, srcDirMeta.Name()))
logger.SetOutput(os.Stdout)
continue
}
tmplPath := path.Join(config.TemplateDir, srcDirMeta.Name())
logger.Debugf("Building template (%v)", tmplPath)
tmpl := GetTemplate(config, srcDirMeta.Name())
if tmpl == nil {
continue
}
config := buildConfig{
Config: config,
Template: tmpl,
Data: data,
TemplateName: srcDirMeta.Name(),
}
srcDirPath := path.Join(config.Config.TemplatedSrcDir, config.TemplateName)
srcDir := os.DirFS(srcDirPath)
err := fs.WalkDir(srcDir, ".", config.buildWalk)
check(err)
}
}
func (config *buildConfig) buildWalk(path_ string, d fs.DirEntry, err error) error {
excludeMatch, excludeIndex := PathMatch(path_, config.ExcludePaths)
if excludeMatch != nil {
logger.Debugf("Excluding path (%v): rule %v", *excludeMatch, excludeIndex+1)
if d.IsDir() {
return fs.SkipDir
} else {
return nil
}
}
srcPath := path.Join(config.Config.TemplatedSrcDir, config.TemplateName, path_)
dstPath := path.Join(config.DstDir, path_)
if d.IsDir() {
check(err)
logger.Debugf("Building directory (%v -> %v)", srcPath, dstPath)
err := os.Mkdir(dstPath, config.Config.DstMode.FileMode)
if err != nil && !errors.Is(err, os.ErrExist) {
check(err)
}
} else {
logger.Debugf("Building file (%v -> %v)", srcPath, dstPath)
content, err := os.ReadFile(srcPath)
check(err)
contentTmpl, err := createTemplate("Content").Parse(string(content))
if err != nil {
logger.SetOutput(os.Stderr)
logger.Errorf("Failed to parse template (%v): %v", srcPath, err.Error())
logger.SetOutput(os.Stdout)
return nil
}
// Clone the template so that the original is not affected
// This is used because if Content is entirely whitespace and comments,
// the previous one is used, which we don't want
newTmpl, err := config.Template.Clone()
check(err)
newTmpl.AddParseTree("Content", contentTmpl.Tree)
dst, err := os.Create(dstPath)
check(err)
defer dst.Close()
err = dst.Chmod(config.Config.DstMode.FileMode)
check(err)
data := BuildData(config, path_)
if config.Config.FmtTemplatedHTML && path.Ext(d.Name()) == ".html" {
logger.Debug("Formatting HTML")
preformat := new(bytes.Buffer)
err := newTmpl.Execute(preformat, data)
if err != nil {
logger.Errorf("Error executing template (%v) on file (%v)", config.TemplateName, srcPath)
logger.Infof("Error: %v", err)
}
formatted := gohtml.FormatBytes(preformat.Bytes())
dst.Write(formatted)
} else {
err := newTmpl.Execute(dst, data)
if err != nil {
logger.Errorf("Error executing template (%v) on file (%v)", config.TemplateName, srcPath)
logger.Infof("Error: %v", err)
}
}
}
return nil
}