Skip to content

Commit

Permalink
commands: Fix server without watch
Browse files Browse the repository at this point in the history
This was broken in Hugo 0.30.

Fixes #4275
  • Loading branch information
bep committed Jan 14, 2018
1 parent 64f0e9d commit 4e524ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
15 changes: 2 additions & 13 deletions commands/hugo.go
Expand Up @@ -30,7 +30,6 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"sync"
"time" "time"


src "github.com/gohugoio/hugo/source" src "github.com/gohugoio/hugo/source"
Expand Down Expand Up @@ -633,7 +632,7 @@ func (c *commandeer) build(watches ...bool) error {
} }
c.Logger.FEEDBACK.Println("Watching for changes in", c.PathSpec().AbsPathify(c.Cfg.GetString("contentDir"))) c.Logger.FEEDBACK.Println("Watching for changes in", c.PathSpec().AbsPathify(c.Cfg.GetString("contentDir")))
c.Logger.FEEDBACK.Println("Press Ctrl+C to stop") c.Logger.FEEDBACK.Println("Press Ctrl+C to stop")
utils.CheckErr(c.Logger, c.newWatcher(false, watchDirs...)) utils.CheckErr(c.Logger, c.newWatcher(watchDirs...))
} }


return nil return nil
Expand Down Expand Up @@ -969,9 +968,7 @@ func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
} }


// newWatcher creates a new watcher to watch filesystem events. // newWatcher creates a new watcher to watch filesystem events.
// if serve is set it will also start one or more HTTP servers to serve those func (c *commandeer) newWatcher(dirList ...string) error {
// files.
func (c *commandeer) newWatcher(serve bool, dirList ...string) error {
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
tweakLimit() tweakLimit()
} }
Expand All @@ -982,16 +979,13 @@ func (c *commandeer) newWatcher(serve bool, dirList ...string) error {
} }


watcher, err := watcher.New(1 * time.Second) watcher, err := watcher.New(1 * time.Second)
var wg sync.WaitGroup


if err != nil { if err != nil {
return err return err
} }


defer watcher.Close() defer watcher.Close()


wg.Add(1)

for _, d := range dirList { for _, d := range dirList {
if d != "" { if d != "" {
_ = watcher.Add(d) _ = watcher.Add(d)
Expand Down Expand Up @@ -1189,11 +1183,6 @@ func (c *commandeer) newWatcher(serve bool, dirList ...string) error {
} }
}() }()


if serve {
go c.serve()
}

wg.Wait()
return nil return nil
} }


Expand Down
17 changes: 14 additions & 3 deletions commands/server.go
Expand Up @@ -19,10 +19,12 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"syscall"
"time" "time"


"github.com/gohugoio/hugo/livereload" "github.com/gohugoio/hugo/livereload"
Expand Down Expand Up @@ -229,14 +231,16 @@ func server(cmd *cobra.Command, args []string) error {
rootWatchDirs := strings.Join(helpers.UniqueStrings(helpers.ExtractRootPaths(relWatchDirs)), ",") rootWatchDirs := strings.Join(helpers.UniqueStrings(helpers.ExtractRootPaths(relWatchDirs)), ",")


jww.FEEDBACK.Printf("Watching for changes in %s%s{%s}\n", baseWatchDir, helpers.FilePathSeparator, rootWatchDirs) jww.FEEDBACK.Printf("Watching for changes in %s%s{%s}\n", baseWatchDir, helpers.FilePathSeparator, rootWatchDirs)
err = c.newWatcher(true, watchDirs...) err = c.newWatcher(watchDirs...)


if err != nil { if err != nil {
return err return err
} }

} }


return nil return c.serve()

} }


type fileServer struct { type fileServer struct {
Expand Down Expand Up @@ -313,7 +317,7 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
return mu, u.String(), endpoint, nil return mu, u.String(), endpoint, nil
} }


func (c *commandeer) serve() { func (c *commandeer) serve() error {


isMultiHost := Hugo.IsMultihost() isMultiHost := Hugo.IsMultihost()


Expand Down Expand Up @@ -345,6 +349,9 @@ func (c *commandeer) serve() {
livereload.Initialize() livereload.Initialize()
} }


var sigs = make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

for i, _ := range baseURLs { for i, _ := range baseURLs {
mu, serverURL, endpoint, err := srv.createEndpoint(i) mu, serverURL, endpoint, err := srv.createEndpoint(i)


Expand All @@ -363,6 +370,10 @@ func (c *commandeer) serve() {
} }


jww.FEEDBACK.Println("Press Ctrl+C to stop") jww.FEEDBACK.Println("Press Ctrl+C to stop")

<-sigs

return nil
} }


// fixURL massages the baseURL into a form needed for serving // fixURL massages the baseURL into a form needed for serving
Expand Down

0 comments on commit 4e524ff

Please sign in to comment.