This commit is contained in:
asoseil 2017-11-20 09:18:18 +01:00
parent 3b44ec6291
commit 586f3f2fb5
6 changed files with 50 additions and 33 deletions

12
cli.go
View File

@ -1093,12 +1093,12 @@ func start(c *cli.Context) (err error) {
project := r.Schema.New(c) project := r.Schema.New(c)
// Add to projects list // Add to projects list
r.Schema.Add(project) r.Schema.Add(project)
} // save config
// save config if !c.Bool("no-config") {
if !c.Bool("no-config") { err = r.Settings.Write(r)
err = r.Settings.Write(r) if err != nil {
if err != nil { return err
return err }
} }
} }
// config and start server // config and start server

View File

@ -234,24 +234,33 @@ func (p *Project) Reload(watcher FileWatcher, path string, stop <-chan bool) {
var start time.Time var start time.Time
result := make(chan Response) result := make(chan Response)
go func() { go func() {
select { for {
case r := <-result: select {
if r.Err != nil { case <-stop:
msg := fmt.Sprintln(p.pname(p.Name, 2), ":", red.regular(r.Err)) return
out := BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: "Go Run"} case r := <-result:
p.stamp("error", out, msg, "") if r.Err != nil {
} msg := fmt.Sprintln(p.pname(p.Name, 2), ":", red.regular(r.Err))
if r.Out != "" { out := BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: "Go Run"}
msg := fmt.Sprintln(p.pname(p.Name, 3), ":", blue.regular(r.Out)) p.stamp("error", out, msg, "")
out := BufferOut{Time: time.Now(), Text: r.Out, Type: "Go Run"} }
p.stamp("out", out, msg, "") if r.Out != "" {
msg := fmt.Sprintln(p.pname(p.Name, 3), ":", blue.regular(r.Out))
out := BufferOut{Time: time.Now(), Text: r.Out, Type: "Go Run"}
p.stamp("out", out, msg, "")
}
} }
} }
}() }()
go func() { go func() {
log.Println(p.pname(p.Name, 1), ":", "Running..") log.Println(p.pname(p.Name, 1), ":", "Running..")
start = time.Now() start = time.Now()
p.Run(p.Path, stop) err := p.Run(p.Path, result, stop)
if err != nil{
msg := fmt.Sprintln(p.pname(p.Name, 2), ":", red.regular(err))
out := BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Run"}
p.stamp("error", out, msg, "")
}
}() }()
} }
if done { if done {
@ -261,13 +270,13 @@ func (p *Project) Reload(watcher FileWatcher, path string, stop <-chan bool) {
} }
// Run a project // Run a project
func (p *Project) Run(path string, stop <-chan bool) (response chan Response) { func (p *Project) Run(path string, stream chan Response, stop <-chan bool) (err error) {
var args []string var args []string
var build *exec.Cmd var build *exec.Cmd
var r Response var r Response
defer func() { defer func() {
if err := build.Process.Kill(); err != nil { if e := build.Process.Kill(); e != nil{
r.Err = err err = e
} }
}() }()
@ -278,8 +287,7 @@ func (p *Project) Run(path string, stop <-chan bool) (response chan Response) {
errRegexp, err := regexp.Compile(p.ErrorOutputPattern) errRegexp, err := regexp.Compile(p.ErrorOutputPattern)
if err != nil { if err != nil {
r.Err = err r.Err = err
response <- r stream <- r
r.Err = nil
} else { } else {
isErrorText = func(t string) bool { isErrorText = func(t string) bool {
return errRegexp.MatchString(t) return errRegexp.MatchString(t)
@ -309,20 +317,17 @@ func (p *Project) Run(path string, stop <-chan bool) (response chan Response) {
} else if _, err = os.Stat(path + RExtWin); err == nil { } else if _, err = os.Stat(path + RExtWin); err == nil {
build = exec.Command(path+RExtWin, args...) build = exec.Command(path+RExtWin, args...)
} else { } else {
r.Err = errors.New("project not found") return errors.New("project not found")
return
} }
} }
// scan project stream // scan project stream
stdout, err := build.StdoutPipe() stdout, err := build.StdoutPipe()
stderr, err := build.StderrPipe() stderr, err := build.StderrPipe()
if err != nil { if err != nil {
r.Err = err return err
return
} }
if err := build.Start(); err != nil { if err := build.Start(); err != nil {
r.Err = err return err
return
} }
execOutput, execError := bufio.NewScanner(stdout), bufio.NewScanner(stderr) execOutput, execError := bufio.NewScanner(stdout), bufio.NewScanner(stderr)
stopOutput, stopError := make(chan bool, 1), make(chan bool, 1) stopOutput, stopError := make(chan bool, 1), make(chan bool, 1)
@ -331,11 +336,11 @@ func (p *Project) Run(path string, stop <-chan bool) (response chan Response) {
text := output.Text() text := output.Text()
if isError && !isErrorText(text) { if isError && !isErrorText(text) {
r.Err = errors.New(text) r.Err = errors.New(text)
response <- r stream <- r
r.Err = nil r.Err = nil
} else { } else {
r.Out = text r.Out = text
response <- r stream <- r
r.Out = "" r.Out = ""
} }
} }
@ -516,9 +521,10 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
} }
} }
// Print with time after
func (r *Response) printAfter(start time.Time, p *Project) { func (r *Response) printAfter(start time.Time, p *Project) {
if r.Err != nil { if r.Err != nil {
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", red.bold(r.Name), red.regular(r.Err.Error())) msg = fmt.Sprintln(p.pname(p.Name, 2), ":", red.bold(r.Name), "\n", r.Err.Error())
out = BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: r.Name, Stream: r.Out} out = BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: r.Name, Stream: r.Out}
p.stamp("error", out, msg, r.Out) p.stamp("error", out, msg, r.Out)
} else { } else {

View File

@ -33,6 +33,7 @@ type (
LogWriter struct{} LogWriter struct{}
) )
// init check
func init() { func init() {
// custom log // custom log
log.SetFlags(0) log.SetFlags(0)

View File

@ -5,10 +5,12 @@ import (
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"path/filepath" "path/filepath"
"reflect" "reflect"
"fmt"
) )
// Schema projects list
type Schema struct { type Schema struct {
Projects []Project `yaml:"projects" json:"projects"` Projects []Project `yaml:"schema" json:"schema"`
} }
// Add a project if unique // Add a project if unique

View File

@ -13,12 +13,15 @@ var (
magenta = colorBase(color.FgHiMagenta) magenta = colorBase(color.FgHiMagenta)
) )
// ColorBase type
type colorBase color.Attribute type colorBase color.Attribute
// Regular font with a color
func (c colorBase) regular(a ...interface{}) string { func (c colorBase) regular(a ...interface{}) string {
return color.New(color.Attribute(c)).Sprint(a...) return color.New(color.Attribute(c)).Sprint(a...)
} }
// Bold font with a color
func (c colorBase) bold(a ...interface{}) string { func (c colorBase) bold(a ...interface{}) string {
return color.New(color.Attribute(c), color.Bold).Sprint(a...) return color.New(color.Attribute(c), color.Bold).Sprint(a...)
} }

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
) )
// Tool info
type Tool struct { type Tool struct {
Args []string `yaml:"args,omitempty" json:"args,omitempty"` Args []string `yaml:"args,omitempty" json:"args,omitempty"`
Method string `yaml:"method,omitempty" json:"method,omitempty"` Method string `yaml:"method,omitempty" json:"method,omitempty"`
@ -19,6 +20,7 @@ type Tool struct {
name string name string
} }
// Tools go
type Tools struct { type Tools struct {
Fix Tool `yaml:"fix,omitempty" json:"fix,omitempty"` Fix Tool `yaml:"fix,omitempty" json:"fix,omitempty"`
Clean Tool `yaml:"clean,omitempty" json:"clean,omitempty"` Clean Tool `yaml:"clean,omitempty" json:"clean,omitempty"`
@ -31,6 +33,7 @@ type Tools struct {
Run bool `yaml:"run,omitempty" json:"run,omitempty"` Run bool `yaml:"run,omitempty" json:"run,omitempty"`
} }
// Setup go tools
func (t *Tools) Setup() { func (t *Tools) Setup() {
// go clean // go clean
if t.Clean.Status { if t.Clean.Status {
@ -92,6 +95,7 @@ func (t *Tools) Setup() {
} }
} }
// Exec a go tool
func (t *Tool) Exec(path string, stop <-chan bool) (response Response) { func (t *Tool) Exec(path string, stop <-chan bool) (response Response) {
if t.dir && filepath.Ext(path) != "" { if t.dir && filepath.Ext(path) != "" {
path = filepath.Dir(path) path = filepath.Dir(path)
@ -132,6 +136,7 @@ func (t *Tool) Exec(path string, stop <-chan bool) (response Response) {
return return
} }
// Compile is used for build and install
func (t *Tool) Compile(path string, stop <-chan bool) (response Response) { func (t *Tool) Compile(path string, stop <-chan bool) (response Response) {
var out bytes.Buffer var out bytes.Buffer
var stderr bytes.Buffer var stderr bytes.Buffer