Watch StaticDir and sync to PublishDir on change

New behavior adds a special case for file changes inside the static directory to fsync PublishDir
This commit is contained in:
rawfalafel 2013-08-19 01:17:37 -04:00
parent c4bcdebc59
commit 2dcdd67378

26
main.go
View file

@ -24,6 +24,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime/pprof" "runtime/pprof"
"strings"
"sync" "sync"
"time" "time"
) )
@ -95,8 +96,7 @@ func main() {
} }
} }
// Copy Static to Destination first err := copyStatic(config)
err := fsync.SyncDel(config.GetAbsPath(config.PublishDir+"/"), config.GetAbsPath(config.StaticDir+"/"))
if err != nil { if err != nil {
log.Fatalf("Error copying static files to %s: %v", config.GetAbsPath(config.PublishDir), err) log.Fatalf("Error copying static files to %s: %v", config.GetAbsPath(config.PublishDir), err)
} }
@ -130,6 +130,11 @@ func main() {
} }
func copyStatic(config *hugolib.Config) error {
// Copy Static to Destination
return fsync.SyncDel(config.GetAbsPath(config.PublishDir+"/"), config.GetAbsPath(config.StaticDir+"/"))
}
func serve(port string, config *hugolib.Config) { func serve(port string, config *hugolib.Config) {
if config.Verbose { if config.Verbose {
@ -153,9 +158,14 @@ func buildSite(config *hugolib.Config) (site *hugolib.Site, err error) {
return site, nil return site, nil
} }
func watchChange(c *hugolib.Config) { func watchChange(c *hugolib.Config, ev *fsnotify.FileEvent) {
fmt.Println("Change detected, rebuilding site\n") if strings.HasPrefix(ev.Name, c.GetAbsPath(c.StaticDir)) {
buildSite(c) fmt.Println("Static file changed, syncing\n")
copyStatic(c)
} else {
fmt.Println("Change detected, rebuilding site\n")
buildSite(c)
}
} }
func NewWatcher(c *hugolib.Config, port string, server bool) error { func NewWatcher(c *hugolib.Config, port string, server bool) error {
@ -163,8 +173,8 @@ func NewWatcher(c *hugolib.Config, port string, server bool) error {
var wg sync.WaitGroup var wg sync.WaitGroup
if err != nil { if err != nil {
return err
fmt.Println(err) fmt.Println(err)
return err
} }
defer watcher.Close() defer watcher.Close()
@ -174,11 +184,10 @@ func NewWatcher(c *hugolib.Config, port string, server bool) error {
for { for {
select { select {
case ev := <-watcher.Event: case ev := <-watcher.Event:
var _ = ev
if c.Verbose { if c.Verbose {
fmt.Println(ev) fmt.Println(ev)
} }
watchChange(c) watchChange(c, ev)
// TODO add newly created directories to the watch list // TODO add newly created directories to the watch list
case err := <-watcher.Error: case err := <-watcher.Error:
if err != nil { if err != nil {
@ -218,6 +227,7 @@ func getDirList(c *hugolib.Config) []string {
filepath.Walk(c.GetAbsPath(c.ContentDir), walker) filepath.Walk(c.GetAbsPath(c.ContentDir), walker)
filepath.Walk(c.GetAbsPath(c.LayoutDir), walker) filepath.Walk(c.GetAbsPath(c.LayoutDir), walker)
filepath.Walk(c.GetAbsPath(c.StaticDir), walker)
return a return a
} }