From 5981ff06f276d8c42f9b28e2ff924ad3bba211da Mon Sep 17 00:00:00 2001 From: alessio Date: Tue, 16 Aug 2016 12:20:55 +0200 Subject: [PATCH] better routines workflow --- realize/app.go | 4 ++++ realize/project.go | 9 +++++++-- realize/watcher.go | 20 +++++++++++++------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/realize/app.go b/realize/app.go index 5a962b5..7a77af1 100644 --- a/realize/app.go +++ b/realize/app.go @@ -53,6 +53,10 @@ func LogSuccess(msg string) { log.Println(green(msg)) } +func LogFail(msg string) { + log.Println(red(msg)) +} + func (app *App) Information() { fmt.Println(blue(app.Name) + " - " + blue(app.Version)) fmt.Println(bluel(app.Description) + "\n") diff --git a/realize/project.go b/realize/project.go index 4730064..274ce91 100644 --- a/realize/project.go +++ b/realize/project.go @@ -7,6 +7,7 @@ import ( "bytes" "bufio" "log" + "sync" ) type Project struct { @@ -20,11 +21,15 @@ type Project struct { Watcher Watcher `yaml:"app_watcher,omitempty"` } -func (p *Project) GoRun(channel chan bool) error { +func (p *Project) GoRun(channel chan bool, wr *sync.WaitGroup) error { base, _ := os.Getwd() build := exec.Command("go", "run", p.Main) path := base + p.Path build.Dir = path + defer func() { + LogFail(p.Name + ": Stopped") + wr.Done() + }() stdout, err := build.StdoutPipe() if err != nil { @@ -39,7 +44,7 @@ func (p *Project) GoRun(channel chan bool) error { select { default: log.Println(p.Name + ":", in.Text()) - case <-channel: + case <- channel: return nil } } diff --git a/realize/watcher.go b/realize/watcher.go index b55c1db..f33e854 100644 --- a/realize/watcher.go +++ b/realize/watcher.go @@ -8,6 +8,7 @@ import ( "log" "strings" "time" + "sync" ) type Watcher struct { @@ -37,8 +38,9 @@ func (h *Config) Watch() error { func (p *Project) Watching() { + channel := make(chan bool,1) + var wr sync.WaitGroup var watcher *fsnotify.Watcher - channel := make(chan bool) watcher, _ = fsnotify.NewWatcher() defer func() { watcher.Close() @@ -58,6 +60,11 @@ func (p *Project) Watching() { } return nil } + routines := func(){ + channel = make(chan bool) + wr.Add(1) + go p.build(); p.install(); p.run(channel, &wr); + } for _, dir := range p.Watcher.Paths { base, _ := os.Getwd() @@ -83,8 +90,7 @@ func (p *Project) Watching() { } } - // go build, install, run - go p.build(); p.install(); p.run(channel); + routines() fmt.Println(red("\n Watching: '" + p.Name + "'\n")) @@ -103,8 +109,8 @@ func (p *Project) Watching() { // stop and run again close(channel) - channel = make(chan bool) - go p.build(); p.install(); p.run(channel); + wr.Wait() + routines() p.reload = time.Now().Truncate(time.Second) } @@ -143,10 +149,10 @@ func (p *Project) build() { return } -func (p *Project) run(channel chan bool) { +func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { if p.Run { LogSuccess(p.Name + ": Running..") - go p.GoRun(channel) + go p.GoRun(channel, wr) LogSuccess(p.Name + ": Runned") } return