/
container.go
210 lines (187 loc) · 5.46 KB
/
container.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
package radiantpro
import (
"fmt"
"io/ioutil"
"sync"
"time"
"github.com/W3-Engineers-Ltd/Radical/internal/pkg/git"
"github.com/W3-Engineers-Ltd/Radical/internal/pkg/system"
radicalLogger "github.com/W3-Engineers-Ltd/Radical/logger"
"github.com/W3-Engineers-Ltd/Radical/utils"
"github.com/davecgh/go-spew/spew"
"github.com/pelletier/go-toml"
"github.com/spf13/viper"
)
const MDateFormat = "20060102_150405"
var DefaultradiantPro = &Container{
radiantProFile: system.CurrentDir + "/radiantpro.toml",
TimestampFile: system.CurrentDir + "/.radiantpro.timestamp",
GoModFile: system.CurrentDir + "/go.mod",
UserOption: UserOption{
Debug: false,
ContextDebug: false,
Dsn: "",
Driver: "mysql",
ProType: "default",
ApiPrefix: "/api",
EnableModule: nil,
Models: make(map[string]TextModel),
GitRemotePath: "https://github.com/radiant/radiant-pro.git",
Branch: "master",
GitLocalPath: system.RadiantHome + "/radiant-pro",
EnableFormat: true,
SourceGen: "text",
EnableGitPull: true,
Path: map[string]string{
"radiant": ".",
},
EnableGomod: true,
RefreshGitTime: 24 * 3600,
Extend: nil,
},
GenerateTime: time.Now().Format(MDateFormat),
GenerateTimeUnix: time.Now().Unix(),
TmplOption: TmplOption{},
CurPath: system.CurrentDir,
EnableModules: make(map[string]interface{}), // get the user configuration, get the enable module result
FunctionOnce: make(map[string]sync.Once), // get the tmpl configuration, get the function once result
}
func (c *Container) Run() {
// init git refresh cache time
c.initTimestamp()
c.initUserOption()
c.initTemplateOption()
c.initParser()
c.initRender()
c.flushTimestamp()
}
func (c *Container) initUserOption() {
if !utils.IsExist(c.radiantProFile) {
radicalLogger.Log.Fatalf("radiant pro config is not exist, radiant json path: %s", c.radiantProFile)
return
}
viper.SetConfigFile(c.radiantProFile)
err := viper.ReadInConfig()
if err != nil {
radicalLogger.Log.Fatalf("read radiant pro config content, err: %s", err.Error())
return
}
err = viper.Unmarshal(&c.UserOption)
if err != nil {
radicalLogger.Log.Fatalf("radiant pro config unmarshal error, err: %s", err.Error())
return
}
if c.UserOption.Debug {
viper.Debug()
}
if c.UserOption.EnableGomod {
if !utils.IsExist(c.GoModFile) {
radicalLogger.Log.Fatalf("go mod not exist, please create go mod file")
return
}
}
for _, value := range c.UserOption.EnableModule {
c.EnableModules[value] = struct{}{}
}
if len(c.EnableModules) == 0 {
c.EnableModules["*"] = struct{}{}
}
if c.UserOption.Debug {
fmt.Println("c.modules", c.EnableModules)
}
}
func (c *Container) initTemplateOption() {
if c.UserOption.EnableGitPull && (c.GenerateTimeUnix-c.Timestamp.GitCacheLastRefresh > c.UserOption.RefreshGitTime) {
err := git.CloneORPullRepo(c.UserOption.GitRemotePath, c.UserOption.GitLocalPath)
if err != nil {
radicalLogger.Log.Fatalf("radiant pro git clone or pull repo error, err: %s", err)
return
}
c.Timestamp.GitCacheLastRefresh = c.GenerateTimeUnix
}
tree, err := toml.LoadFile(c.UserOption.GitLocalPath + "/" + c.UserOption.ProType + "/radical.toml")
if err != nil {
radicalLogger.Log.Fatalf("radiant tmpl exec error, err: %s", err)
return
}
err = tree.Unmarshal(&c.TmplOption)
if err != nil {
radicalLogger.Log.Fatalf("radiant tmpl parse error, err: %s", err)
return
}
if c.UserOption.Debug {
spew.Dump("tmpl", c.TmplOption)
}
for _, value := range c.TmplOption.Descriptor {
if value.Once {
c.FunctionOnce[value.SrcName] = sync.Once{}
}
}
}
func (c *Container) initParser() {
driver, flag := ParserDriver[c.UserOption.SourceGen]
if !flag {
radicalLogger.Log.Fatalf("parse driver not exit, source gen %s", c.UserOption.SourceGen)
}
driver.RegisterOption(c.UserOption, c.TmplOption)
c.Parser = driver
}
func (c *Container) initRender() {
for _, desc := range c.TmplOption.Descriptor {
_, allFlag := c.EnableModules["*"]
_, moduleFlag := c.EnableModules[desc.Module]
if !allFlag && !moduleFlag {
continue
}
models := c.Parser.GetRenderInfos(desc)
// model table name, model table schema
for _, m := range models {
// some render exec once
syncOnce, flag := c.FunctionOnce[desc.SrcName]
if flag {
syncOnce.Do(func() {
c.renderModel(m)
})
continue
}
c.renderModel(m)
}
}
}
func (c *Container) renderModel(m RenderInfo) {
// todo optimize
m.GenerateTime = c.GenerateTime
render := NewRender(m)
render.Exec(m.Descriptor.SrcName)
if render.Descriptor.IsExistScript() {
err := render.Descriptor.ExecScript(c.CurPath)
if err != nil {
radicalLogger.Log.Fatalf("radiant exec shell error, err: %s", err)
}
}
}
func (c *Container) initTimestamp() {
if utils.IsExist(c.TimestampFile) {
tree, err := toml.LoadFile(c.TimestampFile)
if err != nil {
radicalLogger.Log.Fatalf("radiant timestamp tmpl exec error, err: %s", err)
return
}
err = tree.Unmarshal(&c.Timestamp)
if err != nil {
radicalLogger.Log.Fatalf("radiant timestamp tmpl parse error, err: %s", err)
return
}
}
c.Timestamp.Generate = c.GenerateTimeUnix
}
func (c *Container) flushTimestamp() {
tomlByte, err := toml.Marshal(c.Timestamp)
if err != nil {
radicalLogger.Log.Fatalf("marshal timestamp tmpl parse error, err: %s", err)
}
err = ioutil.WriteFile(c.TimestampFile, tomlByte, 0644)
if err != nil {
radicalLogger.Log.Fatalf("flush timestamp tmpl parse error, err: %s", err)
}
}