Skip to content

Commit

Permalink
Add support for dynamic reloading of config file when watching
Browse files Browse the repository at this point in the history
  • Loading branch information
spf13 committed Nov 10, 2015
1 parent 488966d commit bccf957
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
16 changes: 15 additions & 1 deletion commands/hugo.go
Expand Up @@ -17,7 +17,6 @@ package commands

import (
"fmt"
"github.com/spf13/hugo/parser"
"io/ioutil"
"net/http"
"os"
Expand All @@ -27,6 +26,8 @@ import (
"sync"
"time"

"github.com/spf13/hugo/parser"

"github.com/spf13/cobra"
"github.com/spf13/fsync"
"github.com/spf13/hugo/helpers"
Expand All @@ -53,6 +54,7 @@ built with love by spf13 and friends in Go.
Complete documentation is available at http://gohugo.io/.`,
Run: func(cmd *cobra.Command, args []string) {
InitializeConfig()
watchConfig()
build()
},
}
Expand Down Expand Up @@ -312,6 +314,18 @@ func InitializeConfig() {
}
}

func watchConfig() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
utils.CheckErr(buildSite(true))
if !viper.GetBool("DisableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
livereload.ForceRefresh()
}
})
}

func build(watches ...bool) {
utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir"))))
watch := false
Expand Down
1 change: 1 addition & 0 deletions commands/server.go
Expand Up @@ -93,6 +93,7 @@ func server(cmd *cobra.Command, args []string) {

if viper.GetBool("watch") {
serverWatch = true
watchConfig()
}

l, err := net.Listen("tcp", net.JoinHostPort(serverInterface, strconv.Itoa(serverPort)))
Expand Down
10 changes: 5 additions & 5 deletions docs/content/meta/release-notes.md
Expand Up @@ -14,19 +14,20 @@ weight: 10
* Have Jekyll site, but dreaming of porting it to Hugo? This release introduces a new `hugo import jekyll`command that makes this easier than ever. [1469](https://github.com/spf13/hugo/pull/1469)
* We now use a custom-built `LazyFileReader` for reading file contents, which means we don't read media files in `/content` into memory anymore -- and file reading is now performed in parallel on multicore PCs. [1181](https://github.com/spf13/hugo/issues/1181)
* Hugo is now built with `Go 1.5` which, among many other improvements, have fixed the last known data race in Hugo. [917] (https://github.com/spf13/hugo/issues/917)
* Hugo now supports dynamic reloading of the config file when watching.
* Lots of fixes and improvements in the template funcs:
* The new `dict` function that could be used to pass maps into a template.[1463](https://github.com/spf13/hugo/pull/1463)
* The new `pluralize` and `singularize` template funcs.
* The new `dict` function that could be used to pass maps into a template.[1463](https://github.com/spf13/hugo/pull/1463)
* The new `pluralize` and `singularize` template funcs.
* The new `base64Decode` and `base64Encode` template funcs.
* The `sort` template func now accepts field/key chaining arguments and pointer values. [1330](https://github.com/spf13/hugo/issues/1330)
* Several fixes for `slicestr` and `substr`, most importantly, they now have full `utf-8`-support. [1190](https://github.com/spf13/hugo/issues/1190) [1333](https://github.com/spf13/hugo/issues/1333) [1347](https://github.com/spf13/hugo/issues/1347)
* Several fixes for `slicestr` and `substr`, most importantly, they now have full `utf-8`-support. [1190](https://github.com/spf13/hugo/issues/1190) [1333](https://github.com/spf13/hugo/issues/1333) [1347](https://github.com/spf13/hugo/issues/1347)
* The new `last` template function allows the user to select the last `N` items of a slice. [1148](https://github.com/spf13/hugo/issues/1148)
* The new `after` func allows the user to select the items after the `Nth` item. [1200] (https://github.com/spf13/hugo/pull/1200)
* Add `time.Time` type support to the `where` func.
* It is now possible to use constructs like `where Values ".Param.key" nil` to filter pages that doesn't have a particular parameter. [1232](https://github.com/spf13/hugo/issues/1232)
* `getJSON`/`getCSV`: Add retry on invalid content. [1166](https://github.com/spf13/hugo/issues/1166)
* The new `readDir` func lists local files. [1204](https://github.com/spf13/hugo/pull/1204)
* The new `Param` convenience method on `Page` and `Node` can be used to get the most specific parameter value for a given key. [1462](https://github.com/spf13/hugo/issues/1462)
* The new `Param` convenience method on `Page` and `Node` can be used to get the most specific parameter value for a given key. [1462](https://github.com/spf13/hugo/issues/1462)
* Several new Blackfriday options are added:
* Option to disable Blackfriday's `Smartypants`.
* Option for Blackfriday to open links in a new window/tab. [1220](https://github.com/spf13/hugo/issues/1220)
Expand Down Expand Up @@ -372,4 +373,3 @@ This release represents over 65 code commits from 6 different contributors.

## **0.5.0** June 25, 2013
* Hugo is quite usable and able to build spf13.com

1 comment on commit bccf957

@bep
Copy link
Member

@bep bep commented on bccf957 Nov 10, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Please sign in to comment.