realize/realize/watcher.go

123 lines
2.4 KiB
Go
Raw Normal View History

2016-07-29 23:02:11 +00:00
package realize
import (
"github.com/fsnotify/fsnotify"
2016-07-31 21:39:56 +00:00
"fmt"
2016-07-29 23:02:11 +00:00
"path/filepath"
"os"
2016-07-31 21:39:56 +00:00
"log"
"strings"
2016-07-31 23:52:11 +00:00
"sync"
2016-08-01 14:16:09 +00:00
"time"
2016-08-01 17:08:37 +00:00
"bytes"
2016-07-29 23:02:11 +00:00
)
2016-08-01 17:08:37 +00:00
var wg sync.WaitGroup
2016-07-31 21:39:56 +00:00
func InArray(str string, list []string) bool{
for _, v := range list {
if v == str {
return true
}
}
return false
}
func Ignore(str string, list []string) bool{
for _, v := range list {
if strings.Contains(str, v) {
return true
}
}
return false
}
2016-08-01 19:03:38 +00:00
func (p *Project) Watching(){
2016-07-31 23:52:11 +00:00
var watcher *fsnotify.Watcher
2016-08-01 17:08:37 +00:00
watcher, _ = fsnotify.NewWatcher()
defer func(){
watcher.Close()
wg.Done()
}()
2016-07-29 23:02:11 +00:00
2016-07-31 21:39:56 +00:00
walk := func(path string, info os.FileInfo, err error) error{
2016-08-01 19:03:38 +00:00
if !Ignore(path,p.Watcher.Ignore) {
if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.Contains(path, "/.")) || (InArray(filepath.Ext(path), p.Watcher.Exts)){
if p.Watcher.Preview {
2016-08-01 22:52:17 +00:00
fmt.Println(p.Name + ": \t" + path)
2016-07-31 21:39:56 +00:00
}
if err = watcher.Add(path); err != nil {
return filepath.SkipDir
}
2016-07-29 23:02:11 +00:00
}
}
return nil
}
2016-08-01 17:08:37 +00:00
// run, bin, build
2016-08-01 14:16:09 +00:00
2016-08-01 19:03:38 +00:00
p.reload = time.Now().Truncate(time.Second)
2016-08-01 14:16:09 +00:00
2016-08-01 19:03:38 +00:00
for _, dir := range p.Watcher.Paths {
2016-08-01 14:16:09 +00:00
2016-08-01 17:08:37 +00:00
var base bytes.Buffer
path, _ := os.Getwd()
2016-08-01 19:03:38 +00:00
split := strings.Split(p.Main, "/")
2016-08-01 17:08:37 +00:00
// get base path from mail field
for key, str := range split{
if(key < len(split)-1) {
base.WriteString("/" + str)
2016-07-31 23:52:11 +00:00
}
}
2016-08-01 17:08:37 +00:00
2016-08-01 23:23:12 +00:00
// add slash if doesn't exist
if string(dir[0]) != "/"{
dir = "/"+dir
}
if _, err := os.Stat(path + base.String() + dir); err == nil {
if err := filepath.Walk(path + base.String() + dir, walk); err != nil {
fmt.Println(err)
}
}else{
fmt.Println(red(p.Name + ": \t"+path + base.String() + dir +" doesn't exist"))
2016-08-01 17:08:37 +00:00
}
}
2016-08-01 22:52:17 +00:00
fmt.Println("\nWatching..\n")
2016-08-01 17:08:37 +00:00
for {
select {
case event := <-watcher.Events:
2016-08-01 19:03:38 +00:00
if time.Now().Truncate(time.Second).After(p.reload) {
2016-08-01 17:08:37 +00:00
if event.Op & fsnotify.Chmod == fsnotify.Chmod {
continue
}
if _, err := os.Stat(event.Name); err == nil {
i := strings.Index(event.Name, filepath.Ext(event.Name))
2016-08-01 19:03:38 +00:00
log.Println(green(p.Name+":")+"\t", event.Name[:i])
2016-08-01 17:08:37 +00:00
// run, bin, build
2016-08-01 19:03:38 +00:00
p.reload = time.Now().Truncate(time.Second)
2016-08-01 17:08:37 +00:00
}
2016-07-31 21:39:56 +00:00
}
2016-08-01 17:08:37 +00:00
case err := <-watcher.Errors:
log.Println("error:", err)
2016-07-31 21:39:56 +00:00
}
2016-07-31 23:52:11 +00:00
}
2016-08-01 17:08:37 +00:00
}
2016-07-31 21:39:56 +00:00
2016-08-01 17:08:37 +00:00
func (h *Config) Watch() error{
2016-07-29 23:02:11 +00:00
if err := h.Read(); err == nil {
// loop projects
2016-07-31 23:52:11 +00:00
wg.Add(len(h.Projects))
2016-08-01 19:03:38 +00:00
for k := range h.Projects {
go h.Projects[k].Watching()
2016-07-29 23:02:11 +00:00
}
2016-07-31 23:52:11 +00:00
wg.Wait()
2016-07-29 23:02:11 +00:00
return nil
}else{
return err
}
}