decreased routines complexity

This commit is contained in:
asoseil 2017-11-12 15:25:57 +01:00
parent 4664efe735
commit 989c898dbf
1 changed files with 34 additions and 36 deletions

View File

@ -14,6 +14,7 @@ import (
"time" "time"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"reflect"
) )
var ( var (
@ -465,7 +466,6 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
// Routines launches the toolchain run, build, install // Routines launches the toolchain run, build, install
func (p *Project) routines(stop <-chan bool, watcher FileWatcher, path string) { func (p *Project) routines(stop <-chan bool, watcher FileWatcher, path string) {
var done bool var done bool
var install, build error
go func() { go func() {
for { for {
select { select {
@ -475,45 +475,43 @@ func (p *Project) routines(stop <-chan bool, watcher FileWatcher, path string) {
} }
} }
}() }()
if !done { invoke(done,p.cmd,stop,"before",false)
// before command invoke(done,p.tool,stop,path)
p.cmd(stop, "before", false) // prevent init error on walk
} p.init = true
if !done {
// Go supported tools
p.tool(stop, path)
// Prevent fake events on polling startup
p.init = true
}
// prevent errors using realize without config with only run flag // prevent errors using realize without config with only run flag
if p.Cmds.Run && !p.Cmds.Install.Status && !p.Cmds.Build.Status { if p.Cmds.Run && !p.Cmds.Install.Status && !p.Cmds.Build.Status {
p.Cmds.Install.Status = true p.Cmds.Install.Status = true
} }
if !done { invoke(done,p.compile,stop,p.Cmds.Install)
install = p.compile(stop, p.Cmds.Install) invoke(done,p.compile,stop,p.Cmds.Build)
} if !done && p.Cmds.Run {
if !done { start := time.Now()
build = p.compile(stop, p.Cmds.Build) runner := make(chan bool, 1)
} go func() {
if !done && (install == nil && build == nil) { log.Println(p.pname(p.Name, 1), ":", "Running..")
if p.Cmds.Run { p.goRun(stop, runner)
start := time.Now() }()
runner := make(chan bool, 1) select {
go func() { case <-runner:
log.Println(p.pname(p.Name, 1), ":", "Running..") msg = fmt.Sprintln(p.pname(p.Name, 5), ":", green.regular("Started"), "in", magenta.regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
p.goRun(stop, runner) out = BufferOut{Time: time.Now(), Text: "Started in " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"}
}() p.stamp("log", out, msg, "")
select { case <-stop:
case <-runner: return
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", green.regular("Started"), "in", magenta.regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
out = BufferOut{Time: time.Now(), Text: "Started in " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"}
p.stamp("log", out, msg, "")
case <-stop:
return
}
} }
} }
invoke(done,p.cmd,stop,"after",false)
}
// Invoke is used to exec func from routines and check done
func invoke(done bool, fn interface{}, args ...interface{}) {
if !done { if !done {
p.cmd(stop, "after", false) v := reflect.ValueOf(fn)
rargs := make([]reflect.Value, len(args))
for i, a := range args {
rargs[i] = reflect.ValueOf(a)
}
v.Call(rargs)
} }
} }