Example #1
0
// RunWithEvent runs this task when triggered from a watch.
// *e* FileEvent contains information about the file/directory which changed
// in watch mode.
func (task *Task) RunWithEvent(logName string, e *watcher.FileEvent) error {
	if task.RunOnce && task.Complete {
		//util.Debug(task.Name, "Already ran\n")
		return nil
	}

	start := time.Now()
	if len(task.WatchGlobs) > 0 && len(task.WatchFiles) == 0 {
		task.expandGlobs()
		if len(task.WatchFiles) == 0 {
			util.Error("task", "\""+task.Name+"\" '%v' did not match any files\n", task.WatchGlobs)
		}
	}
	// Run this task only if the file matches watch Regexps
	rebuilt := ""
	if e != nil {
		rebuilt = "rebuilt "
		if !task.isWatchedFile(e) {
			return nil
		}
		if verbose {
			util.Debug(logName, "%s\n", e.String())
		}
	}

	var err error
	log := true
	if task.Handler != nil {
		context := Context{Task: task, Args: contextArgm}
		err = task.Handler.Handle(&context)
		if err != nil {
			return fmt.Errorf("%q: %s", logName, err.Error())
		}

	} else if len(task.Dependencies) > 0 {
		// no need to log if just dependency
		log = false
	} else {
		util.Info(task.Name, "Ignored. Task does not have a handler or dependencies.\n")
		return nil
	}

	elapsed := time.Now().Sub(start)
	if log {
		util.Info(logName, "%s%vms\n", rebuilt, elapsed.Nanoseconds()/1e6)
	}

	task.Complete = true
	return nil
}
Example #2
0
func watchTask(root string, logName string, handler func(e *watcher.FileEvent)) {
	bufferSize := 2048
	watchr, err := watcher.NewWatcher(bufferSize)
	if err != nil {
		util.Panic("project", "%v\n", err)
	}
	watchr.WatchRecursive(root)
	watchr.ErrorHandler = func(err error) {
		util.Error("project", "%v\n", err)
	}

	// this function will block forever, Ctrl+C to quit app
	var lastHappenedTime int64
	firstTime := true
	for {
		if firstTime {
			util.Info(logName, "watching %s ...\n", root)
			firstTime = false
		}
		event := <-watchr.Event
		//util.Debug("DBG", "watchr.Event %+v\n", event)
		isOlder := event.UnixNano < lastHappenedTime
		lastHappenedTime = event.UnixNano

		if isOlder {
			continue
		}
		handler(event)
	}
}
Example #3
0
func buildMain(src string) string {
	mustBeMain(src)

	exeFile := "godobin-" + godo.Version
	if isWindows {
		exeFile += ".exe"
	}

	dir := filepath.Dir(src)
	exe := filepath.Join(dir, exeFile)
	reasonFormat := "Godofile changed. Rebuilding %s...\n"

	argm := minimist.Parse()
	rebuild := argm.ZeroBool("rebuild")
	if rebuild {
		os.Remove(exe)
	}

	// see if last run exists .godoinfo
	fiExe, err := os.Stat(exe)
	build := os.IsNotExist(err)
	if build {
		reasonFormat = "Building %s...\n"
	}

	fiGodofile, err := os.Stat(src)
	if os.IsNotExist(err) {
		log.Fatalln(err)
		os.Exit(1)
	}
	build = build || fiExe.ModTime().Before(fiGodofile.ModTime())

	if build {
		util.Debug("godo", reasonFormat, exe)

		err := godo.Run("go build -a -o "+exeFile, godo.In{dir})
		if err != nil {
			panic(err)
		}
	}

	if rebuild {
		util.Info("godo", "ok")
		os.Exit(0)
	}

	return exe
}