2014-06-19 13:09:17 +00:00
|
|
|
// Copyright 2013 bee authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
// under the License.
|
|
|
|
|
2014-06-18 04:19:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "os"
|
|
|
|
|
|
|
|
var cmdGenerate = &Command{
|
|
|
|
UsageLine: "generate [Command]",
|
2014-08-19 10:48:02 +00:00
|
|
|
Short: "source code generator",
|
2014-06-18 04:19:03 +00:00
|
|
|
Long: `
|
2014-08-14 06:57:57 +00:00
|
|
|
bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
2014-08-14 06:53:28 +00:00
|
|
|
The generate scaffold command will do a number of things for you.
|
2014-08-19 10:48:02 +00:00
|
|
|
-fields: a list of table fields. Format: field:type, ...
|
2014-08-21 02:19:44 +00:00
|
|
|
-driver: [mysql | postgres | sqlite], the default is mysql
|
2014-08-14 06:53:28 +00:00
|
|
|
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
|
|
|
|
example: bee generate scaffold post -fields="title:string,body:text"
|
2014-08-01 08:25:28 +00:00
|
|
|
|
2014-08-11 09:01:00 +00:00
|
|
|
bee generate model [modelname] [-fields=""]
|
2014-08-14 06:53:28 +00:00
|
|
|
generate RESTFul model based on fields
|
2014-08-19 10:48:02 +00:00
|
|
|
-fields: a list of table fields. Format: field:type, ...
|
2014-08-07 02:47:06 +00:00
|
|
|
|
2016-03-31 15:25:08 +00:00
|
|
|
bee generate structure [structurefile] [-fields=""]
|
|
|
|
generate struct based
|
|
|
|
-fields: a list of table fields. Format: field:type, ...
|
|
|
|
|
2014-08-08 17:37:55 +00:00
|
|
|
bee generate controller [controllerfile]
|
|
|
|
generate RESTFul controllers
|
2014-08-01 08:25:28 +00:00
|
|
|
|
2014-08-11 02:01:56 +00:00
|
|
|
bee generate view [viewpath]
|
|
|
|
generate CRUD view in viewpath
|
2014-08-01 08:25:28 +00:00
|
|
|
|
2014-08-18 03:51:57 +00:00
|
|
|
bee generate migration [migrationfile] [-fields=""]
|
2014-08-11 09:01:00 +00:00
|
|
|
generate migration file for making database schema update
|
2014-08-19 10:48:02 +00:00
|
|
|
-fields: a list of table fields. Format: field:type, ...
|
2014-08-11 09:01:00 +00:00
|
|
|
|
2014-08-01 06:49:30 +00:00
|
|
|
bee generate docs
|
2014-08-01 02:15:32 +00:00
|
|
|
generate swagger doc file
|
2014-08-01 08:25:28 +00:00
|
|
|
|
2014-08-01 06:49:30 +00:00
|
|
|
bee generate test [routerfile]
|
2014-08-01 02:15:32 +00:00
|
|
|
generate testcase
|
2014-08-11 09:01:00 +00:00
|
|
|
|
2014-08-14 06:57:57 +00:00
|
|
|
bee generate appcode [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-level=3]
|
2014-08-11 09:01:00 +00:00
|
|
|
generate appcode based on an existing database
|
|
|
|
-tables: a list of table names separated by ',', default is empty, indicating all tables
|
2014-08-22 07:52:58 +00:00
|
|
|
-driver: [mysql | postgres | sqlite], the default is mysql
|
2014-08-21 04:22:49 +00:00
|
|
|
-conn: the connection string used by the driver.
|
|
|
|
default for mysql: root:@tcp(127.0.0.1:3306)/test
|
|
|
|
default for postgres: postgres://postgres:postgres@127.0.0.1:5432/postgres
|
2014-08-19 10:48:02 +00:00
|
|
|
-level: [1 | 2 | 3], 1 = models; 2 = models,controllers; 3 = models,controllers,router
|
2014-06-18 04:19:03 +00:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2014-08-01 07:52:25 +00:00
|
|
|
var driver docValue
|
|
|
|
var conn docValue
|
|
|
|
var level docValue
|
2014-08-07 08:39:28 +00:00
|
|
|
var tables docValue
|
2014-08-11 09:01:00 +00:00
|
|
|
var fields docValue
|
2014-08-01 07:52:25 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdGenerate.Run = generateCode
|
2014-08-07 08:39:28 +00:00
|
|
|
cmdGenerate.Flag.Var(&tables, "tables", "specify tables to generate model")
|
2014-08-01 07:52:25 +00:00
|
|
|
cmdGenerate.Flag.Var(&driver, "driver", "database driver: mysql, postgresql, etc.")
|
|
|
|
cmdGenerate.Flag.Var(&conn, "conn", "connection string used by the driver to connect to a database instance")
|
|
|
|
cmdGenerate.Flag.Var(&level, "level", "1 = models only; 2 = models and controllers; 3 = models, controllers and routers")
|
2014-08-11 09:01:00 +00:00
|
|
|
cmdGenerate.Flag.Var(&fields, "fields", "specify the fields want to generate.")
|
2014-08-01 07:52:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 09:38:51 +00:00
|
|
|
func generateCode(cmd *Command, args []string) int {
|
2014-06-18 04:19:03 +00:00
|
|
|
curpath, _ := os.Getwd()
|
|
|
|
if len(args) < 1 {
|
|
|
|
ColorLog("[ERRO] command is missing\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
Debugf("gopath:%s", gopath)
|
|
|
|
if gopath == "" {
|
|
|
|
ColorLog("[ERRO] $GOPATH not found\n")
|
|
|
|
ColorLog("[HINT] Set $GOPATH in your environment vairables\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
gcmd := args[0]
|
|
|
|
switch gcmd {
|
2014-08-11 09:01:00 +00:00
|
|
|
case "scaffold":
|
|
|
|
if len(args) < 2 {
|
|
|
|
ColorLog("[ERRO] Wrong number of arguments\n")
|
|
|
|
ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"\"]\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-08-14 06:53:28 +00:00
|
|
|
err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err)
|
|
|
|
}
|
2014-08-11 09:01:00 +00:00
|
|
|
cmd.Flag.Parse(args[2:])
|
2014-08-14 06:53:28 +00:00
|
|
|
if driver == "" {
|
|
|
|
driver = docValue(conf.Database.Driver)
|
|
|
|
if driver == "" {
|
|
|
|
driver = "mysql"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if conn == "" {
|
|
|
|
conn = docValue(conf.Database.Conn)
|
|
|
|
if conn == "" {
|
|
|
|
conn = "root:@tcp(127.0.0.1:3306)/test"
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 09:01:00 +00:00
|
|
|
if fields == "" {
|
|
|
|
ColorLog("[ERRO] Wrong number of arguments\n")
|
|
|
|
ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"title:string,body:text\"]\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
sname := args[1]
|
|
|
|
ColorLog("[INFO] Using '%s' as scaffold name\n", sname)
|
2014-08-14 06:53:28 +00:00
|
|
|
generateScaffold(sname, fields.String(), curpath, driver.String(), conn.String())
|
2014-06-18 04:19:03 +00:00
|
|
|
case "docs":
|
|
|
|
generateDocs(curpath)
|
2014-08-11 09:01:00 +00:00
|
|
|
case "appcode":
|
2014-08-13 06:48:20 +00:00
|
|
|
// load config
|
|
|
|
err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err)
|
|
|
|
}
|
2014-08-01 09:42:04 +00:00
|
|
|
cmd.Flag.Parse(args[1:])
|
|
|
|
if driver == "" {
|
2014-08-13 06:48:20 +00:00
|
|
|
driver = docValue(conf.Database.Driver)
|
|
|
|
if driver == "" {
|
|
|
|
driver = "mysql"
|
|
|
|
}
|
2014-08-01 09:42:04 +00:00
|
|
|
}
|
|
|
|
if conn == "" {
|
2014-08-13 06:48:20 +00:00
|
|
|
conn = docValue(conf.Database.Conn)
|
|
|
|
if conn == "" {
|
2014-08-21 04:22:49 +00:00
|
|
|
if driver == "mysql" {
|
|
|
|
conn = "root:@tcp(127.0.0.1:3306)/test"
|
|
|
|
} else if driver == "postgres" {
|
|
|
|
conn = "postgres://postgres:postgres@127.0.0.1:5432/postgres"
|
|
|
|
}
|
2014-08-13 06:48:20 +00:00
|
|
|
}
|
2014-08-01 09:42:04 +00:00
|
|
|
}
|
|
|
|
if level == "" {
|
2014-08-13 06:13:07 +00:00
|
|
|
level = "3"
|
2014-08-01 09:42:04 +00:00
|
|
|
}
|
|
|
|
ColorLog("[INFO] Using '%s' as 'driver'\n", driver)
|
|
|
|
ColorLog("[INFO] Using '%s' as 'conn'\n", conn)
|
2014-08-13 06:48:20 +00:00
|
|
|
ColorLog("[INFO] Using '%s' as 'tables'\n", tables)
|
2014-08-01 09:42:04 +00:00
|
|
|
ColorLog("[INFO] Using '%s' as 'level'\n", level)
|
2014-08-14 06:53:28 +00:00
|
|
|
generateAppcode(driver.String(), conn.String(), level.String(), tables.String(), curpath)
|
2014-08-07 02:47:06 +00:00
|
|
|
case "migration":
|
2014-08-18 03:51:57 +00:00
|
|
|
if len(args) < 2 {
|
2014-08-07 02:47:06 +00:00
|
|
|
ColorLog("[ERRO] Wrong number of arguments\n")
|
2014-08-18 03:51:57 +00:00
|
|
|
ColorLog("[HINT] Usage: bee generate migration [migrationname] [-fields=\"\"]\n")
|
2014-08-07 02:47:06 +00:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-08-18 03:51:57 +00:00
|
|
|
cmd.Flag.Parse(args[2:])
|
|
|
|
mname := args[1]
|
|
|
|
ColorLog("[INFO] Using '%s' as migration name\n", mname)
|
|
|
|
upsql := ""
|
2014-08-18 04:21:21 +00:00
|
|
|
downsql := ""
|
2014-08-18 03:51:57 +00:00
|
|
|
if fields != "" {
|
2015-11-10 02:09:26 +00:00
|
|
|
upsql = `m.SQL("CREATE TABLE ` + mname + "(" + generateSQLFromFields(fields.String()) + `)");`
|
|
|
|
downsql = `m.SQL("DROP TABLE ` + "`" + mname + "`" + `")`
|
2014-08-18 03:51:57 +00:00
|
|
|
}
|
2014-08-18 04:21:21 +00:00
|
|
|
generateMigration(mname, upsql, downsql, curpath)
|
2014-08-08 17:37:55 +00:00
|
|
|
case "controller":
|
|
|
|
if len(args) == 2 {
|
|
|
|
cname := args[1]
|
|
|
|
generateController(cname, curpath)
|
|
|
|
} else {
|
|
|
|
ColorLog("[ERRO] Wrong number of arguments\n")
|
2014-08-11 09:01:00 +00:00
|
|
|
ColorLog("[HINT] Usage: bee generate controller [controllername]\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2016-03-31 15:25:08 +00:00
|
|
|
case "structure":
|
|
|
|
sname := args[1]
|
|
|
|
switch len(args) {
|
|
|
|
case 2:
|
|
|
|
generateStructure(sname, "", curpath)
|
|
|
|
case 3:
|
|
|
|
cmd.Flag.Parse(args[2:])
|
|
|
|
if fields == "" {
|
|
|
|
ColorLog("[ERRO] Wrong number of arguments\n")
|
|
|
|
ColorLog("[HINT] Usage: bee generate structure [structurename] [-fields=\"title:string,body:text\"]\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
sname := args[1]
|
|
|
|
ColorLog("[INFO] Using '%s' as structure name\n", sname)
|
|
|
|
generateStructure(sname, fields.String(), curpath)
|
|
|
|
default:
|
|
|
|
ColorLog("[ERRO] Wrong number of arguments\n")
|
|
|
|
ColorLog("[HINT] Usage: bee generate structure [structurename] [-fields=\"title:string,body:text\"]\n")
|
|
|
|
os.Exit(2)
|
|
|
|
|
|
|
|
}
|
2014-08-11 09:01:00 +00:00
|
|
|
case "model":
|
|
|
|
if len(args) < 2 {
|
|
|
|
ColorLog("[ERRO] Wrong number of arguments\n")
|
|
|
|
ColorLog("[HINT] Usage: bee generate model [modelname] [-fields=\"\"]\n")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
cmd.Flag.Parse(args[2:])
|
|
|
|
if fields == "" {
|
|
|
|
ColorLog("[ERRO] Wrong number of arguments\n")
|
|
|
|
ColorLog("[HINT] Usage: bee generate model [modelname] [-fields=\"title:string,body:text\"]\n")
|
2014-08-08 17:37:55 +00:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-08-11 09:01:00 +00:00
|
|
|
sname := args[1]
|
|
|
|
ColorLog("[INFO] Using '%s' as model name\n", sname)
|
|
|
|
generateModel(sname, fields.String(), curpath)
|
2014-08-11 02:01:56 +00:00
|
|
|
case "view":
|
|
|
|
if len(args) == 2 {
|
|
|
|
cname := args[1]
|
|
|
|
generateView(cname, curpath)
|
|
|
|
} else {
|
|
|
|
ColorLog("[ERRO] Wrong number of arguments\n")
|
2014-08-11 09:01:00 +00:00
|
|
|
ColorLog("[HINT] Usage: bee generate view [viewpath]\n")
|
2014-08-11 02:01:56 +00:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-06-18 04:19:03 +00:00
|
|
|
default:
|
|
|
|
ColorLog("[ERRO] command is missing\n")
|
|
|
|
}
|
|
|
|
ColorLog("[SUCC] generate successfully created!\n")
|
2014-08-15 09:38:51 +00:00
|
|
|
return 0
|
2014-06-18 04:19:03 +00:00
|
|
|
}
|