diff --git a/main.go b/bee.go similarity index 97% rename from main.go rename to bee.go index 6a479f6..1ad6cb7 100644 --- a/main.go +++ b/bee.go @@ -1,3 +1,4 @@ +// Bee is a tool for developling applications based on beego framework. package main import ( @@ -56,8 +57,8 @@ func (c *Command) Runnable() bool { } var commands = []*Command{ - cmdCreate, - cmdStart, + cmdNew, + cmdRun, cmdPack, cmdApiapp, //cmdReStart, diff --git a/bee.json b/bee.json new file mode 100644 index 0000000..164b7f6 --- /dev/null +++ b/bee.json @@ -0,0 +1,9 @@ +{ + "dir_structure":{ + "controllers": "", + "models": "" + }, + "files": [ + "main.go" + ] +} \ No newline at end of file diff --git a/createapp.go b/createapp.go deleted file mode 100644 index 7c2fce9..0000000 --- a/createapp.go +++ /dev/null @@ -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 = ` - - - beego welcome template - - -

Hello, world!{{.Username}},{{.Email}}

- - -` - -func writetofile(filename, content string) { - f, err := os.Create(filename) - if err != nil { - panic(err) - } - defer f.Close() - f.WriteString(content) -} diff --git a/new.go b/new.go new file mode 100644 index 0000000..63ee0d9 --- /dev/null +++ b/new.go @@ -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 = ` + + + + Beego + + + + + + +
+
+
+
+

Welcome to Beego!

+

+ Beego is a simple & powerful Go web framework which is inspired by tornado and sinatra. +
+ Official website: {{.Website}} +
+ Contact me: {{.Email}} +

+
+
+
+
+ + +` + +func writetofile(filename, content string) { + f, err := os.Create(filename) + if err != nil { + panic(err) + } + defer f.Close() + f.WriteString(content) +} diff --git a/run.go b/run.go new file mode 100644 index 0000000..4b3b3be --- /dev/null +++ b/run.go @@ -0,0 +1,95 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + path "path/filepath" + "runtime" +) + +var cmdRun = &Command{ + UsageLine: "run [appname]", + Short: "run the app which can hot compile", + Long: ` +start the appname throw exec.Command + +then start a inotify watch for current dir + +when the file has changed bee will auto go build and restart the app + + file changed + | + check if it's go file + | + yes no + | | + go build do nothing + | + restart app +`, +} + +func init() { + cmdRun.Run = runApp +} + +var appname string +var conf struct { + DirStruct struct { + Controllers string + Models string + } `json:"dir_structure"` + Files []string +} + +func runApp(cmd *Command, args []string) { + if len(args) != 1 { + fmt.Println("[ERRO] Argument [appname] is missing") + os.Exit(2) + } + crupath, _ := os.Getwd() + Debugf("current path:%s\n", crupath) + + err := loadConfig() + if err != nil { + fmt.Println("[ERRO] Fail to parse bee.json:", err) + } + var paths []string + paths = append(paths, + path.Join(crupath, conf.DirStruct.Controllers), + path.Join(crupath, conf.DirStruct.Models)) + paths = append(paths, conf.Files...) + + NewWatcher(paths) + appname = args[0] + Autobuild() + for { + runtime.Gosched() + } +} + +// loadConfig loads customized configuration. +func loadConfig() error { + f, err := os.Open("bee.json") + if err != nil { + // Use default. + return nil + } + defer f.Close() + + d := json.NewDecoder(f) + err = d.Decode(&conf) + if err != nil { + return err + } + + // Set variables. + if len(conf.DirStruct.Controllers) == 0 { + conf.DirStruct.Controllers = "controllers" + } + if len(conf.DirStruct.Models) == 0 { + conf.DirStruct.Models = "models" + } + return nil +} diff --git a/start.go b/start.go deleted file mode 100644 index 4e8acef..0000000 --- a/start.go +++ /dev/null @@ -1,54 +0,0 @@ -package main - -import ( - "fmt" - "os" - path "path/filepath" - "runtime" -) - -var cmdStart = &Command{ - UsageLine: "start [appname]", - Short: "start the app which can hot compile", - Long: ` -start the appname throw exec.Command - -then start a inotify watch for current dir - -when the file has changed bee will auto go build and restart the app - - file changed - | - checked is go file - | - yes no - | | - go build do nothing - | - restart app -`, -} - -func init() { - cmdStart.Run = startapp -} - -var appname string - -func startapp(cmd *Command, args []string) { - if len(args) != 1 { - fmt.Println("error args") - os.Exit(2) - } - crupath, _ := os.Getwd() - Debugf("current path:%s\n", crupath) - - var paths []string - paths = append(paths, path.Join(crupath, "controllers"), path.Join(crupath, "models")) - NewWatcher(paths) - appname = args[0] - Autobuild() - for { - runtime.Gosched() - } -} diff --git a/watch.go b/watch.go index 012fc4f..db6bc0b 100644 --- a/watch.go +++ b/watch.go @@ -45,7 +45,7 @@ func NewWatcher(paths []string) { eventTime[e.Name] = time.Now() if isbuild { - fmt.Println(e) + fmt.Println("[EVEN]", e) go Autobuild() } case err := <-watcher.Error: @@ -53,6 +53,8 @@ func NewWatcher(paths []string) { } } }() + + fmt.Println("[INFO] Initializing watcher...") for _, path := range paths { fmt.Println(path) err = watcher.Watch(path) @@ -67,7 +69,7 @@ func Autobuild() { state.Lock() defer state.Unlock() - fmt.Println("start autobuild") + fmt.Println("[INFO] Start building...") path, _ := os.Getwd() os.Chdir(path) bcmd := exec.Command("go", "build") @@ -76,10 +78,10 @@ func Autobuild() { err := bcmd.Run() if err != nil { - fmt.Println("============== build failed ===================") + fmt.Println("[ERRO] ============== Build failed ===================") return } - fmt.Println("build success") + fmt.Println("[SUCC] Build was successful") Restart(appname) } @@ -101,8 +103,7 @@ func Restart(appname string) { } func Start(appname string) { - fmt.Println("start", appname) - + fmt.Println("[INFO] Restarting", appname) if strings.Index(appname, "./") == -1 { appname = "./" + appname }