2012-12-15 16:56:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/howeyc/fsnotify"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2013-07-24 06:58:13 +00:00
|
|
|
"strings"
|
2013-07-06 07:30:57 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2012-12-15 16:56:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2013-07-06 08:13:30 +00:00
|
|
|
cmd *exec.Cmd
|
|
|
|
state sync.Mutex
|
|
|
|
eventTime = make(map[string]time.Time)
|
2012-12-15 16:56:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewWatcher(paths []string) {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-watcher.Event:
|
2013-07-06 07:30:57 +00:00
|
|
|
isbuild := true
|
2013-07-24 06:58:13 +00:00
|
|
|
|
|
|
|
// Skip TMP files for Sublime Text.
|
|
|
|
if checkTMPFile(e.Name) {
|
|
|
|
continue
|
|
|
|
}
|
2013-07-30 07:23:14 +00:00
|
|
|
if !checkIsGoFile(e.Name) {
|
|
|
|
continue
|
|
|
|
}
|
2013-07-24 06:58:13 +00:00
|
|
|
|
|
|
|
if t, ok := eventTime[e.Name]; ok {
|
2013-07-06 08:13:30 +00:00
|
|
|
// if 500ms change many times, then ignore it.
|
|
|
|
// for liteide often gofmt code after save.
|
2013-07-06 07:30:57 +00:00
|
|
|
if t.Add(time.Millisecond * 500).After(time.Now()) {
|
2013-07-24 06:58:13 +00:00
|
|
|
fmt.Println("[SKIP]", e.String())
|
2013-07-06 07:30:57 +00:00
|
|
|
isbuild = false
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 06:58:13 +00:00
|
|
|
eventTime[e.Name] = time.Now()
|
2013-07-06 07:30:57 +00:00
|
|
|
|
|
|
|
if isbuild {
|
2013-07-24 12:01:14 +00:00
|
|
|
fmt.Println("[EVEN]", e)
|
2013-07-06 07:30:57 +00:00
|
|
|
go Autobuild()
|
|
|
|
}
|
2012-12-15 16:56:28 +00:00
|
|
|
case err := <-watcher.Error:
|
|
|
|
log.Fatal("error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2013-07-24 12:36:22 +00:00
|
|
|
|
|
|
|
fmt.Println("[INFO] Initializing watcher...")
|
2012-12-15 16:56:28 +00:00
|
|
|
for _, path := range paths {
|
|
|
|
fmt.Println(path)
|
|
|
|
err = watcher.Watch(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Autobuild() {
|
2013-07-06 08:13:30 +00:00
|
|
|
state.Lock()
|
|
|
|
defer state.Unlock()
|
2012-12-15 16:56:28 +00:00
|
|
|
|
2013-07-24 12:01:14 +00:00
|
|
|
fmt.Println("[INFO] Start building...")
|
2012-12-15 16:56:28 +00:00
|
|
|
path, _ := os.Getwd()
|
|
|
|
os.Chdir(path)
|
2013-07-25 08:26:54 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
// For applications use full import path like "github.com/.../.."
|
|
|
|
// are able to use "go install" to reduce build time.
|
|
|
|
if conf.GoInstall {
|
|
|
|
icmd := exec.Command("go", "install")
|
|
|
|
icmd.Stdout = os.Stdout
|
|
|
|
icmd.Stderr = os.Stderr
|
|
|
|
err = icmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
bcmd := exec.Command("go", "build")
|
|
|
|
bcmd.Stdout = os.Stdout
|
|
|
|
bcmd.Stderr = os.Stderr
|
|
|
|
err = bcmd.Run()
|
|
|
|
}
|
2013-07-06 08:13:30 +00:00
|
|
|
|
2012-12-15 16:56:28 +00:00
|
|
|
if err != nil {
|
2013-07-24 12:01:14 +00:00
|
|
|
fmt.Println("[ERRO] ============== Build failed ===================")
|
2013-07-06 07:30:57 +00:00
|
|
|
return
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
2013-07-24 12:01:14 +00:00
|
|
|
fmt.Println("[SUCC] Build was successful")
|
2013-07-06 07:30:57 +00:00
|
|
|
Restart(appname)
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Kill() {
|
2013-07-06 08:13:30 +00:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
fmt.Println("Kill -> ", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if cmd != nil {
|
|
|
|
cmd.Process.Kill()
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-06 07:30:57 +00:00
|
|
|
func Restart(appname string) {
|
2013-07-06 08:13:30 +00:00
|
|
|
Debugf("kill running process")
|
2013-07-06 07:30:57 +00:00
|
|
|
Kill()
|
2013-07-06 08:13:30 +00:00
|
|
|
go Start(appname)
|
2013-07-06 07:30:57 +00:00
|
|
|
}
|
2013-07-06 08:13:30 +00:00
|
|
|
|
2012-12-15 16:56:28 +00:00
|
|
|
func Start(appname string) {
|
2013-07-24 12:01:14 +00:00
|
|
|
fmt.Println("[INFO] Restarting", appname)
|
2013-07-22 09:17:08 +00:00
|
|
|
if strings.Index(appname, "./") == -1 {
|
|
|
|
appname = "./" + appname
|
|
|
|
}
|
2013-07-06 07:30:57 +00:00
|
|
|
|
2012-12-15 16:56:28 +00:00
|
|
|
cmd = exec.Command(appname)
|
2013-07-06 07:30:57 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
2013-07-06 08:13:30 +00:00
|
|
|
go cmd.Run()
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
2013-07-24 06:58:13 +00:00
|
|
|
|
|
|
|
// checkTMPFile returns true if the event was for TMP files.
|
|
|
|
func checkTMPFile(name string) bool {
|
|
|
|
if strings.HasSuffix(strings.ToLower(name), ".tmp") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2013-07-30 07:23:14 +00:00
|
|
|
|
2013-07-31 06:44:56 +00:00
|
|
|
// checkIsGoFile returns true if the name HasSuffix ".go".
|
2013-07-30 07:23:14 +00:00
|
|
|
func checkIsGoFile(name string) bool {
|
|
|
|
if strings.HasSuffix(name, ".go") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|