1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 06:48:59 +01:00
This commit is contained in:
Oliver Steele 2017-07-15 08:59:19 -04:00
parent d26e9671d1
commit 59e61a5eca
7 changed files with 57 additions and 56 deletions

View File

@ -17,12 +17,12 @@ install:
script:
- $HOME/gopath/bin/goveralls -service=travis-ci
- gometalinter --deadline=5m --disable=aligncheck ./...
- make lint
after_success:
- test -n "$TRAVIS_TAG" && curl -sL https://git.io/goreleaser | bash
notifications:
email:
on_success: change
on_success: never
on_failure: change

View File

@ -6,7 +6,6 @@ import (
"path/filepath"
"sort"
"strings"
"sync"
"time"
yaml "gopkg.in/yaml.v2"
@ -38,55 +37,18 @@ func buildCommand(site *site.Site) error {
// server watch is implemented inside Server.Run, in contrast to this command
if watch {
// never returns unless error
return watchRebuild(site)
events, err := site.WatchRebuild(buildOptions)
if err != nil {
return err
}
logger.label("Auto-regeneration", "enabled for %q\n", site.SourceDir())
for event := range events {
fmt.Print(event)
}
}
return nil
}
// never returns
func watchRebuild(site *site.Site) error {
var mu sync.Mutex
messages := make(chan string)
errors := make(chan error)
err := site.WatchFiles(func(filenames []string) {
mu.Lock()
defer mu.Unlock()
// DRY w/ occurrence in server.reloadSite
count := len(filenames)
start := time.Now()
inflect := map[bool]string{true: "", false: "s"}[count == 1]
messages <- fmt.Sprintf("Regenerating: %d file%s changed at %s...", count, inflect, start.Format(time.Stamp))
// replace the previous value of the global site variable
s, err := site.Reload()
if err == nil {
count, e := s.Build(buildOptions)
if e == nil {
site = s
elapsed := time.Since(start)
messages <- fmt.Sprintf("wrote %d files in %.2fs.\n", count, elapsed.Seconds())
}
err = e
}
if err != nil {
errors <- err
return
}
})
if err != nil {
return err
}
for {
select {
case err := <-errors:
fmt.Println()
fmt.Fprintln(os.Stderr, err.Error())
case msg := <-messages:
fmt.Print(msg)
}
}
}
func cleanCommand(site *site.Site) error {
logger.label("Cleaner:", "Removing %s...", site.DestDir())
return site.Clean(buildOptions)

View File

@ -82,7 +82,7 @@ func (s *Server) handler(rw http.ResponseWriter, r *http.Request) {
}
}
func (s *Server) reloadSite(count int) {
func (s *Server) reload(count int) {
s.Lock()
defer s.Unlock()
@ -90,7 +90,7 @@ func (s *Server) reloadSite(count int) {
start := time.Now()
inflect := map[bool]string{true: "", false: "s"}[count == 1]
fmt.Printf("Regenerating: %d file%s changed at %s...", count, inflect, start.Format(time.Stamp))
site, err := s.Site.Reload()
site, err := s.Site.Reloaded()
if err != nil {
fmt.Println()
fmt.Fprintln(os.Stderr, err.Error())

View File

@ -12,7 +12,7 @@ func (s *Server) watchAndReload() error {
urls[url] = true
}
}
s.reloadSite(len(filenames))
s.reload(len(filenames))
for url := range urls {
s.lr.Reload(url)
}

View File

@ -43,13 +43,15 @@ func (s *Site) Read() error {
if err := s.readFiles(); err != nil {
return err
}
if err:=s.initializeRenderingPipeline(); err!=nil{return err}
if err := s.initializeRenderingPipeline(); err != nil {
return err
}
return s.runHooks(func(p plugins.Plugin) error { return p.PostRead(s) })
}
// Reload reloads the config file and pages.
// It returns a copy.
func (s *Site) Reload() (*Site, error) {
// Reloaded returns a new site read the same source directory, configuration file, and load flags.
// It does not read the site files.
func (s *Site) Reloaded() (*Site, error) {
copy, err := FromDirectory(s.SourceDir(), s.flags)
if err != nil {
return nil, err

View File

@ -3,6 +3,7 @@ package site
import (
"fmt"
"os"
"sync"
"time"
"github.com/fsnotify/fsnotify"
@ -61,6 +62,42 @@ func (s *Site) sitePaths(filenames []string) []string {
return paths
}
// WatchRebuild watches the directory. Each time a file changes, it
// rebuilds the site. It sends status messages and error to its output
// channel.
//
// WatchRebuild never returns, unless there was an error creating the file watcher.
func (s *Site) WatchRebuild(o BuildOptions) (<-chan interface{}, error) {
var mu sync.Mutex
events := make(chan interface{})
return events, s.WatchFiles(func(filenames []string) {
mu.Lock()
defer mu.Unlock()
// DRY w/ similar logic, messages in server.reload
count := len(filenames)
start := time.Now()
inflect := map[bool]string{true: "", false: "s"}[count == 1]
events <- fmt.Sprintf("Regenerating: %d file%s changed at %s...", count, inflect, start.Format(time.Stamp))
r, err := s.Reloaded()
if err == nil {
count, e := r.Build(o)
if e == nil {
// use the new site value the next time
s = r
elapsed := time.Since(start)
events <- fmt.Sprintf("wrote %d files in %.2fs.\n", count, elapsed.Seconds())
}
err = e
}
if err != nil {
fmt.Println()
fmt.Fprintln(os.Stderr, err)
return
}
})
}
// debounce relays values from input to output, merging successive values within interval
// TODO consider https://github.com/ReactiveX/RxGo
func debounce(interval time.Duration, input <-chan string) <-chan []string {

View File

@ -13,7 +13,7 @@ func (tc tagContext) includeTag(ctx render.Context) (string, error) {
}
func (tc tagContext) includeRelativeTag(ctx render.Context) (string, error) {
// TODO Note that you cannot use the ../ syntax
// TODO "Note that you cannot use the ../ syntax"
return includeFromDir(ctx, path.Dir(ctx.SourceFile()))
}