Exemplo n.º 1
0
func runBuiltProgram() {
	cmdRunPath := filepath.Join(goPath, "bin", goInstPath[strings.LastIndex(goInstPath, "/")+1:])
	log.Printf("[RUN]\t%s\t\t(in %s)\n%s\n\n", cmdRunPath, origDirPath, strings.Repeat("_", 28+len(cmdRunPath)))
	var (
		watch *fsnotify.Watcher
		err   error
	)
	if watch, err = fsnotify.NewWatcher(); err == nil {
		defer watch.Close()
		if err = watch.Watch(origDirPath); err == nil {
			for impDirPath, _ := range pkgImpDirPaths {
				if err = watch.Watch(impDirPath); err != nil {
					break
				}
			}
			if err == nil {
				cmd := exec.Command(cmdRunPath)
				cmd.Dir = origDirPath
				cmd.Stderr = os.Stderr
				cmd.Stdout = os.Stdout
				cmd.Stdin = os.Stdin
				go func() {
					for {
						select {
						case evt := <-watch.Event:
							if evt != nil && strings.HasSuffix(strings.ToLower(evt.Name), ".go") && cmd.Process != nil {
								log.Printf("[WATCH]\tchange: %s", evt.Name)
								cmd.Process.Kill()
								break
							}
						case err = <-watch.Error:
							if err != nil {
								log.Printf("[WATCH]\terror: %s", err.Error())
								break
							}
						}
					}
				}()
				err = cmd.Run()
			}
		}
	}
	if err != nil {
		log.Printf("[ERROR]\t%+v\n", err)
	}
}
//	Always returns a new `Watcher`, even if `err` is not `nil` (in which case, however, `me.Watcher` might be `nil`).
func NewWatcher() (me *Watcher, err error) {
	me = &Watcher{dirsWatching: map[string]bool{}, allHandlers: map[string][]WatcherHandler{}, closed: make(chan bool)}
	me.DebounceNano = time.Duration(250 * time.Millisecond).Nanoseconds()
	me.Watcher, err = fsnotify.NewWatcher()
	return
}