Handle self rename operations gracefully

This commit is contained in:
Steve Francia 2016-01-25 14:40:44 -05:00
parent 74c90553b4
commit 9413cf8499
2 changed files with 22 additions and 4 deletions

View file

@ -483,6 +483,7 @@ func copyStatic() error {
publishDir = helpers.FilePathSeparator publishDir = helpers.FilePathSeparator
} }
// Includes both theme/static & /static
staticSourceFs := getStaticSourceFs() staticSourceFs := getStaticSourceFs()
if staticSourceFs == nil { if staticSourceFs == nil {
@ -499,8 +500,12 @@ func copyStatic() error {
syncer.Delete = true syncer.Delete = true
jww.INFO.Println("syncing static files to", publishDir) jww.INFO.Println("syncing static files to", publishDir)
// because we are using a baseFs (to get the union right). Sync from the root // because we are using a baseFs (to get the union right).
syncer.Sync(publishDir, helpers.FilePathSeparator) // set sync src to root
err := syncer.Sync(publishDir, helpers.FilePathSeparator)
if err != nil {
return err
}
return nil return nil
// //
// themeDir, err := helpers.GetThemeStaticDirPath() // themeDir, err := helpers.GetThemeStaticDirPath()
@ -718,7 +723,6 @@ func NewWatcher(port int) error {
jww.FEEDBACK.Printf("Syncing all static files\n") jww.FEEDBACK.Printf("Syncing all static files\n")
err := copyStatic() err := copyStatic()
if err != nil { if err != nil {
fmt.Println(err)
utils.StopOnErr(err, fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir")))) utils.StopOnErr(err, fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir"))))
} }
} else { } else {

View file

@ -44,6 +44,7 @@ import (
"github.com/spf13/nitro" "github.com/spf13/nitro"
"github.com/spf13/viper" "github.com/spf13/viper"
"gopkg.in/fsnotify.v1" "gopkg.in/fsnotify.v1"
"github.com/spf13/afero"
) )
var _ = transform.AbsURL var _ = transform.AbsURL
@ -500,13 +501,26 @@ func (s *Site) ReBuild(events []fsnotify.Event) error {
go converterCollator(s, convertResults, errs) go converterCollator(s, convertResults, errs)
for _, ev := range sourceChanged { for _, ev := range sourceChanged {
if ev.Op&fsnotify.Rename == fsnotify.Rename || ev.Op&fsnotify.Remove == fsnotify.Remove {
if ev.Op&fsnotify.Remove == fsnotify.Remove {
//remove the file & a create will follow //remove the file & a create will follow
path, _ := helpers.GetRelativePath(ev.Name, s.absContentDir()) path, _ := helpers.GetRelativePath(ev.Name, s.absContentDir())
s.RemovePageByPath(path) s.RemovePageByPath(path)
continue continue
} }
// Some editors (Vim) sometimes issue only a Rename operation when writing an existing file
// Sometimes a rename operation means that file has been renamed other times it means
// it's been updated
if ev.Op&fsnotify.Rename == fsnotify.Rename {
// If the file is still on disk, it's only been updated, if it's not, it's been moved
if ex, err := afero.Exists(hugofs.SourceFs, ev.Name); !ex || err != nil {
path, _ := helpers.GetRelativePath(ev.Name, s.absContentDir())
s.RemovePageByPath(path)
continue
}
}
file, err := s.ReReadFile(ev.Name) file, err := s.ReReadFile(ev.Name)
if err != nil { if err != nil {
errs <- err errs <- err