package watcher import ( "errors" "fmt" "github.com/tockins/realize/style" cli "gopkg.in/urfave/cli.v2" "os" "path/filepath" "strings" "time" ) // Clean duplicate projects func (h *Blueprint) Clean() { arr := h.Projects for key, val := range arr { if _, err := duplicates(val, arr[key+1:]); err != nil { h.Projects = append(arr[:key], arr[key+1:]...) break } } } // List of all the projects func (h *Blueprint) List() error { err := h.check() if err == nil { for _, val := range h.Projects { fmt.Fprintln(style.Output, style.Blue.Bold("[")+strings.ToUpper(val.Name)+style.Blue.Bold("]")) name := style.Magenta.Bold("[") + strings.ToUpper(val.Name) + style.Magenta.Bold("]") fmt.Fprintln(style.Output, name, style.Yellow.Regular("Base Path"), ":", style.Magenta.Regular(val.Path)) fmt.Fprintln(style.Output, name, style.Yellow.Regular("Fmt"), ":", style.Magenta.Regular(val.Cmds.Fmt)) fmt.Fprintln(style.Output, name, style.Yellow.Regular("Generate"), ":", style.Magenta.Regular(val.Cmds.Generate)) fmt.Fprintln(style.Output, name, style.Yellow.Regular("Test"), ":", style.Magenta.Regular(val.Cmds.Test)) fmt.Fprintln(style.Output, name, style.Yellow.Regular("Install"), ":", style.Magenta.Regular(val.Cmds.Install)) fmt.Fprintln(style.Output, name, style.Yellow.Regular("Build"), ":", style.Magenta.Regular(val.Cmds.Build)) fmt.Fprintln(style.Output, name, style.Yellow.Regular("Run"), ":", style.Magenta.Regular(val.Cmds.Run)) if len(val.Args) > 0 { fmt.Fprintln(style.Output, name, style.Yellow.Regular("Params"), ":", style.Magenta.Regular(val.Args)) } fmt.Fprintln(style.Output, name, style.Yellow.Regular("Watcher"), ":") fmt.Fprintln(style.Output, name, "\t", style.Yellow.Regular("Preview"), ":", style.Magenta.Regular(val.Watcher.Preview)) if len(val.Watcher.Exts) > 0 { fmt.Fprintln(style.Output, name, "\t", style.Yellow.Regular("Extensions"), ":", style.Magenta.Regular(val.Watcher.Exts)) } if len(val.Watcher.Paths) > 0 { fmt.Fprintln(style.Output, name, "\t", style.Yellow.Regular("Paths"), ":", style.Magenta.Regular(val.Watcher.Paths)) } if len(val.Watcher.Ignore) > 0 { fmt.Fprintln(style.Output, name, "\t", style.Yellow.Regular("Ignored paths"), ":", style.Magenta.Regular(val.Watcher.Ignore)) } if len(val.Watcher.Scripts) > 0 { fmt.Fprintln(style.Output, name, "\t", style.Yellow.Regular("Scripts"), ":") for _, v := range val.Watcher.Scripts { if v.Command != "" { fmt.Fprintln(style.Output, name, "\t\t", style.Magenta.Regular("-"), style.Yellow.Regular("Command"), ":", style.Magenta.Regular(v.Command)) if v.Path != "" { fmt.Fprintln(style.Output, name, "\t\t", style.Yellow.Regular("Path"), ":", style.Magenta.Regular(v.Path)) } if v.Type != "" { fmt.Fprintln(style.Output, name, "\t\t", style.Yellow.Regular("Type"), ":", style.Magenta.Regular(v.Type)) } } } } } return nil } return err } // Check whether there is a project func (h *Blueprint) check() error { if len(h.Projects) > 0 { h.Clean() return nil } return errors.New("There are no projects.") } // 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")), Cmds: Cmds{ Vet: p.Bool("vet"), Fmt: Cmd{ Status: p.Bool("fmt"), }, Test: Cmd{ Status: p.Bool("test"), }, Generate: Cmd{ Status: p.Bool("generate"), }, Build: Cmd{ Status: p.Bool("build"), }, Install: Cmd{ Status: p.Bool("install"), }, Run: p.Bool("run"), }, Args: argsParam(p), Watcher: Watcher{ Paths: []string{"/"}, Ignore: []string{"vendor"}, Exts: []string{".go"}, }, } if _, err := duplicates(project, h.Projects); err != nil { return err } h.Projects = append(h.Projects, project) return nil } // Run launches the toolchain for each project func (h *Blueprint) Run(p *cli.Context) error { err := h.check() if err == nil { // loop projects if p.String("name") != "" { wg.Add(1) } else { wg.Add(len(h.Projects)) } for k, element := range h.Projects { if p.String("name") != "" && h.Projects[k].Name != p.String("name") { continue } if element.Cmds.Fmt.Status { if len(element.Cmds.Fmt.Args) == 0{ element.Cmds.Fmt.Args = []string{"-s", "-w", "-e"} } h.Projects[k].tools.Fmt = tool{ status: element.Cmds.Fmt.Status, cmd: "gofmt", options: arguments([]string{}, element.Cmds.Fmt.Args), name: "Go Fmt", } } if element.Cmds.Generate.Status { h.Projects[k].tools.Generate = tool{ status: element.Cmds.Generate.Status, cmd: "go", options: arguments([]string{"generate"}, element.Cmds.Generate.Args), name: "Go Generate", } } if element.Cmds.Test.Status { h.Projects[k].tools.Test = tool{ status: element.Cmds.Test.Status, cmd: "go", options: arguments([]string{"test"}, element.Cmds.Test.Args), name: "Go Test", } } if element.Cmds.Vet { h.Projects[k].tools.Vet = tool{ status: element.Cmds.Vet, cmd: "go", options: []string{"vet"}, name: "Go Vet", } } h.Projects[k].parent = h h.Projects[k].Settings = *h.Settings h.Projects[k].path = h.Projects[k].Path // env variables for key, item := range h.Projects[k].Environment { if err := os.Setenv(key, item); err != nil { h.Projects[k].Buffer.StdErr = append(h.Projects[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 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) } if h.Legacy.Interval != 0 { go h.Projects[k].watchByPolling() } else { go h.Projects[k].watchByNotify() } } wg.Wait() return nil } return err } // Remove a project func (h *Blueprint) Remove(p *cli.Context) error { for key, val := range h.Projects { if p.String("name") == val.Name { h.Projects = append(h.Projects[:key], h.Projects[key+1:]...) return nil } } return errors.New("No project found.") }