Merge pull request #25 from Unknwon/master

Fixed bug: repeat build in Mac OS, change to check file ModTime instead of event time
This commit is contained in:
astaxie 2013-08-15 06:00:58 -07:00
commit aa51639b25
4 changed files with 46 additions and 17 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.DS_Store
bee
*.exe
*.exe~

3
run.go
View File

@ -68,7 +68,8 @@ var conf struct {
func runApp(cmd *Command, args []string) { func runApp(cmd *Command, args []string) {
exit := make(chan bool) exit := make(chan bool)
if len(args) != 1 { if len(args) != 1 {
colorLog("[ERRO] Argument [appname] is missing\n") colorLog("[ERRO] Cannot start running[ %s ]\n",
"argument 'appname' is missing")
os.Exit(2) os.Exit(2)
} }
crupath, _ := os.Getwd() crupath, _ := os.Getwd()

14
util.go
View File

@ -62,12 +62,16 @@ func colorLog(format string, a ...interface{}) {
log = log[i+1:] log = log[i+1:]
// Error. // Error.
log = strings.Replace(log, "[ ", fmt.Sprintf("[ \033[%dm", Red), -1) log = strings.Replace(log, "[ ", fmt.Sprintf("[\033[%dm", Red), -1)
log = strings.Replace(log, " ]", EndColor+" ]", -1) log = strings.Replace(log, " ]", EndColor+"]", -1)
// Path. // Path.
log = strings.Replace(log, "(", fmt.Sprintf("(\033[%dm", Yellow), -1) log = strings.Replace(log, "( ", fmt.Sprintf("(\033[%dm", Yellow), -1)
log = strings.Replace(log, ")", EndColor+")", -1) log = strings.Replace(log, " )", EndColor+")", -1)
// Highlights.
log = strings.Replace(log, "# ", fmt.Sprintf("\033[%dm", Gray), -1)
log = strings.Replace(log, " #", EndColor, -1)
log = clog + log log = clog + log
} }
@ -79,6 +83,8 @@ func colorLog(format string, a ...interface{}) {
func getColorLevel(level string) string { func getColorLevel(level string) string {
level = strings.ToUpper(level) level = strings.ToUpper(level)
switch level { switch level {
case "TRAC":
return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level)
case "ERRO": case "ERRO":
return fmt.Sprintf("\033[%dm%s\033[0m", Red, level) return fmt.Sprintf("\033[%dm%s\033[0m", Red, level)
case "WARN": case "WARN":

View File

@ -14,13 +14,14 @@ import (
var ( var (
cmd *exec.Cmd cmd *exec.Cmd
state sync.Mutex state sync.Mutex
eventTime = make(map[string]time.Time) eventTime = make(map[string]int64)
) )
func NewWatcher(paths []string) { func NewWatcher(paths []string) {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
log.Fatal(err) colorLog("[ERRO] Fail to create new Watcher[ %s ]\n", err)
os.Exit(2)
} }
go func() { go func() {
@ -37,15 +38,13 @@ func NewWatcher(paths []string) {
continue continue
} }
if t, ok := eventTime[e.Name]; ok { mt := getFileModTime(e.Name)
// if 500ms change many times, then ignore it. if t := eventTime[e.Name]; mt == t {
// for liteide often gofmt code after save. colorLog("[SKIP] # %s #\n", e.String())
if t.Add(time.Millisecond * 500).After(time.Now()) { isbuild = false
colorLog("[SKIP] %s\n", e.String())
isbuild = false
}
} }
eventTime[e.Name] = time.Now()
eventTime[e.Name] = mt
if isbuild { if isbuild {
colorLog("[EVEN] %s\n", e) colorLog("[EVEN] %s\n", e)
@ -59,15 +58,34 @@ func NewWatcher(paths []string) {
colorLog("[INFO] Initializing watcher...\n") colorLog("[INFO] Initializing watcher...\n")
for _, path := range paths { for _, path := range paths {
fmt.Println(path) colorLog("[TRAC] Directory( %s )\n", path)
err = watcher.Watch(path) err = watcher.Watch(path)
if err != nil { if err != nil {
log.Fatal(err) colorLog("[ERRO] Fail to watch directory[ %s ]\n", err)
os.Exit(2)
} }
} }
} }
// getFileModTime retuens unix timestamp of `os.File.ModTime` by given path.
func getFileModTime(path string) int64 {
f, err := os.Open(path)
if err != nil {
colorLog("[ERRO] Fail to open file[ %s ]", err)
return time.Now().Unix()
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
colorLog("[ERRO] Fail to get file information[ %s ]", err)
return time.Now().Unix()
}
return fi.ModTime().Unix()
}
func Autobuild() { func Autobuild() {
state.Lock() state.Lock()
defer state.Unlock() defer state.Unlock()