From f5cd6ebb02831278341da37cd3f09154e4005022 Mon Sep 17 00:00:00 2001 From: alessio Date: Mon, 28 Aug 2017 10:49:38 +0200 Subject: [PATCH] bugs fixed --- realize.go | 4 +-- settings/settings_test.go | 2 +- watcher/exec.go | 8 ++--- watcher/main.go | 3 +- watcher/watcher.go | 73 ++++++++++++++++++--------------------- 5 files changed, 42 insertions(+), 48 deletions(-) diff --git a/realize.go b/realize.go index 7c39c38..6d3e0bb 100644 --- a/realize.go +++ b/realize.go @@ -677,7 +677,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, Startup: 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}) d.Reload() return nil }, @@ -719,7 +719,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, Startup: 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}) d.Reload() return nil }, diff --git a/settings/settings_test.go b/settings/settings_test.go index 17a2c77..c4d41a8 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -47,5 +47,5 @@ func TestSettings_Record(t *testing.T) { if err := s.Record(a); err != nil { t.Fatal(err) } - s.Remove(filepath.Join(Directory, s.Resources.Config)) + s.Remove(filepath.Join(directory, s.Resources.Config)) } diff --git a/watcher/exec.go b/watcher/exec.go index 692148c..35123fd 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -27,7 +27,7 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) if err != nil { msg := fmt.Sprintln(p.pname(p.Name, 3), ":", style.Blue.Regular(err.Error())) out := BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Run"} - p.print("error", out, msg, "") + p.stamp("error", out, msg, "") } else { isErrorText = func(t string) bool { return errRegexp.MatchString(t) @@ -63,7 +63,7 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } msg := fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Regular("Ended")) out := BufferOut{Time: time.Now(), Text: "Ended", Type: "Go Run"} - p.print("log", out, msg, "") + p.stamp("log", out, msg, "") wr.Done() }() @@ -87,10 +87,10 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) msg := fmt.Sprintln(p.pname(p.Name, 3), ":", style.Blue.Regular(text)) if isError && !isErrorText(text) { out := BufferOut{Time: time.Now(), Text: text, Type: "Go Run"} - p.print("error", out, msg, "") + p.stamp("error", out, msg, "") } else { out := BufferOut{Time: time.Now(), Text: text, Type: "Go Run"} - p.print("out", out, msg, "") + p.stamp("out", out, msg, "") } } close(stop) diff --git a/watcher/main.go b/watcher/main.go index fd57bd8..17540d8 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -90,8 +90,7 @@ type Command struct { Type string `yaml:"type" json:"type"` Command string `yaml:"command" json:"command"` Path string `yaml:"path,omitempty" json:"path,omitempty"` - Changed bool `yaml:"changed,omitempty" json:"changed,omitempty"` - Startup bool `yaml:"startup,omitempty" json:"startup,omitempty"` + Global bool `yaml:"global,omitempty" json:"changed,global"` } // Streams is a collection of names and values for the logs functionality diff --git a/watcher/watcher.go b/watcher/watcher.go index 56818a0..454da49 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -72,7 +72,7 @@ func (p *Project) watchByPolling() { // repeat the initial cycle msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file)) out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file} - p.print("log", out, msg, "") + p.stamp("log", out, msg, "") go p.routines(&wr, channel, watcher, file) } return nil @@ -84,16 +84,15 @@ func (p *Project) watchByPolling() { if err := filepath.Walk(base, walk); err != nil { msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Regular(err.Error())) out = BufferOut{Time: time.Now(), Text: err.Error()} - p.print("error", out, msg, "") + p.stamp("error", out, msg, "") } } else { msg = fmt.Sprintln(p.pname(p.Name, 2), ":", base, "path doesn't exist") out = BufferOut{Time: time.Now(), Text: base + " path doesn't exist"} - p.print("error", out, msg, "") + p.stamp("error", out, msg, "") } select { case <-exit: - p.cmd("after", false) return case <-time.After(p.parent.Legacy.Interval / time.Duration(len(p.Watcher.Paths))): } @@ -138,7 +137,7 @@ func (p *Project) watchByNotify() { // repeat the initial cycle msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file)) out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file} - p.print("log", out, msg, "") + p.stamp("log", out, msg, "") go p.routines(&wr, channel, watcher, file) p.LastChangedOn = time.Now().Truncate(time.Second) } @@ -147,9 +146,8 @@ func (p *Project) watchByNotify() { case err := <-watcher.Errors: msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Regular(err.Error())) out = BufferOut{Time: time.Now(), Text: err.Error()} - p.print("error", out, msg, "") + p.stamp("error", out, msg, "") case <-exit: - p.cmd("after", false) return } } @@ -201,7 +199,7 @@ func (p *Project) watch(watcher watcher) error { } msg = fmt.Sprintln(p.pname(p.Name, 1), ":", style.Blue.Bold("Watching"), style.Magenta.Bold(files), "file/s", style.Magenta.Bold(folders), "folder/s") out = BufferOut{Time: time.Now(), Text: "Watching " + strconv.FormatInt(files, 10) + " files/s " + strconv.FormatInt(folders, 10) + " folder/s"} - p.print("log", out, msg, "") + p.stamp("log", out, msg, "") return nil } @@ -214,11 +212,11 @@ func (p *Project) install() error { if err != nil { msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Install"), style.Red.Regular(err.Error())) out = BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Install", Stream: stream} - p.print("error", out, msg, stream) + p.stamp("error", out, msg, stream) } else { msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Installed")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) out = BufferOut{Time: time.Now(), Text: "Installed after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} - p.print("log", out, msg, stream) + p.stamp("log", out, msg, stream) } return err } @@ -237,7 +235,7 @@ func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { case <-runner: msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Started")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) out = BufferOut{Time: time.Now(), Text: "Started after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} - p.print("log", out, msg, "") + p.stamp("log", out, msg, "") return } } @@ -253,11 +251,11 @@ func (p *Project) build() error { if err != nil { msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Build"), style.Red.Regular(err.Error())) out = BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Build", Stream: stream} - p.print("error", out, msg, stream) + p.stamp("error", out, msg, stream) } else { msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Built")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) out = BufferOut{Time: time.Now(), Text: "Built after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} - p.print("log", out, msg, stream) + p.stamp("log", out, msg, stream) } return err } @@ -275,7 +273,7 @@ func (p *Project) tool(path string, tool tool) error { if stream, err := p.goTools(path, tool.cmd, tool.options...); err != nil { msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold(tool.name), style.Red.Regular("there are some errors in"), ":", style.Magenta.Bold(path)) out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: tool.name, Stream: stream} - p.print("error", out, msg, stream) + p.stamp("error", out, msg, stream) return err } } @@ -284,29 +282,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) { +func (p *Project) cmd(flag string, global bool) { for _, cmd := range p.Watcher.Scripts { 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) - } + err, 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.stamp("log", out, msg, "") + } + if err != "" { + p.stamp("error", out, msg, "") + } + if logs != "" { + msg = fmt.Sprintln(logs) + out = BufferOut{Time: time.Now(), Text: logs, Type: flag} + p.stamp("log", out, "", msg) + } + if err != "" { + msg = fmt.Sprintln(style.Red.Regular(err)) + out = BufferOut{Time: time.Now(), Text: err, Type: flag} + p.stamp("error", out, "", msg) } } } @@ -325,14 +321,13 @@ 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 { - p.cmd("before", true) + p.cmd("before", false) 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 { - p.cmd("before", false) p.Fatal(p.watch(watcher)) } install := p.install() @@ -343,7 +338,7 @@ func (p *Project) routines(wr *sync.WaitGroup, channel chan bool, watcher watche } wr.Wait() if len(file) > 0 { - p.cmd("after", true) + p.cmd("after", false) } } @@ -371,7 +366,7 @@ func (p *Project) pname(name string, color int) string { } // Print on files, cli, ws -func (p *Project) print(t string, o BufferOut, msg string, stream string) { +func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { switch t { case "out": p.Buffer.StdOut = append(p.Buffer.StdOut, o)