From 01e3ee95b58465f2f97619852c51ece74932f7f6 Mon Sep 17 00:00:00 2001 From: alessio Date: Sun, 16 Apr 2017 12:02:47 +0200 Subject: [PATCH 01/15] realize dir as const --- realize.go | 49 +++++++++++++++++++++++++++++--------------- settings/io.go | 4 ++-- settings/settings.go | 16 ++++++++------- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/realize.go b/realize.go index 2042088..c925635 100644 --- a/realize.go +++ b/realize.go @@ -17,14 +17,13 @@ import ( const ( appVersion = "1.3" - - config = "realize.yaml" - outputs = "outputs.log" - errs = "errors.log" - logs = "logs.log" - host = "localhost" - port = 5001 - interval = 200 + config = "realize.yaml" + outputs = "outputs.log" + errs = "errors.log" + logs = "logs.log" + host = "localhost" + port = 5001 + interval = 200 ) // Cli commands @@ -195,7 +194,7 @@ func main() { Questions: []*interact.Question{ { Before: func(d interact.Context) error { - if _, err := os.Stat(".realize/" + config); err != nil { + if _, err := os.Stat(settings.Dir + config); err != nil { d.Skip() } d.SetDef(false, style.Green.Regular("(n)")) @@ -462,7 +461,25 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Fmt = val + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Fmt = val + return nil + }, + }, + { + Before: func(d interact.Context) error { + d.SetDef(true, style.Green.Regular("(y)")) + return nil + }, + Quest: interact.Quest{ + Options: style.Yellow.Regular("[y/n]"), + Msg: "Enable go vet", + }, + Action: func(d interact.Context) interface{} { + val, err := d.Ans().Bool() + if err != nil { + return d.Err() + } + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Vet = val return nil }, }, @@ -480,7 +497,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Test = val + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Test = val return nil }, }, @@ -498,7 +515,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Generate = val + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Generate = val return nil }, }, @@ -516,7 +533,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Bin = val + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Bin.Status = val return nil }, }, @@ -534,7 +551,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Build = val + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Build.Status = val return nil }, }, @@ -552,7 +569,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Run = val + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Run = val return nil }, }, @@ -674,7 +691,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Params = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Params, val) + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Args = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Args, val) d.Reload() return nil }, diff --git a/settings/io.go b/settings/io.go index e3fc2c3..d622ee8 100644 --- a/settings/io.go +++ b/settings/io.go @@ -26,8 +26,8 @@ func (s Settings) Write(name string, data []byte) error { // Create a new file and return its pointer func (s Settings) Create(path string, name string) *os.File { var file string - if _, err := os.Stat(".realize/"); err == nil { - file = filepath.Join(path, ".realize/", name) + if _, err := os.Stat(Dir); err == nil { + file = filepath.Join(path, Dir, name) } else { file = filepath.Join(path, name) } diff --git a/settings/settings.go b/settings/settings.go index 58ae93a..41a4054 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -7,6 +7,8 @@ import ( yaml "gopkg.in/yaml.v2" ) +var Dir = ".realize/" + // Settings defines a group of general settings type Settings struct { Config `yaml:",inline" json:"config"` @@ -46,8 +48,8 @@ type Resources struct { // Read from config file func (s *Settings) Read(out interface{}) error { localConfigPath := s.Resources.Config - if _, err := os.Stat(".realize/" + s.Resources.Config); err == nil { - localConfigPath = ".realize/" + s.Resources.Config + if _, err := os.Stat(Dir + s.Resources.Config); err == nil { + localConfigPath = Dir + s.Resources.Config } content, err := s.Stream(localConfigPath) if err == nil { @@ -64,20 +66,20 @@ func (s *Settings) Record(out interface{}) error { if err != nil { return err } - if _, err := os.Stat(".realize/"); os.IsNotExist(err) { - if err = os.Mkdir(".realize/", 0770); err != nil { + if _, err := os.Stat(Dir); os.IsNotExist(err) { + if err = os.Mkdir(Dir, 0770); err != nil { return s.Write(s.Resources.Config, y) } } - return s.Write(".realize/"+s.Resources.Config, y) + return s.Write(Dir+s.Resources.Config, y) } return nil } // Remove realize folder func (s *Settings) Remove() error { - if _, err := os.Stat(".realize/"); !os.IsNotExist(err) { - return os.RemoveAll(".realize/") + if _, err := os.Stat(Dir); !os.IsNotExist(err) { + return os.RemoveAll(Dir) } return nil } From f980722912538334de992b84748906f451814ca6 Mon Sep 17 00:00:00 2001 From: alessio Date: Sun, 16 Apr 2017 12:04:40 +0200 Subject: [PATCH 02/15] go vet support, array of cmds and args options for build/install --- watcher/cmd.go | 81 ++++++++++++++++++++++++++++++++++----------- watcher/exec.go | 31 +++++++++++------- watcher/main.go | 43 +++++++++++++++++------- watcher/watcher.go | 82 ++++++++++++++++++---------------------------- 4 files changed, 144 insertions(+), 93 deletions(-) diff --git a/watcher/cmd.go b/watcher/cmd.go index 5f88012..a8edf6e 100644 --- a/watcher/cmd.go +++ b/watcher/cmd.go @@ -3,10 +3,9 @@ package watcher import ( "errors" "fmt" - "strings" - "github.com/tockins/realize/style" cli "gopkg.in/urfave/cli.v2" + "strings" ) // Run launches the toolchain for each project @@ -15,7 +14,41 @@ func (h *Blueprint) Run() error { if err == nil { // loop projects wg.Add(len(h.Projects)) - for k := range h.Projects { + for k, element := range h.Projects { + tools := tools{} + if element.Cmds.Fmt { + tools.Fmt = tool{ + status: &h.Projects[k].Cmds.Fmt, + cmd: "gofmt", + options: []string{"-s", "-w", "-e"}, + name: "Go Fmt", + } + } + if element.Cmds.Generate { + tools.Generate = tool{ + status: &h.Projects[k].Cmds.Generate, + cmd: "go", + options: []string{"generate"}, + name: "Go Generate", + } + } + if element.Cmds.Test { + tools.Test = tool{ + status: &h.Projects[k].Cmds.Test, + cmd: "go", + options: []string{"test"}, + name: "Go Test", + } + } + if element.Cmds.Vet { + tools.Vet = tool{ + status: &h.Projects[k].Cmds.Vet, + cmd: "go", + options: []string{"test"}, + name: "Go Test", + } + } + h.Projects[k].tools = tools h.Projects[k].parent = h h.Projects[k].path = h.Projects[k].Path if h.Legacy.Status { @@ -33,15 +66,23 @@ func (h *Blueprint) Run() error { // Add a new project func (h *Blueprint) Add(p *cli.Context) error { project := Project{ - Name: h.Name(p.String("name"), p.String("path")), - Path: h.Path(p.String("path")), - Fmt: !p.Bool("no-fmt"), - Generate: p.Bool("generate"), - Test: p.Bool("test"), - Build: p.Bool("build"), - Bin: !p.Bool("no-bin"), - Run: !p.Bool("no-run"), - Params: argsParam(p), + Name: h.Name(p.String("name"), p.String("path")), + Path: h.Path(p.String("path")), + Cmds: Cmds{ + + Vet: p.Bool("vet"), + Fmt: !p.Bool("no-fmt"), + Test: p.Bool("test"), + Generate: p.Bool("generate"), + Build: Cmd{ + Status: p.Bool("build"), + }, + Bin: Cmd{ + Status: !p.Bool("no-bin"), + }, + Run: !p.Bool("no-run"), + }, + Args: argsParam(p), Watcher: Watcher{ Paths: []string{"/"}, Ignore: []string{"vendor"}, @@ -94,14 +135,14 @@ func (h *Blueprint) List() error { name := style.Magenta.Bold("[") + strings.ToUpper(val.Name) + style.Magenta.Bold("]") fmt.Println(name, style.Yellow.Regular("Base Path"), ":", style.Magenta.Regular(val.Path)) - fmt.Println(name, style.Yellow.Regular("Fmt"), ":", style.Magenta.Regular(val.Fmt)) - fmt.Println(name, style.Yellow.Regular("Generate"), ":", style.Magenta.Regular(val.Generate)) - fmt.Println(name, style.Yellow.Regular("Test"), ":", style.Magenta.Regular(val.Test)) - fmt.Println(name, style.Yellow.Regular("Install"), ":", style.Magenta.Regular(val.Bin)) - fmt.Println(name, style.Yellow.Regular("Build"), ":", style.Magenta.Regular(val.Build)) - fmt.Println(name, style.Yellow.Regular("Run"), ":", style.Magenta.Regular(val.Run)) - if len(val.Params) > 0 { - fmt.Println(name, style.Yellow.Regular("Params"), ":", style.Magenta.Regular(val.Params)) + fmt.Println(name, style.Yellow.Regular("Fmt"), ":", style.Magenta.Regular(val.Cmds.Fmt)) + fmt.Println(name, style.Yellow.Regular("Generate"), ":", style.Magenta.Regular(val.Cmds.Generate)) + fmt.Println(name, style.Yellow.Regular("Test"), ":", style.Magenta.Regular(val.Cmds.Test)) + fmt.Println(name, style.Yellow.Regular("Install"), ":", style.Magenta.Regular(val.Cmds.Bin)) + fmt.Println(name, style.Yellow.Regular("Build"), ":", style.Magenta.Regular(val.Cmds.Build)) + fmt.Println(name, style.Yellow.Regular("Run"), ":", style.Magenta.Regular(val.Cmds.Run)) + if len(val.Args) > 0 { + fmt.Println(name, style.Yellow.Regular("Params"), ":", style.Magenta.Regular(val.Args)) } fmt.Println(name, style.Yellow.Regular("Watcher"), ":") fmt.Println(name, "\t", style.Yellow.Regular("Preview"), ":", style.Magenta.Regular(val.Watcher.Preview)) diff --git a/watcher/exec.go b/watcher/exec.go index 2797c88..72a6d27 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "github.com/tockins/realize/style" "log" "os" "os/exec" @@ -11,18 +12,16 @@ import ( "strings" "sync" "time" - - "github.com/tockins/realize/style" ) // GoRun is an implementation of the bin execution func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error { var build *exec.Cmd - var params []string + var args []string var path = "" - for _, param := range p.Params { - arr := strings.Fields(param) - params = append(params, arr...) + for _, arg := range p.Args { + arr := strings.Fields(arg) + args = append(args, arr...) } if _, err := os.Stat(filepath.Join(p.base, p.path)); err == nil { path = filepath.Join(p.base, p.path) @@ -32,12 +31,12 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } if path != "" { - build = exec.Command(path, params...) + build = exec.Command(path, args...) } else { if _, err := os.Stat(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path))); err == nil { - build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)), params...) + build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)), args...) } else if _, err := os.Stat(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)) + ".exe"); err == nil { - build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path))+".exe", params...) + build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path))+".exe", args...) } else { 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", ":") @@ -102,7 +101,12 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) func (p *Project) goBuild() (string, error) { var out bytes.Buffer var stderr bytes.Buffer - build := exec.Command("go", "build") + args := []string{"build"} + for _, arg := range p.Cmds.Build.Args { + arr := strings.Fields(arg) + args = append(args, arr...) + } + build := exec.Command("go", args...) build.Dir = p.base build.Stdout = &out build.Stderr = &stderr @@ -120,7 +124,12 @@ func (p *Project) goInstall() (string, error) { if err != nil { return "", err } - build := exec.Command("go", "install") + args := []string{"install"} + for _, arg := range p.Cmds.Bin.Args { + arr := strings.Fields(arg) + args = append(args, arr...) + } + build := exec.Command("go", args...) build.Dir = p.base build.Stdout = &out build.Stderr = &stderr diff --git a/watcher/main.go b/watcher/main.go index 2364b4e..ac38a06 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -1,11 +1,10 @@ package watcher import ( + "github.com/tockins/realize/settings" "log" "sync" "time" - - "github.com/tockins/realize/settings" ) var wg sync.WaitGroup @@ -21,8 +20,7 @@ type pollWatcher struct { } // Log struct -type logWriter struct { -} +type logWriter struct{} // Blueprint struct contains a projects list type Blueprint struct { @@ -38,18 +36,41 @@ type Project struct { base string Name string `yaml:"name" json:"name"` Path string `yaml:"path" json:"path"` - Fmt bool `yaml:"fmt" json:"fmt"` - Generate bool `yaml:"generate" json:"generate"` - Test bool `yaml:"test" json:"test"` - Bin bool `yaml:"bin" json:"bin"` - Build bool `yaml:"build" json:"build"` - Run bool `yaml:"run" json:"run"` - Params []string `yaml:"params,omitempty" json:"params,omitempty"` + Cmds Cmds `yaml:"cmds" json:"cmds"` + Args []string `yaml:"args,omitempty" json:"args,omitempty"` Watcher Watcher `yaml:"watcher" json:"watcher"` Streams Streams `yaml:"streams" json:"streams"` Buffer Buffer `yaml:"-" json:"buffer"` parent *Blueprint path string + tools tools +} + +type tools struct { + Fmt, Test, Generate, Vet tool +} + +type tool struct { + status *bool + cmd string + options []string + name string +} + +type Cmds struct { + Vet bool `yaml:"vet" json:"vet"` + Fmt bool `yaml:"fmt" json:"fmt"` + Test bool `yaml:"test" json:"test"` + Generate bool `yaml:"generate" json:"generate"` + Bin Cmd `yaml:"bin" json:"bin"` + Build Cmd `yaml:"build" json:"build"` + Run bool `yaml:"run" json:"run"` +} + +// Buildmode options +type Cmd struct { + Status bool `yaml:"status" json:"status"` + Args []string `yaml:"args,omitempty" json:"args,omitempty"` } // Watcher struct defines the livereload's logic diff --git a/watcher/watcher.go b/watcher/watcher.go index b7e16ba..d3880c5 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -3,19 +3,19 @@ package watcher import ( "errors" "fmt" + "github.com/fsnotify/fsnotify" + "github.com/tockins/realize/style" "log" "math/big" "os" "os/signal" "path/filepath" + "reflect" "strconv" "strings" "sync" "syscall" "time" - - "github.com/fsnotify/fsnotify" - "github.com/tockins/realize/style" ) var msg string @@ -67,7 +67,7 @@ func (p *Project) watchByPolling() { file := changed[:i] + ext path := filepath.Dir(changed[:i]) if changed[:i] != "" && inArray(ext, p.Watcher.Exts) { - if p.Run { + if p.Cmds.Run { close(channel) channel = make(chan bool) } @@ -78,9 +78,10 @@ func (p *Project) watchByPolling() { p.print("log", out, msg, "") p.cmd("change") - p.fmt(file) - p.test(path) - p.generate(path) + p.tool(file, p.tools.Fmt) + p.tool(path, p.tools.Vet) + p.tool(path, p.tools.Test) + p.tool(path, p.tools.Generate) go p.routines(channel, &wr) } return nil @@ -143,7 +144,7 @@ func (p *Project) watchByNotify() { file := event.Name[:i] + ext path := filepath.Dir(event.Name[:i]) if event.Name[:i] != "" && inArray(ext, p.Watcher.Exts) { - if p.Run { + if p.Cmds.Run { close(channel) channel = make(chan bool) } @@ -154,9 +155,10 @@ func (p *Project) watchByNotify() { p.print("log", out, msg, "") p.cmd("change") - p.fmt(file) - p.test(path) - p.generate(path) + p.tool(file, p.tools.Fmt) + p.tool(path, p.tools.Vet) + p.tool(path, p.tools.Test) + p.tool(path, p.tools.Generate) go p.routines(channel, &wr) } } @@ -186,11 +188,12 @@ func (p *Project) watch(watcher watcher) error { } if inArray(filepath.Ext(path), p.Watcher.Exts) { files++ - p.fmt(path) + p.tool(path, p.tools.Fmt) } else { folders++ - p.generate(path) - p.test(path) + p.tool(path, p.tools.Vet) + p.tool(path, p.tools.Test) + p.tool(path, p.tools.Generate) } } } @@ -222,7 +225,7 @@ func (p *Project) watch(watcher watcher) error { // Install calls an implementation of "go install" func (p *Project) install() error { - if p.Bin { + if p.Cmds.Bin.Status { start := time.Now() log.Println(p.pname(p.Name, 1), ":", "Installing..") stream, err := p.goInstall() @@ -242,7 +245,7 @@ func (p *Project) install() error { // Install calls an implementation of "go run" func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { - if p.Run { + if p.Cmds.Run { start := time.Now() runner := make(chan bool, 1) log.Println(p.pname(p.Name, 1), ":", "Running..") @@ -261,7 +264,7 @@ func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { // Build calls an implementation of the "go build" func (p *Project) build() error { - if p.Build { + if p.Cmds.Build.Status { start := time.Now() log.Println(p.pname(p.Name, 1), ":", "Building..") stream, err := p.goBuild() @@ -279,40 +282,17 @@ func (p *Project) build() error { return nil } -// Fmt calls an implementation of the "go fmt" -func (p *Project) fmt(path string) error { - if p.Fmt && strings.HasSuffix(path, ".go") { - if stream, err := p.goTools(p.base, "gofmt", "-s", "-w", "-e", path); err != nil { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Fmt"), style.Red.Regular("there are some errors in"), ":", style.Magenta.Bold(path)) - out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Fmt", Stream: stream} - p.print("error", out, msg, stream) - return err - } - } - return nil -} - -// Generate calls an implementation of the "go generate" -func (p *Project) generate(path string) error { - if p.Generate { - if stream, err := p.goTools(path, "go", "generate"); err != nil { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Generate"), style.Red.Regular("there are some errors in"), ":", style.Magenta.Bold(path)) - out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Generate", Stream: stream} - p.print("error", out, msg, stream) - return err - } - } - return nil -} - -// Test calls an implementation of the "go test" -func (p *Project) test(path string) error { - if p.Test { - if stream, err := p.goTools(path, "go", "test"); err != nil { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Test"), style.Red.Regular("there are some errors in "), ":", style.Magenta.Bold(path)) - out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Test", Stream: stream} - p.print("error", out, msg, stream) - return err +func (p *Project) tool(path string, tool tool) error { + if tool.status != nil { + v := reflect.ValueOf(tool.status).Elem() + if v.Interface().(bool) && strings.HasSuffix(path, ".go") { + options := append(tool.options, path) + if stream, err := p.goTools(p.base, tool.cmd, options...); err != nil { + msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold(tool.name), style.Red.Regular("there are some errors in"), ":", style.Magenta.Bold(path)) + out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: tool.name, Stream: stream} + p.print("error", out, msg, stream) + return err + } } } return nil From 455a243951400faf8c7546862a2f4234f63e2783 Mon Sep 17 00:00:00 2001 From: Kyoichiro Yamada Date: Thu, 20 Apr 2017 17:22:09 +0900 Subject: [PATCH 03/15] append "errorOutputPattern" property for projects --- watcher/exec.go | 32 ++++++++++++++++++++++---------- watcher/main.go | 35 ++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/watcher/exec.go b/watcher/exec.go index 2797c88..943f1ec 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "sync" "time" @@ -20,6 +21,19 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) var build *exec.Cmd var params []string var path = "" + 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"} + p.print("error", out, msg, "") + } else { + isErrorText = func(t string) bool { + return errRegexp.MatchString(t) + } + } for _, param := range p.Params { arr := strings.Fields(param) params = append(params, arr...) @@ -70,16 +84,14 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) stopOutput, stopError := make(chan bool, 1), make(chan bool, 1) scanner := func(stop chan bool, output *bufio.Scanner, isError bool) { for output.Scan() { - select { - default: - msg := fmt.Sprintln(p.pname(p.Name, 3), ":", style.Blue.Regular(output.Text())) - if isError { - out := BufferOut{Time: time.Now(), Text: output.Text(), Type: "Go Run"} - p.print("error", out, msg, "") - } else { - out := BufferOut{Time: time.Now(), Text: output.Text()} - p.print("out", out, msg, "") - } + text := output.Text() + msg := fmt.Sprintln(p.pname(p.Name, 3), ":", style.Blue.Regular(text)) + if isError || isErrorText(text) { + out := BufferOut{Time: time.Now(), Text: text, Type: "Go Run"} + p.print("error", out, msg, "") + } else { + out := BufferOut{Time: time.Now(), Text: text} + p.print("out", out, msg, "") } } close(stop) diff --git a/watcher/main.go b/watcher/main.go index 2364b4e..de700ba 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -33,23 +33,24 @@ type Blueprint struct { // Project defines the informations of a single project type Project struct { - settings.Settings `yaml:"-"` - LastChangedOn time.Time `yaml:"-" json:"-"` - base string - Name string `yaml:"name" json:"name"` - Path string `yaml:"path" json:"path"` - Fmt bool `yaml:"fmt" json:"fmt"` - Generate bool `yaml:"generate" json:"generate"` - Test bool `yaml:"test" json:"test"` - Bin bool `yaml:"bin" json:"bin"` - Build bool `yaml:"build" json:"build"` - Run bool `yaml:"run" json:"run"` - Params []string `yaml:"params,omitempty" json:"params,omitempty"` - Watcher Watcher `yaml:"watcher" json:"watcher"` - Streams Streams `yaml:"streams" json:"streams"` - Buffer Buffer `yaml:"-" json:"buffer"` - parent *Blueprint - path string + settings.Settings `yaml:"-"` + LastChangedOn time.Time `yaml:"-" json:"-"` + base string + Name string `yaml:"name" json:"name"` + Path string `yaml:"path" json:"path"` + Fmt bool `yaml:"fmt" json:"fmt"` + Generate bool `yaml:"generate" json:"generate"` + Test bool `yaml:"test" json:"test"` + Bin bool `yaml:"bin" json:"bin"` + Build bool `yaml:"build" json:"build"` + Run bool `yaml:"run" json:"run"` + Params []string `yaml:"params,omitempty" json:"params,omitempty"` + Watcher Watcher `yaml:"watcher" json:"watcher"` + Streams Streams `yaml:"streams" json:"streams"` + ErrorOutputPattern string `yaml:"errorOutputPattern" json:"errorOutputPattern"` + Buffer Buffer `yaml:"-" json:"buffer"` + parent *Blueprint + path string } // Watcher struct defines the livereload's logic From 0c9d42e996df9524da99d3766b53f3920262e7a7 Mon Sep 17 00:00:00 2001 From: alessio Date: Mon, 24 Apr 2017 02:07:37 +0200 Subject: [PATCH 04/15] tools fixed --- watcher/cmd.go | 4 ++-- watcher/exec.go | 2 +- watcher/watcher.go | 13 ++++++------- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/watcher/cmd.go b/watcher/cmd.go index a8edf6e..a67d6e7 100644 --- a/watcher/cmd.go +++ b/watcher/cmd.go @@ -44,8 +44,8 @@ func (h *Blueprint) Run() error { tools.Vet = tool{ status: &h.Projects[k].Cmds.Vet, cmd: "go", - options: []string{"test"}, - name: "Go Test", + options: []string{"vet"}, + name: "Go Vet", } } h.Projects[k].tools = tools diff --git a/watcher/exec.go b/watcher/exec.go index 72a6d27..fcad084 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -4,7 +4,6 @@ import ( "bufio" "bytes" "fmt" - "github.com/tockins/realize/style" "log" "os" "os/exec" @@ -12,6 +11,7 @@ import ( "strings" "sync" "time" + "github.com/tockins/realize/style" ) // GoRun is an implementation of the bin execution diff --git a/watcher/watcher.go b/watcher/watcher.go index d3880c5..dbf6f5b 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -3,19 +3,19 @@ package watcher import ( "errors" "fmt" - "github.com/fsnotify/fsnotify" - "github.com/tockins/realize/style" "log" "math/big" "os" "os/signal" "path/filepath" - "reflect" "strconv" "strings" "sync" "syscall" "time" + "reflect" + "github.com/fsnotify/fsnotify" + "github.com/tockins/realize/style" ) var msg string @@ -79,7 +79,7 @@ func (p *Project) watchByPolling() { p.cmd("change") p.tool(file, p.tools.Fmt) - p.tool(path, p.tools.Vet) + p.tool(file, p.tools.Vet) p.tool(path, p.tools.Test) p.tool(path, p.tools.Generate) go p.routines(channel, &wr) @@ -285,9 +285,8 @@ func (p *Project) build() error { func (p *Project) tool(path string, tool tool) error { if tool.status != nil { v := reflect.ValueOf(tool.status).Elem() - if v.Interface().(bool) && strings.HasSuffix(path, ".go") { - options := append(tool.options, path) - if stream, err := p.goTools(p.base, tool.cmd, options...); err != nil { + if v.Interface().(bool) && (strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "")) { + if stream, err := p.goTools(path, tool.cmd, tool.options...); err != nil { msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold(tool.name), style.Red.Regular("there are some errors in"), ":", style.Magenta.Bold(path)) out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: tool.name, Stream: stream} p.print("error", out, msg, stream) From c6bd96e86d95607dc5fa7bccae5a763e1ddd56a5 Mon Sep 17 00:00:00 2001 From: alessio Date: Mon, 24 Apr 2017 02:14:17 +0200 Subject: [PATCH 05/15] dir/file fixed --- watcher/watcher.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/watcher/watcher.go b/watcher/watcher.go index dbf6f5b..1468d25 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -286,6 +286,10 @@ func (p *Project) tool(path string, tool tool) error { if tool.status != nil { v := reflect.ValueOf(tool.status).Elem() if v.Interface().(bool) && (strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "")) { + if strings.HasSuffix(path, ".go"){ + tool.options = append(tool.options, path) + path = p.base + } if stream, err := p.goTools(path, tool.cmd, tool.options...); err != nil { msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold(tool.name), style.Red.Regular("there are some errors in"), ":", style.Magenta.Bold(path)) out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: tool.name, Stream: stream} From 1190e5dbedd2a9b9a54195a01f9da406ea2077b2 Mon Sep 17 00:00:00 2001 From: alessio Date: Mon, 24 Apr 2017 11:52:22 +0200 Subject: [PATCH 06/15] last change fixed --- watcher/watcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/watcher/watcher.go b/watcher/watcher.go index 1468d25..e7425c1 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -148,7 +148,6 @@ func (p *Project) watchByNotify() { close(channel) channel = make(chan bool) } - p.LastChangedOn = time.Now().Truncate(time.Second) // repeat the initial cycle msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file)) out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file} @@ -160,6 +159,7 @@ func (p *Project) watchByNotify() { p.tool(path, p.tools.Test) p.tool(path, p.tools.Generate) go p.routines(channel, &wr) + p.LastChangedOn = time.Now().Truncate(time.Second) } } } From e8a6e29ae4839ecf9f2de4875d50892019a150a3 Mon Sep 17 00:00:00 2001 From: alessio Date: Wed, 26 Apr 2017 16:09:23 +0200 Subject: [PATCH 07/15] dep updated --- Gopkg.lock | 91 +++++++++++++++++++++++++++++++++ Gopkg.toml | 28 ++++++++++ lock.json | 138 -------------------------------------------------- manifest.json | 25 --------- 4 files changed, 119 insertions(+), 163 deletions(-) create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml delete mode 100644 lock.json delete mode 100644 manifest.json diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..ea471a9 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,91 @@ +memo = "4f11729a341d710751e002460e3767a15151cb5aee2e11ba6f70409107016f15" + +[[projects]] + name = "github.com/dgrijalva/jwt-go" + packages = ["."] + revision = "d2709f9f1f31ebcda9651b03077758c1f3a0018c" + version = "v3.0.0" + +[[projects]] + name = "github.com/fatih/color" + packages = ["."] + revision = "9131ab34cf20d2f6d83fdc67168a5430d1c7dc23" + version = "v1.4.1" + +[[projects]] + branch = "master" + name = "github.com/fsnotify/fsnotify" + packages = ["."] + revision = "4da3e2cfbabc9f751898f250b49f2439785783a1" + +[[projects]] + branch = "master" + name = "github.com/labstack/echo" + packages = [".","middleware"] + revision = "4256489c0e1ff50bdb71aedd1a21483fe2d67607" + +[[projects]] + name = "github.com/labstack/gommon" + packages = ["bytes","color","log","random"] + revision = "9cedb429ffbe71a32a3ae7c65fd109cb7ae07804" + version = "v0.2.0" + +[[projects]] + name = "github.com/mattn/go-colorable" + packages = ["."] + revision = "d228849504861217f796da67fae4f6e347643f15" + version = "v0.0.7" + +[[projects]] + name = "github.com/mattn/go-isatty" + packages = ["."] + revision = "fc9e8d8ef48496124e79ae0df75490096eccf6fe" + version = "v0.0.2" + +[[projects]] + branch = "master" + name = "github.com/tockins/interact" + packages = ["."] + revision = "c424f9a549ee8ca6b36332a8cb4374701fff6ecb" + +[[projects]] + branch = "master" + name = "github.com/valyala/bytebufferpool" + packages = ["."] + revision = "e746df99fe4a3986f4d4f79e13c1e0117ce9c2f7" + +[[projects]] + branch = "master" + name = "github.com/valyala/fasttemplate" + packages = ["."] + revision = "dcecefd839c4193db0d35b88ec65b4c12d360ab0" + +[[projects]] + branch = "master" + name = "golang.org/x/crypto" + packages = ["acme","acme/autocert"] + revision = "c7af5bf2638a1164f2eb5467c39c6cffbd13a02e" + +[[projects]] + branch = "master" + name = "golang.org/x/net" + packages = ["websocket"] + revision = "da118f7b8e5954f39d0d2130ab35d4bf0e3cb344" + +[[projects]] + branch = "master" + name = "golang.org/x/sys" + packages = ["unix"] + revision = "9f30dcbe5be197894515a338a9bda9253567ea8f" + +[[projects]] + branch = "v2" + name = "gopkg.in/urfave/cli.v2" + packages = ["."] + revision = "61c3eb6ba1da1df17cd50f1456a9252d92bf5edd" + +[[projects]] + branch = "v2" + name = "gopkg.in/yaml.v2" + packages = ["."] + revision = "cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b" diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..86a0a37 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,28 @@ + +[[dependencies]] + branch = "master" + name = "github.com/fatih/color" + +[[dependencies]] + branch = "master" + name = "github.com/fsnotify/fsnotify" + +[[dependencies]] + branch = "master" + name = "github.com/labstack/echo" + +[[dependencies]] + branch = "master" + name = "github.com/tockins/interact" + +[[dependencies]] + branch = "master" + name = "golang.org/x/net" + +[[dependencies]] + branch = "v2" + name = "gopkg.in/urfave/cli.v2" + +[[dependencies]] + branch = "v2" + name = "gopkg.in/yaml.v2" diff --git a/lock.json b/lock.json deleted file mode 100644 index e5a03c0..0000000 --- a/lock.json +++ /dev/null @@ -1,138 +0,0 @@ -{ - "memo": "0dae7877c338a668c3b0b2c64112895cd2076b90c0792ca2a0edf22fb10d14d2", - "projects": [ - { - "name": "github.com/dgrijalva/jwt-go", - "version": "v3.0.0", - "revision": "d2709f9f1f31ebcda9651b03077758c1f3a0018c", - "packages": [ - "." - ] - }, - { - "name": "github.com/fatih/color", - "version": "v1.4.1", - "revision": "9131ab34cf20d2f6d83fdc67168a5430d1c7dc23", - "packages": [ - "." - ] - }, - { - "name": "github.com/fsnotify/fsnotify", - "branch": "master", - "revision": "4da3e2cfbabc9f751898f250b49f2439785783a1", - "packages": [ - "." - ] - }, - { - "name": "github.com/labstack/echo", - "version": "v3.0.3", - "revision": "8d504c1b699c757b267255c53b3e5219f9974abc", - "packages": [ - ".", - "middleware" - ] - }, - { - "name": "github.com/labstack/gommon", - "version": "v0.2.0", - "revision": "9cedb429ffbe71a32a3ae7c65fd109cb7ae07804", - "packages": [ - "bytes", - "color", - "log", - "random" - ] - }, - { - "name": "github.com/mattn/go-colorable", - "version": "v0.0.7", - "revision": "d228849504861217f796da67fae4f6e347643f15", - "packages": [ - "." - ] - }, - { - "name": "github.com/mattn/go-isatty", - "version": "v0.0.2", - "revision": "fc9e8d8ef48496124e79ae0df75490096eccf6fe", - "packages": [ - "." - ] - }, - { - "name": "github.com/tockins/interact", - "branch": "master", - "revision": "c424f9a549ee8ca6b36332a8cb4374701fff6ecb", - "packages": [ - "." - ] - }, - { - "name": "github.com/tylerb/graceful", - "version": "v1.2.15", - "revision": "4654dfbb6ad53cb5e27f37d99b02e16c1872fbbb", - "packages": [ - "." - ] - }, - { - "name": "github.com/valyala/bytebufferpool", - "branch": "master", - "revision": "e746df99fe4a3986f4d4f79e13c1e0117ce9c2f7", - "packages": [ - "." - ] - }, - { - "name": "github.com/valyala/fasttemplate", - "branch": "master", - "revision": "dcecefd839c4193db0d35b88ec65b4c12d360ab0", - "packages": [ - "." - ] - }, - { - "name": "golang.org/x/crypto", - "branch": "master", - "revision": "cbc3d0884eac986df6e78a039b8792e869bff863", - "packages": [ - "acme", - "acme/autocert" - ] - }, - { - "name": "golang.org/x/net", - "branch": "master", - "revision": "5602c733f70afc6dcec6766be0d5034d4c4f14de", - "packages": [ - "websocket" - ] - }, - { - "name": "golang.org/x/sys", - "branch": "master", - "revision": "f3918c30c5c2cb527c0b071a27c35120a6c0719a", - "packages": [ - "unix" - ] - }, - { - "name": "gopkg.in/urfave/cli.v2", - "branch": "v2", - "revision": "04b2f4ff79cf1fd71e138bafc67df8bbdb5b81c2", - "packages": [ - "." - ] - }, - { - "name": "gopkg.in/yaml.v2", - "branch": "v2", - "revision": "cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b", - "packages": [ - "." - ] - } - ] -} diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 8821db0..0000000 --- a/manifest.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "dependencies": { - "github.com/fatih/color": { - "branch": "master" - }, - "github.com/fsnotify/fsnotify": { - "branch": "master" - }, - "github.com/labstack/echo": { - "version": "^3.0.3" - }, - "github.com/tockins/interact": { - "branch": "master" - }, - "golang.org/x/net": { - "branch": "master" - }, - "gopkg.in/urfave/cli.v2": { - "branch": "v2" - }, - "gopkg.in/yaml.v2": { - "branch": "v2" - } - } -} From d4d9addada702ab45149e5d905ae71fe09016465 Mon Sep 17 00:00:00 2001 From: Nikola Kovacs Date: Tue, 13 Jun 2017 15:57:26 +0200 Subject: [PATCH 08/15] Builded -> built --- watcher/watcher.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/watcher/watcher.go b/watcher/watcher.go index b7e16ba..fc5c54d 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -270,8 +270,8 @@ func (p *Project) build() error { out = BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Build", Stream: stream} p.print("error", out, msg, stream) } else { - msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Builded")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) - out = BufferOut{Time: time.Now(), Text: "Builded after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} + msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Built")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) + out = BufferOut{Time: time.Now(), Text: "Built after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} p.print("log", out, msg, stream) } return err From 7aa4fef10f50f0d78bf4ff5fd3ae607ecc1a6b99 Mon Sep 17 00:00:00 2001 From: Nikola Kovacs Date: Tue, 13 Jun 2017 16:06:37 +0200 Subject: [PATCH 09/15] Has been run -> started This makes it clearer that the process did not finish running, and matches "Ended". --- watcher/watcher.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/watcher/watcher.go b/watcher/watcher.go index b7e16ba..007e86a 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -250,8 +250,8 @@ func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { for { select { case <-runner: - msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Has been run")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) - out = BufferOut{Time: time.Now(), Text: "Has been run after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} + msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Started")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) + out = BufferOut{Time: time.Now(), Text: "Started after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} p.print("log", out, msg, "") return } From e2c06878f576337b1fab52317f2cd0211cb22ded Mon Sep 17 00:00:00 2001 From: alessio Date: Fri, 16 Jun 2017 11:15:56 +0200 Subject: [PATCH 10/15] #58 fixed --- realize.go | 16 ++++++++++++---- settings/settings.go | 1 - watcher/cmd.go | 8 ++++---- watcher/exec.go | 2 +- watcher/main.go | 26 +++++++++++++------------- watcher/watcher.go | 10 +++++----- 6 files changed, 35 insertions(+), 28 deletions(-) diff --git a/realize.go b/realize.go index c925635..24073fd 100644 --- a/realize.go +++ b/realize.go @@ -80,7 +80,6 @@ func main() { if err := r.Read(&r); err != nil { return err } - // increase the file limit if r.Config.Flimit != 0 { if err := r.Flimit(); err != nil { @@ -111,6 +110,7 @@ func main() { Description: "Run a toolchain on a project or a list of projects. If not exist a config file it creates a new one", Flags: []cli.Flag{ &cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: "", Usage: "Project base path."}, + &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "", Usage: "Run a project by its name."}, &cli.BoolFlag{Name: "test", Aliases: []string{"t"}, Value: false, Usage: "Enable go test."}, &cli.BoolFlag{Name: "generate", Aliases: []string{"g"}, Value: false, Usage: "Enable go generate."}, &cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build."}, @@ -121,6 +121,14 @@ func main() { &cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Ignore existing configurations."}, }, Action: func(p *cli.Context) error { + c := r + if p.String("name") != ""{ + for index, project := range r.Blueprint.Projects{ + if project.Name == p.String("name"){ + c.Blueprint.Projects = []watcher.Project{r.Blueprint.Projects[index]} + } + } + } if p.Bool("legacy") { r.Config.Legacy = settings.Legacy{ Status: p.Bool("legacy"), @@ -136,13 +144,13 @@ func main() { return err } } - if err := r.Server.Start(p); err != nil { + if err := c.Server.Start(p); err != nil { return err } - if err := r.Blueprint.Run(); err != nil { + if err := c.Blueprint.Run(p); err != nil { return err } - if err := r.Record(r); err != nil { + if err := r.Record(c); err != nil { return err } return nil diff --git a/settings/settings.go b/settings/settings.go index 41a4054..21e56ae 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -3,7 +3,6 @@ package settings import ( "os" "time" - yaml "gopkg.in/yaml.v2" ) diff --git a/watcher/cmd.go b/watcher/cmd.go index a67d6e7..46d7c02 100644 --- a/watcher/cmd.go +++ b/watcher/cmd.go @@ -9,14 +9,14 @@ import ( ) // Run launches the toolchain for each project -func (h *Blueprint) Run() error { +func (h *Blueprint) Run(p *cli.Context) error { err := h.check() if err == nil { // loop projects wg.Add(len(h.Projects)) for k, element := range h.Projects { tools := tools{} - if element.Cmds.Fmt { + if element.Cmds.Fmt{ tools.Fmt = tool{ status: &h.Projects[k].Cmds.Fmt, cmd: "gofmt", @@ -24,7 +24,7 @@ func (h *Blueprint) Run() error { name: "Go Fmt", } } - if element.Cmds.Generate { + if element.Cmds.Generate{ tools.Generate = tool{ status: &h.Projects[k].Cmds.Generate, cmd: "go", @@ -32,7 +32,7 @@ func (h *Blueprint) Run() error { name: "Go Generate", } } - if element.Cmds.Test { + if element.Cmds.Test{ tools.Test = tool{ status: &h.Projects[k].Cmds.Test, cmd: "go", diff --git a/watcher/exec.go b/watcher/exec.go index 850f7b4..c8e0e3f 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "github.com/tockins/realize/style" "log" "os" "os/exec" @@ -12,7 +13,6 @@ import ( "strings" "sync" "time" - "github.com/tockins/realize/style" ) // GoRun is an implementation of the bin execution diff --git a/watcher/main.go b/watcher/main.go index 3474e26..3a4c3bc 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -31,20 +31,20 @@ type Blueprint struct { // Project defines the informations of a single project type Project struct { - settings.Settings `yaml:"-"` - LastChangedOn time.Time `yaml:"-" json:"-"` - base string - Name string `yaml:"name" json:"name"` - Path string `yaml:"path" json:"path"` - Cmds Cmds `yaml:"cmds" json:"cmds"` - Args []string `yaml:"args,omitempty" json:"args,omitempty"` - Watcher Watcher `yaml:"watcher" json:"watcher"` - Streams Streams `yaml:"streams" json:"streams"` - Buffer Buffer `yaml:"-" json:"buffer"` + settings.Settings `yaml:"-"` + LastChangedOn time.Time `yaml:"-" json:"-"` + base string + Name string `yaml:"name" json:"name"` + Path string `yaml:"path" json:"path"` + Cmds Cmds `yaml:"commands" json:"commands"` + Args []string `yaml:"args,omitempty" json:"args,omitempty"` + Watcher Watcher `yaml:"watcher" json:"watcher"` + Streams Streams `yaml:"streams" json:"streams"` + Buffer Buffer `yaml:"-" json:"buffer"` ErrorOutputPattern string `yaml:"errorOutputPattern" json:"errorOutputPattern"` - parent *Blueprint - path string - tools tools + parent *Blueprint + path string + tools tools } type tools struct { diff --git a/watcher/watcher.go b/watcher/watcher.go index e7425c1..0e9f2be 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -3,19 +3,19 @@ package watcher import ( "errors" "fmt" + "github.com/fsnotify/fsnotify" + "github.com/tockins/realize/style" "log" "math/big" "os" "os/signal" "path/filepath" + "reflect" "strconv" "strings" "sync" "syscall" "time" - "reflect" - "github.com/fsnotify/fsnotify" - "github.com/tockins/realize/style" ) var msg string @@ -285,8 +285,8 @@ func (p *Project) build() error { func (p *Project) tool(path string, tool tool) error { if tool.status != nil { v := reflect.ValueOf(tool.status).Elem() - if v.Interface().(bool) && (strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "")) { - if strings.HasSuffix(path, ".go"){ + if v.Interface().(bool) && (strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "")) { + if strings.HasSuffix(path, ".go") { tool.options = append(tool.options, path) path = p.base } From f2f11c7e476309db65e21717dadd2f87b7906998 Mon Sep 17 00:00:00 2001 From: alessio Date: Fri, 16 Jun 2017 11:48:25 +0200 Subject: [PATCH 11/15] error output pattern added to init cmd --- realize.go | 18 ++++++++++++++++++ watcher/cmd.go | 2 -- watcher/main.go | 5 ++--- watcher/watcher.go | 3 --- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/realize.go b/realize.go index 24073fd..e67d5a2 100644 --- a/realize.go +++ b/realize.go @@ -869,6 +869,24 @@ func main() { return nil }, }, + { + Before: func(d interact.Context) error { + d.SetDef("", style.Green.Regular("(none)")) + return nil + }, + Quest: interact.Quest{ + Options: style.Yellow.Regular("[string]"), + Msg: "Set an error output pattern", + }, + Action: func(d interact.Context) interface{} { + val, err := d.Ans().String() + if err != nil { + return d.Err() + } + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].ErrorOutputPattern = val + return nil + }, + }, }, Action: func(d interact.Context) interface{} { if val, err := d.Ans().Bool(); err != nil { diff --git a/watcher/cmd.go b/watcher/cmd.go index 46d7c02..4693235 100644 --- a/watcher/cmd.go +++ b/watcher/cmd.go @@ -91,7 +91,6 @@ func (h *Blueprint) Add(p *cli.Context) error { Scripts: []Command{}, }, Streams: Streams{ - CliOut: true, FileOut: false, FileLog: false, FileErr: false, @@ -170,7 +169,6 @@ func (h *Blueprint) List() error { } } fmt.Println(name, style.Yellow.Regular("Streams"), ":") - fmt.Println(name, "\t", style.Yellow.Regular("Cli Out"), ":", style.Magenta.Regular(val.Streams.CliOut)) 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)) diff --git a/watcher/main.go b/watcher/main.go index 3a4c3bc..2041862 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -39,9 +39,9 @@ type Project struct { Cmds Cmds `yaml:"commands" json:"commands"` Args []string `yaml:"args,omitempty" json:"args,omitempty"` Watcher Watcher `yaml:"watcher" json:"watcher"` - Streams Streams `yaml:"streams" json:"streams"` + Streams Streams `yaml:"streams,omitempty" json:"streams,omitempty"` Buffer Buffer `yaml:"-" json:"buffer"` - ErrorOutputPattern string `yaml:"errorOutputPattern" json:"errorOutputPattern"` + ErrorOutputPattern string `yaml:"errorOutputPattern,omitempty" json:"errorOutputPattern,omitempty"` parent *Blueprint path string tools tools @@ -92,7 +92,6 @@ type Command struct { // Streams is a collection of names and values for the logs functionality type Streams struct { - CliOut bool `yaml:"cli_out" json:"cli_out"` FileOut bool `yaml:"file_out" json:"file_out"` FileLog bool `yaml:"file_log" json:"file_log"` FileErr bool `yaml:"file_err" json:"file_err"` diff --git a/watcher/watcher.go b/watcher/watcher.go index 0e9f2be..2dc7fd7 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -384,9 +384,6 @@ func (p *Project) print(t string, o BufferOut, msg string, stream string) { p.Fatal(err, "") } } - if msg != "" && p.Streams.CliOut { - log.Print(msg) - } case "log": p.Buffer.StdLog = append(p.Buffer.StdLog, o) if p.Streams.FileLog { From f4547b9e873c57e93c6ad44d11d5227f98bd30ae Mon Sep 17 00:00:00 2001 From: alessio Date: Fri, 16 Jun 2017 16:35:00 +0200 Subject: [PATCH 12/15] #56 fixed --- realize.go | 4 ++-- watcher/cmd.go | 6 ------ watcher/main.go | 1 + watcher/watcher.go | 54 +++++++++++++++++++++------------------------- 4 files changed, 28 insertions(+), 37 deletions(-) diff --git a/realize.go b/realize.go index e67d5a2..85e5e9c 100644 --- a/realize.go +++ b/realize.go @@ -741,7 +741,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val}) + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val, Changed: true}) d.Reload() return nil }, @@ -783,7 +783,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val}) + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val, Changed: true}) d.Reload() return nil }, diff --git a/watcher/cmd.go b/watcher/cmd.go index 4693235..8e367aa 100644 --- a/watcher/cmd.go +++ b/watcher/cmd.go @@ -88,12 +88,6 @@ func (h *Blueprint) Add(p *cli.Context) error { Ignore: []string{"vendor"}, Exts: []string{".go"}, Preview: p.Bool("preview"), - Scripts: []Command{}, - }, - Streams: Streams{ - FileOut: false, - FileLog: false, - FileErr: false, }, } if _, err := duplicates(project, h.Projects); err != nil { diff --git a/watcher/main.go b/watcher/main.go index 2041862..9b53cd0 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -88,6 +88,7 @@ type Command struct { Type string `yaml:"type" json:"type"` Command string `yaml:"command" json:"command"` Path string `yaml:"path" json:"path"` + Changed bool `yaml:"refresh" json:"refresh"` } // Streams is a collection of names and values for the logs functionality diff --git a/watcher/watcher.go b/watcher/watcher.go index 2dc7fd7..9e92f54 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -21,6 +21,7 @@ import ( var msg string var out BufferOut +// Add a path to paths list func (w *pollWatcher) Add(path string) error { if w.paths == nil { w.paths = map[string]bool{} @@ -29,6 +30,7 @@ func (w *pollWatcher) Add(path string) error { return nil } +// Check if is watching func (w *pollWatcher) isWatching(path string) bool { a, b := w.paths[path] return a && b @@ -41,13 +43,9 @@ func (p *Project) watchByPolling() { channel, exit := make(chan bool, 1), make(chan os.Signal, 2) signal.Notify(exit, os.Interrupt, syscall.SIGTERM) defer func() { - p.cmd("after") wg.Done() }() - - p.cmd("before") - p.Fatal(p.watch(watcher)) - go p.routines(channel, &wr) + go p.routines(&wr, channel, watcher, "") p.LastChangedOn = time.Now().Truncate(time.Second) walk := func(changed string, info os.FileInfo, err error) error { var ext string @@ -65,7 +63,6 @@ func (p *Project) watchByPolling() { } i := strings.Index(changed, filepath.Ext(changed)) file := changed[:i] + ext - path := filepath.Dir(changed[:i]) if changed[:i] != "" && inArray(ext, p.Watcher.Exts) { if p.Cmds.Run { close(channel) @@ -76,13 +73,7 @@ func (p *Project) watchByPolling() { msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file)) out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file} p.print("log", out, msg, "") - - p.cmd("change") - p.tool(file, p.tools.Fmt) - p.tool(file, p.tools.Vet) - p.tool(path, p.tools.Test) - p.tool(path, p.tools.Generate) - go p.routines(channel, &wr) + go p.routines(&wr, channel, watcher, file) } return nil } @@ -118,13 +109,9 @@ func (p *Project) watchByNotify() { watcher, err := fsnotify.NewWatcher() p.Fatal(err) defer func() { - p.cmd("after") wg.Done() }() - - p.cmd("before") - p.Fatal(p.watch(watcher)) - go p.routines(channel, &wr) + go p.routines(&wr, channel, watcher, "") p.LastChangedOn = time.Now().Truncate(time.Second) for { select { @@ -142,7 +129,6 @@ func (p *Project) watchByNotify() { } i := strings.Index(event.Name, filepath.Ext(event.Name)) file := event.Name[:i] + ext - path := filepath.Dir(event.Name[:i]) if event.Name[:i] != "" && inArray(ext, p.Watcher.Exts) { if p.Cmds.Run { close(channel) @@ -152,13 +138,7 @@ func (p *Project) watchByNotify() { msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file)) out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file} p.print("log", out, msg, "") - - p.cmd("change") - p.tool(file, p.tools.Fmt) - p.tool(path, p.tools.Vet) - p.tool(path, p.tools.Test) - p.tool(path, p.tools.Generate) - go p.routines(channel, &wr) + go p.routines(&wr, channel, watcher, file) p.LastChangedOn = time.Now().Truncate(time.Second) } } @@ -302,9 +282,9 @@ func (p *Project) tool(path string, tool tool) error { } // Cmd calls an wrapper for execute the commands after/before -func (p *Project) cmd(flag string) { +func (p *Project) cmd(flag string, changed bool) { for _, cmd := range p.Watcher.Scripts { - if strings.ToLower(cmd.Type) == flag { + if strings.ToLower(cmd.Type) == flag && changed == cmd.Changed { errors, logs := p.command(cmd) 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} @@ -339,7 +319,18 @@ func (p *Project) ignore(str string) bool { } // Routines launches the toolchain run, build, install -func (p *Project) routines(channel chan bool, wr *sync.WaitGroup) { +func (p *Project) routines(wr *sync.WaitGroup,channel chan bool, watcher watcher, file string) { + if len(file) > 0{ + p.cmd("before", true) + path := filepath.Dir(file) + p.tool(file, p.tools.Fmt) + p.tool(path, p.tools.Vet) + p.tool(path, p.tools.Test) + p.tool(path, p.tools.Generate) + }else{ + p.cmd("before", false) + p.Fatal(p.watch(watcher)) + } install := p.install() build := p.build() wr.Add(1) @@ -347,6 +338,11 @@ func (p *Project) routines(channel chan bool, wr *sync.WaitGroup) { go p.run(channel, wr) } wr.Wait() + if len(file) > 0 { + p.cmd("after", true) + }else{ + p.cmd("after", false) + } } // Defines the colors scheme for the project name From 979cb3da36b8cf6314d4a68326cb3c4e48db93eb Mon Sep 17 00:00:00 2001 From: Janis Meybohm Date: Mon, 19 Jun 2017 11:29:30 +0200 Subject: [PATCH 13/15] Support multiple values in GOPATH/GOBIN realize generates wrong GOBIN path in case one has multiple paths in GOPATH (like "/go/foo:/go"). --- watcher/exec.go | 10 +++++----- watcher/utils.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/watcher/exec.go b/watcher/exec.go index 2797c88..21a2302 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -34,10 +34,10 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) if path != "" { build = exec.Command(path, params...) } else { - if _, err := os.Stat(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path))); err == nil { - build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)), params...) - } else if _, err := os.Stat(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)) + ".exe"); err == nil { - build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path))+".exe", params...) + 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)), params...) + } 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", params...) } else { 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", ":") @@ -116,7 +116,7 @@ func (p *Project) goBuild() (string, error) { func (p *Project) goInstall() (string, error) { var out bytes.Buffer var stderr bytes.Buffer - err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin")) + err := os.Setenv("GOBIN", filepath.Join(getEnvPath("GOPATH"), "bin")) if err != nil { return "", err } diff --git a/watcher/utils.go b/watcher/utils.go index 9535a2d..4509080 100644 --- a/watcher/utils.go +++ b/watcher/utils.go @@ -3,6 +3,8 @@ package watcher import ( "errors" "fmt" + "os" + "path/filepath" "time" "github.com/tockins/realize/style" @@ -46,3 +48,13 @@ func inArray(str string, list []string) bool { func (w logWriter) Write(bytes []byte) (int, error) { return fmt.Print(style.Yellow.Regular("[") + time.Now().Format("15:04:05") + style.Yellow.Regular("]") + string(bytes)) } + +// getEnvPath returns the first path found in env or empty string +func getEnvPath(env string) string { + path := filepath.SplitList(os.Getenv(env)) + if len(path) == 0 { + return "" + } else { + return path[0] + } +} From fb8c09c7989033293b115272b515753b7cce7b68 Mon Sep 17 00:00:00 2001 From: alessio Date: Mon, 19 Jun 2017 12:08:51 +0200 Subject: [PATCH 14/15] #56 fixed --- realize.go | 4 ++-- watcher/main.go | 5 +++-- watcher/watcher.go | 49 ++++++++++++++++++++++++---------------------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/realize.go b/realize.go index 85e5e9c..dfcd376 100644 --- a/realize.go +++ b/realize.go @@ -741,7 +741,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val, Changed: true}) + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val, Changed: true, Startup: true}) d.Reload() return nil }, @@ -783,7 +783,7 @@ func main() { if err != nil { return d.Err() } - r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val, Changed: true}) + r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val, Changed: true, Startup: true}) d.Reload() return nil }, diff --git a/watcher/main.go b/watcher/main.go index 9b53cd0..07c42c7 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -87,8 +87,9 @@ type Watcher struct { type Command struct { Type string `yaml:"type" json:"type"` Command string `yaml:"command" json:"command"` - Path string `yaml:"path" json:"path"` - Changed bool `yaml:"refresh" json:"refresh"` + Path string `yaml:"path,omitempty" json:"path,omitempty"` + Changed bool `yaml:"changed,omitempty" json:"changed,omitempty"` + Startup bool `yaml:"startup,omitempty" json:"startup,omitempty"` } // Streams is a collection of names and values for the logs functionality diff --git a/watcher/watcher.go b/watcher/watcher.go index 9e92f54..6954586 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -93,6 +93,7 @@ func (p *Project) watchByPolling() { } select { case <-exit: + p.cmd("after", false) return case <-time.After(p.parent.Legacy.Interval / time.Duration(len(p.Watcher.Paths))): } @@ -148,6 +149,7 @@ func (p *Project) watchByNotify() { out = BufferOut{Time: time.Now(), Text: err.Error()} p.print("error", out, msg, "") case <-exit: + p.cmd("after", false) return } } @@ -284,25 +286,27 @@ func (p *Project) tool(path string, tool tool) error { // Cmd calls an wrapper for execute the commands after/before func (p *Project) cmd(flag string, changed bool) { for _, cmd := range p.Watcher.Scripts { - if strings.ToLower(cmd.Type) == flag && changed == cmd.Changed { - errors, logs := p.command(cmd) - 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} - if logs != "" { - p.print("log", out, msg, "") - } - if errors != "" { - p.print("error", out, msg, "") - } - if logs != "" { - msg = fmt.Sprintln(logs) - out = BufferOut{Time: time.Now(), Text: logs, Type: flag} - p.print("log", out, "", msg) - } - if errors != "" { - msg = fmt.Sprintln(style.Red.Regular(errors)) - out = BufferOut{Time: time.Now(), Text: errors, Type: flag} - p.print("error", out, "", msg) + if strings.ToLower(cmd.Type) == flag{ + if changed && cmd.Changed || !changed && cmd.Startup { + errors, logs := p.command(cmd) + 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} + if logs != "" { + p.print("log", out, msg, "") + } + if errors != "" { + p.print("error", out, msg, "") + } + if logs != "" { + msg = fmt.Sprintln(logs) + out = BufferOut{Time: time.Now(), Text: logs, Type: flag} + p.print("log", out, "", msg) + } + if errors != "" { + msg = fmt.Sprintln(style.Red.Regular(errors)) + out = BufferOut{Time: time.Now(), Text: errors, Type: flag} + p.print("error", out, "", msg) + } } } } @@ -320,14 +324,14 @@ func (p *Project) ignore(str string) bool { // Routines launches the toolchain run, build, install func (p *Project) routines(wr *sync.WaitGroup,channel chan bool, watcher watcher, file string) { - if len(file) > 0{ + if len(file) > 0 { p.cmd("before", true) path := filepath.Dir(file) p.tool(file, p.tools.Fmt) p.tool(path, p.tools.Vet) p.tool(path, p.tools.Test) p.tool(path, p.tools.Generate) - }else{ + } else { p.cmd("before", false) p.Fatal(p.watch(watcher)) } @@ -340,9 +344,8 @@ func (p *Project) routines(wr *sync.WaitGroup,channel chan bool, watcher watcher wr.Wait() if len(file) > 0 { p.cmd("after", true) - }else{ - p.cmd("after", false) } + } // Defines the colors scheme for the project name From 424a659bc5a79fa9ace2c54dfc66ee68cf2c6901 Mon Sep 17 00:00:00 2001 From: alessio Date: Mon, 19 Jun 2017 12:13:21 +0200 Subject: [PATCH 15/15] fixed pull #61 --- watcher/exec.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/watcher/exec.go b/watcher/exec.go index 7792228..70d658f 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -48,9 +48,9 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) build = exec.Command(path, args...) } else { 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)), params...) + 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", params...) + build = exec.Command(filepath.Join(getEnvPath("GOBIN"), filepath.Base(p.path))+".exe", args...) } else { 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", ":")