2020-07-04 14:58:03 +00:00
|
|
|
package beegopro
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-10-09 15:33:58 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-12-16 05:20:41 +00:00
|
|
|
"github.com/beego/bee/v2/internal/pkg/git"
|
|
|
|
"github.com/beego/bee/v2/internal/pkg/system"
|
|
|
|
beeLogger "github.com/beego/bee/v2/logger"
|
|
|
|
"github.com/beego/bee/v2/utils"
|
2020-07-04 14:58:03 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/pelletier/go-toml"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const MDateFormat = "20060102_150405"
|
|
|
|
|
|
|
|
var DefaultBeegoPro = &Container{
|
|
|
|
BeegoProFile: system.CurrentDir + "/beegopro.toml",
|
2020-07-11 09:13:13 +00:00
|
|
|
TimestampFile: system.CurrentDir + "/.beegopro.timestamp",
|
2020-07-04 14:58:03 +00:00
|
|
|
GoModFile: system.CurrentDir + "/go.mod",
|
2020-07-05 06:54:26 +00:00
|
|
|
UserOption: UserOption{
|
2020-07-21 14:24:42 +00:00
|
|
|
Debug: false,
|
|
|
|
ContextDebug: false,
|
|
|
|
Dsn: "",
|
|
|
|
Driver: "mysql",
|
|
|
|
ProType: "default",
|
|
|
|
ApiPrefix: "/api",
|
|
|
|
EnableModule: nil,
|
|
|
|
Models: make(map[string]TextModel),
|
2020-12-14 05:13:50 +00:00
|
|
|
GitRemotePath: "https://github.com/beego/beego-pro.git",
|
2020-07-21 14:24:42 +00:00
|
|
|
Branch: "master",
|
|
|
|
GitLocalPath: system.BeegoHome + "/beego-pro",
|
|
|
|
EnableFormat: true,
|
|
|
|
SourceGen: "text",
|
|
|
|
EnableGitPull: true,
|
2020-07-04 14:58:03 +00:00
|
|
|
Path: map[string]string{
|
|
|
|
"beego": ".",
|
|
|
|
},
|
2020-07-21 14:24:42 +00:00
|
|
|
EnableGomod: true,
|
|
|
|
RefreshGitTime: 24 * 3600,
|
|
|
|
Extend: nil,
|
2020-07-04 14:58:03 +00:00
|
|
|
},
|
|
|
|
GenerateTime: time.Now().Format(MDateFormat),
|
|
|
|
GenerateTimeUnix: time.Now().Unix(),
|
2020-07-05 06:54:26 +00:00
|
|
|
TmplOption: TmplOption{},
|
2020-07-04 14:58:03 +00:00
|
|
|
CurPath: system.CurrentDir,
|
2020-07-21 14:24:42 +00:00
|
|
|
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
|
2020-07-04 14:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Run() {
|
|
|
|
// init git refresh cache time
|
|
|
|
c.initTimestamp()
|
2020-07-05 06:54:26 +00:00
|
|
|
c.initUserOption()
|
|
|
|
c.initTemplateOption()
|
|
|
|
c.initParser()
|
2020-07-04 14:58:03 +00:00
|
|
|
c.initRender()
|
|
|
|
c.flushTimestamp()
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
func (c *Container) initUserOption() {
|
2020-07-04 14:58:03 +00:00
|
|
|
if !utils.IsExist(c.BeegoProFile) {
|
|
|
|
beeLogger.Log.Fatalf("beego pro config is not exist, beego json path: %s", c.BeegoProFile)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
viper.SetConfigFile(c.BeegoProFile)
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("read beego pro config content, err: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
err = viper.Unmarshal(&c.UserOption)
|
2020-07-04 14:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("beego pro config unmarshal error, err: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-07-05 06:54:26 +00:00
|
|
|
if c.UserOption.Debug {
|
2020-07-04 14:58:03 +00:00
|
|
|
viper.Debug()
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
if c.UserOption.EnableGomod {
|
2020-07-04 14:58:03 +00:00
|
|
|
if !utils.IsExist(c.GoModFile) {
|
|
|
|
beeLogger.Log.Fatalf("go mod not exist, please create go mod file")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
for _, value := range c.UserOption.EnableModule {
|
2020-07-04 14:58:03 +00:00
|
|
|
c.EnableModules[value] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.EnableModules) == 0 {
|
|
|
|
c.EnableModules["*"] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
if c.UserOption.Debug {
|
2020-07-04 14:58:03 +00:00
|
|
|
fmt.Println("c.modules", c.EnableModules)
|
|
|
|
}
|
2020-07-05 06:54:26 +00:00
|
|
|
|
2020-07-04 14:58:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
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)
|
2020-07-04 14:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("beego pro git clone or pull repo error, err: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Timestamp.GitCacheLastRefresh = c.GenerateTimeUnix
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
tree, err := toml.LoadFile(c.UserOption.GitLocalPath + "/" + c.UserOption.ProType + "/bee.toml")
|
2020-07-04 14:58:03 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("beego tmpl exec error, err: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2020-07-05 06:54:26 +00:00
|
|
|
err = tree.Unmarshal(&c.TmplOption)
|
2020-07-04 14:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("beego tmpl parse error, err: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
if c.UserOption.Debug {
|
|
|
|
spew.Dump("tmpl", c.TmplOption)
|
2020-07-04 14:58:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
for _, value := range c.TmplOption.Descriptor {
|
2020-07-21 14:24:42 +00:00
|
|
|
if value.Once {
|
2020-07-04 14:58:03 +00:00
|
|
|
c.FunctionOnce[value.SrcName] = sync.Once{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
func (c *Container) initParser() {
|
|
|
|
driver, flag := ParserDriver[c.UserOption.SourceGen]
|
|
|
|
if !flag {
|
|
|
|
beeLogger.Log.Fatalf("parse driver not exit, source gen %s", c.UserOption.SourceGen)
|
|
|
|
}
|
|
|
|
driver.RegisterOption(c.UserOption, c.TmplOption)
|
|
|
|
c.Parser = driver
|
2020-07-04 14:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) initRender() {
|
2020-07-05 06:54:26 +00:00
|
|
|
for _, desc := range c.TmplOption.Descriptor {
|
2020-07-04 14:58:03 +00:00
|
|
|
_, allFlag := c.EnableModules["*"]
|
|
|
|
_, moduleFlag := c.EnableModules[desc.Module]
|
|
|
|
if !allFlag && !moduleFlag {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
models := c.Parser.GetRenderInfos(desc)
|
2020-07-04 14:58:03 +00:00
|
|
|
// model table name, model table schema
|
2020-07-05 06:54:26 +00:00
|
|
|
for _, m := range models {
|
2020-07-04 14:58:03 +00:00
|
|
|
// some render exec once
|
|
|
|
syncOnce, flag := c.FunctionOnce[desc.SrcName]
|
|
|
|
if flag {
|
|
|
|
syncOnce.Do(func() {
|
|
|
|
c.renderModel(m)
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.renderModel(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:54:26 +00:00
|
|
|
func (c *Container) renderModel(m RenderInfo) {
|
|
|
|
// todo optimize
|
|
|
|
m.GenerateTime = c.GenerateTime
|
2020-07-04 14:58:03 +00:00
|
|
|
render := NewRender(m)
|
|
|
|
render.Exec(m.Descriptor.SrcName)
|
|
|
|
if render.Descriptor.IsExistScript() {
|
|
|
|
err := render.Descriptor.ExecScript(c.CurPath)
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("beego exec shell error, err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) initTimestamp() {
|
|
|
|
if utils.IsExist(c.TimestampFile) {
|
|
|
|
tree, err := toml.LoadFile(c.TimestampFile)
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("beego timestamp tmpl exec error, err: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = tree.Unmarshal(&c.Timestamp)
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("beego 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 {
|
|
|
|
beeLogger.Log.Fatalf("marshal timestamp tmpl parse error, err: %s", err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(c.TimestampFile, tomlByte, 0644)
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatalf("flush timestamp tmpl parse error, err: %s", err)
|
|
|
|
}
|
|
|
|
}
|