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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create a goroutine that rebuilds the site when files change
|
2017-07-09 15:37:23 +02:00
|
|
|
func (s *Server) watchAndReload() error {
|
|
|
|
site := s.Site
|
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 _, relpath := range change.Paths {
|
|
|
|
url, ok := site.FilenameURLPath(relpath)
|
|
|
|
if ok {
|
|
|
|
urls[url] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.reload(change)
|
|
|
|
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()
|
|
|
|
|
|
|
|
// DRY w/ site.WatchRebuild
|
|
|
|
start := time.Now()
|
|
|
|
fmt.Printf("Re-reading: %v...", change)
|
|
|
|
site, err := s.Site.Reloaded()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.Site = site
|
|
|
|
s.Site.SetAbsoluteURL("")
|
|
|
|
fmt.Printf("done (%.2fs)\n", time.Since(start).Seconds())
|
2017-07-09 15:37:23 +02:00
|
|
|
}
|