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

255 lines
6.5 KiB
Go
Raw Normal View History

2020-07-04 14:58:03 +00:00
package beegopro
import (
"fmt"
2020-10-09 15:33:58 +00:00
"io/ioutil"
2021-01-10 16:24:24 +00:00
"net/url"
2021-01-07 18:09:11 +00:00
"strings"
2020-10-09 15:33:58 +00:00
"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"
)
2021-01-07 18:09:11 +00:00
var GitRemotePath utils.DocValue
2020-07-04 14:58:03 +00:00
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),
2021-01-07 18:09:11 +00:00
GitRemotePath: "",
//GitRemotePath: "https://github.com/beego/beego-pro.git",
Branch: "master",
//GitLocalPath: system.BeegoHome + "/beego-pro",
2020-07-21 14:24:42 +00:00
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
func (c *Container) initTemplateOption() {
2021-01-07 18:09:11 +00:00
c.GetLocalPath()
2020-07-05 06:54:26 +00:00
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)
}
}
2020-11-26 06:15:00 +00:00
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"
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")
2021-01-07 18:09:11 +00:00
}
//form https://github.com/beego/beego-pro.git
//get beego/beego-pro
func (c *Container) GetLocalPath() {
if c.UserOption.GitLocalPath != "" {
return
}
if GitRemotePath != "" {
c.UserOption.GitRemotePath = GitRemotePath.String()
}
if c.UserOption.GitRemotePath == "" {
c.UserOption.GitRemotePath = "https://github.com/beego/beego-pro.git"
}
2021-01-10 16:24:24 +00:00
parse, err := url.Parse(c.UserOption.GitRemotePath)
if err != nil {
beeLogger.Log.Fatalf("git GitRemotePath err, %s", err.Error())
return
}
s := parse.Path
s = strings.TrimRight(s, ".git")
c.UserOption.GitLocalPath = system.BeegoHome + s
2021-01-07 18:09:11 +00:00
}