mirror of
https://github.com/beego/bee.git
synced 2024-11-25 20:10:55 +00:00
Merge pull request #84 from hilyjiang/master
Wait 1s before autobuild util there is no file change.
This commit is contained in:
commit
9530a63356
34
watch.go
34
watch.go
@ -28,10 +28,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
state sync.Mutex
|
state sync.Mutex
|
||||||
eventTime = make(map[string]int64)
|
eventTime = make(map[string]int64)
|
||||||
buildPeriod time.Time
|
scheduleTime time.Time
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewWatcher(paths []string, files []string, isgenerate bool) {
|
func NewWatcher(paths []string, files []string, isgenerate bool) {
|
||||||
@ -51,16 +51,10 @@ func NewWatcher(paths []string, files []string, isgenerate bool) {
|
|||||||
if checkTMPFile(e.Name) {
|
if checkTMPFile(e.Name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !chekcIfWatchExt(e.Name) {
|
if !checkIfWatchExt(e.Name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent duplicated builds.
|
|
||||||
if buildPeriod.Add(1 * time.Second).After(time.Now()) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
buildPeriod = time.Now()
|
|
||||||
|
|
||||||
mt := getFileModTime(e.Name)
|
mt := getFileModTime(e.Name)
|
||||||
if t := eventTime[e.Name]; mt == t {
|
if t := eventTime[e.Name]; mt == t {
|
||||||
ColorLog("[SKIP] # %s #\n", e.String())
|
ColorLog("[SKIP] # %s #\n", e.String())
|
||||||
@ -71,7 +65,19 @@ func NewWatcher(paths []string, files []string, isgenerate bool) {
|
|||||||
|
|
||||||
if isbuild {
|
if isbuild {
|
||||||
ColorLog("[EVEN] %s\n", e)
|
ColorLog("[EVEN] %s\n", e)
|
||||||
go Autobuild(files, isgenerate)
|
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)
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
case err := <-watcher.Error:
|
case err := <-watcher.Error:
|
||||||
ColorLog("[WARN] %s\n", err.Error()) // No need to exit here
|
ColorLog("[WARN] %s\n", err.Error()) // No need to exit here
|
||||||
@ -227,8 +233,8 @@ func checkTMPFile(name string) bool {
|
|||||||
|
|
||||||
var watchExts = []string{".go"}
|
var watchExts = []string{".go"}
|
||||||
|
|
||||||
// chekcIfWatchExt returns true if the name HasSuffix <watch_ext>.
|
// checkIfWatchExt returns true if the name HasSuffix <watch_ext>.
|
||||||
func chekcIfWatchExt(name string) bool {
|
func checkIfWatchExt(name string) bool {
|
||||||
for _, s := range watchExts {
|
for _, s := range watchExts {
|
||||||
if strings.HasSuffix(name, s) {
|
if strings.HasSuffix(name, s) {
|
||||||
return true
|
return true
|
||||||
|
Loading…
Reference in New Issue
Block a user