1
0
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:
MingZong 2016-04-01 23:21:06 +08:00
parent 48817da755
commit 51a3353cac
2 changed files with 65 additions and 6 deletions

22
g.go
View File

@ -28,17 +28,13 @@ bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-conn="root:@
example: bee generate scaffold post -fields="title:string,body:text" example: bee generate scaffold post -fields="title:string,body:text"
bee generate structure [structurename] bee generate structure [structurename]
generate struct based
bee generate structure [structurename] [-fields=""] bee generate structure [structurename] [-fields=""]
generate struct based on fields generate struct based
-fields: a list of table fields. Format: field:type, ... -fields: a list of table fields. Format: field:type, ...
bee generate model [modelname] bee generate model [modelname]
generate RESTFul model based
bee generate model [modelname] [-fields=""] bee generate model [modelname] [-fields=""]
generate RESTFul model based on fields generate RESTFul model based
-fields: a list of table fields. Format: field:type, ... -fields: a list of table fields. Format: field:type, ...
bee generate controller [controllerfile] bee generate controller [controllerfile]
@ -54,6 +50,9 @@ bee generate migration [migrationfile] [-fields=""]
bee generate docs bee generate docs
generate swagger doc file generate swagger doc file
bee generate helper [filename]
generate helper file
bee generate test [routerfile] bee generate test [routerfile]
generate testcase generate testcase
@ -180,6 +179,15 @@ func generateCode(cmd *Command, args []string) int {
downsql = `m.SQL("DROP TABLE ` + "`" + mname + "`" + `")` downsql = `m.SQL("DROP TABLE ` + "`" + mname + "`" + `")`
} }
generateMigration(mname, upsql, downsql, curpath) 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": case "controller":
if len(args) == 2 { if len(args) == 2 {
cname := args[1] cname := args[1]
@ -206,6 +214,7 @@ func generateCode(cmd *Command, args []string) int {
generateStructure(sname, fields.String(), curpath) generateStructure(sname, fields.String(), curpath)
default: default:
ColorLog("[ERRO] Wrong number of arguments\n") 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") ColorLog("[HINT] Usage: bee generate structure [structurename] [-fields=\"title:string,body:text\"]\n")
os.Exit(2) os.Exit(2)
@ -226,6 +235,7 @@ func generateCode(cmd *Command, args []string) int {
generateModel(mname, fields.String(), curpath) generateModel(mname, fields.String(), curpath)
default: default:
ColorLog("[ERRO] Wrong number of arguments\n") ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate model [modelname]\n")
ColorLog("[HINT] Usage: bee generate model [modelname] [-fields=\"\"]\n") ColorLog("[HINT] Usage: bee generate model [modelname] [-fields=\"\"]\n")
os.Exit(2) os.Exit(2)
} }

View File

@ -1 +1,50 @@
package main 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}}() {
}
`
)