Restoring build and watch functionality

This commit is contained in:
spf13 2013-09-30 22:38:32 -04:00
parent aa9b9d596e
commit 3ae8dda203
2 changed files with 65 additions and 66 deletions

View file

@ -15,12 +15,15 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/howeyc/fsnotify"
"github.com/mostafah/fsync" "github.com/mostafah/fsync"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/hugo/hugolib" "github.com/spf13/hugo/hugolib"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"sync"
"time" "time"
) )
@ -88,15 +91,14 @@ func build(cmd *cobra.Command, args []string) {
os.Exit(-1) os.Exit(-1)
} }
// Does this even make sense without the server setting? if BuildWatch {
//if BuildWatch { fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
//fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir)) fmt.Println("Press ctrl+c to stop")
//_, err = buildSite() err := NewWatcher(0)
//if err != nil { if err != nil {
//fmt.Println(err) fmt.Println(err)
//os.Exit(-1) }
//} }
//}
} }
func copyStatic() error { func copyStatic() error {
@ -136,3 +138,56 @@ func buildSite() (site *hugolib.Site, err error) {
fmt.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds())) fmt.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
return site, nil return site, nil
} }
func NewWatcher(port int) error {
watcher, err := fsnotify.NewWatcher()
var wg sync.WaitGroup
if err != nil {
fmt.Println(err)
return err
}
defer watcher.Close()
wg.Add(1)
go func() {
for {
select {
case ev := <-watcher.Event:
if Verbose {
fmt.Println(ev)
}
watchChange(ev)
// TODO add newly created directories to the watch list
case err := <-watcher.Error:
if err != nil {
fmt.Println("error:", err)
}
}
}
}()
for _, d := range getDirList() {
if d != "" {
_ = watcher.Watch(d)
}
}
if port > 0 {
go serve(port)
}
wg.Wait()
return nil
}
func watchChange(ev *fsnotify.FileEvent) {
if strings.HasPrefix(ev.Name, Config.GetAbsPath(Config.StaticDir)) {
fmt.Println("Static file changed, syncing\n")
copyStatic()
} else {
fmt.Println("Change detected, rebuilding site\n")
buildSite()
}
}

View file

@ -15,12 +15,9 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/howeyc/fsnotify"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"sync"
) )
var serverPort int var serverPort int
@ -50,7 +47,7 @@ func server(cmd *cobra.Command, args []string) {
// Watch runs its own server as part of the routine // Watch runs its own server as part of the routine
if serverWatch { if serverWatch {
fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir)) fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
err := NewWatcher(serverPort, true) err := NewWatcher(serverPort)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -68,56 +65,3 @@ func serve(port int) {
fmt.Println("Press ctrl+c to stop") fmt.Println("Press ctrl+c to stop")
panic(http.ListenAndServe(":"+strconv.Itoa(port), http.FileServer(http.Dir(Config.GetAbsPath(Config.PublishDir))))) panic(http.ListenAndServe(":"+strconv.Itoa(port), http.FileServer(http.Dir(Config.GetAbsPath(Config.PublishDir)))))
} }
func NewWatcher(port int, server bool) error {
watcher, err := fsnotify.NewWatcher()
var wg sync.WaitGroup
if err != nil {
fmt.Println(err)
return err
}
defer watcher.Close()
wg.Add(1)
go func() {
for {
select {
case ev := <-watcher.Event:
if Verbose {
fmt.Println(ev)
}
watchChange(ev)
// TODO add newly created directories to the watch list
case err := <-watcher.Error:
if err != nil {
fmt.Println("error:", err)
}
}
}
}()
for _, d := range getDirList() {
if d != "" {
_ = watcher.Watch(d)
}
}
if server {
go serve(port)
}
wg.Wait()
return nil
}
func watchChange(ev *fsnotify.FileEvent) {
if strings.HasPrefix(ev.Name, Config.GetAbsPath(Config.StaticDir)) {
fmt.Println("Static file changed, syncing\n")
copyStatic()
} else {
fmt.Println("Change detected, rebuilding site\n")
buildSite()
}
}