build/install performance improved

This commit is contained in:
alessio 2016-08-20 12:47:32 +02:00
parent 2cf9aab133
commit 6f6175e7dd
2 changed files with 26 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import (
"strings"
"sync"
"time"
"errors"
)
// The Project struct defines the informations about a project
@ -36,7 +37,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
}else {
run = name[len(name)-1]
}
build := exec.Command(os.Getenv("GOPATH") + slash("bin") + slash(run))
build := exec.Command(os.Getenv("GOBIN") + slash(run))
build.Dir = p.base
defer func() {
if err := build.Process.Kill(); err != nil {
@ -49,9 +50,11 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
stdout, err := build.StdoutPipe()
if err != nil {
Fail(err.Error())
return err
}
if err := build.Start(); err != nil {
Fail(err.Error())
return err
}
close(runner)
@ -86,8 +89,8 @@ func (p *Project) GoBuild() error {
return err
}
}
build := exec.Command("go", "build", p.base+p.Main)
build.Dir = p.base + "/bin"
build := exec.Command("go", "build")
build.Dir = p.base
build.Stdout = &out
if err := build.Run(); err != nil {
return err
@ -101,6 +104,16 @@ func (p *Project) GoInstall() error {
base, _ := os.Getwd()
path := base + p.Path
// check gopath and set gobin
gopath := os.Getenv("GOPATH")
if gopath == ""{
return errors.New("$GOPATH not set")
}
err := os.Setenv("GOBIN",os.Getenv("GOPATH") + "bin")
if err != nil {
return err
}
build := exec.Command("go", "install")
build.Dir = path
build.Stdout = &out

View File

@ -110,14 +110,16 @@ func (p *Project) Watching() {
}
if _, err := os.Stat(event.Name); err == nil {
i := strings.Index(event.Name, filepath.Ext(event.Name))
log.Println(bluel(p.Name+": File changed ->"), bluel(event.Name[:i]))
if event.Name[:i] != "" {
log.Println(bluel(p.Name + ": File changed ->"), bluel(event.Name[:i]))
// stop and run again
close(channel)
wr.Wait()
routines()
// stop and run again
close(channel)
wr.Wait()
routines()
p.reload = time.Now().Truncate(time.Second)
p.reload = time.Now().Truncate(time.Second)
}
}
}
case err := <-watcher.Errors:
@ -131,7 +133,7 @@ func (p *Project) install(channel chan bool,wr *sync.WaitGroup) {
if p.Bin {
LogSuccess(p.Name + ": Installing..")
if err := p.GoInstall(); err != nil {
Fail(err.Error())
Fail(p.Name + ": "+err.Error())
wr.Done()
} else {
LogSuccess(p.Name + ": Installed")
@ -157,7 +159,7 @@ func (p *Project) build() {
if p.Build {
LogSuccess(p.Name + ": Building..")
if err := p.GoBuild(); err != nil {
Fail(err.Error())
Fail(p.Name + ": "+err.Error())
} else {
LogSuccess(p.Name + ": Builded")
}