diff options
author | Steve Francia <steve.francia@gmail.com> | 2016-01-26 22:12:18 +0300 |
---|---|---|
committer | Steve Francia <steve.francia@gmail.com> | 2016-01-26 22:56:42 +0300 |
commit | d08e4c87a72f57a836722700900690857a4225cb (patch) | |
tree | 035a87996064a09615988ded4f42a10eeb4c2179 | |
parent | b0b4b821651c7fa2866816ced0c1a1a52ca094ed (diff) |
Rewrite commentary on static event handling
-rw-r--r-- | commands/hugo.go | 78 | ||||
-rw-r--r-- | hugolib/page.go | 16 |
2 files changed, 22 insertions, 72 deletions
diff --git a/commands/hugo.go b/commands/hugo.go index 1fd107285..e95c4a5cd 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -507,32 +507,6 @@ func copyStatic() error { return err } return nil -// -// themeDir, err := helpers.GetThemeStaticDirPath() -// if err != nil { -// jww.WARN.Println(err) -// } -// -// staticDir := helpers.GetStaticDirPath() + helpers.FilePathSeparator -// if _, err := os.Stat(staticDir); os.IsNotExist(err) { -// jww.WARN.Println("Unable to find Static Directory:", staticDir) -// } -// -// // Copy the theme's static directory -// if themeDir != "" { -// jww.INFO.Println("syncing from", themeDir, "to", publishDir) -// utils.CheckErr(syncer.Sync(publishDir, themeDir), fmt.Sprintf("Error copying static files of theme to %s", publishDir)) -// } -// -// // Copy the site's own static directory -// staticDir := helpers.GetStaticDirPath() + helpers.FilePathSeparator -// if _, err := os.Stat(staticDir); err == nil { -// jww.INFO.Println("syncing from", staticDir, "to", publishDir) -// return syncer.Sync(publishDir, staticDir) -// } else if os.IsNotExist(err) { -// jww.WARN.Println("Unable to find Static Directory:", staticDir) -// } -// return nil } // getDirList provides NewWatcher() with a list of directories to watch for changes. @@ -653,8 +627,8 @@ func NewWatcher(port int) error { case evs := <-watcher.Events: jww.INFO.Println("Recieved System Events:", evs) - staticEvents := []fsnotify.Event{} //ev make(map[string]bool) - dynamicEvents := []fsnotify.Event{} //make(map[string]bool) + staticEvents := []fsnotify.Event{} + dynamicEvents := []fsnotify.Event{} for _, ev := range evs { ext := filepath.Ext(ev.Name) @@ -700,7 +674,6 @@ func NewWatcher(port int) error { if isstatic { staticEvents = append(staticEvents, ev) -// } } else { dynamicEvents = append(dynamicEvents, ev) } @@ -715,7 +688,6 @@ func NewWatcher(port int) error { } jww.FEEDBACK.Println("\n Static file changes detected") - jww.FEEDBACK.Println("syncing to", publishDir) const layout = "2006-01-02 15:04 -0700" fmt.Println(time.Now().Format(layout)) @@ -744,17 +716,17 @@ func NewWatcher(port int) error { // into one we can't accurately remove a file not in one of the source directories. // If a file is in the local static dir and also in the theme static dir and we remove // it from one of those locations we expect it to still exist in the destination - // If a file is generated by the content over a static file we expect it to remain as well. - // Because we are never certain if the file was overwritten by the content generation - // We can't effectively remove anything. // - // This leads to two approaches: - // 1. Not overwrite anything - // 2. Assume these cases are rare and overwrite anyway. If things get out of sync - // a clean sync will be needed. - // There is an alternative which is quite heavy. We would have to track every single file - // placed into the publishedPath and which pipeline put it there. - // We have chosen to take the 2nd approach + // If Hugo generates a file (from the content dir) over a static file + // the content generated file should take precedence. + // + // Because we are now watching and handling individual events it is possible that a static + // event that occupies the same path as a content generated file will take precedence + // until a regeneration of the content takes places. + // + // Hugo assumes that these cases are very rare and will permit this bad behavior + // The alternative is to track every single file and which pipeline rendered it + // and then to handle conflict resolution on every event. fmt.Println(ev) fromPath := ev.Name @@ -765,13 +737,17 @@ func NewWatcher(port int) error { fmt.Println(err) continue } - fmt.Println("relpath", relPath) - - // if remove or rename ignore.. as in leave the old file in the publishDir + // Remove || rename is harder and will require an assumption. + // Hugo takes the following approach: + // If the static file exists in any of the static source directories after this event + // Hugo will re-sync it. + // If it does not exist in all of the static directories Hugo will remove it. + // + // This assumes that Hugo has not generated content on top of a static file and then removed + // the source of that static file. In this case Hugo will incorrectly remove that file + // from the published directory. if ev.Op&fsnotify.Rename == fsnotify.Rename || ev.Op&fsnotify.Remove == fsnotify.Remove { - // What about the case where a file in the theme is moved so the local static file can - // take it's place. if _, err := staticSourceFs.Stat(relPath); os.IsNotExist(err) { // If file doesn't exist in any static dir, remove it toRemove :=filepath.Join(publishDir, relPath) @@ -788,19 +764,9 @@ func NewWatcher(port int) error { continue } -// if strings.HasPrefix(fromPath, staticDir) { -// publishPath = filepath.Join(publishDir, strings.TrimPrefix(fromPath, staticDir)) -// } else if strings.HasPrefix(relPath, themeStaticDir) { -// publishPath = filepath.Join(publishDir, strings.TrimPrefix(fromPath, themeStaticDir)) -// } + // For all other event operations Hugo will sync static. jww.FEEDBACK.Println("Syncing", relPath, "to", publishDir) syncer.Sync(filepath.Join(publishDir, relPath), relPath) - - -// jww.INFO.Println("syncing from ", fromPath, " to ", publishPath) -// if er := syncer.Sync(publishPath, fromPath); er != nil { -// jww.ERROR.Printf("Error on syncing file '%s'\n %s\n", relPath, er) -// } } } diff --git a/hugolib/page.go b/hugolib/page.go index 58496e81b..407b7c6db 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -105,12 +105,6 @@ type Position struct { } type Pages []*Page -// -//func (ps Pages) Replace(page *Page) { -// if i := ps.FindPagePos(page); i >= 0 { -// ps[i] = page -// } -//} func (ps Pages) FindPagePosByFilePath(inPath string) int { for i, x := range ps { @@ -132,16 +126,6 @@ func (ps Pages) FindPagePos(page *Page) int { return -1 } -// FindPage Given a page, it will return the page in Pages -// will return nil if not found -//func (ps Pages) FindPage(page *Page) *Page { -// if i := ps.FindPagePos(page); i >= 0 { -// return ps[i] -// } -// -// return nil -//} - func (p *Page) Plain() string { p.initPlain() return p.plain |