2013-09-03 17:23:58 +00:00
|
|
|
// Copyright 2013 bee authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
// under the License.
|
|
|
|
|
2012-12-15 16:56:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-12-15 23:08:51 +00:00
|
|
|
"bytes"
|
2012-12-15 16:56:28 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2013-11-26 23:37:29 +00:00
|
|
|
"runtime"
|
2013-07-24 06:58:13 +00:00
|
|
|
"strings"
|
2013-07-06 07:30:57 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2013-08-24 08:41:46 +00:00
|
|
|
|
|
|
|
"github.com/howeyc/fsnotify"
|
2012-12-15 16:56:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-11-24 16:42:16 +00:00
|
|
|
cmd *exec.Cmd
|
|
|
|
state sync.Mutex
|
|
|
|
eventTime = make(map[string]int64)
|
|
|
|
scheduleTime time.Time
|
2012-12-15 16:56:28 +00:00
|
|
|
)
|
|
|
|
|
2014-06-18 13:31:54 +00:00
|
|
|
func NewWatcher(paths []string, files []string, isgenerate bool) {
|
2012-12-15 16:56:28 +00:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[ERRO] Fail to create new Watcher[ %s ]\n", err)
|
2013-08-15 07:24:23 +00:00
|
|
|
os.Exit(2)
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2014-11-24 16:42:16 +00:00
|
|
|
if !checkIfWatchExt(e.Name) {
|
2013-07-30 07:23:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-07-24 06:58:13 +00:00
|
|
|
|
2013-08-15 04:51:26 +00:00
|
|
|
mt := getFileModTime(e.Name)
|
|
|
|
if t := eventTime[e.Name]; mt == t {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[SKIP] # %s #\n", e.String())
|
2013-08-15 04:51:26 +00:00
|
|
|
isbuild = false
|
2013-07-06 07:30:57 +00:00
|
|
|
}
|
2013-08-15 04:51:26 +00:00
|
|
|
|
|
|
|
eventTime[e.Name] = mt
|
2013-07-06 07:30:57 +00:00
|
|
|
|
|
|
|
if isbuild {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[EVEN] %s\n", e)
|
2014-11-24 16:42:16 +00:00
|
|
|
go func() {
|
|
|
|
// Wait 1s before autobuild util there is no file change.
|
|
|
|
scheduleTime = time.Now().Add(1 * time.Second)
|
|
|
|
for {
|
|
|
|
time.Sleep(scheduleTime.Sub(time.Now()))
|
|
|
|
if time.Now().After(scheduleTime) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Autobuild(files, isgenerate)
|
|
|
|
}()
|
2013-07-06 07:30:57 +00:00
|
|
|
}
|
2012-12-15 16:56:28 +00:00
|
|
|
case err := <-watcher.Error:
|
2013-10-30 23:54:53 +00:00
|
|
|
ColorLog("[WARN] %s\n", err.Error()) // No need to exit here
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2013-07-24 12:36:22 +00:00
|
|
|
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[INFO] Initializing watcher...\n")
|
2012-12-15 16:56:28 +00:00
|
|
|
for _, path := range paths {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[TRAC] Directory( %s )\n", path)
|
2012-12-15 16:56:28 +00:00
|
|
|
err = watcher.Watch(path)
|
|
|
|
if err != nil {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[ERRO] Fail to watch directory[ %s ]\n", err)
|
2013-08-15 07:24:23 +00:00
|
|
|
os.Exit(2)
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-15 04:51:26 +00:00
|
|
|
// getFileModTime retuens unix timestamp of `os.File.ModTime` by given path.
|
|
|
|
func getFileModTime(path string) int64 {
|
2013-08-23 20:39:10 +00:00
|
|
|
path = strings.Replace(path, "\\", "/", -1)
|
2013-08-15 04:51:26 +00:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[ERRO] Fail to open file[ %s ]\n", err)
|
2013-08-15 04:51:26 +00:00
|
|
|
return time.Now().Unix()
|
|
|
|
}
|
2013-08-15 04:52:32 +00:00
|
|
|
defer f.Close()
|
2013-08-15 04:51:26 +00:00
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[ERRO] Fail to get file information[ %s ]\n", err)
|
2013-08-15 04:51:26 +00:00
|
|
|
return time.Now().Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
return fi.ModTime().Unix()
|
|
|
|
}
|
|
|
|
|
2014-06-18 13:31:54 +00:00
|
|
|
func Autobuild(files []string, isgenerate bool) {
|
2013-07-06 08:13:30 +00:00
|
|
|
state.Lock()
|
|
|
|
defer state.Unlock()
|
2012-12-15 16:56:28 +00:00
|
|
|
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[INFO] Start building...\n")
|
2012-12-15 16:56:28 +00:00
|
|
|
path, _ := os.Getwd()
|
|
|
|
os.Chdir(path)
|
2013-07-25 08:26:54 +00:00
|
|
|
|
2013-11-04 03:16:15 +00:00
|
|
|
cmdName := "go"
|
|
|
|
if conf.Gopm.Enable {
|
|
|
|
cmdName = "gopm"
|
|
|
|
}
|
|
|
|
|
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.
|
2013-12-12 22:13:50 +00:00
|
|
|
if conf.GoInstall || conf.Gopm.Install {
|
2013-12-15 23:08:51 +00:00
|
|
|
icmd := exec.Command("go", "list", "./...")
|
|
|
|
buf := bytes.NewBuffer([]byte(""))
|
|
|
|
icmd.Stdout = buf
|
2016-03-12 12:05:15 +00:00
|
|
|
icmd.Env = append(os.Environ(), "GOGC=off")
|
2013-07-25 08:26:54 +00:00
|
|
|
err = icmd.Run()
|
2013-12-15 23:08:51 +00:00
|
|
|
if err == nil {
|
|
|
|
list := strings.Split(buf.String(), "\n")[1:]
|
|
|
|
for _, pkg := range list {
|
|
|
|
if len(pkg) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
icmd = exec.Command(cmdName, "install", pkg)
|
2013-12-15 23:28:17 +00:00
|
|
|
icmd.Stdout = os.Stdout
|
|
|
|
icmd.Stderr = os.Stderr
|
2016-03-12 12:05:15 +00:00
|
|
|
icmd.Env = append(os.Environ(), "GOGC=off")
|
2013-12-15 23:08:51 +00:00
|
|
|
err = icmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-25 08:26:54 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 13:31:54 +00:00
|
|
|
if isgenerate {
|
|
|
|
icmd := exec.Command("bee", "generate", "docs")
|
2016-03-12 12:05:15 +00:00
|
|
|
icmd.Env = append(os.Environ(), "GOGC=off")
|
2014-06-18 13:31:54 +00:00
|
|
|
icmd.Stdout = os.Stdout
|
|
|
|
icmd.Stderr = os.Stderr
|
|
|
|
icmd.Run()
|
|
|
|
ColorLog("============== generate docs ===================\n")
|
|
|
|
}
|
|
|
|
|
2013-07-25 08:26:54 +00:00
|
|
|
if err == nil {
|
2013-11-26 23:37:29 +00:00
|
|
|
appName := appname
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
appName += ".exe"
|
|
|
|
}
|
2013-12-15 23:08:51 +00:00
|
|
|
|
2014-02-21 17:51:18 +00:00
|
|
|
args := []string{"build"}
|
|
|
|
args = append(args, "-o", appName)
|
2016-02-10 19:39:02 +00:00
|
|
|
if buildTags != "" {
|
|
|
|
args = append(args, "-tags", buildTags)
|
|
|
|
}
|
2014-02-21 17:51:18 +00:00
|
|
|
args = append(args, files...)
|
|
|
|
|
|
|
|
bcmd := exec.Command(cmdName, args...)
|
2016-03-12 12:05:15 +00:00
|
|
|
bcmd.Env = append(os.Environ(), "GOGC=off")
|
2013-12-15 23:08:51 +00:00
|
|
|
bcmd.Stdout = os.Stdout
|
|
|
|
bcmd.Stderr = os.Stderr
|
|
|
|
err = bcmd.Run()
|
2013-07-25 08:26:54 +00:00
|
|
|
}
|
2013-07-06 08:13:30 +00:00
|
|
|
|
2012-12-15 16:56:28 +00:00
|
|
|
if err != nil {
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[ERRO] ============== Build failed ===================\n")
|
2013-07-06 07:30:57 +00:00
|
|
|
return
|
2012-12-15 16:56:28 +00:00
|
|
|
}
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[SUCC] Build was successful\n")
|
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 {
|
2013-12-15 23:08:51 +00:00
|
|
|
fmt.Println("Kill.recover -> ", e)
|
2013-07-06 08:13:30 +00:00
|
|
|
}
|
|
|
|
}()
|
2013-09-24 02:42:02 +00:00
|
|
|
if cmd != nil && cmd.Process != nil {
|
2013-12-15 23:08:51 +00:00
|
|
|
err := cmd.Process.Kill()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Kill -> ", err)
|
|
|
|
}
|
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-10-30 23:39:44 +00:00
|
|
|
ColorLog("[INFO] Restarting %s ...\n", 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-11-27 05:15:00 +00:00
|
|
|
cmd.Args = append([]string{appname}, conf.CmdArgs...)
|
|
|
|
cmd.Env = append(os.Environ(), conf.Envs...)
|
2013-07-06 07:30:57 +00:00
|
|
|
|
2013-07-06 08:13:30 +00:00
|
|
|
go cmd.Run()
|
2013-10-30 23:39:44 +00:00
|
|
|
ColorLog("[INFO] %s is running...\n", appname)
|
2013-08-24 11:00:08 +00:00
|
|
|
started <- true
|
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-09-04 15:23:51 +00:00
|
|
|
var watchExts = []string{".go"}
|
|
|
|
|
2014-11-24 16:42:16 +00:00
|
|
|
// checkIfWatchExt returns true if the name HasSuffix <watch_ext>.
|
|
|
|
func checkIfWatchExt(name string) bool {
|
2013-09-04 15:23:51 +00:00
|
|
|
for _, s := range watchExts {
|
|
|
|
if strings.HasSuffix(name, s) {
|
|
|
|
return true
|
|
|
|
}
|
2013-07-30 07:23:14 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|