1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 05:00:54 +00:00
bee/createapp.go

165 lines
4.0 KiB
Go
Raw Normal View History

2013-04-11 03:13:58 +00:00
package main
import (
"fmt"
"os"
2013-06-26 13:02:05 +00:00
path "path/filepath"
2013-04-11 03:13:58 +00:00
"strings"
)
var cmdCreate = &Command{
UsageLine: "create [appname]",
Short: "create an application base on beego framework",
Long: `
create an application base on beego framework
In the current path, will create a folder named [appname]
In the appname folder has the follow struct:
2013-06-26 13:02:05 +00:00
|- main.go
|- conf
|- app.conf
|- controllers
|- default.go
|- models
|- static
|- js
|- css
|- img
|- views
index.tpl
2013-04-11 03:13:58 +00:00
`,
}
func init() {
cmdCreate.Run = createapp
}
func createapp(cmd *Command, args []string) {
2013-06-26 13:02:05 +00:00
curpath, _ := os.Getwd()
2013-04-11 03:13:58 +00:00
if len(args) != 1 {
fmt.Println("error args")
os.Exit(2)
}
2013-06-26 13:02:05 +00:00
2013-04-11 03:13:58 +00:00
gopath := os.Getenv("GOPATH")
if gopath == "" {
fmt.Println("you should set GOPATH in the env")
os.Exit(2)
}
haspath := false
2013-06-25 15:52:26 +00:00
appsrcpath := ""
2013-04-11 03:13:58 +00:00
2013-06-26 13:02:05 +00:00
wgopath := path.SplitList(gopath)
for _, wg := range wgopath {
wg = path.Join(wg, "src")
if path.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) {
haspath = true
appsrcpath = wg
break
}
2013-04-11 03:13:58 +00:00
}
2013-06-26 13:02:05 +00:00
2013-04-11 03:13:58 +00:00
if !haspath {
2013-06-26 13:02:05 +00:00
fmt.Printf("can't create application outside of GOPATH `%s`\n", gopath)
fmt.Printf("you first should `cd $GOPATH%ssrc` then use create\n", string(path.Separator))
2013-04-11 03:13:58 +00:00
os.Exit(2)
}
2013-06-26 13:02:05 +00:00
apppath := path.Join(curpath, args[0])
if _, err := os.Stat(apppath); os.IsNotExist(err) == false {
fmt.Printf("path `%s` exists, can not create app without remove it\n", apppath)
os.Exit(2)
}
os.MkdirAll(apppath, 0755)
2013-04-11 03:13:58 +00:00
fmt.Println("create app folder:", apppath)
os.Mkdir(path.Join(apppath, "conf"), 0755)
fmt.Println("create conf:", path.Join(apppath, "conf"))
os.Mkdir(path.Join(apppath, "controllers"), 0755)
fmt.Println("create controllers:", path.Join(apppath, "controllers"))
os.Mkdir(path.Join(apppath, "models"), 0755)
fmt.Println("create models:", path.Join(apppath, "models"))
os.Mkdir(path.Join(apppath, "static"), 0755)
fmt.Println("create static:", path.Join(apppath, "static"))
os.Mkdir(path.Join(apppath, "static", "js"), 0755)
fmt.Println("create static js:", path.Join(apppath, "static", "js"))
os.Mkdir(path.Join(apppath, "static", "css"), 0755)
fmt.Println("create static css:", path.Join(apppath, "static", "css"))
os.Mkdir(path.Join(apppath, "static", "img"), 0755)
fmt.Println("create static img:", path.Join(apppath, "static", "img"))
fmt.Println("create views:", path.Join(apppath, "views"))
os.Mkdir(path.Join(apppath, "views"), 0755)
fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf"))
2013-04-11 06:49:19 +00:00
writetofile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", args[0], -1))
2013-04-11 03:13:58 +00:00
fmt.Println("create controllers default.go:", path.Join(apppath, "controllers", "default.go"))
writetofile(path.Join(apppath, "controllers", "default.go"), controllers)
fmt.Println("create views index.tpl:", path.Join(apppath, "views", "index.tpl"))
writetofile(path.Join(apppath, "views", "index.tpl"), indextpl)
fmt.Println("create main.go:", path.Join(apppath, "main.go"))
2013-06-26 13:02:05 +00:00
writetofile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1))
2013-04-11 03:13:58 +00:00
}
2013-04-11 06:49:19 +00:00
var appconf = `
appname = {{.Appname}}
httpport = 8080
runmode = dev
`
2013-04-11 03:13:58 +00:00
var maingo = `package main
import (
"{{.Appname}}/controllers"
"github.com/astaxie/beego"
)
func main() {
beego.Router("/", &controllers.MainController{})
beego.Run()
}
`
var controllers = `package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (this *MainController) Get() {
this.Data["Username"] = "astaxie"
this.Data["Email"] = "astaxie@gmail.com"
this.TplNames = "index.tpl"
}
`
var indextpl = `<!DOCTYPE html>
<html>
<head>
<title>beego welcome template</title>
</head>
<body>
<h1>Hello, world!{{.Username}},{{.Email}}</h1>
</body>
</html>
`
func writetofile(filename, content string) {
f, err := os.Create(filename)
if err != nil {
panic(err)
}
defer f.Close()
f.WriteString(content)
}