Rewrite commentary on static event handling

This commit is contained in:
Steve Francia 2016-01-26 14:12:18 -05:00
parent b0b4b82165
commit d08e4c87a7
2 changed files with 22 additions and 72 deletions

View file

@ -507,32 +507,6 @@ func copyStatic() error {
return err return err
} }
return nil 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. // 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: case evs := <-watcher.Events:
jww.INFO.Println("Recieved System Events:", evs) jww.INFO.Println("Recieved System Events:", evs)
staticEvents := []fsnotify.Event{} //ev make(map[string]bool) staticEvents := []fsnotify.Event{}
dynamicEvents := []fsnotify.Event{} //make(map[string]bool) dynamicEvents := []fsnotify.Event{}
for _, ev := range evs { for _, ev := range evs {
ext := filepath.Ext(ev.Name) ext := filepath.Ext(ev.Name)
@ -700,7 +674,6 @@ func NewWatcher(port int) error {
if isstatic { if isstatic {
staticEvents = append(staticEvents, ev) staticEvents = append(staticEvents, ev)
// }
} else { } else {
dynamicEvents = append(dynamicEvents, ev) dynamicEvents = append(dynamicEvents, ev)
} }
@ -715,7 +688,6 @@ func NewWatcher(port int) error {
} }
jww.FEEDBACK.Println("\n Static file changes detected") jww.FEEDBACK.Println("\n Static file changes detected")
jww.FEEDBACK.Println("syncing to", publishDir)
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))
@ -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. // 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 // 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 // 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: // If Hugo generates a file (from the content dir) over a static file
// 1. Not overwrite anything // the content generated file should take precedence.
// 2. Assume these cases are rare and overwrite anyway. If things get out of sync //
// a clean sync will be needed. // Because we are now watching and handling individual events it is possible that a static
// There is an alternative which is quite heavy. We would have to track every single file // event that occupies the same path as a content generated file will take precedence
// placed into the publishedPath and which pipeline put it there. // until a regeneration of the content takes places.
// We have chosen to take the 2nd approach //
// 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) fmt.Println(ev)
fromPath := ev.Name fromPath := ev.Name
@ -765,13 +737,17 @@ func NewWatcher(port int) error {
fmt.Println(err) fmt.Println(err)
continue continue
} }
fmt.Println("relpath", relPath)
// Remove || rename is harder and will require an assumption.
// if remove or rename ignore.. as in leave the old file in the publishDir // 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 { 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 _, err := staticSourceFs.Stat(relPath); os.IsNotExist(err) {
// If file doesn't exist in any static dir, remove it // If file doesn't exist in any static dir, remove it
toRemove :=filepath.Join(publishDir, relPath) toRemove :=filepath.Join(publishDir, relPath)
@ -788,19 +764,9 @@ func NewWatcher(port int) error {
continue continue
} }
// if strings.HasPrefix(fromPath, staticDir) { // For all other event operations Hugo will sync static.
// publishPath = filepath.Join(publishDir, strings.TrimPrefix(fromPath, staticDir))
// } else if strings.HasPrefix(relPath, themeStaticDir) {
// publishPath = filepath.Join(publishDir, strings.TrimPrefix(fromPath, themeStaticDir))
// }
jww.FEEDBACK.Println("Syncing", relPath, "to", publishDir) jww.FEEDBACK.Println("Syncing", relPath, "to", publishDir)
syncer.Sync(filepath.Join(publishDir, relPath), relPath) 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)
// }
} }
} }

View file

@ -105,12 +105,6 @@ type Position struct {
} }
type Pages []*Page 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 { func (ps Pages) FindPagePosByFilePath(inPath string) int {
for i, x := range ps { for i, x := range ps {
@ -132,16 +126,6 @@ func (ps Pages) FindPagePos(page *Page) int {
return -1 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 { func (p *Page) Plain() string {
p.initPlain() p.initPlain()
return p.plain return p.plain