Restoring build and watch functionality
authorspf13 <steve.francia@gmail.com>
Tue, 1 Oct 2013 02:38:32 +0000 (22:38 -0400)
committerspf13 <steve.francia@gmail.com>
Tue, 1 Oct 2013 02:38:32 +0000 (22:38 -0400)
commands/hugo.go
commands/server.go

index 2fdaff00803766742e0d0955bc94d77cf500ac8e..05bce19951f82680e25a376cf95b527bdcd268f9 100644 (file)
@@ -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()
+       }
+}
index 459609e81c9ea0b2a5307de472c912471c4daaeb..fb2feead6868bf4b21c8041cb52ac4c53a697045 100644 (file)
@@ -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()
-       }
-}