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

132 lines
2.5 KiB
Go
Raw Normal View History

2013-07-24 12:01:14 +00:00
package main
import (
"encoding/json"
"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
`,
}
var defaultJson = `
{
"go_install": false,
"dir_structure":{
"controllers": "",
"models": "",
"others": []
},
"main_files":{
"main.go": "",
"others": []
}
}
`
2013-07-24 12:01:14 +00:00
func init() {
cmdRun.Run = runApp
}
var appname string
var conf struct {
2013-07-25 08:26:54 +00:00
// Indicates whether execute "go install" before "go build".
GoInstall bool `json:"go_install"`
2013-07-27 01:44:44 +00:00
2013-07-24 12:01:14 +00:00
DirStruct struct {
Controllers string
Models string
2013-07-27 01:44:44 +00:00
Others []string // Other directories.
2013-07-24 12:01:14 +00:00
} `json:"dir_structure"`
2013-07-27 01:44:44 +00:00
MainFiles struct {
Main string `json:"main.go"`
Others []string // Others files of package main.
} `json:"main_files"`
2013-07-24 12:01:14 +00:00
}
func runApp(cmd *Command, args []string) {
2013-08-09 09:49:14 +00:00
exit := make(chan bool)
2013-07-24 12:01:14 +00:00
if len(args) != 1 {
2013-08-09 14:40:46 +00:00
colorLog("[ERRO] Argument [appname] is missing\n")
2013-07-24 12:01:14 +00:00
os.Exit(2)
}
crupath, _ := os.Getwd()
Debugf("current path:%s\n", crupath)
err := loadConfig()
if err != nil {
2013-08-09 14:40:46 +00:00
colorLog("[ERRO] Fail to parse bee.json[ %s ]", err)
2013-07-24 12:01:14 +00:00
}
var paths []string
paths = append(paths,
path.Join(crupath, conf.DirStruct.Controllers),
2013-07-27 01:44:44 +00:00
path.Join(crupath, conf.DirStruct.Models),
path.Join(crupath, "./")) // Current path.
// Because monitor files has some issues, we watch current directory
// and ignore non-go files.
2013-07-27 01:44:44 +00:00
paths = append(paths, conf.DirStruct.Others...)
paths = append(paths, conf.MainFiles.Others...)
2013-07-24 12:01:14 +00:00
NewWatcher(paths)
appname = args[0]
Autobuild()
for {
2013-08-09 09:49:14 +00:00
select {
case <-exit:
runtime.Goexit()
}
2013-07-24 12:01:14 +00:00
}
}
// loadConfig loads customized configuration.
func loadConfig() error {
f, err := os.Open("bee.json")
if err != nil {
// Use default.
err = json.Unmarshal([]byte(defaultJson), &conf)
if err != nil {
return err
}
} else {
defer f.Close()
2013-08-09 14:40:46 +00:00
colorLog("[INFO] Detected bee.json\n")
d := json.NewDecoder(f)
err = d.Decode(&conf)
if err != nil {
return err
}
2013-07-24 12:01:14 +00:00
}
// Set variables.
if len(conf.DirStruct.Controllers) == 0 {
conf.DirStruct.Controllers = "controllers"
}
if len(conf.DirStruct.Models) == 0 {
conf.DirStruct.Models = "models"
}
2013-07-27 01:44:44 +00:00
if len(conf.MainFiles.Main) == 0 {
conf.MainFiles.Main = "main.go"
}
2013-07-24 12:01:14 +00:00
return nil
}