diff --git a/watcher/exec.go b/watcher/exec.go index bf489fa..283b276 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -16,21 +16,34 @@ import ( func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error { var build *exec.Cmd - if len(p.Params) != 0 { - var params []string - for _, param := range p.Params { - arr := strings.Fields(param) - params = append(params, arr...) - } - build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)), params...) - } else { - build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path))) + var params []string + var path = "" + + for _, param := range p.Params { + arr := strings.Fields(param) + params = append(params, arr...) + } + if _, err := os.Stat(filepath.Join(p.base, p.path)); err == nil { + path = filepath.Join(p.base, p.path) + } + if _, err := os.Stat(filepath.Join(p.base, p.path+".exe")); err == nil { + path = filepath.Join(p.base, p.path+".exe") + } + + if path != "" { + build = exec.Command(path, params...) + } else { + if _, err := os.Stat(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path))); err == nil { + build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)), params...) + } else { + p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Can't run a not compiled project"}) + p.Fatal(err, "Can't run a not compiled project", ":") + } } - build.Dir = p.base defer func() { if err := build.Process.Kill(); err != nil { p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Failed to stop: " + err.Error()}) - p.Fatal("Failed to stop:", err) + p.Fatal(err, "Failed to stop", ":") } p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Ended"}) log.Println(p.pname(p.Name, 2), ":", p.Red.Regular("Ended")) @@ -72,7 +85,7 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) f := p.Create(path) t := time.Now() if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + output.Text() + "\r\n"); err != nil { - p.Fatal("", err) + p.Fatal(err, "") } } } diff --git a/watcher/watcher.go b/watcher/watcher.go index 468f303..774fea4 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -84,51 +84,47 @@ func (p *Project) watching() { } // Install calls an implementation of the "go install" -func (p *Project) install(channel chan bool, wr *sync.WaitGroup) { - defer func() { - p.sync() - }() +func (p *Project) install() { if p.Bin { - log.Println(p.pname(p.Name, 1), ":", "Installing..") start := time.Now() + log.Println(p.pname(p.Name, 1), ":", "Installing..") if stream, err := p.goInstall(); err != nil { msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Install"), p.Red.Regular(err.Error())) out := BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Install", Stream: stream} p.print("error", out, msg, stream) - wr.Done() } else { msg := fmt.Sprintln(p.pname(p.Name, 5), ":", p.Green.Regular("Installed")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) out := BufferOut{Time: time.Now(), Text: "Installed"} p.print("log", out, msg, stream) - - if p.Run { - runner := make(chan bool, 1) - log.Println(p.pname(p.Name, 1), ":", "Running..") - start = time.Now() - go p.goRun(channel, runner, wr) - for { - select { - case <-runner: - msg := fmt.Sprintln(p.pname(p.Name, 5), ":", p.Green.Regular("Has been run")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) - out := BufferOut{Time: time.Now(), Text: "Has been run"} - p.print("log", out, msg, stream) - return - } - } - } } + p.sync() } return } +func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { + if p.Run { + start := time.Now() + runner := make(chan bool, 1) + log.Println(p.pname(p.Name, 1), ":", "Running..") + go p.goRun(channel, runner, wr) + for { + select { + case <-runner: + msg := fmt.Sprintln(p.pname(p.Name, 5), ":", p.Green.Regular("Has been run")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) + out := BufferOut{Time: time.Now(), Text: "Has been run"} + p.print("log", out, msg, "") + return + } + } + } +} + // Build calls an implementation of the "go build" func (p *Project) build() { - defer func() { - p.sync() - }() if p.Build { - log.Println(p.pname(p.Name, 1), ":", "Building..") start := time.Now() + log.Println(p.pname(p.Name, 1), ":", "Building..") if stream, err := p.goBuild(); err != nil { msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Build"), p.Red.Regular(err.Error())) out := BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Build", Stream: stream} @@ -138,6 +134,7 @@ func (p *Project) build() { out := BufferOut{Time: time.Now(), Text: "Builded"} p.print("log", out, msg, stream) } + p.sync() } return } @@ -281,9 +278,10 @@ func (p *Project) ignore(str string) bool { // Routines launches the following methods: run, build, install func (p *Project) routines(channel chan bool, wr *sync.WaitGroup) { + p.install() + p.build() wr.Add(1) - go p.build() - go p.install(channel, wr) + go p.run(channel, wr) wr.Wait() } @@ -318,7 +316,7 @@ func (p *Project) print(t string, o BufferOut, msg string, stream string) { f := p.Create(path) t := time.Now() if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + o.Text + "\r\n"); err != nil { - p.Fatal("", err) + p.Fatal(err, "") } } case "log": @@ -328,7 +326,7 @@ func (p *Project) print(t string, o BufferOut, msg string, stream string) { f := p.Create(path) t := time.Now() if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + o.Text + "\r\n"); err != nil { - p.Fatal("", err) + p.Fatal(err, "") } } case "error": @@ -338,7 +336,7 @@ func (p *Project) print(t string, o BufferOut, msg string, stream string) { f := p.Create(path) t := time.Now() if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + o.Text + "\r\n"); err != nil { - p.Fatal("", err) + p.Fatal(err, "") } }