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-08-01 14:16:09 +00:00
|
|
|
"time"
|
2016-07-29 23:02:11 +00:00
|
|
|
)
|
|
|
|
|
2016-08-03 08:30:45 +00:00
|
|
|
type Watcher struct{
|
|
|
|
// different before and after on re-run?
|
|
|
|
Before []string `yaml:"before,omitempty"`
|
|
|
|
After []string `yaml:"after,omitempty"`
|
|
|
|
Paths []string `yaml:"paths,omitempty"`
|
|
|
|
Ignore []string `yaml:"ignore_paths,omitempty"`
|
|
|
|
Exts []string `yaml:"exts,omitempty"`
|
|
|
|
Preview bool `yaml:"preview,omitempty"`
|
2016-07-31 21:39:56 +00:00
|
|
|
}
|
|
|
|
|
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-03 08:30:45 +00:00
|
|
|
p.GoBuild()
|
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-03 08:30:45 +00:00
|
|
|
base, _ := os.Getwd()
|
2016-08-01 23:23:12 +00:00
|
|
|
|
2016-08-03 08:30:45 +00:00
|
|
|
// check path existence
|
|
|
|
if _, err := os.Stat(base + p.Path + dir); err == nil {
|
|
|
|
if err := filepath.Walk(base + p.Path + dir, walk); err != nil {
|
2016-08-01 23:23:12 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}else{
|
2016-08-03 08:30:45 +00:00
|
|
|
fmt.Println(red(p.Name + ": \t"+base + p.Path + dir +" path doesn't exist"))
|
2016-08-01 17:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 08:30:45 +00:00
|
|
|
fmt.Println(red("Watching: '"+ p.Name +"'\n"))
|
2016-08-01 22:52:17 +00:00
|
|
|
|
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
|
|
|
|
}
|
2016-08-03 08:30:45 +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-07-29 23:02:11 +00:00
|
|
|
}
|