commit 3ae8dda203fd8583120402afa73c7cf3518e7d11
parent aa9b9d596efae9b6cd534022bb24d408ae153563
Author: spf13 <steve.francia@gmail.com>
Date: Mon, 30 Sep 2013 22:38:32 -0400
Restoring build and watch functionality
Diffstat:
| M | commands/hugo.go | | | 73 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- |
| M | commands/server.go | | | 58 | +--------------------------------------------------------- |
2 files changed, 65 insertions(+), 66 deletions(-)
diff --git a/commands/hugo.go b/commands/hugo.go
@@ -15,12 +15,15 @@ package commands
import (
"fmt"
+ "github.com/howeyc/fsnotify"
"github.com/mostafah/fsync"
"github.com/spf13/cobra"
"github.com/spf13/hugo/hugolib"
"log"
"os"
"path/filepath"
+ "strings"
+ "sync"
"time"
)
@@ -88,15 +91,14 @@ func build(cmd *cobra.Command, args []string) {
os.Exit(-1)
}
- // Does this even make sense without the server setting?
- //if BuildWatch {
- //fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
- //_, err = buildSite()
- //if err != nil {
- //fmt.Println(err)
- //os.Exit(-1)
- //}
- //}
+ if BuildWatch {
+ fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
+ fmt.Println("Press ctrl+c to stop")
+ err := NewWatcher(0)
+ if err != nil {
+ fmt.Println(err)
+ }
+ }
}
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()))
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()
+ }
+}
diff --git a/commands/server.go b/commands/server.go
@@ -15,12 +15,9 @@ package commands
import (
"fmt"
- "github.com/howeyc/fsnotify"
"github.com/spf13/cobra"
"net/http"
"strconv"
- "strings"
- "sync"
)
var serverPort int
@@ -50,7 +47,7 @@ func server(cmd *cobra.Command, args []string) {
// Watch runs its own server as part of the routine
if serverWatch {
fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
- err := NewWatcher(serverPort, true)
+ err := NewWatcher(serverPort)
if err != nil {
fmt.Println(err)
}
@@ -68,56 +65,3 @@ func serve(port int) {
fmt.Println("Press ctrl+c to stop")
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()
- }
-}