1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 14:27:47 +01:00
gojekyll/server/watcher.go

65 lines
1.5 KiB
Go
Raw Normal View History

2017-06-22 23:37:46 +02:00
package server
2017-07-15 15:44:22 +02:00
import (
"fmt"
"os"
"time"
"github.com/osteele/gojekyll/site"
)
2017-07-21 22:29:38 +02:00
// Create a goroutine that rebuilds the site when files change.
func (s *Server) watchReload() error {
2017-07-09 15:37:23 +02:00
site := s.Site
2017-07-25 15:05:29 +02:00
// FIXME reload swaps in a new site but we're still watching the old one.
// This won't pick up changes to include, exclude, etc.
2017-07-15 15:44:22 +02:00
changes, err := site.WatchFiles()
if err != nil {
return err
}
go func() {
for change := range changes {
// Resolves filenames to URLS *before* reloading the site, in case the latter
// changes the url -> filename routes.
urls := map[string]bool{}
for _, rel := range change.Paths {
url, ok := site.FilenameURLPath(rel)
2017-07-15 15:44:22 +02:00
if ok {
urls[url] = true
}
}
2017-08-09 01:44:04 +02:00
if site.RequiresFullReload(change.Paths) {
for u := range site.Routes {
urls[u] = true
}
}
2017-07-21 22:29:38 +02:00
// reload the site
2017-07-15 15:44:22 +02:00
s.reload(change)
2017-07-21 22:29:38 +02:00
// tell the pages their files (may have) changed
2017-07-15 15:44:22 +02:00
for url := range urls {
s.lr.Reload(url)
2017-07-09 15:37:23 +02:00
}
}
2017-07-15 15:44:22 +02:00
}()
return nil
}
func (s *Server) reload(change site.FilesEvent) {
s.Lock()
defer s.Unlock()
2017-07-21 22:29:38 +02:00
// similar code to site.WatchRebuild
2017-07-15 15:44:22 +02:00
fmt.Printf("Re-reading: %v...", change)
2017-07-21 22:29:38 +02:00
start := time.Now()
site, err := s.Site.Reloaded(change.Paths)
2017-07-15 15:44:22 +02:00
if err != nil {
fmt.Println()
fmt.Fprintln(os.Stderr, err.Error())
s.lr.Alert(fmt.Sprintf("Error reading site configuration: %s", err))
2017-07-15 15:44:22 +02:00
return
}
s.Site = site
s.Site.SetAbsoluteURL("")
fmt.Printf("done (%.2fs)\n", time.Since(start).Seconds())
2017-07-09 15:37:23 +02:00
}