diff --git a/realize.go b/realize.go index a468c91..95d9edc 100644 --- a/realize.go +++ b/realize.go @@ -15,7 +15,9 @@ const ( version = "1.2" description = "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths" config = "realize.yaml" - output = "realize.log" + output = "outputs.log" + log = "logs.log" + err = "errors.log" host = "localhost" port = 5000 server = true @@ -48,6 +50,7 @@ func init() { Resources: c.Resources{ Config: config, Output: output, + Log: log, }, Server: c.Server{ Enabled: server, @@ -116,23 +119,7 @@ func main() { Commands: []*cli.Command{ { Name: "run", - Usage: "Build and watch file changes", - Flags: []cli.Flag{ - &cli.BoolFlag{Name: "no-server", Usage: "Disables the web panel"}, - &cli.BoolFlag{Name: "open", Usage: "Automatically opens the web panel"}, - }, - Action: func(p *cli.Context) error { - handle(r.Server.Start(p)) - handle(r.Blueprint.Run()) - return nil - }, - Before: func(c *cli.Context) error { - return before() - }, - }, - { - Name: "fast", - Usage: "Build and watch file changes for a single project without any Configuration file", + Usage: "Build and watch file changes. Can be used even with a single project or without the config file", Flags: []cli.Flag{ &cli.StringFlag{Name: "path", Aliases: []string{"b"}, Value: "", Usage: "Project base path"}, &cli.BoolFlag{Name: "build", Value: false, Usage: "Enables the build"}, @@ -140,17 +127,33 @@ func main() { &cli.BoolFlag{Name: "no-bin", Usage: "Disables the installation"}, &cli.BoolFlag{Name: "no-fmt", Usage: "Disables the fmt (go fmt)"}, &cli.BoolFlag{Name: "no-server", Usage: "Disables the web panel"}, + &cli.BoolFlag{Name: "no-config", Value: false, Usage: "Uses the config settings"}, &cli.BoolFlag{Name: "open", Usage: "Automatically opens the web panel"}, &cli.BoolFlag{Name: "test", Value: false, Usage: "Enables the tests"}, - &cli.BoolFlag{Name: "config", Value: false, Usage: "Take the defined settings if exist a Configuration file."}, }, Action: func(p *cli.Context) error { - if !p.Bool("config") { + if p.Bool("no-config") { + r.Settings = c.Settings{ + Config: c.Config{ + Flimit: 0, + }, + Resources: c.Resources{ + Config: config, + Output: output, + Log: log, + }, + Server: c.Server{ + Enabled: server, + Open: open, + Host: host, + Port: port, + }, + } r.Blueprint.Projects = r.Blueprint.Projects[:0] } - handle(r.Blueprint.Add(p)) + r.Blueprint.Add(p) handle(r.Server.Start(p)) - handle(r.Blueprint.Fast(p)) + handle(r.Blueprint.Run()) return nil }, Before: func(c *cli.Context) error { diff --git a/watcher/cmd.go b/watcher/cmd.go index 406e169..92f230c 100644 --- a/watcher/cmd.go +++ b/watcher/cmd.go @@ -24,20 +24,6 @@ func (h *Blueprint) Run() error { return err } -// Fast method run a project from his working directory without makes a config file -func (h *Blueprint) Fast(p *cli.Context) error { - // Takes the values from config if wd path match with someone else - wg.Add(1) - for i := 0; i < len(h.Projects); i++ { - v := &h.Projects[i] - v.parent = h - v.path = v.Path - go v.watching() - } - wg.Wait() - return nil -} - // Add a new project func (h *Blueprint) Add(p *cli.Context) error { project := Project{ @@ -142,7 +128,7 @@ func (h *Blueprint) List() error { return err } -// Check if there are projects +// Check whether there is a project func (h *Blueprint) check() error { if len(h.Projects) > 0 { h.Clean() diff --git a/watcher/exec.go b/watcher/exec.go index 66ded29..8ed7a73 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -3,6 +3,7 @@ package cli import ( "bufio" "bytes" + "fmt" "io" "log" "os" @@ -11,16 +12,12 @@ import ( "strings" "sync" "time" - "fmt" ) // GoRun is an implementation of the bin execution -func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error { +func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error { var build *exec.Cmd - sync := func() { - p.parent.Sync <- "sync" - } if len(p.Params) != 0 { build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.path)), p.Params...) } else { @@ -34,7 +31,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Ended"}) log.Println(p.pname(p.Name, 2), ":", p.Red.Regular("Ended")) - go sync() + go p.sync() wr.Done() }() @@ -63,7 +60,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } else { p.Buffer.StdOut = append(p.Buffer.StdOut, BufferOut{Time: time.Now(), Text: output.Text()}) } - go sync() + go p.sync() if p.Cli.Streams { log.Println(p.pname(p.Name, 3), ":", p.Blue.Regular(output.Text())) } @@ -96,9 +93,9 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } // GoBuild is an implementation of the "go build" -func (p *Project) GoBuild() (string, error) { +func (p *Project) goBuild() (string, error) { defer func() { - p.parent.Sync <- "sync" + p.sync() }() var out bytes.Buffer var stderr bytes.Buffer @@ -114,9 +111,9 @@ func (p *Project) GoBuild() (string, error) { } // GoInstall is an implementation of the "go install" -func (p *Project) GoInstall() (string, error) { +func (p *Project) goInstall() (string, error) { defer func() { - p.parent.Sync <- "sync" + p.sync() }() var out bytes.Buffer var stderr bytes.Buffer @@ -137,9 +134,9 @@ func (p *Project) GoInstall() (string, error) { } // GoFmt is an implementation of the gofmt -func (p *Project) GoFmt(path string) (io.Writer, error) { +func (p *Project) goFmt(path string) (io.Writer, error) { defer func() { - p.parent.Sync <- "sync" + p.sync() }() var out bytes.Buffer build := exec.Command("gofmt", "-s", "-w", "-e", path) @@ -155,9 +152,9 @@ func (p *Project) GoFmt(path string) (io.Writer, error) { } // GoTest is an implementation of the go test -func (p *Project) GoTest(path string) (io.Writer, error) { +func (p *Project) goTest(path string) (io.Writer, error) { defer func() { - p.parent.Sync <- "sync" + p.sync() }() var out bytes.Buffer build := exec.Command("go", "test") @@ -172,9 +169,9 @@ func (p *Project) GoTest(path string) (io.Writer, error) { } // GoGenerate is an implementation of the go test -func (p *Project) GoGenerate(path string) (io.Writer, error) { +func (p *Project) goGenerate(path string) (io.Writer, error) { defer func() { - p.parent.Sync <- "sync" + p.sync() }() var out bytes.Buffer build := exec.Command("go", "generate") @@ -188,11 +185,8 @@ func (p *Project) GoGenerate(path string) (io.Writer, error) { return nil, nil } -// Cmd exec a list of defined commands -func (p *Project) Cmd(cmds []string) (errors []error) { - defer func() { - p.parent.Sync <- "sync" - }() +// Cmds exec a list of defined commands +func (p *Project) cmds(cmds []string) (errors []error) { for _, cmd := range cmds { cmd := strings.Replace(strings.Replace(cmd, "'", "", -1), "\"", "", -1) c := strings.Split(cmd, " ") @@ -201,7 +195,15 @@ func (p *Project) Cmd(cmds []string) (errors []error) { if err := build.Run(); err != nil { p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error()}) errors = append(errors, err) + return errors } } - return errors + return nil +} + +// Sync datas with the web server +func (p *Project) sync() { + go func() { + p.parent.Sync <- "sync" + }() } diff --git a/watcher/watcher.go b/watcher/watcher.go index ec066ce..30f0ef8 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -89,7 +89,7 @@ func (p *Project) install(channel chan bool, wr *sync.WaitGroup) { if p.Bin { log.Println(p.pname(p.Name, 1), ":", "Installing..") start := time.Now() - if std, err := p.GoInstall(); err != nil { + if std, err := p.goInstall(); err != nil { log.Println(p.pname(p.Name, 1), ":", fmt.Sprint(p.Red.Bold(err)), std) wr.Done() } else { @@ -98,7 +98,7 @@ func (p *Project) install(channel chan bool, wr *sync.WaitGroup) { runner := make(chan bool, 1) log.Println(p.pname(p.Name, 1), ":", "Running..") start = time.Now() - go p.GoRun(channel, runner, wr) + go p.goRun(channel, runner, wr) for { select { case <-runner: @@ -117,7 +117,7 @@ func (p *Project) build() { if p.Build { log.Println(p.pname(p.Name, 1), ":", "Building..") start := time.Now() - if std, err := p.GoBuild(); err != nil { + if std, err := p.goBuild(); err != nil { log.Println(p.pname(p.Name, 1), ":", fmt.Sprint(p.Red.Bold(err)), std) } else { log.Println(p.pname(p.Name, 5), ":", p.Green.Regular("Builded")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) @@ -129,8 +129,8 @@ func (p *Project) build() { // Fmt calls an implementation of the "go fmt" func (p *Project) fmt(path string) error { if p.Fmt { - if stream, err := p.GoFmt(path); err != nil { - log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Fmt"), p.Red.Bold("there are some errors in"), ":", p.Magenta.Bold(path)) + if stream, err := p.goFmt(path); err != nil { + log.Println(p.pname(p.Name, 1), p.Red.Bold("go Fmt"), p.Red.Bold("there are some errors in"), ":", p.Magenta.Bold(path)) fmt.Println(stream) return err } @@ -141,8 +141,8 @@ func (p *Project) fmt(path string) error { // Generate calls an implementation of the "go generate" func (p *Project) generate(path string) error { if p.Generate { - if stream, err := p.GoGenerate(path); err != nil { - log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Generate"), p.Red.Bold("there are some errors in"), ":", p.Magenta.Bold(path)) + if stream, err := p.goGenerate(path); err != nil { + log.Println(p.pname(p.Name, 1), p.Red.Bold("go Generate"), p.Red.Bold("there are some errors in"), ":", p.Magenta.Bold(path)) fmt.Println(stream) return err } @@ -155,7 +155,7 @@ func (p *Project) cmd(exit chan bool) { c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) cast := func(commands []string) { - if errs := p.Cmd(commands); errs != nil { + if errs := p.cmds(commands); errs != nil { for _, err := range errs { log.Println(p.pname(p.Name, 2), p.Red.Bold(err)) } @@ -182,8 +182,8 @@ func (p *Project) cmd(exit chan bool) { // Test calls an implementation of the "go test" func (p *Project) test(path string) error { if p.Test { - if stream, err := p.GoTest(path); err != nil { - log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Test fails in "), ":", p.Magenta.Bold(path)) + if stream, err := p.goTest(path); err != nil { + log.Println(p.pname(p.Name, 1), p.Red.Bold("go Test fails in "), ":", p.Magenta.Bold(path)) fmt.Println(stream) return err } @@ -216,6 +216,7 @@ func (p *Project) walks(watcher *fsnotify.Watcher) error { } return nil } + if p.path == "." || p.path == "/" { p.base = wd p.path = p.Wdir()