1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 05:34:50 +01:00
gojekyll/site/rebuild.go

88 lines
2.0 KiB
Go
Raw Normal View History

2017-07-25 14:57:32 +02:00
package site
import (
"fmt"
"os"
"time"
2017-07-25 17:08:53 +02:00
"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) {
2017-08-09 01:44:04 +02:00
if s.RequiresFullReload(paths) {
2017-07-25 15:05:29 +02:00
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-08-09 01:44:04 +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
}
2017-08-16 21:50:31 +02:00
n, err = r.Write()
2017-07-25 17:08:53 +02:00
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
}