-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (157 loc) · 3.88 KB
/
main.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
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
package main
import (
"bufio"
"errors"
"flag"
"html/template"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
uuid "github.com/satori/go.uuid"
"github.com/thoas/go-funk"
"github.com/bmatcuk/doublestar"
)
type runtimeInfo struct {
inDir string
outDir string
dummyRun bool
template *template.Template
files []string
filesToTemplateNames map[string]string
}
var runInfo runtimeInfo
func parseFlags() {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
inDir := flag.String("in", dir, "input directory")
outDir := flag.String("out", dir, "output directory")
test := flag.Bool("test", false, "parse and execute but do not actually write anything")
flag.Parse()
if *inDir == dir && *outDir == dir {
*outDir = filepath.Join(*outDir, "generated")
}
runInfo.inDir = *inDir
runInfo.outDir = *outDir
runInfo.dummyRun = *test
}
func filenameToTemplateName(fname string) string {
fname, err := filepath.Rel(runInfo.inDir, fname)
if err != nil {
log.Fatal(err)
}
return filepath.ToSlash(fname)
}
func initializeTemplate() {
funcMap := template.FuncMap{
"minus": func(a, b int) int {
return a - b
},
"plus": func(a, b int) int {
return a + b
},
"minus64": func(a, b int64) int64 {
return a - b
},
"plus64": func(a, b int64) int64 {
return a + b
},
"uuid": func() string {
id, err := uuid.NewV4()
if err == nil {
return id.String()
}
return ""
},
"dict": func(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
},
}
extensions := []string{".html", ".template"}
runInfo.files = []string{}
f, err := doublestar.Glob(filepath.Join(runInfo.inDir, "**"))
if err != nil {
log.Fatal(err)
}
runInfo.files = append(runInfo.files, funk.FilterString(f, func(s string) bool {
fileInfo, err := os.Stat(s)
if err != nil {
log.Fatal(err)
}
isInOut, err := doublestar.PathMatch(filepath.Join(runInfo.outDir, "**"), s)
if err != nil {
log.Fatal(err)
}
return !isInOut && !fileInfo.IsDir() && funk.ContainsString(extensions, filepath.Ext(s))
})...)
runInfo.filesToTemplateNames = make(map[string]string)
runInfo.template = template.New("main").Funcs(funcMap)
for i, fname := range runInfo.files {
fname = filenameToTemplateName(fname)
b, err := ioutil.ReadFile(runInfo.files[i])
if err != nil {
log.Fatal(err)
}
s := string(b)
runInfo.template, err = runInfo.template.New(fname).Parse(s)
if err != nil {
log.Fatal(err)
}
}
}
func chewUp() {
// .html files produce pages; do not parse output files
mainFiles := funk.FilterString(runInfo.files, func(s string) bool {
isInOut, err := doublestar.PathMatch(filepath.Join(runInfo.outDir, "**"), s)
if err != nil {
log.Fatal(err)
}
return strings.HasSuffix(s, ".html") && !isInOut
})
for _, fname := range mainFiles {
fname = filenameToTemplateName(fname)
outPath := filepath.Join(runInfo.outDir, fname)
log.Println("Processing", fname)
if runInfo.dummyRun {
err := runInfo.template.ExecuteTemplate(ioutil.Discard, fname, struct{}{})
if err != nil {
log.Fatal(err)
}
continue
}
os.MkdirAll(filepath.Dir(outPath), os.ModePerm)
f, err := os.Create(outPath)
if err != nil {
log.Fatal(err)
}
w := bufio.NewWriter(f)
err = runInfo.template.ExecuteTemplate(w, fname, struct{}{})
if err != nil {
f.Close()
log.Fatal(err)
}
w.Flush()
f.Close()
}
}
func main() {
parseFlags()
log.Println("Input directory is", runInfo.inDir)
log.Println("Output directory is", runInfo.outDir)
initializeTemplate()
chewUp()
}