From fb8c09c7989033293b115272b515753b7cce7b68 Mon Sep 17 00:00:00 2001 From: alessio Date: Mon, 19 Jun 2017 12:08:51 +0200 Subject: [PATCH] #56 fixed --- realize.go | 4 ++-- watcher/main.go | 5 +++-- watcher/watcher.go | 49 ++++++++++++++++++++++++---------------------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/realize.go b/realize.go index 85e5e9c..dfcd376 100644 --- a/realize.go +++ b/realize.go @@ -741,7 +741,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val, Changed: true}) + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val, Changed: true, Startup: true}) d.Reload() return nil }, @@ -783,7 +783,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val, Changed: true}) + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val, Changed: true, Startup: true}) d.Reload() return nil }, diff --git a/watcher/main.go b/watcher/main.go index 9b53cd0..07c42c7 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -87,8 +87,9 @@ type Watcher struct { type Command struct { Type string `yaml:"type" json:"type"` Command string `yaml:"command" json:"command"` - Path string `yaml:"path" json:"path"` - Changed bool `yaml:"refresh" json:"refresh"` + Path string `yaml:"path,omitempty" json:"path,omitempty"` + Changed bool `yaml:"changed,omitempty" json:"changed,omitempty"` + Startup bool `yaml:"startup,omitempty" json:"startup,omitempty"` } // Streams is a collection of names and values for the logs functionality diff --git a/watcher/watcher.go b/watcher/watcher.go index 9e92f54..6954586 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -93,6 +93,7 @@ func (p *Project) watchByPolling() { } select { case <-exit: + p.cmd("after", false) return case <-time.After(p.parent.Legacy.Interval / time.Duration(len(p.Watcher.Paths))): } @@ -148,6 +149,7 @@ func (p *Project) watchByNotify() { out = BufferOut{Time: time.Now(), Text: err.Error()} p.print("error", out, msg, "") case <-exit: + p.cmd("after", false) return } } @@ -284,25 +286,27 @@ func (p *Project) tool(path string, tool tool) error { // Cmd calls an wrapper for execute the commands after/before func (p *Project) cmd(flag string, changed bool) { for _, cmd := range p.Watcher.Scripts { - if strings.ToLower(cmd.Type) == flag && changed == cmd.Changed { - errors, logs := p.command(cmd) - msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Bold("Command"), style.Green.Bold("\"")+cmd.Command+style.Green.Bold("\"")) - out = BufferOut{Time: time.Now(), Text: cmd.Command, Type: flag} - if logs != "" { - p.print("log", out, msg, "") - } - if errors != "" { - p.print("error", out, msg, "") - } - if logs != "" { - msg = fmt.Sprintln(logs) - out = BufferOut{Time: time.Now(), Text: logs, Type: flag} - p.print("log", out, "", msg) - } - if errors != "" { - msg = fmt.Sprintln(style.Red.Regular(errors)) - out = BufferOut{Time: time.Now(), Text: errors, Type: flag} - p.print("error", out, "", msg) + if strings.ToLower(cmd.Type) == flag{ + if changed && cmd.Changed || !changed && cmd.Startup { + errors, logs := p.command(cmd) + msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Bold("Command"), style.Green.Bold("\"") + cmd.Command + style.Green.Bold("\"")) + out = BufferOut{Time: time.Now(), Text: cmd.Command, Type: flag} + if logs != "" { + p.print("log", out, msg, "") + } + if errors != "" { + p.print("error", out, msg, "") + } + if logs != "" { + msg = fmt.Sprintln(logs) + out = BufferOut{Time: time.Now(), Text: logs, Type: flag} + p.print("log", out, "", msg) + } + if errors != "" { + msg = fmt.Sprintln(style.Red.Regular(errors)) + out = BufferOut{Time: time.Now(), Text: errors, Type: flag} + p.print("error", out, "", msg) + } } } } @@ -320,14 +324,14 @@ func (p *Project) ignore(str string) bool { // Routines launches the toolchain run, build, install func (p *Project) routines(wr *sync.WaitGroup,channel chan bool, watcher watcher, file string) { - if len(file) > 0{ + if len(file) > 0 { p.cmd("before", true) path := filepath.Dir(file) p.tool(file, p.tools.Fmt) p.tool(path, p.tools.Vet) p.tool(path, p.tools.Test) p.tool(path, p.tools.Generate) - }else{ + } else { p.cmd("before", false) p.Fatal(p.watch(watcher)) } @@ -340,9 +344,8 @@ func (p *Project) routines(wr *sync.WaitGroup,channel chan bool, watcher watcher wr.Wait() if len(file) > 0 { p.cmd("after", true) - }else{ - p.cmd("after", false) } + } // Defines the colors scheme for the project name