-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_build.go
174 lines (139 loc) · 3.45 KB
/
1_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
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
package moontpl
import (
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
)
type Link string
type Filename string
type siteBuilder struct {
testBuild bool
printOutput bool
done map[Link]bool
buildQueue []Link
copyLuaSourceFiles bool
}
func newSiteBuilder() *siteBuilder {
builder := &siteBuilder{
done: map[Link]bool{},
buildQueue: []Link{},
}
return builder
}
func (m *Moontpl) queueLink(link string) {
if !m.builder.done[Link(link)] {
m.builder.buildQueue = append(m.builder.buildQueue, Link(link))
}
}
func (m *Moontpl) build(src, dest string) error {
L := m.getState(src)
defer m.putState(L)
output, err := m.RenderFile(src)
if err != nil {
return err
}
if !m.builder.testBuild {
log.Print("exec ", mustRel(mustGetwd(), src), " -> ", mustRel(mustGetwd(), dest))
// ignore error
_ = os.MkdirAll(filepath.Dir(dest), 0755)
if err := os.WriteFile(dest, []byte(output), 0644); err != nil {
panic(err)
}
}
if m.builder.printOutput {
header := "---------[" + src + "]---------"
println(header)
println(output)
println(strings.Repeat("-", len(header)))
println()
}
return nil
}
func (m *Moontpl) BuildAll(outputDir string) error {
defer clear(m.builder.done)
filenames, err := m.GetPageFilenames(m.SiteDir)
if err != nil {
return err
}
for _, p := range filenames {
m.queueLink(p.Link)
}
for len(m.builder.buildQueue) > 0 {
linkWithParams := m.builder.buildQueue[0]
hashIndex := strings.Index(string(linkWithParams), "#")
if hashIndex >= 0 {
linkWithParams = linkWithParams[0:hashIndex]
}
m.builder.buildQueue = m.builder.buildQueue[1:]
if _, ok := m.builder.done[linkWithParams]; ok {
continue
}
src := filepath.Join(m.SiteDir, string(linkWithParams)+".lua")
dest := filepath.Join(outputDir, string(linkWithParams))
_, actualFilename := extractPathParams(src)
if !fsExists(actualFilename) {
log.Printf("LINK NOT FOUND: %s", linkWithParams)
}
if err := m.build(src, dest); err != nil {
panic(err)
}
m.builder.done[linkWithParams] = true
}
plainFiles, err := m.getNonHtmlLuaFilenames(m.SiteDir)
if err != nil {
return err
}
for _, p := range plainFiles {
src := filepath.Join(m.SiteDir, string(p.Link)+".lua")
dest := filepath.Join(outputDir, string(p.Link))
// Build only files with extension such as .css.lua,
// skip .lua files since they could be just regular modules.
if filepath.Ext(dest) == "" {
continue
}
if err := m.build(src, dest); err != nil {
panic(err)
}
}
if !m.builder.testBuild {
if err := m.CopyNonSourceFiles(m.SiteDir, outputDir); err != nil {
return err
}
}
return nil
}
func (m *Moontpl) CopyNonSourceFiles(srcDir, destDir string) error {
return fs.WalkDir(os.DirFS(srcDir), ".", func(p string, dir fs.DirEntry, err error) error {
if err != nil {
return err
}
if dir.IsDir() {
return nil
}
if filepath.Ext(p) == ".lua" && !m.builder.copyLuaSourceFiles {
return nil
}
src := filepath.Join(srcDir, p)
dest := filepath.Join(destDir, p)
// ignore error
_ = os.MkdirAll(filepath.Dir(dest), 0755)
log.Print("copy ", mustRel(mustGetwd(), src), " -> ", mustRel(mustGetwd(), dest))
inputFile, err := os.Open(src)
if err != nil {
return err
}
defer inputFile.Close()
outputFile, err := os.Create(dest)
if err != nil {
return err
}
defer outputFile.Close()
if _, err = io.Copy(outputFile, inputFile); err != nil {
return err
}
return inputFile.Sync()
})
}