mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Separate handling content, data and template changes
Data & Templates reading independently Need to work on page source reading and other source files
This commit is contained in:
parent
bcc42c0549
commit
ca6ca4f4fc
2 changed files with 120 additions and 20 deletions
|
@ -44,6 +44,8 @@ import (
|
||||||
"gopkg.in/fsnotify.v1"
|
"gopkg.in/fsnotify.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var mainSite *hugolib.Site
|
||||||
|
|
||||||
// userError is an error used to signal different error situations in command handling.
|
// userError is an error used to signal different error situations in command handling.
|
||||||
type commandError struct {
|
type commandError struct {
|
||||||
s string
|
s string
|
||||||
|
@ -528,15 +530,29 @@ func getDirList() []string {
|
||||||
|
|
||||||
func buildSite(watching ...bool) (err error) {
|
func buildSite(watching ...bool) (err error) {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
site := &hugolib.Site{}
|
if mainSite == nil {
|
||||||
if len(watching) > 0 && watching[0] {
|
mainSite = new(hugolib.Site)
|
||||||
site.RunMode.Watching = true
|
|
||||||
}
|
}
|
||||||
err = site.Build()
|
if len(watching) > 0 && watching[0] {
|
||||||
|
mainSite.RunMode.Watching = true
|
||||||
|
}
|
||||||
|
err = mainSite.Build()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
site.Stats()
|
mainSite.Stats()
|
||||||
|
jww.FEEDBACK.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func rebuildSite(changes map[string]bool) error {
|
||||||
|
startTime := time.Now()
|
||||||
|
err := mainSite.ReBuild(changes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
mainSite.Stats()
|
||||||
jww.FEEDBACK.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
|
jww.FEEDBACK.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -574,6 +590,7 @@ func NewWatcher(port int) error {
|
||||||
staticChanged := false
|
staticChanged := false
|
||||||
dynamicChanged := false
|
dynamicChanged := false
|
||||||
staticFilesChanged := make(map[string]bool)
|
staticFilesChanged := make(map[string]bool)
|
||||||
|
dynamicFilesChanged := make(map[string]bool)
|
||||||
|
|
||||||
for _, ev := range evs {
|
for _, ev := range evs {
|
||||||
ext := filepath.Ext(ev.Name)
|
ext := filepath.Ext(ev.Name)
|
||||||
|
@ -603,7 +620,11 @@ func NewWatcher(port int) error {
|
||||||
dynamicChanged = dynamicChanged || !isstatic
|
dynamicChanged = dynamicChanged || !isstatic
|
||||||
|
|
||||||
if isstatic {
|
if isstatic {
|
||||||
staticFilesChanged[ev.Name] = true
|
if staticPath, err := helpers.MakeStaticPathRelative(ev.Name); err == nil {
|
||||||
|
staticFilesChanged[staticPath] = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dynamicFilesChanged[ev.Name] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// add new directory to watch list
|
// add new directory to watch list
|
||||||
|
@ -680,7 +701,10 @@ func NewWatcher(port int) error {
|
||||||
fmt.Print("\nChange detected, rebuilding site\n")
|
fmt.Print("\nChange detected, rebuilding site\n")
|
||||||
const layout = "2006-01-02 15:04 -0700"
|
const layout = "2006-01-02 15:04 -0700"
|
||||||
fmt.Println(time.Now().Format(layout))
|
fmt.Println(time.Now().Format(layout))
|
||||||
utils.CheckErr(buildSite(true))
|
//TODO here
|
||||||
|
|
||||||
|
// utils.CheckErr(buildSite(true))
|
||||||
|
rebuildSite(dynamicFilesChanged)
|
||||||
|
|
||||||
if !BuildWatch && !viper.GetBool("DisableLiveReload") {
|
if !BuildWatch && !viper.GetBool("DisableLiveReload") {
|
||||||
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
|
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
|
||||||
|
|
102
hugolib/site.go
102
hugolib/site.go
|
@ -426,6 +426,78 @@ func (s *Site) Build() (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Site) ReBuild(changed map[string]bool) error {
|
||||||
|
s.timerStep("initialize rebuild")
|
||||||
|
// First we need to determine what changed
|
||||||
|
|
||||||
|
sourceChanged := []string{}
|
||||||
|
tmplChanged := []string{}
|
||||||
|
dataChanged := []string{}
|
||||||
|
var err error
|
||||||
|
|
||||||
|
for f := range changed {
|
||||||
|
// Need to re-read source
|
||||||
|
if strings.HasPrefix(f, s.absContentDir()) {
|
||||||
|
fmt.Println("Source changed", f)
|
||||||
|
sourceChanged = append(sourceChanged, f)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(f, s.absLayoutDir()) || strings.HasPrefix(f, s.absThemeDir()) {
|
||||||
|
fmt.Println("Template changed", f)
|
||||||
|
tmplChanged = append(tmplChanged, f)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(f, s.absDataDir()) {
|
||||||
|
fmt.Println("Data changed", f)
|
||||||
|
dataChanged = append(dataChanged, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if len(tmplChanged) > 0 {
|
||||||
|
s.prepTemplates()
|
||||||
|
s.Tmpl.PrintErrors()
|
||||||
|
s.timerStep("template prep")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(dataChanged) > 0 {
|
||||||
|
s.ReadDataFromSourceFS()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len (sourceChanged) > 0 {
|
||||||
|
if err = s.CreatePages(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.setupPrevNext()
|
||||||
|
if err = s.BuildSiteMeta(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.timerStep("build taxonomies")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = s.Render(); err != nil {
|
||||||
|
// Better reporting when the template is missing (commit 2bbecc7b)
|
||||||
|
jww.ERROR.Printf("Error rendering site: %s", err)
|
||||||
|
|
||||||
|
jww.ERROR.Printf("Available templates:")
|
||||||
|
var keys []string
|
||||||
|
for _, template := range s.Tmpl.Templates() {
|
||||||
|
if name := template.Name(); name != "" {
|
||||||
|
keys = append(keys, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
for _, k := range keys {
|
||||||
|
jww.ERROR.Printf("\t%s\n", k)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Site) Analyze() error {
|
func (s *Site) Analyze() error {
|
||||||
if err := s.Process(); err != nil {
|
if err := s.Process(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -508,6 +580,21 @@ func readData(f *source.File) (interface{}, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Site) ReadDataFromSourceFS() error {
|
||||||
|
dataSources := make([]source.Input, 0, 2)
|
||||||
|
dataSources = append(dataSources, &source.Filesystem{Base: s.absDataDir()})
|
||||||
|
|
||||||
|
// have to be last - duplicate keys in earlier entries will win
|
||||||
|
themeStaticDir, err := helpers.GetThemeDataDirPath()
|
||||||
|
if err == nil {
|
||||||
|
dataSources = append(dataSources, &source.Filesystem{Base: themeStaticDir})
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.loadData(dataSources)
|
||||||
|
s.timerStep("load data")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Site) Process() (err error) {
|
func (s *Site) Process() (err error) {
|
||||||
s.timerStep("Go initialization")
|
s.timerStep("Go initialization")
|
||||||
if err = s.initialize(); err != nil {
|
if err = s.initialize(); err != nil {
|
||||||
|
@ -517,20 +604,9 @@ func (s *Site) Process() (err error) {
|
||||||
s.Tmpl.PrintErrors()
|
s.Tmpl.PrintErrors()
|
||||||
s.timerStep("initialize & template prep")
|
s.timerStep("initialize & template prep")
|
||||||
|
|
||||||
dataSources := make([]source.Input, 0, 2)
|
if err = s.ReadDataFromSourceFS(); err != nil {
|
||||||
|
|
||||||
dataSources = append(dataSources, &source.Filesystem{Base: s.absDataDir()})
|
|
||||||
|
|
||||||
// have to be last - duplicate keys in earlier entries will win
|
|
||||||
themeStaticDir, err := helpers.GetThemeDataDirPath()
|
|
||||||
if err == nil {
|
|
||||||
dataSources = append(dataSources, &source.Filesystem{Base: themeStaticDir})
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = s.loadData(dataSources); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.timerStep("load data")
|
|
||||||
|
|
||||||
if err = s.CreatePages(); err != nil {
|
if err = s.CreatePages(); err != nil {
|
||||||
return
|
return
|
||||||
|
@ -864,7 +940,6 @@ func readCollator(s *Site, results <-chan HandledResult, errs chan<- error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) BuildSiteMeta() (err error) {
|
func (s *Site) BuildSiteMeta() (err error) {
|
||||||
|
|
||||||
s.assembleMenus()
|
s.assembleMenus()
|
||||||
|
|
||||||
if len(s.Pages) == 0 {
|
if len(s.Pages) == 0 {
|
||||||
|
@ -929,6 +1004,7 @@ func (s *SiteInfo) createNodeMenuEntryURL(in string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) assembleMenus() {
|
func (s *Site) assembleMenus() {
|
||||||
|
s.Menus = Menus{}
|
||||||
|
|
||||||
type twoD struct {
|
type twoD struct {
|
||||||
MenuName, EntryName string
|
MenuName, EntryName string
|
||||||
|
|
Loading…
Reference in a new issue