shithub: hugo

Download patch

ref: 3ae8dda203fd8583120402afa73c7cf3518e7d11
parent: aa9b9d596efae9b6cd534022bb24d408ae153563
author: spf13 <[email protected]>
date: Mon Sep 30 18:38:32 EDT 2013

Restoring build and watch functionality

--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -15,6 +15,7 @@
 
 import (
 	"fmt"
+	"github.com/howeyc/fsnotify"
 	"github.com/mostafah/fsync"
 	"github.com/spf13/cobra"
 	"github.com/spf13/hugo/hugolib"
@@ -21,6 +22,8 @@
 	"log"
 	"os"
 	"path/filepath"
+	"strings"
+	"sync"
 	"time"
 )
 
@@ -88,15 +91,14 @@
 		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 {
@@ -135,4 +137,57 @@
 	site.Stats()
 	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()
+	}
 }
--- a/commands/server.go
+++ b/commands/server.go
@@ -15,12 +15,9 @@
 
 import (
 	"fmt"
-	"github.com/howeyc/fsnotify"
 	"github.com/spf13/cobra"
 	"net/http"
 	"strconv"
-	"strings"
-	"sync"
 )
 
 var serverPort int
@@ -50,7 +47,7 @@
 	// 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)
 		}
@@ -67,57 +64,4 @@
 	fmt.Println("Web Server is available at http://localhost:", port)
 	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()
-	}
 }