func liveReload(e string, lr *lrserver.Server) { if strings.Contains(e, ":") { fname := strings.Split(e, ":")[0] es := fname[1 : len(fname)-1] mainLog("===========>LiveReload: %s", es) lr.Reload(es) } }
// watchDir fires off a goroutine and hands back a signalling channel that // can be used to tell it to gracefully die or be used to block a function // until it dies. The channel is closed automatically via defer when the // goroutine is terminated or encounters an unrecoverable error func watchDir(dir string, lrserv *lr.Server) chan signal { die := make(chan signal) /* The code below has been copied with modifications from github.com/spf13/hugo // Copyright © 2013-2015 Steve Francia <*****@*****.**>. // // Licensed under the Simple Public License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://opensource.org/licenses/Simple-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. */ /* TODO: See if we actually need to smash and grab tweakLimit() from hugo.go to bump up OSX file descriptors */ watcher, err := watcher.New(250 * time.Millisecond) if err != nil { fmt.Printf("Couldn't launch filesystem watcher: %v\n", err) os.Exit(1) } addPaths(watcher) lrserv.SetLiveCSS(true) go func() { reload := make(chan string) defer func() { close(reload) close(die) watcher.Close() }() for { select { case events := <-watcher.Events: for _, event := range events { ext := filepath.Ext(event.Name) temp := strings.HasSuffix(ext, "~") || (ext == ".swp") || (ext == ".swx") || (ext == ".tmp") || strings.HasPrefix(ext, ".goutputstream") if temp { continue } // renames are always followed with Create/Modify if event.Op&fsnotify.Rename == fsnotify.Rename { continue } // add new directory to watch list if s, err := os.Stat(event.Name); err == nil && s.Mode().IsDir() { if event.Op&fsnotify.Create == fsnotify.Create { watcher.Add(event.Name) } } // Drop the changed name into the queue to be reloaded go func() { reload <- event.Name }() } case file := <-reload: absFile, err := filepath.Abs(file) if err != nil { fmt.Printf("Unable to convert %s to absolute path %#v\n", file, err) continue } lrserv.Reload(absFile) case err := <-watcher.Errors: if err != nil { fmt.Println("Received filesystem error: ", err) } } } }() return die }