realize/realize/watcher.go

185 lines
3.7 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-08-01 14:16:09 +00:00
"time"
2016-07-29 23:02:11 +00:00
)
2016-08-04 22:59:41 +00:00
type Watcher struct {
2016-08-03 08:30:45 +00:00
// different before and after on re-run?
2016-08-04 22:59:41 +00:00
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"`
2016-08-03 08:30:45 +00:00
Preview bool `yaml:"preview,omitempty"`
2016-07-31 21:39:56 +00:00
}
2016-08-04 22:59:41 +00:00
func (h *Config) Watch() error {
if err := h.Read(); err == nil {
// loop projects
wg.Add(len(h.Projects))
for k := range h.Projects {
if !slash(string(h.Projects[k].Path[0])) {
2016-08-04 22:59:41 +00:00
h.Projects[k].Path = "/" + h.Projects[k].Path
}
go h.Projects[k].Watching()
2016-08-03 16:49:37 +00:00
}
wg.Wait()
return nil
2016-08-04 22:59:41 +00:00
} else {
return err
2016-08-03 16:49:37 +00:00
}
}
2016-08-04 22:59:41 +00:00
func (p *Project) Watching() {
2016-07-31 23:52:11 +00:00
var watcher *fsnotify.Watcher
channel := make(chan bool)
2016-08-01 17:08:37 +00:00
watcher, _ = fsnotify.NewWatcher()
2016-08-04 22:59:41 +00:00
defer func() {
2016-08-04 22:58:00 +00:00
watcher.Close()
wg.Done()
}()
2016-07-29 23:02:11 +00:00
2016-08-04 22:59:41 +00:00
walk := func(path string, info os.FileInfo, err error) error {
if !Ignore(path, p.Watcher.Ignore) {
if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.Contains(path, "/.")) || (InArray(filepath.Ext(path), p.Watcher.Exts)) {
2016-08-01 19:03:38 +00:00
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 19:03:38 +00:00
for _, dir := range p.Watcher.Paths {
2016-08-03 08:30:45 +00:00
base, _ := os.Getwd()
2016-08-04 22:58:00 +00:00
// check main existence
if _, err := os.Stat(base + p.Path + dir + p.Main); err != nil {
2016-08-04 22:59:41 +00:00
Fail(p.Name + ": \t" + base + p.Path + dir + p.Main + " doesn't exist. Main is required")
2016-08-04 22:58:00 +00:00
return
}
// check paths existence
if slash(dir) {
base = base + p.Path
}else{
base = base + p.Path + dir
}
if _, err := os.Stat(base); err == nil {
if err := filepath.Walk(base, walk); err != nil {
Fail(err.Error())
2016-08-01 23:23:12 +00:00
}
if slash(dir) {
break
}
2016-08-04 22:59:41 +00:00
} else {
Fail(p.Name + ": \t" + base + " path doesn't exist")
2016-08-01 17:08:37 +00:00
}
}
// go build, install, run
go p.build(); p.install(); p.run(channel);
2016-08-04 22:59:41 +00:00
fmt.Println(red("\n Watching: '" + p.Name + "'\n"))
p.reload = time.Now().Truncate(time.Second)
2016-08-01 22:52:17 +00:00
2016-08-01 17:08:37 +00:00
for {
select {
2016-08-04 22:59:41 +00:00
case event := <-watcher.Events:
if time.Now().Truncate(time.Second).After(p.reload) {
if event.Op & fsnotify.Chmod == fsnotify.Chmod {
continue
2016-08-01 17:08:37 +00:00
}
2016-08-04 22:59:41 +00:00
if _, err := os.Stat(event.Name); err == nil {
i := strings.Index(event.Name, filepath.Ext(event.Name))
log.Println(green(p.Name + ":"), event.Name[:i])
// stop and run again
close(channel)
channel = make(chan bool)
go p.build(); p.install(); p.run(channel);
p.reload = time.Now().Truncate(time.Second)
}
}
case err := <-watcher.Errors:
Fail(err.Error())
}
}
}
2016-08-03 16:49:37 +00:00
2016-08-04 22:59:41 +00:00
func (p *Project) install() {
if p.Bin {
LogSuccess(p.Name + ": Installing..")
2016-08-04 22:59:41 +00:00
if err := p.GoInstall(); err != nil {
Fail(err.Error())
return
2016-08-04 22:59:41 +00:00
} else {
LogSuccess(p.Name + ": Installed")
return
2016-07-31 21:39:56 +00:00
}
2016-07-31 23:52:11 +00:00
}
return
2016-08-01 17:08:37 +00:00
}
2016-07-31 21:39:56 +00:00
2016-08-04 22:59:41 +00:00
func (p *Project) build() {
if p.Build {
LogSuccess(p.Name + ": Building..")
2016-08-04 22:59:41 +00:00
if err := p.GoBuild(); err != nil {
Fail(err.Error())
return
2016-08-04 22:59:41 +00:00
} else {
LogSuccess(p.Name + ": Builded")
return
2016-07-29 23:02:11 +00:00
}
}
return
}
2016-08-04 22:59:41 +00:00
func (p *Project) run(channel chan bool) {
if p.Run {
LogSuccess(p.Name + ": Running..")
go p.GoRun(channel)
LogSuccess(p.Name + ": Runned")
}
return
2016-08-03 08:30:45 +00:00
}
2016-08-04 22:59:41 +00:00
func InArray(str string, list []string) bool {
2016-08-03 08:30:45 +00:00
for _, v := range list {
if v == str {
return true
}
}
return false
}
2016-08-04 22:59:41 +00:00
func Ignore(str string, list []string) bool {
base, _ := os.Getwd()
2016-08-03 08:30:45 +00:00
for _, v := range list {
if !slash(v) {
v = "/" +v
}
if strings.Contains(str, base + v) {
2016-08-03 08:30:45 +00:00
return true
}
}
return false
}
func slash(str string) bool{
if string(str[0]) == "/" {
return true
}
return false
2016-07-29 23:02:11 +00:00
}