-
Notifications
You must be signed in to change notification settings - Fork 0
/
blip.go
258 lines (222 loc) · 5.71 KB
/
blip.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package internal
import (
"errors"
"fmt"
"github.com/fsnotify/fsnotify"
"io/fs"
"io/ioutil"
"log"
"os"
"path"
"strings"
)
var Version = "0.8.10"
var Name = "Blip Template Compiler"
type BlipOptions struct {
Sdir string
Rebuild bool
Watch bool
SupportBranch string
RenderLineNumbers bool
}
func GteProcess(opt *BlipOptions) {
fmt.Printf(" __ __ ___ ___ __ ___ ___ __ \n|__) | | |__) | |__ |\\/| |__) | /\\ | |__ /__` \n|__) |___ | | | |___ | | | |___ /~~\\ | |___ .__/ \n")
fmt.Printf("Blip Processing: Version: %s\n", Version)
fmt.Printf("Rebuild All: %v\n", opt.Rebuild)
fmt.Printf("Render: LineNumbers %v\n", opt.Rebuild)
fmt.Printf("Source folder: %s\n", opt.Sdir)
processDir(opt.Sdir, opt)
fmt.Printf("\n")
if opt.Watch {
watchFiles(opt)
}
}
func watchFiles(opt *BlipOptions) {
fmt.Printf("---Watching for file changes in %s\n", opt.Sdir)
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Start listening for events.
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
// log.Println("event:", event)
if event.Has(fsnotify.Create) {
fi, err := os.Stat(event.Name)
if err != nil {
// fmt.Printf("Unable to stat: %s", event.Name)
return
}
if fi.IsDir() {
go func() {
fmt.Printf("**** Please restart the process after adding directories!!!\n")
fmt.Printf("**** Please restart the process after adding directories!!!\n")
fmt.Printf("**** Please restart the process after adding directories!!!\n")
fmt.Printf("**** Please restart the process after adding directories!!!\n")
//fmt.Printf("Watching dir: %s\n", event.Name)
//watcher.Add(event.Name)
}()
return
}
}
if event.Has(fsnotify.Write) {
// log.Println("modified file:", event.Name)
slashIdx := strings.LastIndex(event.Name, "/")
fi, err := os.Stat(event.Name)
if err != nil {
return
}
sDir := event.Name[0:slashIdx]
go func() {
defer func() {
if err := recover(); err != nil {
fmt.Printf("Error: %v", err)
}
}()
processFile(sDir, fi, opt)
}()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
// Add a path.
var dirs []string
getAllSubDirectories(&dirs, opt.Sdir)
for _, dir := range dirs {
err = watcher.Add(dir)
fmt.Printf("Watching dir: %s\n", dir)
if err != nil {
log.Fatal(err)
}
}
// Block main goroutine forever.
<-make(chan struct{})
}
func getAllSubDirectories(list *[]string, sdir string) []string {
files, err := ioutil.ReadDir(sdir)
if err != nil {
log.Fatal(err)
}
*list = append(*list, sdir)
// fmt.Printf("> %s\n", sdir)
for _, file := range files {
if file.IsDir() {
getAllSubDirectories(list, sdir+"/"+file.Name())
}
}
return *list
}
func processDir(sdir string, opt *BlipOptions) {
files, err := ioutil.ReadDir(sdir)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if file.IsDir() {
processDir(sdir+"/"+file.Name(), opt)
} else {
processFile(sdir, file, opt)
}
}
}
func processFile(sdir string, file fs.FileInfo, opt *BlipOptions) {
fileType := "text"
if strings.HasSuffix(file.Name(), ".go") {
return
}
if !strings.Contains(file.Name(), ".blip") {
return
}
trimmedName := file.Name()
if !strings.HasSuffix(file.Name(), ".blip") {
sections := strings.Split(file.Name(), ".")
fileType = sections[len(sections)-1]
trimmedName = strings.TrimSuffix(trimmedName, "."+fileType)
}
// destDir
destDir := findGoMod() + "/blipped/" + path.Base(sdir)
err := os.MkdirAll(destDir, 0755)
if err != nil {
panic(fmt.Sprintf("Error creating directory: %s: %s", destDir, err))
return
}
destFName := destDir + "/" + trimmedName + ".go"
sourceFName := sdir + "/" + file.Name()
fmt.Printf("\nProcess blip: %s --> %s", sourceFName, destDir)
inBytes, err := ioutil.ReadFile(sourceFName)
if err != nil {
panic(fmt.Sprintf("Error reading file: %s: %s", sourceFName, err))
return
}
sfi, err := os.Stat(sourceFName)
if err != nil {
fmt.Printf(" -- Unable to stat: %s : %s\n", sourceFName, err)
return
}
dfi, err := os.Stat(destFName)
if err == nil {
if sfi.ModTime().Before(dfi.ModTime()) {
if !opt.Rebuild {
fmt.Printf("-- Not modified \n")
return
}
}
}
lex := NewLexer(string(inBytes), sourceFName)
parser := New(lex)
parser.Parse()
dirSects := strings.Split(sdir, "/")
fileSects := strings.Split(file.Name(), ".")
// _ = os.Remove(destFName)
dfile, err := os.Create(destFName)
if err != nil {
fmt.Printf(" -- Error: %s\n", err)
return
}
NewRender(parser).RenderOutput(dfile, dirSects[len(dirSects)-1], fileSects[0], fileType, sourceFName, opt)
err = dfile.Close()
if err != nil {
fmt.Printf(" -- Error: %s\n", err)
return
}
if parser.hasErrors() {
fmt.Printf("\n\n*** Errors found in : %s\n", file.Name())
for _, err := range parser.errors {
fmt.Printf("Error: %d:%d %s\n", err.lineNum, err.linePos, err.msg)
}
fmt.Printf("\n\n")
}
// fmt.Printf("\n")
}
// findGoMod --
// Goes up the directories until it find the go.mod file
func findGoMod() string {
dir, err := os.Getwd()
if err != nil {
panic(fmt.Sprintf("Cannot get directory: %s", err))
}
for {
_, err := os.Stat(dir + "/go.mod")
if err == nil {
// This is the bae directory!
return dir
}
if errors.Is(err, os.ErrNotExist) {
dir, _ = path.Split(strings.TrimSuffix(dir, "/"))
continue
} else {
panic(fmt.Sprintf("Cannot get base directory, searching for directory with go.mod: %s", err))
}
}
}