Fixed bug: repeat build in Mac OS, change to check file ModTime insetad of event time

This commit is contained in:
Unknown 2013-08-15 12:51:26 +08:00
parent ea43654872
commit 772e5676f7
1 changed files with 34 additions and 9 deletions

View File

@ -14,7 +14,7 @@ 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) {
@ -37,15 +37,23 @@ 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()
// if t, ok := eventTime[e.Name]; 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()) {
// colorLog("[SKIP] %s\n", e.String())
// isbuild = false
// }
// }
eventTime[e.Name] = mt
fmt.Println(mt)
if isbuild { if isbuild {
colorLog("[EVEN] %s\n", e) colorLog("[EVEN] %s\n", e)
@ -68,6 +76,23 @@ func NewWatcher(paths []string) {
} }
// 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()
}
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()