Skip to content

Commit

Permalink
Added watching of plugins in dev mode. Changed watcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
kabukky committed Apr 25, 2015
1 parent f832643 commit 10dd344
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 69 deletions.
14 changes: 7 additions & 7 deletions plugins/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
2 changes: 2 additions & 0 deletions plugins/loading.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
68 changes: 6 additions & 62 deletions templates/generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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("}}")
Expand Down Expand Up @@ -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
}
76 changes: 76 additions & 0 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 10dd344

Please sign in to comment.