From 10dd344eeb4e01d89ce4e0d81e1606c7c889da22 Mon Sep 17 00:00:00 2001 From: Kai H Date: Sat, 25 Apr 2015 14:10:52 +0200 Subject: [PATCH] Added watching of plugins in dev mode. Changed watcher. --- plugins/conversion.go | 14 ++++---- plugins/loading.go | 2 ++ templates/generation.go | 68 ++++-------------------------------- watcher/watcher.go | 76 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 69 deletions(-) create mode 100644 watcher/watcher.go diff --git a/plugins/conversion.go b/plugins/conversion.go index 1bf8d4d9..73ae2c57 100644 --- a/plugins/conversion.go +++ b/plugins/conversion.go @@ -37,15 +37,15 @@ func convertUser(vm *lua.LState, structureUser *structure.User) *lua.LTable { } func convertTags(vm *lua.LState, structureTags []structure.Tag) *lua.LTable { - array := make([]*lua.LTable, 0) + table := make([]*lua.LTable, 0) for index, _ := range structureTags { tag := vm.NewTable() tag.RawSet(lua.LString("id"), lua.LNumber(structureTags[index].Id)) tag.RawSet(lua.LString("name"), lua.LString(structureTags[index].Name)) tag.RawSet(lua.LString("slug"), lua.LString(structureTags[index].Slug)) - array = append(array, tag) + table = append(table, tag) } - return makeArray(vm, array) + return makeTable(vm, table) } func convertBlog(vm *lua.LState, structureBlog *structure.Blog) *lua.LTable { @@ -62,10 +62,10 @@ func convertBlog(vm *lua.LState, structureBlog *structure.Blog) *lua.LTable { return blog } -func makeArray(vm *lua.LState, tables []*lua.LTable) *lua.LTable { - array := vm.NewTable() +func makeTable(vm *lua.LState, tables []*lua.LTable) *lua.LTable { + table := vm.NewTable() for index, _ := range tables { - array.Append(tables[index]) + table.Append(tables[index]) } - return array + return table } diff --git a/plugins/loading.go b/plugins/loading.go index aaa583a3..0bda87e8 100644 --- a/plugins/loading.go +++ b/plugins/loading.go @@ -11,6 +11,8 @@ import ( ) func Load() error { + // Reset LuaPool for a fresh start + LuaPool = nil // Make map nameMap := make(map[string]string, 0) err := filepath.Walk(filenames.PluginsFilepath, func(filePath string, info os.FileInfo, err error) error { diff --git a/templates/generation.go b/templates/generation.go index 3c48b597..f5f088e8 100644 --- a/templates/generation.go +++ b/templates/generation.go @@ -7,8 +7,9 @@ import ( "github.com/kabukky/journey/filenames" "github.com/kabukky/journey/flags" "github.com/kabukky/journey/helpers" + "github.com/kabukky/journey/plugins" "github.com/kabukky/journey/structure" - "gopkg.in/fsnotify.v1" + "github.com/kabukky/journey/watcher" "io/ioutil" "log" "os" @@ -17,10 +18,6 @@ import ( "strings" ) -// For watching the theme directory for changes -var themeFileWatcher *fsnotify.Watcher -var watchedDirectories []string - // For parsing of the theme files var openTag = []byte("{{") var closeTag = []byte("}}") @@ -233,67 +230,14 @@ func Generate() error { if _, ok := compiledTemplates.m["post"]; !ok { return errors.New("Couldn't compile template 'post'. Is post.hbs missing?") } - // If the dev flag is set, watch the theme directory for changes + // If the dev flag is set, watch the theme directory and the plugin directoy for changes + // TODO: It seems unclean to do the watching of the plugins in the templates package. Move this somewhere else. if flags.IsInDevMode { - err = watchThemeDirectory(currentThemePath) + // Create watcher + err = watcher.Watch([]string{currentThemePath, filenames.PluginsFilepath}, map[string]func() error{".hbs": Generate, ".lua": plugins.Load}) if err != nil { return err } } return nil } - -func watchThemeDirectory(currentThemePath string) error { - // Prepare watcher to generate the theme on changes to the files - if themeFileWatcher == nil { - var err error - themeFileWatcher, err = createThemeFileWatcher() - if err != nil { - return err - } - } else { - // Remove all current directories from watcher - for _, dir := range watchedDirectories { - err := themeFileWatcher.Remove(dir) - if err != nil { - return err - } - } - } - watchedDirectories = make([]string, 0) - // Watch all subdirectories in theme directory - err := filepath.Walk(currentThemePath, func(filePath string, info os.FileInfo, err error) error { - if info.IsDir() { - err := themeFileWatcher.Add(filePath) - if err != nil { - return err - } - watchedDirectories = append(watchedDirectories, filePath) - } - return nil - }) - if err != nil { - return err - } - return nil -} - -func createThemeFileWatcher() (*fsnotify.Watcher, error) { - watcher, err := fsnotify.NewWatcher() - if err != nil { - return nil, err - } - go func() { - for { - select { - case event := <-watcher.Events: - if event.Op&fsnotify.Write == fsnotify.Write && filepath.Ext(event.Name) == ".hbs" { - go Generate() - } - case err := <-watcher.Errors: - log.Println("Error while watching theme directory.", err) - } - } - }() - return watcher, nil -} diff --git a/watcher/watcher.go b/watcher/watcher.go new file mode 100644 index 00000000..81ce5c84 --- /dev/null +++ b/watcher/watcher.go @@ -0,0 +1,76 @@ +package watcher + +import ( + "gopkg.in/fsnotify.v1" + "log" + "os" + "path/filepath" +) + +var watcher *fsnotify.Watcher +var watchedDirectories []string + +func Watch(paths []string, extensionsFunctions map[string]func() error) error { + // Prepare watcher to generate the theme on changes to the files + if watcher == nil { + var err error + watcher, err = createWatcher(extensionsFunctions) + if err != nil { + return err + } + } else { + // Remove all current directories from watcher + for _, dir := range watchedDirectories { + err := watcher.Remove(dir) + if err != nil { + return err + } + } + } + watchedDirectories = make([]string, 0) + // Watch all subdirectories in the given paths + for _, path := range paths { + err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error { + if info.IsDir() { + err := watcher.Add(filePath) + if err != nil { + return err + } + watchedDirectories = append(watchedDirectories, filePath) + } + return nil + }) + if err != nil { + return err + } + } + return nil +} + +func createWatcher(extensionsFunctions map[string]func() error) (*fsnotify.Watcher, error) { + watcher, err := fsnotify.NewWatcher() + if err != nil { + return nil, err + } + go func() { + for { + select { + case event := <-watcher.Events: + if event.Op&fsnotify.Write == fsnotify.Write { + for key, value := range extensionsFunctions { + if filepath.Ext(event.Name) == key { + // Call the function associated with this file extension + err := value() + if err != nil { + log.Panic("Error while reloading theme or plugins:", err) + } + } + } + } + case err := <-watcher.Errors: + log.Println("Error while watching theme directory.", err) + } + } + }() + return watcher, nil +}