生成代码,逐步进行优化
This commit is contained in:
parent
d6c11a5d93
commit
a003612810
@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
var Config = Map{
|
var Config = Map{
|
||||||
"name": "HoTimeDashBoard",
|
"name": "HoTimeDashBoard",
|
||||||
"ID": "2f92h3herh23rh2y8",
|
"id": "2f92h3herh23rh2y8",
|
||||||
"label": "HoTime管理平台",
|
"label": "HoTime管理平台",
|
||||||
"menu": []Map{
|
"menu": []Map{
|
||||||
{"label": "平台首页", "name": "HelloWorld", "icon": "el-icon-s-home"},
|
{"label": "平台首页", "name": "HelloWorld", "icon": "el-icon-s-home"},
|
||||||
|
@ -6,10 +6,12 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MakeCode struct {
|
type MakeCode struct {
|
||||||
@ -56,6 +58,8 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
myInit := strings.Replace(InitTpt, "{{name}}", name, -1)
|
||||||
|
ctrList := ""
|
||||||
if db.Type == "sqlite" {
|
if db.Type == "sqlite" {
|
||||||
tables := db.Select("sqlite_sequence", "name")
|
tables := db.Select("sqlite_sequence", "name")
|
||||||
|
|
||||||
@ -104,7 +108,23 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//创建模块文件
|
||||||
|
//判断文件是否存在
|
||||||
|
_, err := os.OpenFile(name+"/"+v.GetString("name"), os.O_RDONLY, os.ModePerm)
|
||||||
|
if err != nil { //文件不存在,则根据模板创建
|
||||||
|
myCtr := strings.Replace(CtrTpt, "{{name}}", name, -1)
|
||||||
|
myCtr = strings.Replace(myCtr, "{{table}}", v.GetString("name"), -1)
|
||||||
|
_ = os.MkdirAll(name, os.ModeDir)
|
||||||
|
err = ioutil.WriteFile(name+"/"+v.GetString("name")+".go", []byte(myCtr), os.ModeAppend)
|
||||||
|
if err != nil {
|
||||||
|
that.Error.SetError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrList = ctrList + `"` + v.GetString("name") + `":` + v.GetString("name") + `Ctr,`
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
newTables := []Map{}
|
newTables := []Map{}
|
||||||
for k, _ := range that.TableConfig {
|
for k, _ := range that.TableConfig {
|
||||||
newTable := that.TableConfig.GetMap(k)
|
newTable := that.TableConfig.GetMap(k)
|
||||||
@ -116,21 +136,32 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
|||||||
newTable["columns"] = columns
|
newTable["columns"] = columns
|
||||||
}
|
}
|
||||||
that.Config["tables"] = newTables
|
that.Config["tables"] = newTables
|
||||||
|
|
||||||
|
}
|
||||||
|
id := Md5(ObjToStr(that.Config["tables"]))
|
||||||
|
if id == that.Config.GetString("id") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
that.Config["id"] = id
|
||||||
|
//init文件初始化
|
||||||
|
myInit = strings.Replace(myInit, "{{id}}", id, -1)
|
||||||
|
myInit = strings.Replace(myInit, "{{tablesCtr}}", ctrList, -1)
|
||||||
|
_ = os.MkdirAll(name, os.ModeDir)
|
||||||
|
err = ioutil.WriteFile(name+"/init.go", []byte(myInit), os.ModeAppend)
|
||||||
|
if err != nil {
|
||||||
|
that.Error.SetError(err)
|
||||||
}
|
}
|
||||||
//写入配置文件
|
//写入配置文件
|
||||||
var configByte bytes.Buffer
|
var configByte bytes.Buffer
|
||||||
|
|
||||||
err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t")
|
err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t")
|
||||||
//判断配置文件是否序列有变化,有则修改配置,无则不变
|
|
||||||
//fmt.Println(len(btes))
|
|
||||||
if len(btes) != 0 && configByte.String() == string(btes) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_ = os.MkdirAll(filepath.Dir(path), os.ModeDir)
|
_ = os.MkdirAll(filepath.Dir(path), os.ModeDir)
|
||||||
err = ioutil.WriteFile(path, configByte.Bytes(), os.ModeAppend)
|
err = ioutil.WriteFile(path, configByte.Bytes(), os.ModeAppend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
that.Error.SetError(err)
|
that.Error.SetError(err)
|
||||||
}
|
}
|
||||||
|
fmt.Println("有新的代码生成,请重新运行")
|
||||||
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
func (that *MakeCode) Info(table string) string {
|
func (that *MakeCode) Info(table string) string {
|
||||||
reStr := ""
|
reStr := ""
|
||||||
|
77
code/template.go
Normal file
77
code/template.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package code
|
||||||
|
|
||||||
|
var InitTpt = `package {{name}}
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "../../../hotime"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ID = "{{id}}"
|
||||||
|
|
||||||
|
// Project 管理端项目
|
||||||
|
var Project = Proj{
|
||||||
|
//"user": UserCtr,
|
||||||
|
{{tablesCtr}}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
var CtrTpt = `package {{name}}
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "../../../hotime"
|
||||||
|
. "../../../hotime/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
var {{table}}Ctr = Ctr{
|
||||||
|
"info": func(that *Context) {
|
||||||
|
re := that.Db.Get(that.RouterString[1], that.MakeCode.Info(that.RouterString[1]), Map{"id": that.RouterString[2]})
|
||||||
|
that.Display(0, re)
|
||||||
|
},
|
||||||
|
"add": func(that *Context) {
|
||||||
|
inData := that.MakeCode.Add(that.RouterString[1], that.Req)
|
||||||
|
if inData == nil {
|
||||||
|
that.Display(3, "请求参数不足")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
re := that.Db.Insert(that.RouterString[1], inData)
|
||||||
|
|
||||||
|
if re == 0 {
|
||||||
|
that.Display(4, "无法插入对应数据")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
that.Display(0, re)
|
||||||
|
},
|
||||||
|
"update": func(that *Context) {
|
||||||
|
inData := that.MakeCode.Edit(that.RouterString[1], that.Req)
|
||||||
|
if inData == nil {
|
||||||
|
that.Display(3, "没有找到要更新的数据")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
re := that.Db.Update(that.RouterString[1], inData, Map{"id": that.RouterString[2]})
|
||||||
|
|
||||||
|
if re == 0 {
|
||||||
|
that.Display(4, "更新数据失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
that.Display(0, re)
|
||||||
|
},
|
||||||
|
"remove": func(that *Context) {
|
||||||
|
re := that.Db.Delete(that.RouterString[1], Map{"id": that.RouterString[2]})
|
||||||
|
|
||||||
|
if re == 0 {
|
||||||
|
that.Display(4, "删除数据失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
that.Display(0, re)
|
||||||
|
},
|
||||||
|
"search": func(that *Context) {
|
||||||
|
|
||||||
|
columnStr, where := that.MakeCode.Search(that.RouterString[1], that.Req)
|
||||||
|
reData := that.Db.Page(ObjToInt(that.Req.FormValue("page")), ObjToInt(that.Req.FormValue("pageRow"))).
|
||||||
|
Select(that.RouterString[1], columnStr, where)
|
||||||
|
|
||||||
|
that.Display(0, reData)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
`
|
@ -7,11 +7,11 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"codeConfig": {
|
"codeConfig": {
|
||||||
"admin": "example/config/app.json"
|
"admin": "config/app.json"
|
||||||
},
|
},
|
||||||
"db": {
|
"db": {
|
||||||
"sqlite": {
|
"sqlite": {
|
||||||
"path": "example/config/data.db"
|
"path": "config/data.db"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"defFile": [
|
"defFile": [
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
appIns := hotime.Init("example/config/config.json")
|
appIns := hotime.Init("config/config.json")
|
||||||
//RESTfull接口适配
|
//RESTfull接口适配
|
||||||
appIns.SetConnectListener(func(context *hotime.Context) bool {
|
appIns.SetConnectListener(func(context *hotime.Context) bool {
|
||||||
if len(context.RouterString) < 2 || len(context.RouterString) > 3 ||
|
if len(context.RouterString) < 2 || len(context.RouterString) > 3 ||
|
||||||
|
Loading…
Reference in New Issue
Block a user