From f023dfd7636f73b11c94e86a05c6273941d52c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 27 Jul 2016 10:49:42 +0200 Subject: [PATCH] Move the Build* methods to HugoSites See #2309 --- commands/hugo.go | 38 +++-------------------------- hugolib/hugo_sites.go | 57 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/commands/hugo.go b/commands/hugo.go index b8e4ea643..959006557 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -713,43 +713,13 @@ func getDirList() []string { } func buildSites(watching ...bool) (err error) { - fmt.Println("Started building site") - t0 := time.Now() - - for _, site := range Hugo { - t1 := time.Now() - if len(watching) > 0 && watching[0] { - site.RunMode.Watching = true - } - - if err := site.Build(); err != nil { - return err - } - - site.Stats(t1) - } - - jww.FEEDBACK.Printf("total in %v ms\n", int(1000*time.Since(t0).Seconds())) - - return nil + fmt.Println("Started building sites ...") + w := len(watching) > 0 && watching[0] + return Hugo.Build(w, true) } func rebuildSites(events []fsnotify.Event) error { - t0 := time.Now() - - for _, site := range Hugo { - t1 := time.Now() - - if err := site.ReBuild(events); err != nil { - return err - } - - site.Stats(t1) - } - - jww.FEEDBACK.Printf("total in %v ms\n", int(1000*time.Since(t0).Seconds())) - - return nil + return Hugo.Rebuild(events, true) } // NewWatcher creates a new watcher to watch filesystem events. diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index cc6b4ca12..dd8d3e5d2 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -13,6 +13,14 @@ package hugolib +import ( + "time" + + "github.com/fsnotify/fsnotify" + + jww "github.com/spf13/jwalterweatherman" +) + // HugoSites represents the sites to build. Each site represents a language. type HugoSites []*Site @@ -23,3 +31,52 @@ func (h HugoSites) Reset() { h[i] = s.Reset() } } + +// Build builds all sites. +func (h HugoSites) Build(watching, printStats bool) error { + t0 := time.Now() + + for _, site := range h { + t1 := time.Now() + + site.RunMode.Watching = watching + + if err := site.Build(); err != nil { + return err + } + if printStats { + site.Stats(t1) + } + } + + if printStats { + jww.FEEDBACK.Printf("total in %v ms\n", int(1000*time.Since(t0).Seconds())) + } + + return nil + +} + +// Rebuild rebuilds all sites. +func (h HugoSites) Rebuild(events []fsnotify.Event, printStats bool) error { + t0 := time.Now() + + for _, site := range h { + t1 := time.Now() + + if err := site.ReBuild(events); err != nil { + return err + } + + if printStats { + site.Stats(t1) + } + } + + if printStats { + jww.FEEDBACK.Printf("total in %v ms\n", int(1000*time.Since(t0).Seconds())) + } + + return nil + +}