1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 20:20:55 +00:00
bee/internal/app/module/beegopro/container.go

236 lines
6.0 KiB
Go
Raw Normal View History

2020-07-04 14:58:03 +00:00
package beegopro
import (
"fmt"
"io/ioutil"
"sync"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/pelletier/go-toml"
"github.com/spf13/viper"
2020-07-04 14:58:03 +00:00
"github.com/beego/bee/internal/pkg/git"
"github.com/beego/bee/internal/pkg/system"
beeLogger "github.com/beego/bee/logger"
"github.com/beego/bee/utils"
)
const MDateFormat = "20060102_150405"
var DefaultBeegoPro = &Container{
BeegoProFile: system.CurrentDir + "/beegopro.toml",
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),
GitRemotePath: "https://github.com/beego/beego-pro.git",
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()
}
func (c *Container) InitToml() {
if exist := utils.IsExist(c.BeegoProFile); exist {
beeLogger.Log.Fatalf("file beegopro.toml already exists")
}
sourceFile := c.UserOption.GitLocalPath + "/beegopro.toml"
if !utils.IsExist(c.UserOption.GitLocalPath) {
c.getTemplateFromGit()
}
input, err := ioutil.ReadFile(sourceFile)
if err != nil {
beeLogger.Log.Fatalf("read beegopro.toml file err, %s", err.Error())
return
}
err = ioutil.WriteFile(c.BeegoProFile, input, 0644)
if err != nil {
beeLogger.Log.Fatalf("create beegopro.toml file err, %s", err.Error())
return
}
beeLogger.Log.Success("Successfully created file beegopro.toml")
}
func (c *Container) getTemplateFromGit() {
err := git.CloneORPullRepo(c.UserOption.GitRemotePath, c.UserOption.GitLocalPath)
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
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) {
c.getTemplateFromGit()
2020-07-04 14:58:03 +00:00
}
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)
}
}