performance improved

This commit is contained in:
alessio 2017-08-30 01:54:52 +02:00
parent f5cd6ebb02
commit f8cfd7b716
2 changed files with 39 additions and 29 deletions

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"strings" "strings"
"time" "time"
"path/filepath"
) )
// Run launches the toolchain for each project // Run launches the toolchain for each project
@ -27,8 +28,8 @@ func (h *Blueprint) Run(p *cli.Context) error {
if element.Cmds.Fmt { if element.Cmds.Fmt {
h.Projects[k].tools.Fmt = tool{ h.Projects[k].tools.Fmt = tool{
status: &h.Projects[k].Cmds.Fmt, status: &h.Projects[k].Cmds.Fmt,
cmd: "gofmt", cmd: "go",
options: []string{"-s", "-w", "-e"}, options: []string{"fmt"},
name: "Go Fmt", name: "Go Fmt",
} }
} }
@ -65,6 +66,23 @@ func (h *Blueprint) Run(p *cli.Context) error {
h.Projects[k].Buffer.StdErr = append(h.Projects[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""}) h.Projects[k].Buffer.StdErr = append(h.Projects[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""})
} }
} }
// base path
wd, err := os.Getwd()
if err != nil{
return err
}
if element.path == "." || element.path == "/" {
h.Projects[k].base = wd
h.Projects[k].path = element.Wdir()
} else if filepath.IsAbs(element.path) {
h.Projects[k].base = element.path
} else {
h.Projects[k].base = filepath.Join(wd, element.path)
}
println(h.Projects[k].base)
// watch
if h.Legacy.Status { if h.Legacy.Status {
go h.Projects[k].watchByPolling() go h.Projects[k].watchByPolling()
} else { } else {
@ -175,10 +193,6 @@ func (h *Blueprint) List() error {
} }
} }
} }
fmt.Println(name, style.Yellow.Regular("Streams"), ":")
fmt.Println(name, "\t", style.Yellow.Regular("File Out"), ":", style.Magenta.Regular(val.Streams.FileOut))
fmt.Println(name, "\t", style.Yellow.Regular("File Log"), ":", style.Magenta.Regular(val.Streams.FileLog))
fmt.Println(name, "\t", style.Yellow.Regular("File Err"), ":", style.Magenta.Regular(val.Streams.FileErr))
} }
return nil return nil
} }

View File

