1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 07:08:59 +01:00
gojekyll/site/watch.go

148 lines
3.3 KiB
Go
Raw Permalink Normal View History

2017-07-15 03:19:01 +02:00
package site
import (
"fmt"
2017-07-23 17:25:17 +02:00
"log"
2017-07-15 03:19:01 +02:00
"os"
2017-07-23 17:25:17 +02:00
"path/filepath"
2017-07-15 03:19:01 +02:00
"time"
"github.com/fsnotify/fsnotify"
"github.com/danog/gojekyll/utils"
2017-07-23 17:25:17 +02:00
"github.com/radovskyb/watcher"
2017-07-15 03:19:01 +02:00
)
2017-07-15 15:44:22 +02:00
// FilesEvent is a list of changed or added site source files, with a single
// timestamp that approximates when they were changed.
type FilesEvent struct {
Time time.Time // A single time is used for all the changes
Paths []string // relative to site source
}
func (e FilesEvent) String() string {
count := len(e.Paths)
inflect := map[bool]string{true: "", false: "s"}[count == 1]
return fmt.Sprintf("%d file%s changed at %s", count, inflect, e.Time.Format("3:04:05PM"))
}
2017-07-25 14:57:32 +02:00
// WatchFiles returns a channel that receives FilesEvent on changes within the site directory.
func (s *Site) WatchFiles() (<-chan FilesEvent, error) {
2017-07-23 17:25:17 +02:00
filenames, err := s.makeFileWatcher()
if err != nil {
return nil, err
}
var (
debounced = debounce(time.Second/2, filenames)
filesets = make(chan FilesEvent)
)
2017-07-15 03:19:01 +02:00
go func() {
for {
2017-07-25 15:29:38 +02:00
paths := s.affectsBuildFilter(<-debounced)
2017-07-15 03:19:01 +02:00
if len(paths) > 0 {
2017-07-23 17:25:17 +02:00
// Create a new timestamp. Except under pathological
2017-07-15 15:44:22 +02:00
// circumstances, it will be close enough.
filesets <- FilesEvent{time.Now(), paths}
2017-07-15 03:19:01 +02:00
}
}
}()
return filesets, nil
2017-07-15 03:19:01 +02:00
}
2017-07-25 14:57:32 +02:00
func (s *Site) makeFileWatcher() (<-chan string, error) {
2017-07-25 17:08:53 +02:00
switch {
2017-08-24 18:25:38 +02:00
case s.cfg.ForcePolling:
2017-07-25 14:57:32 +02:00
return s.makePollingWatcher()
2017-07-25 17:08:53 +02:00
default:
return s.makeEventWatcher()
2017-07-15 15:44:22 +02:00
}
}
2017-07-26 18:47:31 +02:00
func (s *Site) makeEventWatcher() (<-chan string, error) {
var (
sourceDir = s.SourceDir()
filenames = make(chan string, 100)
w, err = fsnotify.NewWatcher()
)
if err != nil {
return nil, err
}
go func() {
for {
select {
case event := <-w.Events:
filenames <- utils.MustRel(sourceDir, event.Name)
case err := <-w.Errors:
fmt.Fprintln(os.Stderr, "error:", err)
}
}
}()
return filenames, w.Add(sourceDir)
}
2017-07-23 17:25:17 +02:00
func (s *Site) makePollingWatcher() (<-chan string, error) {
var (
sourceDir = utils.MustAbs(s.SourceDir())
filenames = make(chan string, 100)
w = watcher.New()
)
if err := w.AddRecursive(sourceDir); err != nil {
return nil, err
}
2017-08-24 18:25:38 +02:00
for _, path := range s.cfg.Exclude {
2017-07-23 17:25:17 +02:00
if err := w.Ignore(filepath.Join(sourceDir, path)); err != nil {
return nil, err
}
}
if err := w.Ignore(s.DestDir()); err != nil {
return nil, err
}
go func() {
for {
select {
case event := <-w.Event:
filenames <- utils.MustRel(sourceDir, event.Path)
case err := <-w.Error:
fmt.Fprintln(os.Stderr, "error:", err)
case <-w.Closed:
return
}
}
}()
go func() {
if err := w.Start(time.Millisecond * 250); err != nil {
log.Fatal(err)
}
}()
return filenames, nil
}
// debounce relays values from input to output, merging successive values so long as they keep changing
// faster than interval
2017-07-15 03:19:01 +02:00
// TODO consider https://github.com/ReactiveX/RxGo
func debounce(interval time.Duration, input <-chan string) <-chan []string {
var (
pending = []string{}
2017-07-23 17:25:17 +02:00
output = make(chan []string)
ticker <-chan time.Time
2017-07-15 03:19:01 +02:00
)
go func() {
for {
select {
case value := <-input:
2017-07-23 17:25:17 +02:00
if value == "." {
continue
}
2017-07-15 03:19:01 +02:00
pending = append(pending, value)
ticker = time.After(interval) // replaces the previous ticker
2017-07-15 03:19:01 +02:00
case <-ticker:
ticker = nil
2017-07-15 03:19:01 +02:00
if len(pending) > 0 {
output <- pending
pending = []string{}
}
}
}
}()
return output
}