after, before commands

This commit is contained in:
alessio 2016-09-05 17:53:37 +02:00
parent 0e83ef7bae
commit fd1158ddbe
3 changed files with 57 additions and 10 deletions

View File

@ -170,7 +170,7 @@ A Go build system with file watchers, output streams and live reload. Run, build
- [ ] Testing on windows - [ ] Testing on windows
- [x] Custom paths for the commands fast/add - [x] Custom paths for the commands fast/add
- [x] Save output on a file - [x] Save output on a file
- [ ] Enable the fields Before/After - [x] Enable the fields Before/After
- [ ] Web panel - **Maybe** - [ ] Web panel - **Maybe**

View File

@ -3,11 +3,13 @@ package cli
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"io" "io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"sync" "sync"
"time" "time"
) )
@ -27,7 +29,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
if err := build.Process.Kill(); err != nil { if err := build.Process.Kill(); err != nil {
log.Fatal(Red("Failed to stop: "), Red(err)) log.Fatal(Red("Failed to stop: "), Red(err))
} }
log.Println(pname(p.Name, 2), ":", RedS("Stopped")) log.Println(pname(p.Name, 2), ":", RedS("Ended"))
wr.Done() wr.Done()
}() }()
@ -134,3 +136,16 @@ func (p *Project) GoTest(path string) (io.Writer, error) {
} }
return nil, nil return nil, nil
} }
// Cmd exec a list of defined commands
func (p *Project) Cmd(cmds []string) (errors []error) {
for _, cmd := range cmds {
c := strings.Split(cmd, " ")
build := exec.Command(c[0], c[1:]...)
build.Dir = p.base
if err := build.Run(); err != nil {
errors = append(errors, err)
}
}
return errors
}

View File

@ -7,9 +7,11 @@ import (
"log" "log"
"math/big" "math/big"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"syscall"
"time" "time"
) )
@ -33,11 +35,13 @@ func (p *Project) watching() {
} }
defer end() defer end()
p.cmd()
err = p.walks(watcher) err = p.walks(watcher)
if err != nil { if err != nil {
fmt.Println(pname(p.Name, 1), ":", Red(err.Error())) fmt.Println(pname(p.Name, 1), ":", Red(err.Error()))
return return
} }
go p.routines(channel, &wr) go p.routines(channel, &wr)
p.reload = time.Now().Truncate(time.Second) p.reload = time.Now().Truncate(time.Second)
@ -60,7 +64,7 @@ func (p *Project) watching() {
i := strings.Index(event.Name, filepath.Ext(event.Name)) i := strings.Index(event.Name, filepath.Ext(event.Name))
if event.Name[:i] != "" && inArray(ext, p.Watcher.Exts) { if event.Name[:i] != "" && inArray(ext, p.Watcher.Exts) {
log.Println(pname(p.Name, 4), ":", Magenta(event.Name[:i]+ext)) fmt.Println(pname(p.Name, 4), Magenta(strings.ToUpper(ext[1:])+" changed"), Magenta(event.Name[:i]+ext))
// stop and run again // stop and run again
if p.Run { if p.Run {
close(channel) close(channel)
@ -92,7 +96,7 @@ func (p *Project) install(channel chan bool, wr *sync.WaitGroup) {
log.Println(pname(p.Name, 1), ":", fmt.Sprint(Red(err)), std) log.Println(pname(p.Name, 1), ":", fmt.Sprint(Red(err)), std)
wr.Done() wr.Done()
} else { } else {
log.Println(pname(p.Name, 5), ":", Green("Installed")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s")) log.Println(pname(p.Name, 5), ":", Green("Installed")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
if p.Run { if p.Run {
runner := make(chan bool, 1) runner := make(chan bool, 1)
log.Println(pname(p.Name, 1), ":", "Running..") log.Println(pname(p.Name, 1), ":", "Running..")
@ -101,7 +105,7 @@ func (p *Project) install(channel chan bool, wr *sync.WaitGroup) {
for { for {
select { select {
case <-runner: case <-runner:
log.Println(pname(p.Name, 5), ":", Green("Has been run")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s")) log.Println(pname(p.Name, 5), ":", Green("Has been run")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
return return
} }
} }
@ -119,25 +123,53 @@ func (p *Project) build() {
if std, err := p.GoBuild(); err != nil { if std, err := p.GoBuild(); err != nil {
log.Println(pname(p.Name, 1), ":", fmt.Sprint(Red(err)), std) log.Println(pname(p.Name, 1), ":", fmt.Sprint(Red(err)), std)
} else { } else {
log.Println(pname(p.Name, 5), ":", Green("Builded")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s")) log.Println(pname(p.Name, 5), ":", Green("Builded")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
} }
return return
} }
return return
} }
// Build calls an implementation of the "gofmt" // Fmt calls an implementation of the "gofmt"
func (p *Project) fmt(path string) error { func (p *Project) fmt(path string) error {
if p.Fmt { if p.Fmt {
if _, err := p.GoFmt(path); err != nil { if _, err := p.GoFmt(path); err != nil {
log.Println(pname(p.Name, 1), Red("There are some GoFmt errors in "), ":", Magenta(path)) log.Println(pname(p.Name, 1), Red("There are some GoFmt errors in "), ":", Magenta(path))
//fmt.Println(msg)
} }
} }
return nil return nil
} }
// Build calls an implementation of the "go test" // Cmd calls an wrapper for execute the commands after/before
func (p *Project) cmd() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
cast := func(commands []string) {
if errs := p.Cmd(commands); errs != nil {
for _, err := range errs {
log.Println(pname(p.Name, 2), Red(err))
}
}
}
if len(p.Watcher.Before) > 0 {
cast(p.Watcher.Before)
}
go func() {
for {
select {
case <-c:
if len(p.Watcher.After) > 0 {
cast(p.Watcher.After)
}
os.Exit(1)
}
}
}()
}
// Test calls an implementation of the "go test"
func (p *Project) test(path string) error { func (p *Project) test(path string) error {
if p.Test { if p.Test {
if _, err := p.GoTest(path); err != nil { if _, err := p.GoTest(path); err != nil {
@ -201,7 +233,7 @@ func (p *Project) walks(watcher *fsnotify.Watcher) error {
return errors.New(base + " path doesn't exist") return errors.New(base + " path doesn't exist")
} }
} }
fmt.Println(Red("Watching"), ":", pname(p.Name, 1), Magenta(files), "file/s", Magenta(folders), "folder/s") fmt.Println(pname(p.Name, 1), Red("Watching"), Magenta(files), "file/s", Magenta(folders), "folder/s")
return nil return nil
} }