mirror of
https://github.com/beego/bee.git
synced 2024-11-22 15:10:54 +00:00
do not rely on goimports
This commit is contained in:
parent
daaccf8972
commit
98b3d4ee37
105
g_appcode.go
105
g_appcode.go
@ -20,6 +20,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -78,6 +79,7 @@ type Table struct {
|
|||||||
Uk []string
|
Uk []string
|
||||||
Fk map[string]*ForeignKey
|
Fk map[string]*ForeignKey
|
||||||
Columns []*Column
|
Columns []*Column
|
||||||
|
ImportTimePkg bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Column reprsents a column for a table
|
// Column reprsents a column for a table
|
||||||
@ -191,7 +193,7 @@ func (tag *OrmTag) String() string {
|
|||||||
return fmt.Sprintf("`orm:\"%s\"`", strings.Join(ormOptions, ";"))
|
return fmt.Sprintf("`orm:\"%s\"`", strings.Join(ormOptions, ";"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateAppcode(driver string, connStr string, level string, tables string, currpath string) {
|
func generateAppcode(driver, connStr, level, tables, currpath string) {
|
||||||
var mode byte
|
var mode byte
|
||||||
if level == "1" {
|
if level == "1" {
|
||||||
mode = O_MODEL
|
mode = O_MODEL
|
||||||
@ -216,7 +218,7 @@ func generateAppcode(driver string, connStr string, level string, tables string,
|
|||||||
|
|
||||||
// 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, selectedTableNames map[string]bool, currpath string) {
|
func gen(dbms, connStr string, mode byte, selectedTableNames map[string]bool, currpath string) {
|
||||||
db, err := sql.Open(dbms, connStr)
|
db, err := sql.Open(dbms, connStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ColorLog("[ERRO] Could not connect to %s: %s\n", dbms, connStr)
|
ColorLog("[ERRO] Could not connect to %s: %s\n", dbms, connStr)
|
||||||
@ -231,7 +233,8 @@ func gen(dbms string, connStr string, mode byte, selectedTableNames map[string]b
|
|||||||
mvcPath.ControllerPath = path.Join(currpath, "controllers")
|
mvcPath.ControllerPath = path.Join(currpath, "controllers")
|
||||||
mvcPath.RouterPath = path.Join(currpath, "routers")
|
mvcPath.RouterPath = path.Join(currpath, "routers")
|
||||||
createPaths(mode, mvcPath)
|
createPaths(mode, mvcPath)
|
||||||
writeSourceFiles(tables, mode, mvcPath, selectedTableNames)
|
pkgPath := getPackagePath(currpath)
|
||||||
|
writeSourceFiles(pkgPath, tables, mode, mvcPath, selectedTableNames)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getTables gets a list table names in current database
|
// getTables gets a list table names in current database
|
||||||
@ -398,6 +401,8 @@ func getColumns(db *sql.DB, table *Table, blackList map[string]bool) {
|
|||||||
} else if columnDefault == "CURRENT_TIMESTAMP" {
|
} else if columnDefault == "CURRENT_TIMESTAMP" {
|
||||||
tag.AutoNowAdd = true
|
tag.AutoNowAdd = true
|
||||||
}
|
}
|
||||||
|
// need to import time package
|
||||||
|
table.ImportTimePkg = true
|
||||||
}
|
}
|
||||||
if isSQLDecimal(dataType) {
|
if isSQLDecimal(dataType) {
|
||||||
tag.Digits, tag.Decimals = extractDecimal(columnType)
|
tag.Digits, tag.Decimals = extractDecimal(columnType)
|
||||||
@ -425,18 +430,18 @@ func createPaths(mode byte, paths *MvcPath) {
|
|||||||
// 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, paths *MvcPath, selectedTables map[string]bool) {
|
func writeSourceFiles(pkgPath string, tables []*Table, mode byte, paths *MvcPath, selectedTables map[string]bool) {
|
||||||
if (O_MODEL & mode) == O_MODEL {
|
if (O_MODEL & mode) == O_MODEL {
|
||||||
ColorLog("[INFO] Creating model files...\n")
|
ColorLog("[INFO] Creating model files...\n")
|
||||||
writeModelFiles(tables, paths.ModelPath, selectedTables)
|
writeModelFiles(tables, paths.ModelPath, selectedTables)
|
||||||
}
|
}
|
||||||
if (O_CONTROLLER & mode) == O_CONTROLLER {
|
if (O_CONTROLLER & mode) == O_CONTROLLER {
|
||||||
ColorLog("[INFO] Creating controller files...\n")
|
ColorLog("[INFO] Creating controller files...\n")
|
||||||
writeControllerFiles(tables, paths.ControllerPath, selectedTables)
|
writeControllerFiles(tables, paths.ControllerPath, selectedTables, pkgPath)
|
||||||
}
|
}
|
||||||
if (O_ROUTER & mode) == O_ROUTER {
|
if (O_ROUTER & mode) == O_ROUTER {
|
||||||
ColorLog("[INFO] Creating router files...\n")
|
ColorLog("[INFO] Creating router files...\n")
|
||||||
writeRouterFile(tables, paths.RouterPath, selectedTables)
|
writeRouterFile(tables, paths.RouterPath, selectedTables, pkgPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,18 +485,24 @@ func writeModelFiles(tables []*Table, mPath string, selectedTables map[string]bo
|
|||||||
}
|
}
|
||||||
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 table contains time field, import time.Time package
|
||||||
|
timePkg := ""
|
||||||
|
if tb.ImportTimePkg {
|
||||||
|
timePkg = "\"time\"\n"
|
||||||
|
}
|
||||||
|
fileStr = strings.Replace(fileStr, "{{timePkg}}", timePkg, -1)
|
||||||
if _, err := f.WriteString(fileStr); err != nil {
|
if _, err := f.WriteString(fileStr); err != nil {
|
||||||
ColorLog("[ERRO] Could not write model file to %s\n", fpath)
|
ColorLog("[ERRO] Could not write model file to %s\n", fpath)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
ColorLog("[INFO] model => %s\n", fpath)
|
ColorLog("[INFO] model => %s\n", fpath)
|
||||||
formatAndFixImports(fpath)
|
formatSourceCode(fpath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeControllerFiles generates controller files
|
// writeControllerFiles generates controller files
|
||||||
func writeControllerFiles(tables []*Table, cPath string, selectedTables map[string]bool) {
|
func writeControllerFiles(tables []*Table, cPath string, selectedTables map[string]bool, pkgPath string) {
|
||||||
for _, tb := range tables {
|
for _, tb := range tables {
|
||||||
// if selectedTables map is not nil and this table is not selected, ignore it
|
// if selectedTables map is not nil and this table is not selected, ignore it
|
||||||
if selectedTables != nil {
|
if selectedTables != nil {
|
||||||
@ -526,18 +537,19 @@ func writeControllerFiles(tables []*Table, cPath string, selectedTables map[stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1)
|
fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1)
|
||||||
|
fileStr = strings.Replace(fileStr, "{{pkgPath}}", pkgPath, -1)
|
||||||
if _, err := f.WriteString(fileStr); err != nil {
|
if _, err := f.WriteString(fileStr); err != nil {
|
||||||
ColorLog("[ERRO] Could not write controller file to %s\n", fpath)
|
ColorLog("[ERRO] Could not write controller file to %s\n", fpath)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
ColorLog("[INFO] controller => %s\n", fpath)
|
ColorLog("[INFO] controller => %s\n", fpath)
|
||||||
formatAndFixImports(fpath)
|
formatSourceCode(fpath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeRouterFile generates router file
|
// writeRouterFile generates router file
|
||||||
func writeRouterFile(tables []*Table, rPath string, selectedTables map[string]bool) {
|
func writeRouterFile(tables []*Table, rPath string, selectedTables map[string]bool, pkgPath string) {
|
||||||
var nameSpaces []string
|
var nameSpaces []string
|
||||||
for _, tb := range tables {
|
for _, tb := range tables {
|
||||||
// if selectedTables map is not nil and this table is not selected, ignore it
|
// if selectedTables map is not nil and this table is not selected, ignore it
|
||||||
@ -557,8 +569,7 @@ func writeRouterFile(tables []*Table, rPath string, selectedTables map[string]bo
|
|||||||
// add export controller
|
// add export controller
|
||||||
fpath := path.Join(rPath, "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)
|
||||||
_, projectName := path.Split(path.Dir(rPath))
|
routerStr = strings.Replace(routerStr, "{{pkgPath}}", pkgPath, 1)
|
||||||
routerStr = strings.Replace(routerStr, "{{projectName}}", projectName, 1)
|
|
||||||
var f *os.File
|
var f *os.File
|
||||||
var err error
|
var err error
|
||||||
if isExist(fpath) {
|
if isExist(fpath) {
|
||||||
@ -586,13 +597,15 @@ func writeRouterFile(tables []*Table, rPath string, selectedTables map[string]bo
|
|||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
ColorLog("[INFO] router => %s\n", fpath)
|
ColorLog("[INFO] router => %s\n", fpath)
|
||||||
formatAndFixImports(fpath)
|
formatSourceCode(fpath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// formatAndFixImports formats source files (add imports, too)
|
// formatSourceCode formats source files
|
||||||
func formatAndFixImports(filename string) {
|
func formatSourceCode(filename string) {
|
||||||
cmd := exec.Command("goimports", "-w", filename)
|
cmd := exec.Command("gofmt", "-w", filename)
|
||||||
cmd.Run()
|
if err := cmd.Run(); err != nil {
|
||||||
|
ColorLog("[WARN] gofmt err: %s\n", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// camelCase converts a _ delimited string to camel case
|
// camelCase converts a _ delimited string to camel case
|
||||||
@ -662,6 +675,36 @@ func getFileName(tbName string) (filename string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getPackagePath(curpath string) (packpath string) {
|
||||||
|
gopath := os.Getenv("GOPATH")
|
||||||
|
Debugf("gopath:%s", gopath)
|
||||||
|
if gopath == "" {
|
||||||
|
ColorLog("[ERRO] you should set GOPATH in the env")
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
appsrcpath := ""
|
||||||
|
haspath := false
|
||||||
|
wgopath := filepath.SplitList(gopath)
|
||||||
|
|
||||||
|
for _, wg := range wgopath {
|
||||||
|
wg, _ = filepath.EvalSymlinks(path.Join(wg, "src"))
|
||||||
|
|
||||||
|
if filepath.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) {
|
||||||
|
haspath = true
|
||||||
|
appsrcpath = wg
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !haspath {
|
||||||
|
ColorLog("[ERRO] Can't generate application code outside of GOPATH '%s'\n", gopath)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
packpath = strings.Join(strings.Split(curpath[len(appsrcpath)+1:], string(filepath.Separator)), "/")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
STRUCT_MODEL_TPL = `package models
|
STRUCT_MODEL_TPL = `package models
|
||||||
|
|
||||||
@ -670,6 +713,15 @@ const (
|
|||||||
|
|
||||||
MODEL_TPL = `package models
|
MODEL_TPL = `package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
{{timePkg}}
|
||||||
|
"github.com/astaxie/beego/orm"
|
||||||
|
)
|
||||||
|
|
||||||
{{modelStruct}}
|
{{modelStruct}}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -799,8 +851,17 @@ func Delete{{modelName}}(id int) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
CTRL_TPL = `
|
CTRL_TPL = `package controllers
|
||||||
package controllers
|
|
||||||
|
import (
|
||||||
|
"{{pkgPath}}/models"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
)
|
||||||
|
|
||||||
// oprations for {{ctrlName}}
|
// oprations for {{ctrlName}}
|
||||||
type {{ctrlName}}Controller struct {
|
type {{ctrlName}}Controller struct {
|
||||||
@ -949,8 +1010,7 @@ func (this *{{ctrlName}}Controller) Delete() {
|
|||||||
this.ServeJson()
|
this.ServeJson()
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
ROUTER_TPL = `
|
ROUTER_TPL = `// @APIVersion 1.0.0
|
||||||
// @APIVersion 1.0.0
|
|
||||||
// @Title beego Test API
|
// @Title beego Test API
|
||||||
// @Description beego has a very cool tools to autogenerate documents for your API
|
// @Description beego has a very cool tools to autogenerate documents for your API
|
||||||
// @Contact astaxie@gmail.com
|
// @Contact astaxie@gmail.com
|
||||||
@ -960,7 +1020,8 @@ func (this *{{ctrlName}}Controller) Delete() {
|
|||||||
package routers
|
package routers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"{{projectName}}/controllers"
|
"{{pkgPath}}/controllers"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -60,12 +59,6 @@ func generateMigration(mname, upsql, downsql, curpath string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// formatSourceCode formats the source code using gofmt
|
|
||||||
func formatSourceCode(fpath string) {
|
|
||||||
cmd := exec.Command("gofmt", "-w", fpath)
|
|
||||||
cmd.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
const MIGRATION_TPL = `package main
|
const MIGRATION_TPL = `package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -38,7 +38,7 @@ func generateModel(mname, fields, crupath string) {
|
|||||||
content = strings.Replace(content, "{{modelStruct}}", modelStruct, -1)
|
content = strings.Replace(content, "{{modelStruct}}", modelStruct, -1)
|
||||||
f.WriteString(content)
|
f.WriteString(content)
|
||||||
// gofmt generated source code
|
// gofmt generated source code
|
||||||
formatAndFixImports(fpath)
|
formatSourceCode(fpath)
|
||||||
ColorLog("[INFO] model file generated: %s\n", fpath)
|
ColorLog("[INFO] model file generated: %s\n", fpath)
|
||||||
} else {
|
} else {
|
||||||
// error creating file
|
// error creating file
|
||||||
|
Loading…
Reference in New Issue
Block a user