@ -43,8 +43,10 @@ func (p *Project) watchByPolling() {
channel, exit := make(chan bool, 1), make(chan os.Signal, 2) channel, exit := make(chan bool, 1), make(chan os.Signal, 2)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM) signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
defer func() { defer func() {
p.cmd("after", true)
wg.Done() wg.Done()
}() }()
p.cmd("before", true)
go p.routines(&wr, channel, watcher, "") go p.routines(&wr, channel, watcher, "")
p.LastChangedOn = time.Now().Truncate(time.Second) p.LastChangedOn = time.Now().Truncate(time.Second)
walk := func(changed string, info os.FileInfo, err error) error { walk := func(changed string, info os.FileInfo, err error) error {
@ -109,8 +111,10 @@ func (p *Project) watchByNotify() {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
p.Fatal(err) p.Fatal(err)
defer func() { defer func() {
p.cmd("after", true)
wg.Done() wg.Done()
}() }()
p.cmd("before", true)
go p.routines(&wr, channel, watcher, "") go p.routines(&wr, channel, watcher, "")
p.LastChangedOn = time.Now().Truncate(time.Second) p.LastChangedOn = time.Now().Truncate(time.Second)
for { for {
@ -156,7 +160,6 @@ func (p *Project) watchByNotify() {
// Watch the files tree of a project // Watch the files tree of a project
func (p *Project) watch(watcher watcher) error { func (p *Project) watch(watcher watcher) error {
var files, folders int64 var files, folders int64
wd, _ := os.Getwd()
walk := func(path string, info os.FileInfo, err error) error { walk := func(path string, info os.FileInfo, err error) error {
if !p.ignore(path) { if !p.ignore(path) {
if ((info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.HasPrefix(path, ".")) && !strings.Contains(path, "/.")) || (inArray(filepath.Ext(path), p.Watcher.Exts)) { if ((info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.HasPrefix(path, ".")) && !strings.Contains(path, "/.")) || (inArray(filepath.Ext(path), p.Watcher.Exts)) {
@ -168,30 +171,23 @@ func (p *Project) watch(watcher watcher) error {
} }
if inArray(filepath.Ext(path), p.Watcher.Exts) { if inArray(filepath.Ext(path), p.Watcher.Exts) {
files++ files++
p.tool(path, p.tools.Fmt)
} else { } else {
folders++ folders++
p.tool(path, p.tools.Vet)
p.tool(path, p.tools.Test)
p.tool(path, p.tools.Generate)
} }
} }
} }
return nil return nil
} }
if p.path == "." || p.path == "/" {
p.base = wd
p.path = p.Wdir()
} else if filepath.IsAbs(p.path) {
p.base = p.path
} else {
p.base = filepath.Join(wd, p.path)
}
for _, dir := range p.Watcher.Paths { for _, dir := range p.Watcher.Paths {
base := filepath.Join(p.base, dir) base := filepath.Join(p.base, dir)
if _, err := os.Stat(base); err == nil { if _, err := os.Stat(base); err == nil {
if err := filepath.Walk(base, walk); err != nil { if err := filepath.Walk(base, walk); err != nil {
log.Println(style.Red.Bold(err.Error())) log.Println(style.Red.Bold(err.Error()))
p.tool(base, p.tools.Fmt)
p.tool(base, p.tools.Vet)
p.tool(base, p.tools.Test)
p.tool(base, p.tools.Generate)
} }
} else { } else {
return errors.New(base + " path doesn't exist") return errors.New(base + " path doesn't exist")
@ -262,6 +258,7 @@ func (p *Project) build() error {
return nil return nil
} }
// Tool logs the result of a go command
func (p *Project) tool(path string, tool tool) error { func (p *Project) tool(path string, tool tool) error {
if tool.status != nil { if tool.status != nil {
v := reflect.ValueOf(tool.status).Elem() v := reflect.ValueOf(tool.status).Elem()
@ -284,15 +281,14 @@ func (p *Project) tool(path string, tool tool) error {
// Cmd calls an wrapper for execute the commands after/before // Cmd calls an wrapper for execute the commands after/before
func (p *Project) cmd(flag string, global bool) { func (p *Project) cmd(flag string, global bool) {
for _, cmd := range p.Watcher.Scripts { for _, cmd := range p.Watcher.Scripts {
if strings.ToLower(cmd.Type) == flag { if strings.ToLower(cmd.Type) == flag && cmd.Global == global {
err, logs := p.command(cmd) err, logs := p.command(cmd)
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Bold("Command"), style.Green.Bold("\"")+cmd.Command+style.Green.Bold("\"")) msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Bold("Command"), style.Green.Bold("\"")+cmd.Command+style.Green.Bold("\""))
out = BufferOut{Time: time.Now(), Text: cmd.Command, Type: flag} out = BufferOut{Time: time.Now(), Text: cmd.Command, Type: flag}
if logs != "" {
p.stamp("log", out, msg, "")
}
if err != "" { if err != "" {
p.stamp("error", out, msg, "") p.stamp("error", out, msg, "")
} else {
p.stamp("log", out, msg, "")
} }
if logs != "" { if logs != "" {
msg = fmt.Sprintln(logs) msg = fmt.Sprintln(logs)
@ -370,8 +366,8 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
switch t { switch t {
case "out": case "out":
p.Buffer.StdOut = append(p.Buffer.StdOut, o) p.Buffer.StdOut = append(p.Buffer.StdOut, o)
if p.Streams.FileOut { if p.Resources.Outputs.Status {
f := p.Create(p.base, p.parent.Resources.Outputs) f := p.Create(p.base, p.Resources.Outputs.Name)
t := time.Now() t := time.Now()
s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"} s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"}
if _, err := f.WriteString(strings.Join(s, " ")); err != nil { if _, err := f.WriteString(strings.Join(s, " ")); err != nil {
@ -380,8 +376,8 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
} }
case "log": case "log":
p.Buffer.StdLog = append(p.Buffer.StdLog, o) p.Buffer.StdLog = append(p.Buffer.StdLog, o)
if p.Streams.FileLog { if p.Resources.Logs.Status {
f := p.Create(p.base, p.parent.Resources.Logs) f := p.Create(p.base, p.Resources.Logs.Name)
t := time.Now() t := time.Now()
s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"} s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"}
if stream != "" { if stream != "" {
@ -393,8 +389,8 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
} }
case "error": case "error":
p.Buffer.StdErr = append(p.Buffer.StdErr, o) p.Buffer.StdErr = append(p.Buffer.StdErr, o)
if p.Streams.FileErr { if p.Resources.Errors.Status {
f := p.Create(p.base, p.parent.Resources.Errors) f := p.Create(p.base, p.Resources.Errors.Name)
t := time.Now() t := time.Now()
s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Type, o.Text, o.Path, "\r\n"} s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Type, o.Text, o.Path, "\r\n"}
if stream != "" { if stream != "" {