2016-07-29 23:02:11 +00:00
|
|
|
package realize
|
|
|
|
|
|
|
|
import (
|
2016-07-31 21:39:56 +00:00
|
|
|
"fmt"
|
2016-08-17 23:35:37 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
"log"
|
2016-07-29 23:02:11 +00:00
|
|
|
"os"
|
2016-08-17 23:35:37 +00:00
|
|
|
"path/filepath"
|
2016-07-31 21:39:56 +00:00
|
|
|
"strings"
|
2016-08-16 10:20:55 +00:00
|
|
|
"sync"
|
2016-08-17 23:35:37 +00:00
|
|
|
"time"
|
2016-07-29 23:02:11 +00:00
|
|
|
)
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// The Watcher struct defines the livereload's logic
|
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-17 23:35:37 +00:00
|
|
|
Preview bool `yaml:"preview,omitempty"`
|
2016-07-31 21:39:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// Watch method adds the given paths on the Watcher
|
2016-08-04 22:59:41 +00:00
|
|
|
func (h *Config) Watch() error {
|
2016-08-18 07:35:06 +00:00
|
|
|
err := h.Read()
|
2016-08-18 07:27:08 +00:00
|
|
|
if err == nil {
|
2016-08-04 19:56:42 +00:00
|
|
|
// 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)
|
2016-08-04 19:56:42 +00:00
|
|
|
go h.Projects[k].Watching()
|
2016-08-03 16:49:37 +00:00
|
|
|
}
|
2016-08-04 19:56:42 +00:00
|
|
|
wg.Wait()
|
|
|
|
return nil
|
2016-08-03 16:49:37 +00:00
|
|
|
}
|
2016-08-18 07:27:08 +00:00
|
|
|
return err
|
2016-08-03 16:49:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// Watching method is the main core. It manages the livereload and the watching
|
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()
|
2016-08-17 23:35:37 +00:00
|
|
|
if err != nil {
|
2016-08-17 14:56:06 +00:00
|
|
|
Fail(p.Name + ": \t" + err.Error())
|
|
|
|
}
|
2016-08-17 23:35:37 +00:00
|
|
|
channel := make(chan bool, 1)
|
2016-08-17 14:56:06 +00:00
|
|
|
base, err := os.Getwd()
|
2016-08-17 23:35:37 +00:00
|
|
|
if err != nil {
|
2016-08-17 14:56:06 +00:00
|
|
|
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-17 23:35:37 +00:00
|
|
|
routines := func() {
|
2016-08-16 10:20:55 +00:00
|
|
|
channel = make(chan bool)
|
|
|
|
wr.Add(1)
|
2016-08-17 23:35:37 +00:00
|
|
|
go p.build()
|
2016-08-19 20:52:53 +00:00
|
|
|
go p.install(channel,&wr)
|
2016-08-16 10:20:55 +00:00
|
|
|
}
|
2016-08-17 23:35:37 +00:00
|
|
|
end := func() {
|
2016-08-17 14:56:06 +00:00
|
|
|
watcher.Close()
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
defer end()
|
|
|
|
|
2016-08-17 18:06:09 +00:00
|
|
|
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)
|
2016-08-16 07:35:07 +00:00
|
|
|
|
2016-08-17 18:06:09 +00:00
|
|
|
base = p.base + dir
|
2016-08-14 15:22:20 +00:00
|
|
|
if _, err := os.Stat(base); err == nil {
|
|
|
|
if err := filepath.Walk(base, walk); err != nil {
|
2016-08-04 19:56:42 +00:00
|
|
|
Fail(err.Error())
|
2016-08-01 23:23:12 +00:00
|
|
|
}
|
2016-08-04 22:59:41 +00:00
|
|
|
} else {
|
2016-08-14 15:22:20 +00:00
|
|
|
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-19 21:32:36 +00:00
|
|
|
fmt.Println(red("Watching: '" + p.Name + "'\n"))
|
2016-08-04 19:56:42 +00:00
|
|
|
p.reload = time.Now().Truncate(time.Second)
|
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) {
|
2016-08-17 23:35:37 +00:00
|
|
|
if event.Op&fsnotify.Chmod == fsnotify.Chmod {
|
2016-08-04 22:59:41 +00:00
|
|
|
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))
|
2016-08-20 10:47:32 +00:00
|
|
|
if event.Name[:i] != "" {
|
|
|
|
log.Println(bluel(p.Name + ": File changed ->"), bluel(event.Name[:i]))
|
2016-08-04 22:59:41 +00:00
|
|
|
|
2016-08-20 10:47:32 +00:00
|
|
|
// stop and run again
|
|
|
|
close(channel)
|
|
|
|
wr.Wait()
|
|
|
|
routines()
|
2016-08-04 22:59:41 +00:00
|
|
|
|
2016-08-20 10:47:32 +00:00
|
|
|
p.reload = time.Now().Truncate(time.Second)
|
|
|
|
}
|
2016-08-04 22:59:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case err := <-watcher.Errors:
|
|
|
|
Fail(err.Error())
|
2016-08-04 19:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-03 16:49:37 +00:00
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// Install call an implementation of the "go install"
|
2016-08-19 20:52:53 +00:00
|
|
|
func (p *Project) install(channel chan bool,wr *sync.WaitGroup) {
|
2016-08-04 19:56:42 +00:00
|
|
|
if p.Bin {
|
|
|
|
LogSuccess(p.Name + ": Installing..")
|
2016-08-20 13:49:10 +00:00
|
|
|
//start := time.Now()
|
2016-08-04 22:59:41 +00:00
|
|
|
if err := p.GoInstall(); err != nil {
|
2016-08-20 10:47:32 +00:00
|
|
|
Fail(p.Name + ": "+err.Error())
|
2016-08-19 20:52:53 +00:00
|
|
|
wr.Done()
|
2016-08-04 22:59:41 +00:00
|
|
|
} else {
|
2016-08-20 13:49:10 +00:00
|
|
|
LogSuccess(p.Name + ": Installed ")
|
2016-08-19 20:52:53 +00:00
|
|
|
if p.Run {
|
|
|
|
runner := make(chan bool, 1)
|
|
|
|
LogSuccess(p.Name + ": Running..")
|
|
|
|
go p.GoRun(channel, runner, wr)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-runner:
|
2016-08-19 21:52:04 +00:00
|
|
|
LogSuccess(p.Name + ": Has been run")
|
2016-08-19 20:52:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-31 21:39:56 +00:00
|
|
|
}
|
2016-07-31 23:52:11 +00:00
|
|
|
}
|
2016-08-04 19:56:42 +00:00
|
|
|
return
|
2016-08-01 17:08:37 +00:00
|
|
|
}
|
2016-07-31 21:39:56 +00:00
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// Build call an implementation of the "go build"
|
2016-08-04 22:59:41 +00:00
|
|
|
func (p *Project) build() {
|
2016-08-04 19:56:42 +00:00
|
|
|
if p.Build {
|
|
|
|
LogSuccess(p.Name + ": Building..")
|
2016-08-20 13:49:10 +00:00
|
|
|
//start := time.Now()
|
2016-08-04 22:59:41 +00:00
|
|
|
if err := p.GoBuild(); err != nil {
|
2016-08-20 10:47:32 +00:00
|
|
|
Fail(p.Name + ": "+err.Error())
|
2016-08-04 22:59:41 +00:00
|
|
|
} else {
|
2016-08-20 13:49:10 +00:00
|
|
|
LogSuccess(p.Name + ": Builded ")
|
2016-07-29 23:02:11 +00:00
|
|
|
}
|
2016-08-18 07:27:08 +00:00
|
|
|
return
|
2016-07-29 23:02:11 +00:00
|
|
|
}
|
2016-08-04 19:56:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// Ignore validates a path
|
2016-08-17 14:56:06 +00:00
|
|
|
func (p *Project) ignore(str string) bool {
|
|
|
|
for _, v := range p.Watcher.Ignore {
|
|
|
|
v = slash(v)
|
2016-08-17 23:35:37 +00:00
|
|
|
if strings.Contains(str, p.base+v) {
|
2016-08-03 08:30:45 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// check if a string is inArray
|
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-14 15:22:20 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// add a slash at the beginning if not exist
|
2016-08-17 23:35:37 +00:00
|
|
|
func slash(str string) string {
|
2016-08-17 14:56:06 +00:00
|
|
|
if string(str[0]) != "/" {
|
2016-08-17 23:35:37 +00:00
|
|
|
str = "/" + str
|
2016-08-17 14:56:06 +00:00
|
|
|
}
|
2016-08-17 23:35:37 +00:00
|
|
|
if string(str[len(str)-1]) == "/" {
|
|
|
|
if string(str) == "/" {
|
2016-08-17 14:56:06 +00:00
|
|
|
str = ""
|
2016-08-17 23:35:37 +00:00
|
|
|
} else {
|
|
|
|
str = str[0 : len(str)-2]
|
2016-08-17 14:56:06 +00:00
|
|
|
}
|
2016-08-14 15:22:20 +00:00
|
|
|
}
|
2016-08-17 14:56:06 +00:00
|
|
|
return str
|
2016-08-17 23:35:37 +00:00
|
|
|
}
|