mirror of
https://github.com/beego/bee.git
synced 2024-11-24 18:30:53 +00:00
interface connected
This commit is contained in:
parent
00893f8c0c
commit
0a22533e04
16
g.go
16
g.go
@ -20,15 +20,15 @@ var cmdGenerate = &Command{
|
|||||||
UsageLine: "generate [Command]",
|
UsageLine: "generate [Command]",
|
||||||
Short: "generate code based on application",
|
Short: "generate code based on application",
|
||||||
Long: `
|
Long: `
|
||||||
bee g model [modelfile] [dbconfig]
|
bee generate model [modelfile] [dbconfig]
|
||||||
generate model base on struct
|
generate model base on struct
|
||||||
bee g controller [modelfile]
|
bee generate controller [modelfile]
|
||||||
generate RESTFul controllers based on modelfile
|
generate RESTFul controllers based on modelfile
|
||||||
bee g router [controllerfile]
|
bee generate router [controllerfile]
|
||||||
generate router based on controllerfile
|
generate router based on controllerfile
|
||||||
bee g docs
|
bee generate docs
|
||||||
generate swagger doc file
|
generate swagger doc file
|
||||||
bee g test [routerfile]
|
bee generate test [routerfile]
|
||||||
generate testcase
|
generate testcase
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
@ -53,11 +53,11 @@ func generateCode(cmd *Command, args []string) {
|
|||||||
case "docs":
|
case "docs":
|
||||||
generateDocs(curpath)
|
generateDocs(curpath)
|
||||||
case "model":
|
case "model":
|
||||||
generateModel(curpath)
|
generateModel("mysql", "root@tcp(127.0.0.1:3306)/sgfas?charset=utf8", curpath)
|
||||||
case "controller":
|
case "controller":
|
||||||
generateController(curpath)
|
generateController("mysql", "", curpath)
|
||||||
case "router":
|
case "router":
|
||||||
generateRouter(curpath)
|
generateRouter("mysql", "", curpath)
|
||||||
default:
|
default:
|
||||||
ColorLog("[ERRO] command is missing\n")
|
ColorLog("[ERRO] command is missing\n")
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -12,16 +13,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
O_MODEL = 1 << iota
|
O_MODEL byte = 1 << iota
|
||||||
O_CONTROLLER
|
O_CONTROLLER
|
||||||
O_ROUTER
|
O_ROUTER
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
type MvcPath struct {
|
||||||
MODEL_PATH = "models"
|
ModelPath string
|
||||||
CONTROLLER_PATH = "controllers"
|
ControllerPath string
|
||||||
ROUTER_PATH = "routers"
|
RouterPath string
|
||||||
)
|
}
|
||||||
|
|
||||||
// typeMapping maps a SQL data type to its corresponding Go data type
|
// typeMapping maps a SQL data type to its corresponding Go data type
|
||||||
var typeMapping = map[string]string{
|
var typeMapping = map[string]string{
|
||||||
@ -176,21 +177,24 @@ func (tag *OrmTag) String() string {
|
|||||||
return fmt.Sprintf("`orm:\"%s\"`", strings.Join(ormOptions, ";"))
|
return fmt.Sprintf("`orm:\"%s\"`", strings.Join(ormOptions, ";"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateModel() {
|
func generateModel(driver string, connStr string, currpath string) {
|
||||||
|
mode := O_MODEL
|
||||||
|
gen(driver, connStr, mode, currpath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateController() {
|
func generateController(driver string, connStr string, currpath string) {
|
||||||
|
mode := O_MODEL | O_CONTROLLER
|
||||||
|
gen(driver, connStr, mode, currpath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateRouter() {
|
func generateRouter(driver string, connStr string, currpath string) {
|
||||||
|
mode := O_MODEL | O_CONTROLLER | O_ROUTER
|
||||||
|
gen(driver, connStr, mode, currpath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate takes table, column and foreign key information from database connection
|
// Generate takes table, column and foreign key information from database connection
|
||||||
// and generate corresponding golang source files
|
// and generate corresponding golang source files
|
||||||
func Gen(dbms string, connStr string, mode byte) {
|
func gen(dbms string, connStr string, mode byte, currpath string) {
|
||||||
db, err := sql.Open(dbms, connStr)
|
db, err := sql.Open(dbms, connStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error opening database: %v\n", err)
|
fmt.Printf("error opening database: %v\n", err)
|
||||||
@ -198,8 +202,12 @@ func Gen(dbms string, connStr string, mode byte) {
|
|||||||
defer db.Close()
|
defer db.Close()
|
||||||
tableNames := getTableNames(db)
|
tableNames := getTableNames(db)
|
||||||
tables := getTableObjects(tableNames, db)
|
tables := getTableObjects(tableNames, db)
|
||||||
deleteAndRecreatePaths(mode)
|
mvcPath := new(MvcPath)
|
||||||
writeSourceFiles(tables, mode)
|
mvcPath.ModelPath = path.Join(currpath, "models")
|
||||||
|
mvcPath.ControllerPath = path.Join(currpath, "controllers")
|
||||||
|
mvcPath.RouterPath = path.Join(currpath, "routers")
|
||||||
|
deleteAndRecreatePaths(mode, mvcPath)
|
||||||
|
writeSourceFiles(tables, mode, mvcPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getTables gets a list table names in current database
|
// getTables gets a list table names in current database
|
||||||
@ -369,42 +377,42 @@ func getColumns(db *sql.DB, table *Table, blackList map[string]bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// deleteAndRecreatePaths removes several directories completely
|
// deleteAndRecreatePaths removes several directories completely
|
||||||
func deleteAndRecreatePaths(mode byte) {
|
func deleteAndRecreatePaths(mode byte, paths *MvcPath) {
|
||||||
if (mode & O_MODEL) == O_MODEL {
|
if (mode & O_MODEL) == O_MODEL {
|
||||||
os.RemoveAll(MODEL_PATH)
|
os.RemoveAll(paths.ModelPath)
|
||||||
os.Mkdir(MODEL_PATH, 0777)
|
os.Mkdir(paths.ModelPath, 0777)
|
||||||
}
|
}
|
||||||
if (mode & O_CONTROLLER) == O_CONTROLLER {
|
if (mode & O_CONTROLLER) == O_CONTROLLER {
|
||||||
os.RemoveAll(CONTROLLER_PATH)
|
os.RemoveAll(paths.ControllerPath)
|
||||||
os.Mkdir(CONTROLLER_PATH, 0777)
|
os.Mkdir(paths.ControllerPath, 0777)
|
||||||
}
|
}
|
||||||
if (mode & O_ROUTER) == O_ROUTER {
|
if (mode & O_ROUTER) == O_ROUTER {
|
||||||
os.RemoveAll(ROUTER_PATH)
|
os.RemoveAll(paths.RouterPath)
|
||||||
os.Mkdir(ROUTER_PATH, 0777)
|
os.Mkdir(paths.RouterPath, 0777)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeSourceFiles generates source files for model/controller/router
|
// writeSourceFiles generates source files for model/controller/router
|
||||||
// It will wipe the following directories and recreate them:./models, ./controllers, ./routers
|
// It will wipe the following directories and recreate them:./models, ./controllers, ./routers
|
||||||
// Newly geneated files will be inside these folders.
|
// Newly geneated files will be inside these folders.
|
||||||
func writeSourceFiles(tables []*Table, mode byte) {
|
func writeSourceFiles(tables []*Table, mode byte, paths *MvcPath) {
|
||||||
if (O_MODEL & mode) == O_MODEL {
|
if (O_MODEL & mode) == O_MODEL {
|
||||||
writeModelFiles(tables)
|
writeModelFiles(tables, paths.ModelPath)
|
||||||
}
|
}
|
||||||
if (O_CONTROLLER & mode) == O_CONTROLLER {
|
if (O_CONTROLLER & mode) == O_CONTROLLER {
|
||||||
writeControllerFiles(tables)
|
writeControllerFiles(tables, paths.ControllerPath)
|
||||||
}
|
}
|
||||||
if (O_ROUTER & mode) == O_ROUTER {
|
if (O_ROUTER & mode) == O_ROUTER {
|
||||||
writeRouterFile(tables)
|
writeRouterFile(tables, paths.RouterPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeModelFiles generates model files
|
// writeModelFiles generates model files
|
||||||
func writeModelFiles(tables []*Table) {
|
func writeModelFiles(tables []*Table, mPath string) {
|
||||||
for _, tb := range tables {
|
for _, tb := range tables {
|
||||||
filename := getFileName(tb.Name)
|
filename := getFileName(tb.Name)
|
||||||
path := fmt.Sprintf("%s/%s.go", MODEL_PATH, filename)
|
fpath := path.Join(mPath, filename+".go")
|
||||||
f, _ := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0666)
|
f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0666)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
template := ""
|
template := ""
|
||||||
if tb.Pk == "" {
|
if tb.Pk == "" {
|
||||||
@ -415,32 +423,32 @@ func writeModelFiles(tables []*Table) {
|
|||||||
fileStr := strings.Replace(template, "{{modelStruct}}", tb.String(), 1)
|
fileStr := strings.Replace(template, "{{modelStruct}}", tb.String(), 1)
|
||||||
fileStr = strings.Replace(fileStr, "{{modelName}}", camelCase(tb.Name), -1)
|
fileStr = strings.Replace(fileStr, "{{modelName}}", camelCase(tb.Name), -1)
|
||||||
if _, err := f.WriteString(fileStr); err != nil {
|
if _, err := f.WriteString(fileStr); err != nil {
|
||||||
fmt.Printf("error writing file(%s): %v", path, err)
|
fmt.Printf("error writing file(%s): %v", fpath, err)
|
||||||
}
|
}
|
||||||
formatAndFixImports(path)
|
formatAndFixImports(fpath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeControllerFiles generates controller files
|
// writeControllerFiles generates controller files
|
||||||
func writeControllerFiles(tables []*Table) {
|
func writeControllerFiles(tables []*Table, cPath string) {
|
||||||
for _, tb := range tables {
|
for _, tb := range tables {
|
||||||
if tb.Pk == "" {
|
if tb.Pk == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
filename := getFileName(tb.Name)
|
filename := getFileName(tb.Name)
|
||||||
path := fmt.Sprintf("%s/%s.go", CONTROLLER_PATH, filename)
|
fpath := path.Join(cPath, filename+".go")
|
||||||
f, _ := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0666)
|
f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0666)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1)
|
fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1)
|
||||||
if _, err := f.WriteString(fileStr); err != nil {
|
if _, err := f.WriteString(fileStr); err != nil {
|
||||||
fmt.Printf("error writing file(%s): %v", path, err)
|
fmt.Printf("error writing file(%s): %v", fpath, err)
|
||||||
}
|
}
|
||||||
formatAndFixImports(path)
|
formatAndFixImports(fpath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeRouterFile generates router file
|
// writeRouterFile generates router file
|
||||||
func writeRouterFile(tables []*Table) {
|
func writeRouterFile(tables []*Table, rPath string) {
|
||||||
var nameSpaces []string
|
var nameSpaces []string
|
||||||
for _, tb := range tables {
|
for _, tb := range tables {
|
||||||
if tb.Pk == "" {
|
if tb.Pk == "" {
|
||||||
@ -452,14 +460,14 @@ func writeRouterFile(tables []*Table) {
|
|||||||
nameSpaces = append(nameSpaces, nameSpace)
|
nameSpaces = append(nameSpaces, nameSpace)
|
||||||
}
|
}
|
||||||
// add export controller
|
// add export controller
|
||||||
path := ROUTER_PATH + "/router.go"
|
fpath := path.Join(rPath, "router.go")
|
||||||
routerStr := strings.Replace(ROUTER_TPL, "{{nameSpaces}}", strings.Join(nameSpaces, ""), 1)
|
routerStr := strings.Replace(ROUTER_TPL, "{{nameSpaces}}", strings.Join(nameSpaces, ""), 1)
|
||||||
f, _ := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
|
f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
if _, err := f.WriteString(routerStr); err != nil {
|
if _, err := f.WriteString(routerStr); err != nil {
|
||||||
fmt.Println("error writing file(%s): %v", path, err)
|
fmt.Println("error writing file(%s): %v", fpath, err)
|
||||||
}
|
}
|
||||||
formatAndFixImports(path)
|
formatAndFixImports(fpath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// formatAndFixImports formats source files (add imports, too)
|
// formatAndFixImports formats source files (add imports, too)
|
Loading…
Reference in New Issue
Block a user