1
0
mirror of https://github.com/beego/bee.git synced 2024-11-23 11:50:55 +00:00

New helpers folder defined helper method

New structures defined folder structure method
Complete bee new changes
bee api to modify half
This commit is contained in:
MingZong 2016-03-26 01:09:46 +08:00
parent 64b97f53b4
commit bb75e5773d
4 changed files with 84 additions and 57 deletions

3
.gitignore vendored
View File

@ -28,3 +28,6 @@ _testmain.go
bee bee
*.exe~ *.exe~
.goxc.local.json .goxc.local.json
#IDEA
.idea

View File

@ -42,19 +42,23 @@ on the existing database.
The command 'api' creates a folder named [appname] and inside the folder deploy The command 'api' creates a folder named [appname] and inside the folder deploy
the following files/directories structure: the following files/directories structure:
.
conf conf
app.conf    app.conf
controllers controllers
object.go    object_controller.go
user.go    user_controller.go
routers docs
router.go    doc.go
tests
default_test.go
main.go main.go
models models
object.go    object.go
user.go    user.go
routers
   router.go
structure
tests
default_test.go
`, `,
} }
@ -199,53 +203,40 @@ func Delete(ObjectId string) {
var apiModels2 = `package models var apiModels2 = `package models
import ( import (
"{{.Appname}}/structures"
"errors" "errors"
"strconv" "strconv"
"time" "time"
) )
var ( var (
UserList map[string]*User UserList map[string]*structures.User
) )
func init() { func init() {
UserList = make(map[string]*User) UserList = make(map[string]*structures.User)
u := User{"user_11111", "astaxie", "11111", Profile{"male", 20, "Singapore", "astaxie@gmail.com"}} u := structures.User{"user_11111", "astaxie", "11111", structures.Profile{"male", 20, "Singapore", "astaxie@gmail.com"}}
UserList["user_11111"] = &u UserList["user_11111"] = &u
} }
type User struct { func AddUser(u structures.User) string {
Id string
Username string
Password string
Profile Profile
}
type Profile struct {
Gender string
Age int
Address string
Email string
}
func AddUser(u User) string {
u.Id = "user_" + strconv.FormatInt(time.Now().UnixNano(), 10) u.Id = "user_" + strconv.FormatInt(time.Now().UnixNano(), 10)
UserList[u.Id] = &u UserList[u.Id] = &u
return u.Id return u.Id
} }
func GetUser(uid string) (u *User, err error) { func GetUser(uid string) (u *structures.User, err error) {
if u, ok := UserList[uid]; ok { if u, ok := UserList[uid]; ok {
return u, nil return u, nil
} }
return nil, errors.New("User not exists") return nil, errors.New("User not exists")
} }
func GetAllUsers() map[string]*User { func GetAllUsers() map[string]*structures.User {
return UserList return UserList
} }
func UpdateUser(uid string, uu *User) (a *User, err error) { func UpdateUser(uid string, uu *structures.User) (a *structures.User, err error) {
if u, ok := UserList[uid]; ok { if u, ok := UserList[uid]; ok {
if uu.Username != "" { if uu.Username != "" {
u.Username = uu.Username u.Username = uu.Username
@ -538,6 +529,23 @@ func TestGet(t *testing.T) {
` `
var apistructures = `package structures
type User struct {
Id string
Username string
Password string
Profile Profile
}
type Profile struct {
Gender string
Age int
Address string
Email string
}
`
func init() { func init() {
cmdApiapp.Run = createapi cmdApiapp.Run = createapi
cmdApiapp.Flag.Var(&tables, "tables", "specify tables to generate model") cmdApiapp.Flag.Var(&tables, "tables", "specify tables to generate model")
@ -574,6 +582,8 @@ func createapi(cmd *Command, args []string) int {
fmt.Println("create docs:", path.Join(apppath, "docs")) fmt.Println("create docs:", path.Join(apppath, "docs"))
os.Mkdir(path.Join(apppath, "tests"), 0755) os.Mkdir(path.Join(apppath, "tests"), 0755)
fmt.Println("create tests:", path.Join(apppath, "tests")) fmt.Println("create tests:", path.Join(apppath, "tests"))
os.Mkdir(path.Join(apppath, "helpers"), 0755)
fmt.Println("create structure:",path.Join(apppath, "helpers"))
fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf")) fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf"))
writetofile(path.Join(apppath, "conf", "app.conf"), writetofile(path.Join(apppath, "conf", "app.conf"),
@ -604,14 +614,16 @@ func createapi(cmd *Command, args []string) int {
os.Mkdir(path.Join(apppath, "models"), 0755) os.Mkdir(path.Join(apppath, "models"), 0755)
fmt.Println("create models:", path.Join(apppath, "models")) fmt.Println("create models:", path.Join(apppath, "models"))
os.Mkdir(path.Join(apppath, "routers"), 0755) os.Mkdir(path.Join(apppath, "routers"), 0755)
fmt.Println(path.Join(apppath, "routers") + string(path.Separator)) fmt.Println("create routers:",path.Join(apppath, "routers"))
os.Mkdir(path.Join(apppath, "structures"), 0755)
fmt.Println("create structure:",path.Join(apppath, "structures"))
fmt.Println("create controllers object.go:", path.Join(apppath, "controllers", "object.go")) fmt.Println("create controllers object_controller.go:", path.Join(apppath, "controllers", "object_controller.go"))
writetofile(path.Join(apppath, "controllers", "object.go"), writetofile(path.Join(apppath, "controllers", "object_controller.go"),
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1)) strings.Replace(apiControllers, "{{.Appname}}", packpath, -1))
fmt.Println("create controllers user.go:", path.Join(apppath, "controllers", "user.go")) fmt.Println("create controllers user_controller.go:", path.Join(apppath, "controllers", "user_controller.go"))
writetofile(path.Join(apppath, "controllers", "user.go"), writetofile(path.Join(apppath, "controllers", "user_controller.go"),
strings.Replace(apiControllers2, "{{.Appname}}", packpath, -1)) strings.Replace(apiControllers2, "{{.Appname}}", packpath, -1))
fmt.Println("create tests default.go:", path.Join(apppath, "tests", "default_test.go")) fmt.Println("create tests default.go:", path.Join(apppath, "tests", "default_test.go"))
@ -626,7 +638,11 @@ func createapi(cmd *Command, args []string) int {
writetofile(path.Join(apppath, "models", "object.go"), apiModels) writetofile(path.Join(apppath, "models", "object.go"), apiModels)
fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go")) fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go"))
writetofile(path.Join(apppath, "models", "user.go"), apiModels2) writetofile(path.Join(apppath, "models", "user.go"),
strings.Replace(apiModels2, "{{.Appname}}", packpath, -1))
fmt.Println("create structures user_structure.go:", path.Join(apppath, "structures", "user_structure.go"))
writetofile(path.Join(apppath, "structures", "user_structure.go"), apistructures)
fmt.Println("create docs doc.go:", path.Join(apppath, "docs", "doc.go")) fmt.Println("create docs doc.go:", path.Join(apppath, "docs", "doc.go"))
writetofile(path.Join(apppath, "docs", "doc.go"), "package docs") writetofile(path.Join(apppath, "docs", "doc.go"), "package docs")

View File

@ -25,13 +25,14 @@ import (
// //
func generateController(cname, crupath string) { func generateController(cname, crupath string) {
p, f := path.Split(cname) p, f := path.Split(cname)
fileName := strings.Title(f) + "_controllers"
controllerName := strings.Title(f) controllerName := strings.Title(f)
packageName := "controllers" packageName := "controllers"
if p != "" { if p != "" {
i := strings.LastIndex(p[:len(p)-1], "/") i := strings.LastIndex(p[:len(p)-1], "/")
packageName = p[i+1 : len(p)-1] packageName = p[i+1 : len(p)-1]
} }
ColorLog("[INFO] Using '%s' as controller name\n", controllerName) ColorLog("[INFO] Using '%s' as controller name\n", fileName)
ColorLog("[INFO] Using '%s' as package name\n", packageName) ColorLog("[INFO] Using '%s' as package name\n", packageName)
fp := path.Join(crupath, "controllers", p) fp := path.Join(crupath, "controllers", p)
if _, err := os.Stat(fp); os.IsNotExist(err) { if _, err := os.Stat(fp); os.IsNotExist(err) {
@ -41,7 +42,7 @@ func generateController(cname, crupath string) {
os.Exit(2) os.Exit(2)
} }
} }
fpath := path.Join(fp, strings.ToLower(controllerName)+".go") fpath := path.Join(fp, strings.ToLower(fileName)+".go")
if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
defer f.Close() defer f.Close()
modelPath := path.Join(crupath, "models", strings.ToLower(controllerName)+".go") modelPath := path.Join(crupath, "models", strings.ToLower(controllerName)+".go")

43
new.go
View File

@ -30,22 +30,25 @@ Creates a Beego application for the given app name in the current directory.
The command 'new' creates a folder named [appname] and inside the folder deploy The command 'new' creates a folder named [appname] and inside the folder deploy
the following files/directories structure: the following files/directories structure:
|- main.go .
|- conf conf
|- app.conf    app.conf
|- controllers controllers
|- default.go    index_controller.go
|- models helpers
|- routers main.go
|- router.go models
|- tests routers
|- default_test.go    router.go
|- static static
|- js    css
|- css    img
|- img    js
|- views structure
index.tpl tests
   default_test.go
views
index.tpl
`, `,
} }
@ -116,12 +119,16 @@ func createApp(cmd *Command, args []string) int {
fmt.Println(path.Join(apppath, "conf") + string(path.Separator)) fmt.Println(path.Join(apppath, "conf") + string(path.Separator))
os.Mkdir(path.Join(apppath, "controllers"), 0755) os.Mkdir(path.Join(apppath, "controllers"), 0755)
fmt.Println(path.Join(apppath, "controllers") + string(path.Separator)) fmt.Println(path.Join(apppath, "controllers") + string(path.Separator))
os.Mkdir(path.Join(apppath, "helpers"), 0755)
fmt.Println(path.Join(apppath, "helpers") + string(path.Separator))
os.Mkdir(path.Join(apppath, "models"), 0755) os.Mkdir(path.Join(apppath, "models"), 0755)
fmt.Println(path.Join(apppath, "models") + string(path.Separator)) fmt.Println(path.Join(apppath, "models") + string(path.Separator))
os.Mkdir(path.Join(apppath, "routers"), 0755) os.Mkdir(path.Join(apppath, "routers"), 0755)
fmt.Println(path.Join(apppath, "routers") + string(path.Separator)) fmt.Println(path.Join(apppath, "routers") + string(path.Separator))
os.Mkdir(path.Join(apppath, "tests"), 0755) os.Mkdir(path.Join(apppath, "tests"), 0755)
fmt.Println(path.Join(apppath, "tests") + string(path.Separator)) fmt.Println(path.Join(apppath, "tests") + string(path.Separator))
os.Mkdir(path.Join(apppath, "structures"), 0755)
fmt.Println(path.Join(apppath, "structures") + string(path.Separator))
os.Mkdir(path.Join(apppath, "static"), 0755) os.Mkdir(path.Join(apppath, "static"), 0755)
fmt.Println(path.Join(apppath, "static") + string(path.Separator)) fmt.Println(path.Join(apppath, "static") + string(path.Separator))
os.Mkdir(path.Join(apppath, "static", "js"), 0755) os.Mkdir(path.Join(apppath, "static", "js"), 0755)
@ -135,8 +142,8 @@ func createApp(cmd *Command, args []string) int {
fmt.Println(path.Join(apppath, "conf", "app.conf")) fmt.Println(path.Join(apppath, "conf", "app.conf"))
writetofile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", args[0], -1)) writetofile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", args[0], -1))
fmt.Println(path.Join(apppath, "controllers", "default.go")) fmt.Println(path.Join(apppath, "controllers", "index_controller.go"))
writetofile(path.Join(apppath, "controllers", "default.go"), controllers) writetofile(path.Join(apppath, "controllers", "index_controller.go"), controllers)
fmt.Println(path.Join(apppath, "views", "index.tpl")) fmt.Println(path.Join(apppath, "views", "index.tpl"))
writetofile(path.Join(apppath, "views", "index.tpl"), indextpl) writetofile(path.Join(apppath, "views", "index.tpl"), indextpl)