func (gcmd *command) toExecCmd() (cmd *exec.Cmd, err error) { cmd = exec.Command(gcmd.executable, gcmd.argv...) if gcmd.wd != "" { cmd.Dir = gcmd.wd } cmd.Env = effectiveEnv(gcmd.env) cmd.Stdin = os.Stdin if gcmd.captureOutput { outWrapper := newFileWrapper(os.Stdout, &gcmd.recorder, "") errWrapper := newFileWrapper(os.Stderr, &gcmd.recorder, ansi.ColorCode("red+b")) cmd.Stdout = outWrapper cmd.Stderr = errWrapper } else { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr } if verbose { if Env != "" { util.Debug("#", "Env: %s\n", Env) } util.Debug("#", "%s\n", gcmd.commandstr) } return cmd, nil }
// Run runs all the dependencies of this task and when they have completed, // runs this task. func (task *Task) Run() { if !watching && task.Complete { util.Debug(task.Name, "Already ran\n") return } task.RunWithEvent(task.Name, nil) }
func (gcmd *command) runAsync() (err error) { cmd, err := gcmd.toExecCmd() if err != nil { return } id := gcmd.commandstr // kills previously spawned process (if exists) killSpawned(id) waitgroup.Add(1) waitExit = true go func() { err = cmd.Start() if err != nil { fmt.Println(err.Error()) return } Processes[id] = cmd.Process if verbose { util.Debug("#", "Processes[%q] added\n", id) } c := make(chan error, 1) c <- cmd.Wait() _ = <-c waitgroup.Done() }() return nil }
// 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 }
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 }
func killSpawned(command string) { process := Processes[command] if process == nil { return } err := process.Kill() delete(Processes, command) if err != nil { util.Error("Start", "Could not kill existing process %+v\n%s\n", process, err.Error()) return } if verbose { util.Debug("#", "Processes[%q] killed\n", command) } }