This commit is contained in:
alessio 2017-06-16 16:35:00 +02:00
parent f2f11c7e47
commit f4547b9e87
4 changed files with 28 additions and 37 deletions

View File

@ -741,7 +741,7 @@ func main() {
if err != nil { if err != nil {
return d.Err() return d.Err()
} }
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val}) r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val, Changed: true})
d.Reload() d.Reload()
return nil return nil
}, },
@ -783,7 +783,7 @@ func main() {
if err != nil { if err != nil {
return d.Err() return d.Err()
} }
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val}) r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val, Changed: true})
d.Reload() d.Reload()
return nil return nil
}, },

View File

@ -88,12 +88,6 @@ func (h *Blueprint) Add(p *cli.Context) error {
Ignore: []string{"vendor"}, Ignore: []string{"vendor"},
Exts: []string{".go"}, Exts: []string{".go"},
Preview: p.Bool("preview"), Preview: p.Bool("preview"),
Scripts: []Command{},
},
Streams: Streams{
FileOut: false,
FileLog: false,
FileErr: false,
}, },
} }
if _, err := duplicates(project, h.Projects); err != nil { if _, err := duplicates(project, h.Projects); err != nil {

View File

@ -88,6 +88,7 @@ type Command struct {
Type string `yaml:"type" json:"type"` Type string `yaml:"type" json:"type"`
Command string `yaml:"command" json:"command"` Command string `yaml:"command" json:"command"`
Path string `yaml:"path" json:"path"` Path string `yaml:"path" json:"path"`
Changed bool `yaml:"refresh" json:"refresh"`
} }
// Streams is a collection of names and values for the logs functionality // Streams is a collection of names and values for the logs functionality

View File

@ -21,6 +21,7 @@ import (
var msg string var msg string
var out BufferOut var out BufferOut
// Add a path to paths list
func (w *pollWatcher) Add(path string) error { func (w *pollWatcher) Add(path string) error {
if w.paths == nil { if w.paths == nil {
w.paths = map[string]bool{} w.paths = map[string]bool{}
@ -29,6 +30,7 @@ func (w *pollWatcher) Add(path string) error {
return nil return nil
} }
// Check if is watching
func (w *pollWatcher) isWatching(path string) bool { func (w *pollWatcher) isWatching(path string) bool {
a, b := w.paths[path] a, b := w.paths[path]
return a && b return a && b
@ -41,13 +43,9 @@ func (p *Project) watchByPolling() {
channel, exit := make(chan bool, 1), make(chan os.Signal, 2) channel, exit := make(chan bool, 1), make(chan os.Signal, 2)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM) signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
defer func() { defer func() {
p.cmd("after")
wg.Done() wg.Done()
}() }()
go p.routines(&wr, channel, watcher, "")
p.cmd("before")
p.Fatal(p.watch(watcher))
go p.routines(channel, &wr)
p.LastChangedOn = time.Now().Truncate(time.Second) p.LastChangedOn = time.Now().Truncate(time.Second)
walk := func(changed string, info os.FileInfo, err error) error { walk := func(changed string, info os.FileInfo, err error) error {
var ext string var ext string
@ -65,7 +63,6 @@ func (p *Project) watchByPolling() {
} }
i := strings.Index(changed, filepath.Ext(changed)) i := strings.Index(changed, filepath.Ext(changed))
file := changed[:i] + ext file := changed[:i] + ext
path := filepath.Dir(changed[:i])
if changed[:i] != "" && inArray(ext, p.Watcher.Exts) { if changed[:i] != "" && inArray(ext, p.Watcher.Exts) {
if p.Cmds.Run { if p.Cmds.Run {
close(channel) close(channel)
@ -76,13 +73,7 @@ func (p *Project) watchByPolling() {
msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file)) msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file))
out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file} out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file}
p.print("log", out, msg, "") p.print("log", out, msg, "")
go p.routines(&wr, channel, watcher, file)
p.cmd("change")
p.tool(file, p.tools.Fmt)
p.tool(file, p.tools.Vet)
p.tool(path, p.tools.Test)
p.tool(path, p.tools.Generate)
go p.routines(channel, &wr)
} }
return nil return nil
} }
@ -118,13 +109,9 @@ func (p *Project) watchByNotify() {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
p.Fatal(err) p.Fatal(err)
defer func() { defer func() {
p.cmd("after")
wg.Done() wg.Done()
}() }()
go p.routines(&wr, channel, watcher, "")
p.cmd("before")
p.Fatal(p.watch(watcher))
go p.routines(channel, &wr)
p.LastChangedOn = time.Now().Truncate(time.Second) p.LastChangedOn = time.Now().Truncate(time.Second)
for { for {
select { select {
@ -142,7 +129,6 @@ func (p *Project) watchByNotify() {
} }
i := strings.Index(event.Name, filepath.Ext(event.Name)) i := strings.Index(event.Name, filepath.Ext(event.Name))
file := event.Name[:i] + ext file := event.Name[:i] + ext
path := filepath.Dir(event.Name[:i])
if event.Name[:i] != "" && inArray(ext, p.Watcher.Exts) { if event.Name[:i] != "" && inArray(ext, p.Watcher.Exts) {
if p.Cmds.Run { if p.Cmds.Run {
close(channel) close(channel)
@ -152,13 +138,7 @@ func (p *Project) watchByNotify() {
msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file)) msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file))
out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file} out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file}
p.print("log", out, msg, "") p.print("log", out, msg, "")
go p.routines(&wr, channel, watcher, file)
p.cmd("change")
p.tool(file, p.tools.Fmt)
p.tool(path, p.tools.Vet)
p.tool(path, p.tools.Test)
p.tool(path, p.tools.Generate)
go p.routines(channel, &wr)
p.LastChangedOn = time.Now().Truncate(time.Second) p.LastChangedOn = time.Now().Truncate(time.Second)
} }
} }
@ -302,9 +282,9 @@ func (p *Project) tool(path string, tool tool) error {
} }
// Cmd calls an wrapper for execute the commands after/before // Cmd calls an wrapper for execute the commands after/before
func (p *Project) cmd(flag string) { func (p *Project) cmd(flag string, changed bool) {
for _, cmd := range p.Watcher.Scripts { for _, cmd := range p.Watcher.Scripts {
if strings.ToLower(cmd.Type) == flag { if strings.ToLower(cmd.Type) == flag && changed == cmd.Changed {
errors, logs := p.command(cmd) errors, logs := p.command(cmd)
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Bold("Command"), style.Green.Bold("\"")+cmd.Command+style.Green.Bold("\"")) msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Bold("Command"), style.Green.Bold("\"")+cmd.Command+style.Green.Bold("\""))
out = BufferOut{Time: time.Now(), Text: cmd.Command, Type: flag} out = BufferOut{Time: time.Now(), Text: cmd.Command, Type: flag}
@ -339,7 +319,18 @@ func (p *Project) ignore(str string) bool {
} }
// Routines launches the toolchain run, build, install // Routines launches the toolchain run, build, install
func (p *Project) routines(channel chan bool, wr *sync.WaitGroup) { func (p *Project) routines(wr *sync.WaitGroup,channel chan bool, watcher watcher, file string) {
if len(file) > 0{
p.cmd("before", true)
path := filepath.Dir(file)
p.tool(file, p.tools.Fmt)
p.tool(path, p.tools.Vet)
p.tool(path, p.tools.Test)
p.tool(path, p.tools.Generate)
}else{
p.cmd("before", false)
p.Fatal(p.watch(watcher))
}
install := p.install() install := p.install()
build := p.build() build := p.build()
wr.Add(1) wr.Add(1)
@ -347,6 +338,11 @@ func (p *Project) routines(channel chan bool, wr *sync.WaitGroup) {
go p.run(channel, wr) go p.run(channel, wr)
} }
wr.Wait() wr.Wait()
if len(file) > 0 {
p.cmd("after", true)
}else{
p.cmd("after", false)
}
} }
// Defines the colors scheme for the project name // Defines the colors scheme for the project name