realize/realize/watcher.go

198 lines
3.9 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
"strings"
2016-08-17 14:56:06 +00:00
"log"
2016-08-01 14:16:09 +00:00
"time"
2016-08-16 10:20:55 +00:00
"sync"
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 {
2016-08-17 14:56:06 +00:00
h.Projects[k].Path = slash(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
2016-08-16 10:20:55 +00:00
var wr sync.WaitGroup
2016-07-31 23:52:11 +00:00
var watcher *fsnotify.Watcher
2016-08-17 14:56:06 +00:00
watcher, err := fsnotify.NewWatcher()
if(err != nil){
Fail(p.Name + ": \t" + err.Error())
}
channel := make(chan bool,1)
base, err := os.Getwd()
if(err != nil){
Fail(p.Name + ": \t" + err.Error())
}
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 {
2016-08-17 14:56:06 +00:00
if !p.ignore(path) {
2016-08-16 07:35:07 +00:00
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-16 10:20:55 +00:00
routines := func(){
channel = make(chan bool)
wr.Add(1)
go p.build(); p.install(); p.run(channel, &wr);
}
2016-08-17 14:56:06 +00:00
end := func(){
watcher.Close()
wg.Done()
}
defer end()
p.Path = slash(p.Path)
p.Main = slash(p.Main)
p.Base = base + p.Path
2016-07-29 23:02:11 +00:00
2016-08-01 19:03:38 +00:00
for _, dir := range p.Watcher.Paths {
2016-08-04 22:58:00 +00:00
// check main existence
2016-08-17 14:56:06 +00:00
dir = slash(dir)
if _, err := os.Stat(p.Base + dir + p.Main); err != nil {
Fail(p.Name + ": \t" + p.Base + dir + p.Main + " doesn't exist. Main is required")
2016-08-04 22:58:00 +00:00
return
}
2016-08-16 07:35:07 +00:00
2016-08-17 14:56:06 +00:00
base = p.Base + 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
}
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
}
}
2016-08-16 10:20:55 +00:00
routines()
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)
2016-08-16 10:20:55 +00:00
wr.Wait()
routines()
2016-08-04 22:59:41 +00:00
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-16 10:20:55 +00:00
func (p *Project) run(channel chan bool, wr *sync.WaitGroup) {
if p.Run {
LogSuccess(p.Name + ": Running..")
2016-08-16 10:20:55 +00:00
go p.GoRun(channel, wr)
LogSuccess(p.Name + ": Runned")
}
return
2016-08-03 08:30:45 +00:00
}
2016-08-17 14:56:06 +00:00
func (p *Project) ignore(str string) bool {
for _, v := range p.Watcher.Ignore {
v = slash(v)
if strings.Contains(str, p.Base + v) {
2016-08-03 08:30:45 +00:00
return true
}
}
return false
}
2016-08-17 14:56:06 +00:00
func inArray(str string, list []string) bool {
2016-08-03 08:30:45 +00:00
for _, v := range list {
2016-08-17 14:56:06 +00:00
if v == str {
2016-08-03 08:30:45 +00:00
return true
}
}
return false
}
2016-08-17 14:56:06 +00:00
func slash(str string) string{
if string(str[0]) != "/" {
str = "/"+str
}
if string(str[len(str)-1]) == "/"{
if(string(str) == "/"){
str = ""
}else {
str = str[0:len(str) - 2]
}
}
2016-08-17 14:56:06 +00:00
return str
2016-07-29 23:02:11 +00:00
}