This repository was archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparse.go
More file actions
92 lines (73 loc) · 2.03 KB
/
Copy pathparse.go
File metadata and controls
92 lines (73 loc) · 2.03 KB
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
package gasx
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
)
var tRgxp = regexp.MustCompile(`\$([a-zA-Z0-9]*){((.|\s)*?)[^\\]}\$`)
// ParseFiles parse and compile GOS files
func (builder *Builder) ParseFiles(files []File) error {
for _, fileInfo := range files {
filename := strings.TrimSuffix(fileInfo.Path, "."+fileInfo.Extension) + "_gas.go"
// TODO: Add hook here
fileBytes, err := ioutil.ReadFile(fileInfo.Path)
if err != nil {
return fmt.Errorf("error while opening %s: \n%s", fileInfo.Path, err.Error())
}
fileBody := string(fileBytes)
parsedFileBody, err := builder.CompileFile(fileInfo, fileBody)
if err != nil {
return err
}
osFile, err := os.Create(filename)
if err != nil {
if err == os.ErrPermission {
fmt.Println("Run: \"chmod 777 -R $GOPATH/pkg/mod\"")
}
return err
}
_, err = osFile.Write([]byte(parsedFileBody))
if err != nil {
return err
}
err = osFile.Close()
if err != nil {
return err
}
}
return nil
}
// ParseFile compile GOS file to pure golang
func (builder *Builder) CompileFile(fileInfo File, fileBody string) (string, error) {
var lenDiff int
matches := tRgxp.FindAllStringSubmatchIndex(fileBody, -1)
for _, match := range matches {
n := func(i int) int {
return i + lenDiff
}
var (
blockStart = n(match[0])
blockEnd = n(match[1])
nameStart = n(match[2])
nameEnd = n(match[3])
name = fileBody[nameStart:nameEnd]
valueStart = n(match[4])
valueEnd = n(match[5])
value = fileBody[valueStart:valueEnd]
)
newVal, err := builder.RenderBlock(&BlockInfo{
Name: string(name),
Value: strings.TrimSpace(value),
FileInfo: fileInfo,
FileBytes: fileBody,
})
if err != nil {
return "", fmt.Errorf("error while rendering block in %s (name: %s, valS: %d, valE: %d): \n%s", fileInfo.Path, name, valueStart, valueEnd, err.Error())
}
lenDiff += len(newVal) - len(fileBody[blockStart:blockEnd])
fileBody = fileBody[:blockStart] + newVal + fileBody[blockEnd:]
}
return fileBody, nil
}