From bfff06d38a36b8da10707eb60e1226c263ace198 Mon Sep 17 00:00:00 2001 From: asoseil Date: Sun, 22 Oct 2017 23:22:45 +0200 Subject: [PATCH] ignore hidden dir support --- cmd.go | 258 ++++++++++++++++++++++++++--------------------------- exec.go | 1 + realize.go | 11 +-- utils.go | 10 --- watcher.go | 13 +-- 5 files changed, 137 insertions(+), 156 deletions(-) diff --git a/cmd.go b/cmd.go index 83250a3..958a69b 100644 --- a/cmd.go +++ b/cmd.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "time" + "reflect" ) // Tool options customizable, should be moved in Cmd @@ -50,15 +51,6 @@ func (r *realize) clean() { } } -// Check whether there is a project -func (r *realize) check() error { - if len(r.Schema) > 0 { - r.clean() - return nil - } - return errors.New("there are no projects") -} - // Add a new project func (r *realize) add(p *cli.Context) error { project := Project{ @@ -88,7 +80,7 @@ func (r *realize) add(p *cli.Context) error { Args: params(p), Watcher: Watch{ Paths: []string{"/"}, - Ignore: []string{"vendor"}, + Ignore: []string{".git",".realize","vendor"}, Exts: []string{"go"}, }, } @@ -102,127 +94,133 @@ func (r *realize) add(p *cli.Context) error { // Run launches the toolchain for each project func (r *realize) run(p *cli.Context) error { var match bool - err := r.check() - if err == nil { - // loop projects - if p.String("name") != "" { - wg.Add(1) - } else { - wg.Add(len(r.Schema)) - } - for k, elm := range r.Schema { - // command start using name flag - if p.String("name") != "" && r.Schema[k].Name != p.String("name") { - continue - } - //fields := reflect.Indirect(reflect.ValueOf(&r.Schema[k].Cmds)) - //// Loop struct Cmds fields - //for i := 0; i < fields.NumField(); i++ { - // field := fields.Type().Field(i).Name - // if fields.FieldByName(field).Type().Name() == "Cmd" { - // v := fields.FieldByName(field) - // // Loop struct Cmd - // for i := 0; i < v.NumField(); i++ { - // f := v.Field(i) - // if f.IsValid() { - // if f.CanSet() { - // switch f.Kind() { - // case reflect.Bool: - // case reflect.String: - // case reflect.Slice: - // } - // } - // } - // } - // } - //} - if elm.Cmds.Fmt.Status { - if len(elm.Cmds.Fmt.Args) == 0 { - elm.Cmds.Fmt.Args = []string{"-s", "-w", "-e", "./"} - } - r.Schema[k].tools = append(r.Schema[k].tools, tool{ - status: elm.Cmds.Fmt.Status, - cmd: replace([]string{"gofmt"}, r.Schema[k].Cmds.Fmt.Method), - options: split([]string{}, elm.Cmds.Fmt.Args), - name: "Fmt", - }) - } - if elm.Cmds.Generate.Status { - r.Schema[k].tools = append(r.Schema[k].tools, tool{ - status: elm.Cmds.Generate.Status, - cmd: replace([]string{"go", "generate"}, r.Schema[k].Cmds.Generate.Method), - options: split([]string{}, elm.Cmds.Generate.Args), - name: "Generate", - dir: true, - }) - } - if elm.Cmds.Test.Status { - r.Schema[k].tools = append(r.Schema[k].tools, tool{ - status: elm.Cmds.Test.Status, - cmd: replace([]string{"go", "test"}, r.Schema[k].Cmds.Test.Method), - options: split([]string{}, elm.Cmds.Test.Args), - name: "Test", - dir: true, - }) - } - if elm.Cmds.Vet.Status { - r.Schema[k].tools = append(r.Schema[k].tools, tool{ - status: elm.Cmds.Vet.Status, - cmd: replace([]string{"go", "vet"}, r.Schema[k].Cmds.Vet.Method), - options: split([]string{}, elm.Cmds.Vet.Args), - name: "Vet", - dir: true, - }) - } - // default settings - r.Schema[k].Cmds.Install = Cmd{ - Status: elm.Cmds.Install.Status, - Args: append([]string{}, elm.Cmds.Install.Args...), - method: replace([]string{"go", "install"}, r.Schema[k].Cmds.Install.Method), - name: "Install", - startTxt: "Installing...", - endTxt: "Installed", - } - r.Schema[k].Cmds.Build = Cmd{ - Status: elm.Cmds.Build.Status, - Args: append([]string{}, elm.Cmds.Build.Args...), - method: replace([]string{"go", "build"}, r.Schema[k].Cmds.Build.Method), - name: "Build", - startTxt: "Building...", - endTxt: "Built", - } - - r.Schema[k].parent = r - r.Schema[k].path = r.Schema[k].Path - - // env variables - for key, item := range r.Schema[k].Environment { - if err := os.Setenv(key, item); err != nil { - r.Schema[k].Buffer.StdErr = append(r.Schema[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""}) - } - } - - // base path of the project - wd, err := os.Getwd() - if err != nil { - return err - } - if elm.path == "." || elm.path == "/" { - r.Schema[k].base = wd - r.Schema[k].path = elm.wdir() - } else if filepath.IsAbs(elm.path) { - r.Schema[k].base = elm.path - } else { - r.Schema[k].base = filepath.Join(wd, elm.path) - } - match = true - go r.Schema[k].watch() - } - if !match { - return errors.New("there is no project with the given name") - } - wg.Wait() + // check projects and remove duplicates + if len(r.Schema) > 0 { + r.clean() + }else{ + return errors.New("there are no projects") } + // set gobin + err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin")) + if err != nil { + return err + } + // loop projects + if p.String("name") != "" { + wg.Add(1) + } else { + wg.Add(len(r.Schema)) + } + for k, elm := range r.Schema { + // command start using name flag + if p.String("name") != "" && r.Schema[k].Name != p.String("name") { + continue + } + // validate project path, if invalid get wdir or clean current + if !filepath.IsAbs(elm.path){ + r.Schema[k].path = wdir() + }else{ + r.Schema[k].path = filepath.Clean(elm.path) + } + // env variables + for key, item := range r.Schema[k].Environment { + if err := os.Setenv(key, item); err != nil { + r.Schema[k].Buffer.StdErr = append(r.Schema[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""}) + } + } + // get basepath name + r.Schema[k].name = filepath.Base(r.Schema[k].path) + + fields := reflect.Indirect(reflect.ValueOf(&r.Schema[k].Cmds)) + // Loop struct Cmds fields + for i := 0; i < fields.NumField(); i++ { + field := fields.Type().Field(i).Name + if fields.FieldByName(field).Type().Name() == "Cmd" { + v := fields.FieldByName(field) + // Loop struct Cmd + for i := 0; i < v.NumField(); i++ { + //f := v.Type().Field(i).Name + //fmt.Println(f) + //if f.IsValid() { + // if f.CanSet() { + // fmt.Println(f.) + // //switch f.Kind() { + // //case reflect.Bool: + // //case reflect.String: + // //case reflect.Slice: + // //} + // } + //} + } + } + } + + if elm.Cmds.Fmt.Status { + if len(elm.Cmds.Fmt.Args) == 0 { + elm.Cmds.Fmt.Args = []string{"-s", "-w", "-e", "./"} + } + r.Schema[k].tools = append(r.Schema[k].tools, tool{ + status: elm.Cmds.Fmt.Status, + cmd: replace([]string{"gofmt"}, r.Schema[k].Cmds.Fmt.Method), + options: split([]string{}, elm.Cmds.Fmt.Args), + name: "Fmt", + }) + } + if elm.Cmds.Generate.Status { + r.Schema[k].tools = append(r.Schema[k].tools, tool{ + status: elm.Cmds.Generate.Status, + cmd: replace([]string{"go", "generate"}, r.Schema[k].Cmds.Generate.Method), + options: split([]string{}, elm.Cmds.Generate.Args), + name: "Generate", + dir: true, + }) + } + if elm.Cmds.Test.Status { + r.Schema[k].tools = append(r.Schema[k].tools, tool{ + status: elm.Cmds.Test.Status, + cmd: replace([]string{"go", "test"}, r.Schema[k].Cmds.Test.Method), + options: split([]string{}, elm.Cmds.Test.Args), + name: "Test", + dir: true, + }) + } + if elm.Cmds.Vet.Status { + r.Schema[k].tools = append(r.Schema[k].tools, tool{ + status: elm.Cmds.Vet.Status, + cmd: replace([]string{"go", "vet"}, r.Schema[k].Cmds.Vet.Method), + options: split([]string{}, elm.Cmds.Vet.Args), + name: "Vet", + dir: true, + }) + } + // default settings + r.Schema[k].Cmds.Install = Cmd{ + Status: elm.Cmds.Install.Status, + Args: append([]string{}, elm.Cmds.Install.Args...), + method: replace([]string{"go", "install"}, r.Schema[k].Cmds.Install.Method), + name: "Install", + startTxt: "Installing...", + endTxt: "Installed", + } + r.Schema[k].Cmds.Build = Cmd{ + Status: elm.Cmds.Build.Status, + Args: append([]string{}, elm.Cmds.Build.Args...), + method: replace([]string{"go", "build"}, r.Schema[k].Cmds.Build.Method), + name: "Build", + startTxt: "Building...", + endTxt: "Built", + } + + r.Schema[k].parent = r + r.Schema[k].path = r.Schema[k].Path + + match = true + go r.Schema[k].watch() + } + if !match { + return errors.New("there is no project with the given name") + } + wg.Wait() return err } diff --git a/exec.go b/exec.go index 1397ecc..1e3d88e 100644 --- a/exec.go +++ b/exec.go @@ -70,6 +70,7 @@ func (p *Project) goRun(stop <-chan bool, runner chan bool) { args = append(args, a...) } + gobin := os.Getenv("GOBIN") path := filepath.Join(gobin, p.name) if _, err := os.Stat(path); err == nil { diff --git a/realize.go b/realize.go index 552f5c1..f12df99 100644 --- a/realize.go +++ b/realize.go @@ -344,7 +344,7 @@ func main() { Subs: []*interact.Question{ { Before: func(d interact.Context) error { - d.SetDef(r.Settings.wdir(), green.regular("("+r.Settings.wdir()+")")) + d.SetDef(wdir(), green.regular("("+wdir()+")")) return nil }, Quest: interact.Quest{ @@ -1144,15 +1144,6 @@ func before(*cli.Context) error { return nil } -// Wdir return current working directory -func wdir() string { - dir, err := os.Getwd() - if err != nil { - log.Fatal(prefix(err.Error())) - } - return dir -} - // Rewrite the layout of the log timestamp func (w logWriter) Write(bytes []byte) (int, error) { return fmt.Fprint(output, yellow.regular("["), time.Now().Format("15:04:05"), yellow.regular("]"), string(bytes)) diff --git a/utils.go b/utils.go index 79c94d8..2783d26 100644 --- a/utils.go +++ b/utils.go @@ -4,20 +4,10 @@ import ( "errors" "gopkg.in/urfave/cli.v2" "os" - "path/filepath" "strings" "log" ) -// 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 "" - } - return path[0] -} - // Array check if a string is in given array func array(str string, list []string) bool { for _, v := range list { diff --git a/watcher.go b/watcher.go index 53de11b..dd161ea 100644 --- a/watcher.go +++ b/watcher.go @@ -55,7 +55,7 @@ type Project struct { watcher FileWatcher init bool files, folders int64 - base, path, lastFile string + name, path, lastFile string tools []tool paths []string lastTime time.Time @@ -98,7 +98,7 @@ func (p *Project) watch() { p.cmd(stop, "before", true) // indexing files and dirs for _, dir := range p.Watcher.Paths { - base := filepath.Join(p.base, dir) + base := filepath.Join(p.path, dir) if _, err := os.Stat(base); err == nil { if err := filepath.Walk(base, p.walk); err == nil { p.tool(stop, base) @@ -312,7 +312,8 @@ func (p *Project) changed(event fsnotify.Event, stop chan bool) { // Watch the files tree of a project func (p *Project) walk(path string, info os.FileInfo, err error) error { for _, v := range p.Watcher.Ignore { - if strings.Contains(path, filepath.Join(p.base, v)) { + s := append([]string{p.path},strings.Split(v,string(os.PathSeparator))...) + if strings.Contains(path, filepath.Join(s...)) { return nil } } @@ -338,7 +339,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { case "out": p.Buffer.StdOut = append(p.Buffer.StdOut, o) if p.Files.Outputs.Status { - f := p.create(p.base, p.Files.Outputs.Name) + f := p.create(p.path, p.Files.Outputs.Name) t := time.Now() 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 { @@ -348,7 +349,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { case "log": p.Buffer.StdLog = append(p.Buffer.StdLog, o) if p.Files.Logs.Status { - f := p.create(p.base, p.Files.Logs.Name) + f := p.create(p.path, p.Files.Logs.Name) t := time.Now() s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"} if stream != "" { @@ -361,7 +362,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { case "error": p.Buffer.StdErr = append(p.Buffer.StdErr, o) if p.Files.Errors.Status { - f := p.create(p.base, p.Files.Errors.Name) + f := p.create(p.path, p.Files.Errors.Name) 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"} if stream != "" {