This commit is contained in:
alessio 2016-08-24 14:03:12 +02:00
parent d9635f84a7
commit 74e540bdbf
1 changed files with 57 additions and 51 deletions

View File

@ -63,7 +63,7 @@ func (p *Project) watching() {
var wr sync.WaitGroup var wr sync.WaitGroup
var watcher *fsnotify.Watcher var watcher *fsnotify.Watcher
var files, folders int64
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
log.Println(strings.ToUpper(pname(p.Name, 1)), ":", Red(err.Error())) log.Println(strings.ToUpper(pname(p.Name, 1)), ":", Red(err.Error()))
@ -72,60 +72,17 @@ func (p *Project) watching() {
if err != nil { if err != nil {
log.Println(pname(p.Name, 1), ":", Red(err.Error())) log.Println(pname(p.Name, 1), ":", Red(err.Error()))
} }
wd, _ := os.Getwd()
walk := func(path string, info os.FileInfo, err error) error {
if !p.ignore(path) {
if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.HasPrefix(path, ".")) && !strings.Contains(path, "/.") || (inArray(filepath.Ext(path), p.Watcher.Exts)) {
if p.Watcher.Preview {
fmt.Println(pname(p.Name, 1), ":", path)
}
if err = watcher.Add(path); err != nil {
return filepath.SkipDir
}
if inArray(filepath.Ext(path), p.Watcher.Exts) {
files++
err := p.fmt(path)
if err == nil {
} else {
fmt.Println(Red(err))
}
} else {
folders++
}
}
}
return nil
}
end := func() { end := func() {
watcher.Close() watcher.Close()
wg.Done() wg.Done()
} }
defer end() defer end()
if p.Path == "." || p.Path == "/" { p.walks(watcher)
p.base = wd
p.Path = WorkingDir()
} else {
p.base = filepath.Join(wd, p.Path)
}
for _, dir := range p.Watcher.Paths {
base := filepath.Join(p.base, dir)
if _, err := os.Stat(base); err == nil {
if err := filepath.Walk(base, walk); err != nil {
log.Println(Red(err.Error()))
}
} else {
fmt.Println(pname(p.Name, 1), ":\t", Red(base+" path doesn't exist"))
}
}
fmt.Println(Red("Watching: "), pname(p.Name, 1), Magenta(files), "files", Magenta(folders), "folders")
fmt.Println()
go routines(p, channel, &wr) go routines(p, channel, &wr)
p.reload = time.Now().Truncate(time.Second) p.reload = time.Now().Truncate(time.Second)
// waiting for an event
for { for {
select { select {
case event := <-watcher.Events: case event := <-watcher.Events:
@ -151,16 +108,17 @@ func (p *Project) watching() {
wr.Wait() wr.Wait()
channel = make(chan bool) channel = make(chan bool)
} }
err := p.fmt(event.Name[:i] + ext) err := p.fmt(event.Name[:i] + ext)
if err == nil { if err != nil {
log.Fatal(Red(err))
} else { } else {
fmt.Println(Red(err))
}
go routines(p, channel, &wr) go routines(p, channel, &wr)
p.reload = time.Now().Truncate(time.Second) p.reload = time.Now().Truncate(time.Second)
} }
} }
} }
}
case err := <-watcher.Errors: case err := <-watcher.Errors:
log.Println(Red(err.Error())) log.Println(Red(err.Error()))
} }
@ -216,12 +174,60 @@ func (p *Project) fmt(path string) error {
if msg, err := p.GoFmt(path); err != nil { if msg, err := p.GoFmt(path); err != nil {
log.Println(pname(p.Name, 1), Red("There are some errors in "), Red(path), Red(":")) log.Println(pname(p.Name, 1), Red("There are some errors in "), Red(path), Red(":"))
fmt.Println(msg) fmt.Println(msg)
return err
} }
} }
return nil return nil
} }
// Walks the file tree of a project
func (p *Project) walks(watcher *fsnotify.Watcher) {
var files, folders int64
wd, _ := os.Getwd()
walk := func(path string, info os.FileInfo, err error) error {
if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.HasPrefix(path, ".")) && !strings.Contains(path, "/.") || (inArray(filepath.Ext(path), p.Watcher.Exts)) {
if p.Watcher.Preview {
fmt.Println(pname(p.Name, 1), ":", path)
}
if err = watcher.Add(path); err != nil {
return filepath.SkipDir
}
if inArray(filepath.Ext(path), p.Watcher.Exts) {
files++
} else {
folders++
}
}
return nil
}
if p.Path == "." || p.Path == "/" {
p.base = wd
p.Path = WorkingDir()
} else {
p.base = filepath.Join(wd, p.Path)
}
for _, dir := range p.Watcher.Paths {
base := filepath.Join(p.base, dir)
if _, err := os.Stat(base); err == nil {
if !p.ignore(base) {
// check gofmt errors
if err := p.fmt(base); err != nil {
fmt.Println(err)
}
if err := filepath.Walk(base, walk); err != nil {
log.Println(Red(err.Error()))
}
}
} else {
fmt.Println(pname(p.Name, 1), ":\t", Red(base+" path doesn't exist"))
}
}
fmt.Println(Red("Watching: "), pname(p.Name, 1), Magenta(files), "files", Magenta(folders), "folders")
fmt.Println()
}
// Ignore validates a path // Ignore validates a path
func (p *Project) ignore(str string) bool { func (p *Project) ignore(str string) bool {
for _, v := range p.Watcher.Ignore { for _, v := range p.Watcher.Ignore {