-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added watching of plugins in dev mode. Changed watcher.
- Loading branch information
Showing
4 changed files
with
91 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |