This commit is contained in:
alessio 2017-09-18 03:04:03 +02:00
parent f1bd8e9357
commit bf55c51676
1 changed files with 32 additions and 33 deletions

View File

@ -23,16 +23,13 @@ var out BufferOut
// Project defines the informations of a single project // Project defines the informations of a single project
type Project struct { type Project struct {
settings.Settings settings.Settings `yaml:"-" json:"-"`
parent *Blueprint parent *Blueprint
path string path string
tools tools tools tools
wr sync.WaitGroup
watcher *fsnotify.Watcher
channel chan bool
exit chan os.Signal
base string base string
LastChangedOn time.Time `yaml:"-" json:"-"` paths []string
lastChangedOn time.Time
Name string `yaml:"name" json:"name"` Name string `yaml:"name" json:"name"`
Path string `yaml:"path" json:"path"` Path string `yaml:"path" json:"path"`
Environment map[string]string `yaml:"environment,omitempty" json:"environment,omitempty"` Environment map[string]string `yaml:"environment,omitempty" json:"environment,omitempty"`
@ -58,20 +55,18 @@ func (p *Project) watchByNotify() {
}() }()
p.cmd("before", true) p.cmd("before", true)
go p.routines(&wr, channel, watcher, "") go p.routines(&wr, channel, watcher, "")
p.LastChangedOn = time.Now().Truncate(time.Second) p.lastChangedOn = time.Now().Truncate(time.Second)
L: L:
for { for {
select { select {
case event := <-watcher.Events: case event := <-watcher.Events:
if time.Now().Truncate(time.Second).After(p.LastChangedOn) { if time.Now().Truncate(time.Second).After(p.lastChangedOn) {
if event.Op&fsnotify.Chmod == fsnotify.Chmod { p.lastChangedOn = time.Now().Truncate(time.Second)
continue if file, err := os.Lstat(event.Name); err == nil {
} if file.Size() > 0 {
if index := strings.Index(filepath.Ext(event.Name), "__"); index != -1 { p.lastChangedOn = time.Now().Truncate(time.Second)
continue
}
ext := filepath.Ext(event.Name) ext := filepath.Ext(event.Name)
if inArray(filepath.Ext(event.Name), p.Watcher.Exts) { if inArray(ext, p.Watcher.Exts) {
if p.Cmds.Run { if p.Cmds.Run {
close(channel) close(channel)
channel = make(chan bool) channel = make(chan bool)
@ -82,11 +77,14 @@ L:
p.stamp("log", out, msg, "") p.stamp("log", out, msg, "")
// check if is deleted // check if is deleted
if event.Op&fsnotify.Remove == fsnotify.Remove { if event.Op&fsnotify.Remove == fsnotify.Remove {
watcher.Remove(event.Name)
go p.routines(&wr, channel, watcher, "") go p.routines(&wr, channel, watcher, "")
} else { } else {
go p.routines(&wr, channel, watcher, event.Name) go p.routines(&wr, channel, watcher, event.Name)
} }
p.LastChangedOn = time.Now().Truncate(time.Second) p.lastChangedOn = time.Now().Truncate(time.Second)
}
}
} }
} }
case err := <-watcher.Errors: case err := <-watcher.Errors:
@ -113,13 +111,13 @@ func (p *Project) watchByPolling() {
}() }()
p.cmd("before", true) p.cmd("before", true)
go p.routines(&wr, channel, watcher, "") go p.routines(&wr, channel, watcher, "")
p.LastChangedOn = time.Now().Truncate(time.Second) p.lastChangedOn = time.Now().Truncate(time.Second)
walk := func(changed string, info os.FileInfo, err error) error { walk := func(changed string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} else if !watcher.isWatching(changed) { } else if !watcher.isWatching(changed) {
return nil return nil
} else if !info.ModTime().Truncate(time.Second).After(p.LastChangedOn) { } else if !info.ModTime().Truncate(time.Second).After(p.lastChangedOn) {
return nil return nil
} }
if index := strings.Index(filepath.Ext(changed), "__"); index != -1 { if index := strings.Index(filepath.Ext(changed), "__"); index != -1 {
@ -131,7 +129,7 @@ func (p *Project) watchByPolling() {
close(channel) close(channel)
channel = make(chan bool) channel = make(chan bool)
} }
p.LastChangedOn = time.Now().Truncate(time.Second) p.lastChangedOn = time.Now().Truncate(time.Second)
// repeat the initial cycle // repeat the initial cycle
msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(changed)) msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(changed))
out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + changed} out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + changed}
@ -226,6 +224,7 @@ func (p *Project) walk(watcher watcher) error {
return filepath.SkipDir return filepath.SkipDir
} }
if inArray(filepath.Ext(path), p.Watcher.Exts) { if inArray(filepath.Ext(path), p.Watcher.Exts) {
p.paths = append(p.paths, path)
files++ files++
} else { } else {
folders++ folders++