fix watcher, close and re open channel on reload

This commit is contained in:
alessio 2016-08-04 21:56:42 +02:00
parent 446d7c8071
commit 4ece79c27b
1 changed files with 76 additions and 62 deletions

View File

@ -20,34 +20,28 @@ type Watcher struct{
Preview bool `yaml:"preview,omitempty"` Preview bool `yaml:"preview,omitempty"`
} }
func (p *Project) install(){ func (h *Config) Watch() error{
if p.Bin { if err := h.Read(); err == nil {
if err := p.GoInstall(); err != nil{ // loop projects
Fail(err.Error()) wg.Add(len(h.Projects))
for k := range h.Projects {
if string(h.Projects[k].Path[0]) != "/" {
h.Projects[k].Path = "/"+h.Projects[k].Path
}
go h.Projects[k].Watching()
}
wg.Wait()
return nil
}else{ }else{
Success(p.Name + ": Installed") return err
}
}
}
func (p *Project) build(){
if p.Build {
if err := p.GoBuild(); err != nil{
Fail(err.Error())
}else{
Success(p.Name + ": Builded")
}
} }
} }
func (p *Project) Watching(){ func (p *Project) Watching(){
var watcher *fsnotify.Watcher var watcher *fsnotify.Watcher
channel := make(chan bool)
watcher, _ = fsnotify.NewWatcher() watcher, _ = fsnotify.NewWatcher()
defer func(){
watcher.Close()
wg.Done()
}()
walk := func(path string, info os.FileInfo, err error) error{ walk := func(path string, info os.FileInfo, err error) error{
if !Ignore(path,p.Watcher.Ignore) { if !Ignore(path,p.Watcher.Ignore) {
@ -63,29 +57,24 @@ func (p *Project) Watching(){
return nil return nil
} }
// run go build
go p.build()
// run go install
p.install()
p.reload = time.Now().Truncate(time.Second)
for _, dir := range p.Watcher.Paths { for _, dir := range p.Watcher.Paths {
base, _ := os.Getwd() base, _ := os.Getwd()
// check path existence // check path existence
if _, err := os.Stat(base + p.Path + dir); err == nil { if _, err := os.Stat(base + p.Path + dir); err == nil {
if err := filepath.Walk(base + p.Path + dir, walk); err != nil { if err := filepath.Walk(base + p.Path + dir, walk); err != nil {
fmt.Println(err) Fail(err.Error())
} }
}else{ }else{
fmt.Println(red(p.Name + ": \t"+base + p.Path + dir +" path doesn't exist")) Fail(p.Name + ": \t"+base + p.Path + dir +" path doesn't exist")
} }
} }
fmt.Println(red("Watching: '"+ p.Name +"'\n")) // go build, install, run
go p.build(); p.install(); p.run(channel);
fmt.Println(red("\n Watching: '"+ p.Name +"'\n"))
p.reload = time.Now().Truncate(time.Second)
for { for {
select { select {
@ -98,34 +87,59 @@ func (p *Project) Watching(){
i := strings.Index(event.Name, filepath.Ext(event.Name)) i := strings.Index(event.Name, filepath.Ext(event.Name))
log.Println(green(p.Name+":")+"\t", event.Name[:i]) log.Println(green(p.Name+":")+"\t", event.Name[:i])
// run go build // stop and run again
go p.build() close(channel)
channel = make(chan bool)
// run go install go p.build(); p.install(); p.run(channel);
p.install()
p.reload = time.Now().Truncate(time.Second) p.reload = time.Now().Truncate(time.Second)
} }
} }
case err := <-watcher.Errors: case err := <-watcher.Errors:
log.Println("error:", err) Fail(err.Error())
}
} }
} }
func (h *Config) Watch() error{ watcher.Close()
if err := h.Read(); err == nil { wg.Done()
// loop projects
wg.Add(len(h.Projects))
for k := range h.Projects {
go h.Projects[k].Watching()
} }
wg.Wait()
return nil func (p *Project) install(){
if p.Bin {
LogSuccess(p.Name + ": Installing..")
if err := p.GoInstall(); err != nil{
Fail(err.Error())
return
}else{ }else{
return err LogSuccess(p.Name + ": Installed")
return
} }
} }
return
}
func (p *Project) build(){
if p.Build {
LogSuccess(p.Name + ": Building..")
if err := p.GoBuild(); err != nil{
Fail(err.Error())
return
}else{
LogSuccess(p.Name + ": Builded")
return
}
}
return
}
func (p *Project) run(channel chan bool){
if p.Run {
LogSuccess(p.Name + ": Running..")
go p.GoRun(channel)
LogSuccess(p.Name + ": Runned")
}
return
}
func InArray(str string, list []string) bool{ func InArray(str string, list []string) bool{
for _, v := range list { for _, v := range list {