-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (87 loc) · 2.13 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
package main
import (
"bufio"
"bytes"
"encoding/xml"
"fmt"
"go/format"
"os"
"path"
"sync"
"text/template"
)
var (
pref = template.New("prefix header").Funcs(funcMap)
typeTemplate = template.New("types").Funcs(funcMap)
settingsTemplate = template.New("settings").Funcs(funcMap)
)
const (
typeFile = "autogen_settings_types.go"
settingsFile = "autogen_settings.go"
)
func writeToFileWithFormat(bytes []byte, f *os.File, formatSrc bool) {
output := bufio.NewWriter(f)
if formatSrc {
src, err := format.Source(bytes)
if err != nil {
fmt.Println("format source error:", err)
}
output.Write(src)
} else {
output.Write(bytes)
}
output.Flush()
}
func main() {
parseCMD()
var v SchemaList
if err := xml.Unmarshal([]byte(ReadFile(*_schema)), &v); err != nil {
fmt.Println("unmarshal xml failed", err)
return
}
var (
typeOutputFile *os.File
settingOutputFile *os.File
)
err := Resolve(func() error {
f, err := os.OpenFile(path.Join(*_outputDir, typeFile), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
typeOutputFile = f
return err
}).Resolve(func() error {
f, err := os.OpenFile(path.Join(*_outputDir, settingsFile), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
settingOutputFile = f
return err
}).GetError()
if err != nil {
fmt.Println(err)
return
}
defer typeOutputFile.Close()
defer settingOutputFile.Close()
typeOutput := bytes.NewBufferString("")
settingOutput := bytes.NewBufferString("")
{
t := mustParse(pref, Prefix)
t.Execute(typeOutput, "")
t.Execute(settingOutput, "")
}
var WorkGroup sync.WaitGroup
WorkGroup.Add(2)
go func() {
defer WorkGroup.Done()
if err := mustParse(typeTemplate, typeTpl).Execute(typeOutput, v); err != nil {
fmt.Println("write enum/flags type defined error:", err)
return
}
writeToFileWithFormat(typeOutput.Bytes(), typeOutputFile, true)
}()
go func() {
defer WorkGroup.Done()
if err := mustParse(settingsTemplate, settingsTpl).Execute(settingOutput, v); err != nil {
fmt.Println("write settings error:", err)
return
}
writeToFileWithFormat(settingOutput.Bytes(), settingOutputFile, true)
}()
WorkGroup.Wait()
}