1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-23 05:01:14 +01:00

Site reload takes list of paths

This commit is contained in:
Oliver Steele 2017-07-21 16:29:38 -04:00
parent b53e16f472
commit 5e5aa3acb7
4 changed files with 39 additions and 14 deletions

View File

@ -35,7 +35,7 @@ func (s *Server) Run(open bool, logger func(label, value string)) error {
if err := s.StartLiveReloader(); err != nil {
return err
}
if err := s.watchAndReload(); err != nil {
if err := s.watchReload(); err != nil {
return err
}
}

View File

@ -8,9 +8,10 @@ import (
"github.com/osteele/gojekyll/site"
)
// Create a goroutine that rebuilds the site when files change
func (s *Server) watchAndReload() error {
// Create a goroutine that rebuilds the site when files change.
func (s *Server) watchReload() error {
site := s.Site
// FIXME reload swaps in a new site but we're still watching the old one
changes, err := site.WatchFiles()
if err != nil {
return err
@ -26,7 +27,9 @@ func (s *Server) watchAndReload() error {
urls[url] = true
}
}
// reload the site
s.reload(change)
// tell the pages their files (may have) changed
for url := range urls {
s.lr.Reload(url)
}
@ -39,10 +42,10 @@ func (s *Server) reload(change site.FilesEvent) {
s.Lock()
defer s.Unlock()
// DRY w/ site.WatchRebuild
start := time.Now()
// similar code to site.WatchRebuild
fmt.Printf("Re-reading: %v...", change)
site, err := s.Site.Reloaded()
start := time.Now()
site, err := s.Site.Reloaded(change.Paths)
if err != nil {
fmt.Println()
fmt.Fprintln(os.Stderr, err.Error())

View File

@ -1,9 +1,11 @@
package site
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/osteele/gojekyll/collection"
"github.com/osteele/gojekyll/config"
@ -50,12 +52,31 @@ func (s *Site) Read() error {
}
// Reloaded returns a new site read the same source directory, configuration file, and load flags.
func (s *Site) Reloaded() (*Site, error) {
copy, err := FromDirectory(s.SourceDir(), s.flags)
if err != nil {
return nil, err
func (s *Site) Reloaded(paths []string) (*Site, error) {
if s.requiresFullReload(paths) {
fmt.Println("reload everything")
copy, err := FromDirectory(s.SourceDir(), s.flags)
if err != nil {
return nil, err
}
s = copy
}
return copy, copy.Read()
return s, s.Read()
}
func (s *Site) requiresFullReload(paths []string) bool {
for _, path := range paths {
fmt.Println("test", path)
switch {
case path == "_config.yml":
return true
case strings.HasPrefix(path, s.config.DataDir):
return true
case strings.HasPrefix(path, s.config.LayoutsDir):
return true
}
}
return false
}
// readFiles scans the source directory and creates pages and collection.

View File

@ -78,9 +78,10 @@ func (s *Site) WatchRebuild() (<-chan interface{}, error) {
mu.Lock()
defer mu.Unlock()
// similar code to server.reload
events <- fmt.Sprintf("Regenerating: %s...", change)
start := time.Now()
r, count, err := s.rebuild()
r, count, err := s.rebuild(change.Paths)
if err != nil {
fmt.Println()
fmt.Fprintln(os.Stderr, err)
@ -95,8 +96,8 @@ func (s *Site) WatchRebuild() (<-chan interface{}, error) {
}
// reloads and rebuilds the site; returns a copy and count
func (s *Site) rebuild() (r *Site, n int, err error) {
r, err = s.Reloaded()
func (s *Site) rebuild(paths []string) (r *Site, n int, err error) {
r, err = s.Reloaded(paths)
if err != nil {
return
}