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

Update tpl file and code for create new Beego application

This commit is contained in:
Unknown 2013-07-24 19:20:30 +08:00
parent 0feb778d4d
commit 9e9f0ce1dd
3 changed files with 244 additions and 166 deletions

View File

@ -1,165 +0,0 @@
package main
import (
"fmt"
"os"
path "path/filepath"
"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:
|- main.go
|- conf
|- app.conf
|- controllers
|- default.go
|- models
|- static
|- js
|- css
|- img
|- views
index.tpl
`,
}
func init() {
cmdCreate.Run = createapp
}
func createapp(cmd *Command, args []string) {
curpath, _ := os.Getwd()
if len(args) != 1 {
fmt.Println("error args")
os.Exit(2)
}
gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" {
fmt.Println("you should set GOPATH in the env")
os.Exit(2)
}
haspath := false
appsrcpath := ""
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
}
}
if !haspath {
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))
os.Exit(2)
}
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)
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"))
writetofile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", args[0], -1))
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"))
writetofile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1))
}
var appconf = `
appname = {{.Appname}}
httpport = 8080
runmode = dev
`
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)
}

View File

@ -1,3 +1,4 @@
// Bee is a tool for developling applications based on beego framework.
package main package main
import ( import (
@ -56,7 +57,7 @@ func (c *Command) Runnable() bool {
} }
var commands = []*Command{ var commands = []*Command{
cmdCreate, cmdNew,
cmdStart, cmdStart,
cmdPack, cmdPack,
cmdApiapp, cmdApiapp,

242
new.go Normal file
View File

@ -0,0 +1,242 @@
package main
import (
"fmt"
"os"
path "path/filepath"
"strings"
)
var cmdNew = &Command{
UsageLine: "new [appname]",
Short: "create an application base on beego framework",
Long: `
create an application base on beego framework,
which in the current path with folder named [appname].
The [appname] folder has following structure:
|- main.go
|- conf
|- app.conf
|- controllers
|- default.go
|- models
|- static
|- js
|- css
|- img
|- views
index.tpl
`,
}
func init() {
cmdNew.Run = createApp
}
func createApp(cmd *Command, args []string) {
curpath, _ := os.Getwd()
if len(args) != 1 {
fmt.Println("[ERRO] Argument [appname] is missing")
os.Exit(2)
}
gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" {
fmt.Printf("[ERRO] $GOPATH not found\n")
fmt.Printf("[HINT] Set $GOPATH in your environment vairables\n")
os.Exit(2)
}
haspath := false
appsrcpath := ""
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
}
}
if !haspath {
fmt.Printf("[ERRO] Unable to create an application outside of $GOPATH(%s)\n", gopath)
fmt.Printf("[HINT] Change your work directory by `cd $GOPATH%ssrc`\n", string(path.Separator))
os.Exit(2)
}
apppath := path.Join(curpath, args[0])
if _, err := os.Stat(apppath); os.IsNotExist(err) == false {
fmt.Printf("[ERRO] Path(%s) has alreay existed\n", apppath)
os.Exit(2)
}
fmt.Println("[INFO] Creating application...")
os.MkdirAll(apppath, 0755)
fmt.Println(apppath + string(path.Separator))
os.Mkdir(path.Join(apppath, "conf"), 0755)
fmt.Println(path.Join(apppath, "conf") + string(path.Separator))
os.Mkdir(path.Join(apppath, "controllers"), 0755)
fmt.Println(path.Join(apppath, "controllers") + string(path.Separator))
os.Mkdir(path.Join(apppath, "models"), 0755)
fmt.Println(path.Join(apppath, "models") + string(path.Separator))
os.Mkdir(path.Join(apppath, "static"), 0755)
fmt.Println(path.Join(apppath, "static") + string(path.Separator))
os.Mkdir(path.Join(apppath, "static", "js"), 0755)
fmt.Println(path.Join(apppath, "static", "js") + string(path.Separator))
os.Mkdir(path.Join(apppath, "static", "css"), 0755)
fmt.Println(path.Join(apppath, "static", "css") + string(path.Separator))
os.Mkdir(path.Join(apppath, "static", "img"), 0755)
fmt.Println(path.Join(apppath, "static", "img") + string(path.Separator))
fmt.Println(path.Join(apppath, "views") + string(path.Separator))
os.Mkdir(path.Join(apppath, "views"), 0755)
fmt.Println(path.Join(apppath, "conf", "app.conf"))
writetofile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", args[0], -1))
fmt.Println(path.Join(apppath, "controllers", "default.go"))
writetofile(path.Join(apppath, "controllers", "default.go"), controllers)
fmt.Println(path.Join(apppath, "views", "index.tpl"))
writetofile(path.Join(apppath, "views", "index.tpl"), indextpl)
fmt.Println(path.Join(apppath, "main.go"))
writetofile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), string(path.Separator)), -1))
fmt.Println("[SUCC] New application successfully created!")
}
var appconf = `appname = {{.Appname}}
httpport = 8080
runmode = dev
`
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["Website"] = "beego.me"
this.Data["Email"] = "astaxie@gmail.com"
this.TplNames = "index.tpl"
}
`
var indextpl = `<!DOCTYPE html>
<html>
<head>
<title>Beego</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<style type="text/css">
body {
margin: 0px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 14px;
line-height: 20px;
color: rgb(51, 51, 51);
background-color: rgb(255, 255, 255);
}
.hero-unit {
padding: 60px;
margin-bottom: 30px;
border-radius: 6px 6px 6px 6px;
}
.container {
width: 940px;
margin-right: auto;
margin-left: auto;
}
.row {
margin-left: -20px;
}
h1 {
margin: 10px 0px;
font-family: inherit;
font-weight: bold;
text-rendering: optimizelegibility;
}
.hero-unit h1 {
margin-bottom: 0px;
font-size: 60px;
line-height: 1;
letter-spacing: -1px;
color: inherit;
}
.description {
padding-top: 5px;
padding-left: 5px;
font-size: 18px;
font-weight: 200;
line-height: 30px;
color: inherit;
}
p {
margin: 0px 0px 10px;
}
</style>
<body>
<header class="hero-unit" style="background-color:#A9F16C">
<div class="container">
<div class="row">
<div class="hero-text">
<h1>Welcome to Beego!</h1>
<p class="description">
Beego is a simple & powerful Go web framework which is inspired by tornado and sinatra.
<br />
Official website: <a href="http://{{.Website}}">{{.Website}}</a>
<br />
Contact me: {{.Email}}</a>
</p>
</div>
</div>
</div>
</header>
</body>
</html>
`
func writetofile(filename, content string) {
f, err := os.Create(filename)
if err != nil {
panic(err)
}
defer f.Close()
f.WriteString(content)
}