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

half work

This commit is contained in:
sun shengxiang 2013-07-06 15:30:57 +08:00
parent 14510fc9b6
commit 0cf1a553de
4 changed files with 125 additions and 43 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,31 @@ 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)
}
} }
//go Start(args[0])
//for {
// select {
// case <-restart:
// go Start(args[0])
// case err := <-builderror:
// fmt.Println("build error:", err)
// }
//}
} }

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...)
}
}

113
watch.go
View File

@ -1,27 +1,33 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"github.com/howeyc/fsnotify" "github.com/howeyc/fsnotify"
"io" //"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"runtime" "runtime"
"sync"
"time"
) )
var ( var (
builderror chan string builderror chan string
restart chan bool restart chan bool
cmd *exec.Cmd cmd *exec.Cmd
state sync.Mutex
running bool
) )
func init() { func init() {
builderror = make(chan string) builderror = make(chan string)
restart = make(chan bool) //restart = make(chan bool)
running = false
} }
var eventTime = make(map[string]time.Time)
func NewWatcher(paths []string) { func NewWatcher(paths []string) {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
@ -32,8 +38,21 @@ func NewWatcher(paths []string) {
for { for {
select { select {
case e := <-watcher.Event: case e := <-watcher.Event:
fmt.Println(e)
go Autobuild() isbuild := true
if t, ok := eventTime[e.String()]; ok {
//Debugf("%s pk %s", t, time.Now())
if t.Add(time.Millisecond * 500).After(time.Now()) {
isbuild = false
}
}
Debugf("isbuild:%v", isbuild)
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)
} }
@ -64,23 +83,18 @@ func Autobuild() {
} }
}() }()
fmt.Println("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) builderror <- err.Error()
} return
if out.String() == "" {
Kill()
} else {
builderror <- berr.String()
} }
Restart(appname)
} }
func Kill() { func Kill() {
@ -90,31 +104,58 @@ func Kill() {
} }
} }
func Restart(appname string) {
Kill()
go Start()
}
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)
} ch := Go(func() error {
stderr, err := cmd.StderrPipe() state.Lock()
if err != nil { defer state.Unlock()
fmt.Println("stdin:", err) running = true
} return cmd.Run()
r := io.MultiReader(stdout, stderr) })
err = cmd.Start()
if err != nil {
fmt.Println("cmd start:", err)
}
for { for {
buf := make([]byte, 1024) select {
count, err := r.Read(buf) case err := <-ch:
if err != nil || count == 0 { fmt.Println("cmd start error: ", err)
fmt.Println("process exit") state.Lock()
restart <- true defer state.Unlock()
running = false
return return
} else { case <-time.After(2 * time.Second):
fmt.Println("result:", string(buf))
} }
} }
//stdout, err := cmd.StdoutPipe()
//if err != nil {
//fmt.Println("stdout:", err)
//}
//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))
// }
//}
} }