better routines workflow

This commit is contained in:
alessio 2016-08-16 12:20:55 +02:00
parent 21045786c5
commit 5981ff06f2
3 changed files with 24 additions and 9 deletions

View File

@ -53,6 +53,10 @@ func LogSuccess(msg string) {
log.Println(green(msg)) log.Println(green(msg))
} }
func LogFail(msg string) {
log.Println(red(msg))
}
func (app *App) Information() { func (app *App) Information() {
fmt.Println(blue(app.Name) + " - " + blue(app.Version)) fmt.Println(blue(app.Name) + " - " + blue(app.Version))
fmt.Println(bluel(app.Description) + "\n") fmt.Println(bluel(app.Description) + "\n")

View File

@ -7,6 +7,7 @@ import (
"bytes" "bytes"
"bufio" "bufio"
"log" "log"
"sync"
) )
type Project struct { type Project struct {
@ -20,11 +21,15 @@ type Project struct {
Watcher Watcher `yaml:"app_watcher,omitempty"` 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() base, _ := os.Getwd()
build := exec.Command("go", "run", p.Main) build := exec.Command("go", "run", p.Main)
path := base + p.Path path := base + p.Path
build.Dir = path build.Dir = path
defer func() {
LogFail(p.Name + ": Stopped")
wr.Done()
}()
stdout, err := build.StdoutPipe() stdout, err := build.StdoutPipe()
if err != nil { if err != nil {

View File

@ -8,6 +8,7 @@ import (
"log" "log"
"strings" "strings"
"time" "time"
"sync"
) )
type Watcher struct { type Watcher struct {
@ -37,8 +38,9 @@ func (h *Config) Watch() error {
func (p *Project) Watching() { func (p *Project) Watching() {
channel := make(chan bool,1)
var wr sync.WaitGroup
var watcher *fsnotify.Watcher var watcher *fsnotify.Watcher
channel := make(chan bool)
watcher, _ = fsnotify.NewWatcher() watcher, _ = fsnotify.NewWatcher()
defer func() { defer func() {
watcher.Close() watcher.Close()
@ -58,6 +60,11 @@ func (p *Project) Watching() {
} }
return nil 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 { for _, dir := range p.Watcher.Paths {
base, _ := os.Getwd() base, _ := os.Getwd()
@ -83,8 +90,7 @@ func (p *Project) Watching() {
} }
} }
// go build, install, run routines()
go p.build(); p.install(); p.run(channel);
fmt.Println(red("\n Watching: '" + p.Name + "'\n")) fmt.Println(red("\n Watching: '" + p.Name + "'\n"))
@ -103,8 +109,8 @@ func (p *Project) Watching() {
// stop and run again // stop and run again
close(channel) close(channel)
channel = make(chan bool) wr.Wait()
go p.build(); p.install(); p.run(channel); routines()
p.reload = time.Now().Truncate(time.Second) p.reload = time.Now().Truncate(time.Second)
} }
@ -143,10 +149,10 @@ func (p *Project) build() {
return return
} }
func (p *Project) run(channel chan bool) { func (p *Project) run(channel chan bool, wr *sync.WaitGroup) {
if p.Run { if p.Run {
LogSuccess(p.Name + ": Running..") LogSuccess(p.Name + ": Running..")
go p.GoRun(channel) go p.GoRun(channel, wr)
LogSuccess(p.Name + ": Runned") LogSuccess(p.Name + ": Runned")
} }
return return