2017-04-13 12:48:48 +00:00
|
|
|
package watcher
|
2016-08-31 12:08:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-04-13 13:49:51 +00:00
|
|
|
"github.com/tockins/realize/style"
|
2017-04-13 12:48:48 +00:00
|
|
|
cli "gopkg.in/urfave/cli.v2"
|
2017-08-07 21:01:02 +00:00
|
|
|
"os"
|
2017-08-31 07:54:59 +00:00
|
|
|
"path/filepath"
|
2017-04-16 10:04:40 +00:00
|
|
|
"strings"
|
2017-08-07 21:25:25 +00:00
|
|
|
"time"
|
2016-08-31 12:08:15 +00:00
|
|
|
)
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Run launches the toolchain for each project
|
2017-06-16 09:15:56 +00:00
|
|
|
func (h *Blueprint) Run(p *cli.Context) error {
|
2016-11-01 09:56:12 +00:00
|
|
|
err := h.check()
|
2016-08-31 12:08:15 +00:00
|
|
|
if err == nil {
|
|
|
|
// loop projects
|
2017-08-25 10:52:11 +00:00
|
|
|
if p.String("name") != "" {
|
2017-08-25 10:34:10 +00:00
|
|
|
wg.Add(1)
|
2017-08-25 10:52:11 +00:00
|
|
|
} else {
|
2017-08-25 10:34:10 +00:00
|
|
|
wg.Add(len(h.Projects))
|
|
|
|
}
|
2017-04-16 10:04:40 +00:00
|
|
|
for k, element := range h.Projects {
|
2017-08-24 13:22:14 +00:00
|
|
|
if p.String("name") != "" && h.Projects[k].Name != p.String("name") {
|
|
|
|
continue
|
|
|
|
}
|
2017-09-03 18:00:40 +00:00
|
|
|
if element.Cmds.Fmt.Status {
|
2017-08-27 09:23:55 +00:00
|
|
|
h.Projects[k].tools.Fmt = tool{
|
2017-09-03 18:00:40 +00:00
|
|
|
status: &h.Projects[k].Cmds.Fmt.Status,
|
2017-04-16 10:04:40 +00:00
|
|
|
cmd: "gofmt",
|
2017-09-03 18:00:40 +00:00
|
|
|
options: arguments([]string{}, element.Cmds.Fmt.Args),
|
2017-04-16 10:04:40 +00:00
|
|
|
name: "Go Fmt",
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 18:00:40 +00:00
|
|
|
if element.Cmds.Generate.Status {
|
2017-08-27 09:23:55 +00:00
|
|
|
h.Projects[k].tools.Generate = tool{
|
2017-09-03 18:00:40 +00:00
|
|
|
status: &h.Projects[k].Cmds.Generate.Status,
|
2017-04-16 10:04:40 +00:00
|
|
|
cmd: "go",
|
2017-09-03 18:00:40 +00:00
|
|
|
options: arguments([]string{"generate"}, element.Cmds.Generate.Args),
|
2017-04-16 10:04:40 +00:00
|
|
|
name: "Go Generate",
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 18:00:40 +00:00
|
|
|
if element.Cmds.Test.Status {
|
2017-08-27 09:23:55 +00:00
|
|
|
h.Projects[k].tools.Test = tool{
|
2017-09-03 18:00:40 +00:00
|
|
|
status: &h.Projects[k].Cmds.Test.Status,
|
2017-04-16 10:04:40 +00:00
|
|
|
cmd: "go",
|
2017-09-03 18:00:40 +00:00
|
|
|
options: arguments([]string{"test"}, element.Cmds.Test.Args),
|
2017-04-16 10:04:40 +00:00
|
|
|
name: "Go Test",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if element.Cmds.Vet {
|
2017-08-27 09:23:55 +00:00
|
|
|
h.Projects[k].tools.Vet = tool{
|
2017-04-16 10:04:40 +00:00
|
|
|
status: &h.Projects[k].Cmds.Vet,
|
|
|
|
cmd: "go",
|
2017-04-24 00:07:37 +00:00
|
|
|
options: []string{"vet"},
|
|
|
|
name: "Go Vet",
|
2017-04-16 10:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-14 08:47:43 +00:00
|
|
|
h.Projects[k].parent = h
|
2017-09-03 18:00:40 +00:00
|
|
|
h.Projects[k].Settings = *h.Settings
|
2016-12-25 17:08:22 +00:00
|
|
|
h.Projects[k].path = h.Projects[k].Path
|
2017-08-07 21:01:02 +00:00
|
|
|
|
|
|
|
// env variables
|
|
|
|
for key, item := range h.Projects[k].Environment {
|
2017-08-07 21:37:45 +00:00
|
|
|
if err := os.Setenv(key, item); err != nil {
|
2017-08-07 21:25:25 +00:00
|
|
|
h.Projects[k].Buffer.StdErr = append(h.Projects[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""})
|
2017-08-07 21:01:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-31 07:47:55 +00:00
|
|
|
|
|
|
|
// base path of the project
|
|
|
|
wd, err := os.Getwd()
|
2017-08-31 07:54:59 +00:00
|
|
|
if err != nil {
|
2017-08-31 07:47:55 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-09-03 18:00:40 +00:00
|
|
|
if h.Legacy.Interval != 0 {
|
2016-12-21 23:28:08 +00:00
|
|
|
go h.Projects[k].watchByPolling()
|
|
|
|
} else {
|
2016-12-25 17:08:22 +00:00
|
|
|
go h.Projects[k].watchByNotify()
|
2016-12-21 23:28:08 +00:00
|
|
|
}
|
2016-08-31 12:08:15 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new project
|
2016-11-01 09:56:12 +00:00
|
|
|
func (h *Blueprint) Add(p *cli.Context) error {
|
|
|
|
project := Project{
|
2017-04-16 10:04:40 +00:00
|
|
|
Name: h.Name(p.String("name"), p.String("path")),
|
|
|
|
Path: h.Path(p.String("path")),
|
|
|
|
Cmds: Cmds{
|
2017-09-03 18:00:40 +00:00
|
|
|
Vet: p.Bool("vet"),
|
|
|
|
Fmt: Cmd{
|
|
|
|
Status: !p.Bool("no-fmt"),
|
|
|
|
},
|
|
|
|
Test: Cmd{
|
|
|
|
Status: !p.Bool("test"),
|
|
|
|
},
|
|
|
|
Generate: Cmd{
|
|
|
|
Status: !p.Bool("generate"),
|
|
|
|
},
|
2017-04-16 10:04:40 +00:00
|
|
|
Build: Cmd{
|
|
|
|
Status: p.Bool("build"),
|
|
|
|
},
|
|
|
|
Bin: Cmd{
|
|
|
|
Status: !p.Bool("no-bin"),
|
|
|
|
},
|
|
|
|
Run: !p.Bool("no-run"),
|
|
|
|
},
|
|
|
|
Args: argsParam(p),
|
2016-08-31 12:08:15 +00:00
|
|
|
Watcher: Watcher{
|
2016-11-20 17:38:42 +00:00
|
|
|
Paths: []string{"/"},
|
|
|
|
Ignore: []string{"vendor"},
|
|
|
|
Exts: []string{".go"},
|
2016-12-26 23:14:29 +00:00
|
|
|
Preview: p.Bool("preview"),
|
2016-08-31 12:08:15 +00:00
|
|
|
},
|
|
|
|
}
|
2016-11-01 09:56:12 +00:00
|
|
|
if _, err := duplicates(project, h.Projects); err != nil {
|
2016-08-31 12:08:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-11-01 09:56:12 +00:00
|
|
|
h.Projects = append(h.Projects, project)
|
2016-08-31 12:08:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a project
|
2016-11-01 09:56:12 +00:00
|
|
|
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
|
2016-08-31 12:08:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-27 09:23:55 +00:00
|
|
|
return errors.New("No project found.")
|
2016-08-31 12:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List of all the projects
|
|
|
|
func (h *Blueprint) List() error {
|
2016-11-01 09:56:12 +00:00
|
|
|
err := h.check()
|
2016-08-31 12:08:15 +00:00
|
|
|
if err == nil {
|
|
|
|
for _, val := range h.Projects {
|
2017-08-31 06:58:04 +00:00
|
|
|
fmt.Fprintln(style.Output, style.Blue.Bold("[")+strings.ToUpper(val.Name)+style.Blue.Bold("]"))
|
2017-04-13 13:49:51 +00:00
|
|
|
name := style.Magenta.Bold("[") + strings.ToUpper(val.Name) + style.Magenta.Bold("]")
|
2017-03-19 23:31:52 +00:00
|
|
|
|
2017-08-31 06:58:04 +00:00
|
|
|
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.Bin))
|
|
|
|
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))
|
2017-04-16 10:04:40 +00:00
|
|
|
if len(val.Args) > 0 {
|
2017-08-31 06:58:04 +00:00
|
|
|
fmt.Fprintln(style.Output, name, style.Yellow.Regular("Params"), ":", style.Magenta.Regular(val.Args))
|
2016-11-11 16:21:54 +00:00
|
|
|
}
|
2017-08-31 06:58:04 +00:00
|
|
|
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))
|
2016-11-11 16:21:54 +00:00
|
|
|
if len(val.Watcher.Exts) > 0 {
|
2017-08-31 06:58:04 +00:00
|
|
|
fmt.Fprintln(style.Output, name, "\t", style.Yellow.Regular("Extensions"), ":", style.Magenta.Regular(val.Watcher.Exts))
|
2016-11-11 16:21:54 +00:00
|
|
|
}
|
|
|
|
if len(val.Watcher.Paths) > 0 {
|
2017-08-31 06:58:04 +00:00
|
|
|
fmt.Fprintln(style.Output, name, "\t", style.Yellow.Regular("Paths"), ":", style.Magenta.Regular(val.Watcher.Paths))
|
2016-11-11 16:21:54 +00:00
|
|
|
}
|
|
|
|
if len(val.Watcher.Ignore) > 0 {
|
2017-08-31 06:58:04 +00:00
|
|
|
fmt.Fprintln(style.Output, name, "\t", style.Yellow.Regular("Ignored paths"), ":", style.Magenta.Regular(val.Watcher.Ignore))
|
2016-11-11 16:21:54 +00:00
|
|
|
}
|
2016-12-26 23:14:29 +00:00
|
|
|
if len(val.Watcher.Scripts) > 0 {
|
2017-08-31 06:58:04 +00:00
|
|
|
fmt.Fprintln(style.Output, name, "\t", style.Yellow.Regular("Scripts"), ":")
|
2016-12-26 23:14:29 +00:00
|
|
|
for _, v := range val.Watcher.Scripts {
|
|
|
|
if v.Command != "" {
|
2017-08-31 06:58:04 +00:00
|
|
|
fmt.Fprintln(style.Output, name, "\t\t", style.Magenta.Regular("-"), style.Yellow.Regular("Command"), ":", style.Magenta.Regular(v.Command))
|
2016-12-26 23:14:29 +00:00
|
|
|
if v.Path != "" {
|
2017-08-31 06:58:04 +00:00
|
|
|
fmt.Fprintln(style.Output, name, "\t\t", style.Yellow.Regular("Path"), ":", style.Magenta.Regular(v.Path))
|
2016-12-26 23:14:29 +00:00
|
|
|
}
|
|
|
|
if v.Type != "" {
|
2017-08-31 06:58:04 +00:00
|
|
|
fmt.Fprintln(style.Output, name, "\t\t", style.Yellow.Regular("Type"), ":", style.Magenta.Regular(v.Type))
|
2016-12-26 23:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-31 12:08:15 +00:00
|
|
|
}
|
2016-11-01 09:56:12 +00:00
|
|
|
return nil
|
2016-08-31 12:08:15 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2016-10-21 15:30:12 +00:00
|
|
|
|
2016-11-14 07:59:47 +00:00
|
|
|
// Check whether there is a project
|
2016-11-01 09:56:12 +00:00
|
|
|
func (h *Blueprint) check() error {
|
|
|
|
if len(h.Projects) > 0 {
|
|
|
|
h.Clean()
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-27 09:23:55 +00:00
|
|
|
return errors.New("There are no projects.")
|
2016-11-01 09:56:12 +00:00
|
|
|
}
|