1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 10:49:00 +01:00
gojekyll/site/rebuild.go

168 lines
3.9 KiB
Go
Raw Normal View History

2017-07-25 14:57:32 +02:00
package site
import (
"fmt"
"os"
2017-07-26 18:47:31 +02:00
"path/filepath"
2017-07-25 15:05:29 +02:00
"strings"
2017-07-25 14:57:32 +02:00
"time"
2017-07-25 17:08:53 +02:00
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/utils"
2017-07-25 14:57:32 +02:00
)
// WatchRebuild watches the site directory. Each time a file changes, it
// rebuilds the site. It sends status messages (strings) and errors to its output
// channel.
//
// TODO use a logger instead of a message channel?
func (s *Site) WatchRebuild() (<-chan interface{}, error) {
var (
messages = make(chan interface{})
filesets, err = s.WatchFiles()
)
if err != nil {
return nil, err
}
go func() {
for fileset := range filesets {
s = s.processFilesEvent(fileset, messages)
}
}()
return messages, nil
}
2017-07-25 15:05:29 +02:00
// Reloaded returns the same or a new site reading the same source directory, configuration file, and load flags.
// build --incremental and site --incremental use this.
func (s *Site) Reloaded(paths []string) (*Site, error) {
if s.requiresFullReload(paths) {
copy, err := FromDirectory(s.SourceDir(), s.flags)
if err != nil {
return nil, err
}
s = copy
}
return s, s.Read()
}
2017-07-25 14:57:32 +02:00
func (s *Site) processFilesEvent(fileset FilesEvent, messages chan<- interface{}) *Site {
// similar code to server.reload
messages <- fmt.Sprintf("Regenerating: %s...", fileset)
start := time.Now()
r, count, err := s.rebuild(fileset.Paths)
if err != nil {
fmt.Println()
fmt.Fprintln(os.Stderr, err)
return s
}
elapsed := time.Since(start)
2017-07-25 17:08:53 +02:00
inflect := map[bool]string{true: "", false: "s"}[count == 1]
messages <- fmt.Sprintf("wrote %d file%s in %.2fs.\n", count, inflect, elapsed.Seconds())
2017-07-25 14:57:32 +02:00
return r
}
2017-07-25 15:29:38 +02:00
// reloads and rebuilds the site; returns a copy and count
2017-07-25 17:08:53 +02:00
func (s *Site) rebuild(paths []string) (r *Site, n int, err error) {
2017-07-25 15:29:38 +02:00
if s.requiresFullReload(paths) {
2017-07-25 17:08:53 +02:00
r, err = s.Reloaded(paths)
2017-07-25 15:29:38 +02:00
if err != nil {
2017-07-25 17:08:53 +02:00
return
}
n, err = r.Build()
return
}
r = s
pathSet := utils.StringSet(paths)
for _, d := range s.docs {
if s.invalidatesDoc(pathSet, d) {
err = d.Reload()
if err != nil {
return
}
err = s.WriteDoc(d)
if err != nil {
return
}
n++
2017-07-25 15:29:38 +02:00
}
}
2017-07-25 17:08:53 +02:00
return
2017-07-25 15:29:38 +02:00
}
2017-07-25 17:08:53 +02:00
// Return true if a source file requires a full reload / rebuild.
//
// This is always true outside of incremental mode, since even a
// static asset can cause pages to change if they reference its
// variables.
//
// This function works on relative paths. It does not work for theme
// sources.
2017-07-25 15:05:29 +02:00
func (s *Site) requiresFullReload(paths []string) bool {
for _, path := range paths {
switch {
2017-07-25 15:29:38 +02:00
case s.config.IsConfigPath(path):
return true
case s.Exclude(path):
return false
case !s.config.Incremental:
2017-07-25 15:05:29 +02:00
return true
2017-07-25 17:08:53 +02:00
case strings.HasPrefix(path, s.config.SassDir()):
return true
2017-07-25 15:05:29 +02:00
case strings.HasPrefix(path, s.config.DataDir):
return true
case strings.HasPrefix(path, s.config.LayoutsDir):
return true
}
}
return false
}
2017-07-25 17:08:53 +02:00
// De-dup relative paths, and filter to those that might affect the build.
//
// Site watch uses this to decide when to send events.
func (s *Site) affectsBuildFilter(paths []string) []string {
2017-07-25 14:57:32 +02:00
var (
2017-07-25 17:08:53 +02:00
result = make([]string, 0, len(paths))
2017-07-25 15:29:38 +02:00
seen = map[string]bool{}
2017-07-25 14:57:32 +02:00
)
2017-07-25 15:29:38 +02:00
loop:
2017-07-25 17:08:53 +02:00
for _, path := range paths {
2017-07-25 15:29:38 +02:00
switch {
case s.config.IsConfigPath(path):
2017-07-25 17:08:53 +02:00
// break
2017-07-26 18:47:31 +02:00
case !s.fileAffectsBuild(path):
2017-07-25 15:29:38 +02:00
continue loop
case seen[path]:
continue loop
2017-07-25 14:57:32 +02:00
}
2017-07-25 15:29:38 +02:00
result = append(result, path)
2017-07-25 17:08:53 +02:00
seen[path] = true
2017-07-25 14:57:32 +02:00
}
2017-07-25 15:29:38 +02:00
return result
2017-07-25 14:57:32 +02:00
}
2017-07-25 17:08:53 +02:00
2017-07-26 18:47:31 +02:00
// Returns true if the file or a parent directory is excluded.
// Cf. Site.Exclude.
func (s *Site) fileAffectsBuild(rel string) bool {
for rel != "" {
switch {
case rel == ".":
return true
case utils.MatchList(s.config.Include, rel):
return true
case utils.MatchList(s.config.Exclude, rel):
return false
case strings.HasPrefix(rel, "."):
return false
}
rel = filepath.Dir(rel)
}
return true
}
2017-07-25 17:08:53 +02:00
// returns true if changes to the site-relative paths invalidate doc
func (s *Site) invalidatesDoc(paths map[string]bool, d pages.Document) bool {
rel := utils.MustRel(s.SourceDir(), d.SourcePath())
return paths[rel]
}