Beispiel #1
0
func unwatchAll() {
	fileInfos, err := ioutil.ReadDir(".")
	ftmplting.HandleError(err, "Error listing directory")

	for _, fi := range fileInfos {
		fileName := fi.Name()
		if strings.HasPrefix(fileName, watchLockFile) {
			err := os.Remove(fileName)
			ftmplting.HandleError(err, "Cannot remove "+fileName)
		}
	}
}
Beispiel #2
0
func watchAndRecompile(params ftmplting.Params) {
	fmt.Println("Watching for changes in", params.SourceDir)
	watcher, err := fsnotify.NewWatcher()
	ftmplting.HandleError(err, "Error starting watcher for "+params.SourceDir)

	watchLockFile := watchLockFile + fmt.Sprintf("%d", os.Getpid())
	_, err = os.Create(watchLockFile)
	ftmplting.HandleError(err, "Cannot create "+watchLockFile)

	done := make(chan bool)

	go func() {
		for {
			select {
			case ev := <-watcher.Event:
				if ev != nil {
					if strings.HasSuffix(ev.Name, ".tmpl") {
						fmt.Println(params.SourceDir, "changed => recompiling:", ev.Name)
						ftmplting.Do(params)
					}
					if ev.Name == watchLockFile && ev.IsDelete() {
						fmt.Println("Stop watching " + params.SourceDir)
						os.Exit(0)
					}
				}
			case err := <-watcher.Error:
				ftmplting.HandleError(err, "Error")
			}
		}
	}()

	err = watcher.Watch(params.SourceDir)
	ftmplting.HandleError(err, "Error watching "+params.SourceDir)

	err = watcher.Watch(watchLockFile)
	ftmplting.HandleError(err, "Error watching "+params.SourceDir)

	<-done

	_ = watcher.Close()
}