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

Merge pull request #10 from shxsun/master

简化代码的结构,解决liteide短时间内更新文件频繁的问题
This commit is contained in:
astaxie 2013-07-07 02:05:12 -07:00
commit f242623d30
4 changed files with 86 additions and 71 deletions

View File

@ -45,6 +45,7 @@ func createapp(cmd *Command, args []string) {
} }
gopath := os.Getenv("GOPATH") gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" { if gopath == "" {
fmt.Println("you should set GOPATH in the env") fmt.Println("you should set GOPATH in the env")
os.Exit(2) os.Exit(2)

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
path "path/filepath" path "path/filepath"
"runtime"
) )
var cmdStart = &Command{ var cmdStart = &Command{
@ -32,22 +33,22 @@ func init() {
cmdStart.Run = startapp cmdStart.Run = startapp
} }
var appname string
func startapp(cmd *Command, args []string) { func startapp(cmd *Command, args []string) {
if len(args) != 1 { if len(args) != 1 {
fmt.Println("error args") fmt.Println("error args")
os.Exit(2) os.Exit(2)
} }
crupath, _ := os.Getwd() crupath, _ := os.Getwd()
Debugf("current path:%s\n", crupath)
var paths []string var paths []string
paths = append(paths, path.Join(crupath, "controllers"), path.Join(crupath, "models")) paths = append(paths, path.Join(crupath, "controllers"), path.Join(crupath, "models"))
NewWatcher(paths) NewWatcher(paths)
go Start(args[0]) appname = args[0]
Autobuild()
for { for {
select { runtime.Gosched()
case <-restart:
go Start(args[0])
case <-builderror:
fmt.Println("build error:", builderror)
}
} }
} }

30
util.go Normal file
View File

@ -0,0 +1,30 @@
package main
import "fmt"
import "os"
import "runtime"
import "path/filepath"
// Go is a basic promise implementation: it wraps calls a function in a goroutine
// and returns a channel which will later return the function's return value.
func Go(f func() error) chan error {
ch := make(chan error)
go func() {
ch <- f()
}()
return ch
}
// if os.env DEBUG set, debug is on
func Debugf(format string, a ...interface{}) {
if os.Getenv("DEBUG") != "" {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "<unknown>"
line = -1
} else {
file = filepath.Base(file)
}
fmt.Fprintf(os.Stderr, fmt.Sprintf("[debug] %s:%d %s\n", file, line, format), a...)
}
}

111
watch.go
View File

@ -1,27 +1,21 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"github.com/howeyc/fsnotify" "github.com/howeyc/fsnotify"
"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"runtime" "sync"
"time"
) )
var ( var (
builderror chan string cmd *exec.Cmd
restart chan bool state sync.Mutex
cmd *exec.Cmd eventTime = make(map[string]time.Time)
) )
func init() {
builderror = make(chan string)
restart = make(chan bool)
}
func NewWatcher(paths []string) { func NewWatcher(paths []string) {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
@ -32,8 +26,20 @@ func NewWatcher(paths []string) {
for { for {
select { select {
case e := <-watcher.Event: case e := <-watcher.Event:
fmt.Println(e) isbuild := true
go Autobuild() if t, ok := eventTime[e.String()]; ok {
// if 500ms change many times, then ignore it.
// for liteide often gofmt code after save.
if t.Add(time.Millisecond * 500).After(time.Now()) {
isbuild = false
}
}
eventTime[e.String()] = time.Now()
if isbuild {
fmt.Println(e)
go Autobuild()
}
case err := <-watcher.Error: case err := <-watcher.Error:
log.Fatal("error:", err) log.Fatal("error:", err)
} }
@ -50,71 +56,48 @@ func NewWatcher(paths []string) {
} }
func Autobuild() { func Autobuild() {
defer func() { state.Lock()
if err := recover(); err != nil { defer state.Unlock()
str := ""
for i := 1; ; i += 1 {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
str = str + fmt.Sprintf("%v,%v", file, line)
}
builderror <- str
} fmt.Println("start autobuild")
}()
fmt.Println("Autobuild")
path, _ := os.Getwd() path, _ := os.Getwd()
os.Chdir(path) os.Chdir(path)
bcmd := exec.Command("go", "build") bcmd := exec.Command("go", "build")
var out bytes.Buffer bcmd.Stdout = os.Stdout
var berr bytes.Buffer bcmd.Stderr = os.Stderr
bcmd.Stdout = &out
bcmd.Stderr = &berr
err := bcmd.Run() err := bcmd.Run()
if err != nil { if err != nil {
fmt.Println("run error", err) fmt.Println("============== build failed ===================")
} return
if out.String() == "" {
Kill()
} else {
builderror <- berr.String()
} }
fmt.Println("build success")
Restart(appname)
} }
func Kill() { func Kill() {
err := cmd.Process.Kill() defer func() {
if err != nil { if e := recover(); e != nil {
panic(err) fmt.Println("Kill -> ", e)
}
}()
if cmd != nil {
cmd.Process.Kill()
} }
} }
func Restart(appname string) {
Debugf("kill running process")
Kill()
go Start(appname)
}
func Start(appname string) { func Start(appname string) {
fmt.Println("start", appname) fmt.Println("start", appname)
cmd = exec.Command(appname) cmd = exec.Command(appname)
stdout, err := cmd.StdoutPipe() cmd.Stdout = os.Stdout
if err != nil { cmd.Stderr = os.Stderr
fmt.Println("stdout:", err)
} go cmd.Run()
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println("stdin:", err)
}
r := io.MultiReader(stdout, stderr)
err = cmd.Start()
if err != nil {
fmt.Println("cmd start:", err)
}
for {
buf := make([]byte, 1024)
count, err := r.Read(buf)
if err != nil || count == 0 {
fmt.Println("process exit")
restart <- true
return
} else {
fmt.Println("result:", string(buf))
}
}
} }