mirror of
https://github.com/beego/bee.git
synced 2024-11-23 11:50:55 +00:00
Complete helper modification
This commit is contained in:
parent
48817da755
commit
51a3353cac
22
g.go
22
g.go
@ -28,17 +28,13 @@ bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-conn="root:@
|
||||
example: bee generate scaffold post -fields="title:string,body:text"
|
||||
|
||||
bee generate structure [structurename]
|
||||
generate struct based
|
||||
|
||||
bee generate structure [structurename] [-fields=""]
|
||||
generate struct based on fields
|
||||
generate struct based
|
||||
-fields: a list of table fields. Format: field:type, ...
|
||||
|
||||
bee generate model [modelname]
|
||||
generate RESTFul model based
|
||||
|
||||
bee generate model [modelname] [-fields=""]
|
||||
generate RESTFul model based on fields
|
||||
generate RESTFul model based
|
||||
-fields: a list of table fields. Format: field:type, ...
|
||||
|
||||
bee generate controller [controllerfile]
|
||||
@ -54,6 +50,9 @@ bee generate migration [migrationfile] [-fields=""]
|
||||
bee generate docs
|
||||
generate swagger doc file
|
||||
|
||||
bee generate helper [filename]
|
||||
generate helper file
|
||||
|
||||
bee generate test [routerfile]
|
||||
generate testcase
|
||||
|
||||
@ -180,6 +179,15 @@ func generateCode(cmd *Command, args []string) int {
|
||||
downsql = `m.SQL("DROP TABLE ` + "`" + mname + "`" + `")`
|
||||
}
|
||||
generateMigration(mname, upsql, downsql, curpath)
|
||||
case "helper":
|
||||
if len(args) == 2 {
|
||||
cname := args[1]
|
||||
generateHelper(cname, curpath)
|
||||
} else {
|
||||
ColorLog("[ERRO] Wrong number of arguments\n")
|
||||
ColorLog("[HINT] Usage: bee generate helper [helpername]\n")
|
||||
os.Exit(2)
|
||||
}
|
||||
case "controller":
|
||||
if len(args) == 2 {
|
||||
cname := args[1]
|
||||
@ -206,6 +214,7 @@ func generateCode(cmd *Command, args []string) int {
|
||||
generateStructure(sname, fields.String(), curpath)
|
||||
default:
|
||||
ColorLog("[ERRO] Wrong number of arguments\n")
|
||||
ColorLog("[HINT] Usage: bee generate structure [structurename]\n")
|
||||
ColorLog("[HINT] Usage: bee generate structure [structurename] [-fields=\"title:string,body:text\"]\n")
|
||||
os.Exit(2)
|
||||
|
||||
@ -226,6 +235,7 @@ func generateCode(cmd *Command, args []string) int {
|
||||
generateModel(mname, fields.String(), curpath)
|
||||
default:
|
||||
ColorLog("[ERRO] Wrong number of arguments\n")
|
||||
ColorLog("[HINT] Usage: bee generate model [modelname]\n")
|
||||
ColorLog("[HINT] Usage: bee generate model [modelname] [-fields=\"\"]\n")
|
||||
os.Exit(2)
|
||||
}
|
||||
|
49
g_helper.go
49
g_helper.go
@ -1 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func generateHelper(cname, crupath string) {
|
||||
p, f := path.Split(cname)
|
||||
helperName := strings.Title(f)
|
||||
packageName := "helpers"
|
||||
if p != "" {
|
||||
i := strings.LastIndex(p[:len(p)-1], "/")
|
||||
packageName = p[i+1 : len(p)-1]
|
||||
}
|
||||
ColorLog("[INFO] Using '%s' as helpers name\n", helperName)
|
||||
ColorLog("[INFO] Using '%s' as package name\n", packageName)
|
||||
fp := path.Join(crupath, "helpers", p)
|
||||
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
||||
// create controller directory
|
||||
if err := os.MkdirAll(fp, 0777); err != nil {
|
||||
ColorLog("[ERRO] Could not create helpers directory: %s\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
fpath := path.Join(fp, strings.ToLower(helperName)+"_helper.go")
|
||||
if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
|
||||
defer f.Close()
|
||||
content := strings.Replace(BASE_HELPER_TPL, "{{packageName}}", packageName, -1)
|
||||
content = strings.Replace(content, "{{helperName}}", helperName, -1)
|
||||
f.WriteString(content)
|
||||
// gofmt generated source code
|
||||
formatSourceCode(fpath)
|
||||
ColorLog("[INFO] helpers file generated: %s\n", fpath)
|
||||
} else {
|
||||
// error creating file
|
||||
ColorLog("[ERRO] Could not create helper file: %s\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
BASE_HELPER_TPL = `package {{packageName}}
|
||||
|
||||
func {{helperName}}() {
|
||||
|
||||
}
|
||||
`
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user