realize/watcher/exec.go

190 lines
5.2 KiB
Go
Raw Normal View History

package watcher
2016-08-03 08:28:58 +00:00
import (
2016-08-04 19:57:19 +00:00
"bufio"
2016-08-17 23:35:37 +00:00
"bytes"
2016-12-18 23:29:50 +00:00
"fmt"
2017-06-16 09:15:56 +00:00
"github.com/tockins/realize/style"
2016-08-04 19:57:19 +00:00
"log"
2016-08-17 23:35:37 +00:00
"os"
"os/exec"
2016-08-23 00:07:07 +00:00
"path/filepath"
"regexp"
2016-09-05 15:53:37 +00:00
"strings"
2016-08-17 23:35:37 +00:00
"sync"
"time"
2016-08-03 08:28:58 +00:00
)
2017-09-17 21:36:56 +00:00
// GoBuild is an implementation of the "go build"
func (p *Project) goBuild() (string, error) {
var out bytes.Buffer
var stderr bytes.Buffer
args := []string{"build"}
args = arguments(args, p.Cmds.Build.Args)
build := exec.Command("go", args...)
build.Dir = p.base
build.Stdout = &out
build.Stderr = &stderr
if err := build.Run(); err != nil {
return stderr.String(), err
}
return "", nil
}
// GoInstall is an implementation of the "go install"
func (p *Project) goInstall() (string, error) {
var out bytes.Buffer
var stderr bytes.Buffer
err := os.Setenv("GOBIN", filepath.Join(getEnvPath("GOPATH"), "bin"))
if err != nil {
return "", err
}
args := []string{"install"}
for _, arg := range p.Cmds.Install.Args {
arr := strings.Fields(arg)
args = append(args, arr...)
}
build := exec.Command("go", args...)
build.Dir = p.base
build.Stdout = &out
build.Stderr = &stderr
if err := build.Run(); err != nil {
return stderr.String(), err
}
return "", nil
}
// Exec an additional command from a defined path if specified
func (p *Project) command(cmd Command) (errors string, logs string) {
var stdout bytes.Buffer
var stderr bytes.Buffer
command := strings.Replace(strings.Replace(cmd.Command, "'", "", -1), "\"", "", -1)
c := strings.Split(command, " ")
build := exec.Command(c[0], c[1:]...)
build.Dir = p.base
if cmd.Path != "" {
if strings.Contains(cmd.Path, p.base) {
build.Dir = cmd.Path
} else {
build.Dir = filepath.Join(p.base, cmd.Path)
}
}
build.Stdout = &stdout
build.Stderr = &stderr
err := build.Run()
// check if log
logs = stdout.String()
if err != nil {
errors = stderr.String()
return errors, logs
}
return "", logs
}
// GoTool is used for run go methods such as fmt, test, generate...
func (p *Project) goTool(dir string, name string, cmd ...string) (string, error) {
if s := filepath.Ext(dir); s != "" && s != ".go" {
return "", nil
}
var out, stderr bytes.Buffer
build := exec.Command(name, cmd...)
build.Dir = dir
build.Stdout = &out
build.Stderr = &stderr
if err := build.Run(); err != nil {
return stderr.String() + out.String(), err
}
return "", nil
}
2016-08-18 07:29:36 +00:00
// GoRun is an implementation of the bin execution
2016-11-14 07:59:47 +00:00
func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error {
2016-11-01 09:56:12 +00:00
var build *exec.Cmd
var args []string
isErrorText := func(string) bool {
return false
}
errRegexp, err := regexp.Compile(p.ErrorOutputPattern)
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"}
2017-08-28 08:49:38 +00:00
p.stamp("error", out, msg, "")
} else {
isErrorText = func(t string) bool {
return errRegexp.MatchString(t)
}
}
for _, arg := range p.Args {
arr := strings.Fields(arg)
args = append(args, arr...)
2016-12-16 22:54:07 +00:00
}
2017-09-05 14:58:33 +00:00
2016-12-16 22:54:07 +00:00
if _, err := os.Stat(filepath.Join(p.base, p.path)); err == nil {
2017-09-05 14:58:33 +00:00
p.path = filepath.Join(p.base, p.path)
2016-12-16 22:54:07 +00:00
}
if _, err := os.Stat(filepath.Join(p.base, p.path+".exe")); err == nil {
2017-09-05 14:58:33 +00:00
p.path = filepath.Join(p.base, p.path+".exe")
2016-12-16 22:54:07 +00:00
}
2017-09-05 14:58:33 +00:00
if _, err := os.Stat(filepath.Join(getEnvPath("GOBIN"), filepath.Base(p.path))); err == nil {
build = exec.Command(filepath.Join(getEnvPath("GOBIN"), filepath.Base(p.path)), args...)
} else if _, err := os.Stat(filepath.Join(getEnvPath("GOBIN"), filepath.Base(p.path)) + ".exe"); err == nil {
build = exec.Command(filepath.Join(getEnvPath("GOBIN"), filepath.Base(p.path))+".exe", args...)
2016-08-27 13:07:00 +00:00
} else {
2017-09-05 14:58:33 +00:00
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", ":")
2016-08-26 12:52:27 +00:00
}
2017-09-05 14:58:33 +00:00
2016-08-16 10:20:55 +00:00
defer func() {
2016-08-16 16:36:54 +00:00
if err := build.Process.Kill(); err != nil {
2016-10-14 08:47:43 +00:00
p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Failed to stop: " + err.Error()})
2016-12-16 22:54:07 +00:00
p.Fatal(err, "Failed to stop", ":")
2016-08-16 16:36:54 +00:00
}
msg := fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Regular("Ended"))
2016-12-18 23:29:50 +00:00
out := BufferOut{Time: time.Now(), Text: "Ended", Type: "Go Run"}
2017-08-28 08:49:38 +00:00
p.stamp("log", out, msg, "")
2016-08-16 10:20:55 +00:00
wr.Done()
}()
2016-08-04 19:57:19 +00:00
stdout, err := build.StdoutPipe()
2016-08-23 12:42:01 +00:00
stderr, err := build.StderrPipe()
2016-08-04 19:57:19 +00:00
if err != nil {
log.Println(style.Red.Bold(err.Error()))
2016-08-20 10:47:32 +00:00
return err
2016-08-04 19:57:19 +00:00
}
if err := build.Start(); err != nil {
log.Println(style.Red.Bold(err.Error()))
2016-08-20 10:47:32 +00:00
return err
2016-08-04 19:57:19 +00:00
}
2016-08-17 22:24:06 +00:00
close(runner)
2016-08-04 19:57:19 +00:00
2016-09-17 19:07:22 +00:00
execOutput, execError := bufio.NewScanner(stdout), bufio.NewScanner(stderr)
stopOutput, stopError := make(chan bool, 1), make(chan bool, 1)
scanner := func(stop chan bool, output *bufio.Scanner, isError bool) {
for output.Scan() {
text := output.Text()
msg := fmt.Sprintln(p.pname(p.Name, 3), ":", style.Blue.Regular(text))
2017-08-08 21:56:19 +00:00
if isError && !isErrorText(text) {
out := BufferOut{Time: time.Now(), Text: text, Type: "Go Run"}
2017-08-28 08:49:38 +00:00
p.stamp("error", out, msg, "")
} else {
2017-08-07 23:33:31 +00:00
out := BufferOut{Time: time.Now(), Text: text, Type: "Go Run"}
2017-08-28 08:49:38 +00:00
p.stamp("out", out, msg, "")
2016-08-17 14:56:06 +00:00
}
}
2016-08-17 19:34:06 +00:00
close(stop)
}
go scanner(stopOutput, execOutput, false)
go scanner(stopError, execError, true)
2016-08-17 23:35:37 +00:00
for {
2016-08-04 19:57:19 +00:00
select {
2016-08-17 23:35:37 +00:00
case <-channel:
2016-08-17 19:34:06 +00:00
return nil
case <-stopOutput:
return nil
case <-stopError:
2016-08-04 22:59:41 +00:00
return nil
2016-08-04 19:57:19 +00:00
}
}
2016-08-03 08:28:58 +00:00
}