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

Add user conf support for bee.json

This commit is contained in:
Unknown 2013-07-24 20:01:14 +08:00
parent 9e9f0ce1dd
commit 172bc44b22
5 changed files with 104 additions and 61 deletions

View File

@ -58,7 +58,7 @@ func (c *Command) Runnable() bool {
var commands = []*Command{
cmdNew,
cmdStart,
cmdRun,
cmdPack,
cmdApiapp,
//cmdReStart,

5
bee.json Normal file
View File

@ -0,0 +1,5 @@
{
"dir_structure":{
"controllers": "routers"
}
}

93
run.go Normal file
View File

@ -0,0 +1,93 @@
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"`
}
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))
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
}

View File

@ -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()
}
}

View File

@ -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:
@ -67,7 +67,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 +76,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 +101,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
}