2017-06-22 23:37:46 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2017-07-09 22:17:20 +02:00
|
|
|
"github.com/osteele/gojekyll/utils"
|
2017-06-22 23:37:46 +02:00
|
|
|
)
|
|
|
|
|
2017-07-09 15:37:23 +02:00
|
|
|
func (s *Server) watchAndReload() error {
|
|
|
|
site := s.Site
|
|
|
|
return s.watchFiles(func(filenames []string) {
|
|
|
|
// Resolve to URLS *before* reloading the site, in case the latter
|
|
|
|
// remaps permalinks.
|
|
|
|
urls := map[string]bool{}
|
|
|
|
for _, relpath := range filenames {
|
|
|
|
url, ok := site.FilenameURLPath(relpath)
|
|
|
|
if ok {
|
|
|
|
urls[url] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.reloadSite()
|
|
|
|
for url := range urls {
|
|
|
|
s.lr.Reload(url)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// calls fn repeatedly in a goroutine
|
|
|
|
func (s *Server) watchFiles(fn func([]string)) error {
|
2017-06-22 23:37:46 +02:00
|
|
|
var (
|
|
|
|
site = s.Site
|
2017-07-09 15:37:23 +02:00
|
|
|
sourceDir = site.SourceDir()
|
2017-06-22 23:37:46 +02:00
|
|
|
events = make(chan string)
|
|
|
|
debounced = debounce(time.Second, events)
|
|
|
|
)
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-watcher.Events:
|
|
|
|
events <- event.Name
|
|
|
|
case err := <-watcher.Errors:
|
|
|
|
log.Println("error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
for {
|
2017-06-23 15:32:08 +02:00
|
|
|
filenames := <-debounced
|
2017-07-09 15:37:23 +02:00
|
|
|
relpaths := make([]string, 0, len(filenames))
|
2017-06-23 15:32:08 +02:00
|
|
|
for _, filename := range filenames {
|
2017-07-09 22:17:20 +02:00
|
|
|
relpath := utils.MustRel(sourceDir, filename)
|
2017-07-09 15:37:23 +02:00
|
|
|
if relpath == "_config.yml" || !site.Exclude(relpath) {
|
|
|
|
relpaths = append(relpaths, relpath)
|
2017-06-22 23:37:46 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-09 15:37:23 +02:00
|
|
|
fn(relpaths)
|
2017-06-22 23:37:46 +02:00
|
|
|
}
|
|
|
|
}()
|
2017-07-09 15:37:23 +02:00
|
|
|
return watcher.Add(sourceDir)
|
2017-06-22 23:37:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// debounce relays values from input to output, merging successive values within interval
|
|
|
|
// TODO consider https://github.com/ReactiveX/RxGo
|
2017-06-29 20:21:27 +02:00
|
|
|
func debounce(interval time.Duration, input <-chan string) <-chan []string {
|
|
|
|
output := make(chan []string)
|
2017-06-22 23:37:46 +02:00
|
|
|
var (
|
|
|
|
pending = []string{}
|
2017-06-29 04:46:19 +02:00
|
|
|
ticker = time.Tick(interval) // nolint: staticcheck, megacheck
|
2017-06-22 23:37:46 +02:00
|
|
|
)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case value := <-input:
|
|
|
|
pending = append(pending, value)
|
|
|
|
case <-ticker:
|
|
|
|
if len(pending) > 0 {
|
|
|
|
output <- pending
|
|
|
|
pending = []string{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-06-29 20:21:27 +02:00
|
|
|
return output
|
2017-06-22 23:37:46 +02:00
|
|
|
}
|