print changed file name

This commit is contained in:
alessio 2016-08-01 19:08:37 +02:00
parent 69bc07e3bc
commit 50e392aaff
1 changed files with 54 additions and 39 deletions

View File

@ -9,8 +9,11 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"bytes"
) )
var wg sync.WaitGroup
func InArray(str string, list []string) bool{ func InArray(str string, list []string) bool{
for _, v := range list { for _, v := range list {
if v == str { if v == str {
@ -29,21 +32,25 @@ func Ignore(str string, list []string) bool{
return false return false
} }
func (h *Config) Watch() error{ func Watching(val Project){
var current Watcher
var wg sync.WaitGroup
var current Project
var watcher *fsnotify.Watcher var watcher *fsnotify.Watcher
watcher, _ = fsnotify.NewWatcher()
defer func(){
watcher.Close()
wg.Done()
}()
walk := func(path string, info os.FileInfo, err error) error{ walk := func(path string, info os.FileInfo, err error) error{
if !Ignore(path,current.Ignore) { if !Ignore(path,current.Watcher.Ignore) {
if info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.Contains(path, "/.") { if info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.Contains(path, "/.") {
fmt.Println(current.Name +": "+path)
if err = watcher.Add(path); err != nil { if err = watcher.Add(path); err != nil {
return filepath.SkipDir return filepath.SkipDir
} }
} else if InArray(filepath.Ext(path), current.Exts) { } else if InArray(filepath.Ext(path), current.Watcher.Exts) {
fmt.Println(current.Name +": "+path)
if err = watcher.Add(path); err != nil { if err = watcher.Add(path); err != nil {
return filepath.SkipDir return filepath.SkipDir
} }
@ -52,21 +59,29 @@ func (h *Config) Watch() error{
return nil return nil
} }
watch := func(val Project){
watcher, _ = fsnotify.NewWatcher()
// run, bin, build // run, bin, build
val.reload = time.Now().Truncate(time.Second) val.reload = time.Now().Truncate(time.Second)
for _, dir := range val.Watcher.Paths { for _, dir := range val.Watcher.Paths {
var base bytes.Buffer
path, _ := os.Getwd() path, _ := os.Getwd()
current = val.Watcher current = val
// add dir of project split := strings.Split(val.Main, "/")
if err := filepath.Walk(path + dir, walk); err != nil {
// get base path from mail field
for key, str := range split{
if(key < len(split)-1) {
base.WriteString("/" + str)
}
}
if err := filepath.Walk(path + base.String() + dir, walk); err != nil {
fmt.Println(err) fmt.Println(err)
} }
} }
for { for {
select { select {
case event := <-watcher.Events: case event := <-watcher.Events:
@ -75,8 +90,12 @@ func (h *Config) Watch() error{
continue continue
} }
if _, err := os.Stat(event.Name); err == nil { if _, err := os.Stat(event.Name); err == nil {
log.Println("event:", event)
i := strings.Index(event.Name, filepath.Ext(event.Name))
log.Println("event:", event.Name[:i])
// run, bin, build // run, bin, build
val.reload = time.Now().Truncate(time.Second) val.reload = time.Now().Truncate(time.Second)
} }
} }
@ -84,20 +103,16 @@ func (h *Config) Watch() error{
log.Println("error:", err) log.Println("error:", err)
} }
} }
watcher.Close() }
wg.Done()
}
// add to watcher func (h *Config) Watch() error{
if err := h.Read(); err == nil { if err := h.Read(); err == nil {
// loop projects // loop projects
wg.Add(len(h.Projects)) wg.Add(len(h.Projects))
for _, val := range h.Projects { for _, val := range h.Projects {
go watch(val) go Watching(val)
} }
wg.Wait() wg.Wait()
return nil return nil
}else{ }else{
return err return err