Exemplo n.º 1
0
func (project *Project) mustTask(name string) (*Project, *Task, string) {
	if name == "" {
		panic("Cannot get task for empty string")
	}

	proj := project

	// use root
	if strings.HasPrefix(name, "/") {
		name = name[1:]
		for true {
			if proj.parent != nil {
				proj = proj.parent
			} else {
				break
			}
		}
	} else {
		proj = project
	}

	taskName := "default"
	parts := strings.Split(name, ":")

	if len(parts) == 1 {
		taskName = parts[0]
	} else {
		namespace := ""

		for i := 0; i < len(parts)-1; i++ {

			if namespace != "" {
				namespace += ":"
			}
			ns := parts[i]
			namespace += ns

			proj = proj.Namespace[ns]
			if proj == nil {
				util.Panic("ERR", "Could not find project having namespace \"%s\"\n", namespace)
			}
		}
		taskName = parts[len(parts)-1]
	}

	task := proj.Tasks[taskName]
	if task == nil {
		util.Panic("ERR", `"%s" task is not defined`+"\n", name)
	}
	return proj, task, taskName
}
Exemplo n.º 2
0
// TaskD adds a task which runs other dependencies with no handler.
func (project *Project) TaskD(name string, dependencies Dependency) *Task {
	task := NewTask(name, project.contextArgm)

	if dependencies == nil {
		util.Panic("godo", "Task %s requires a dependency or handler\n", name)
	}

	task.dependencies = append(task.dependencies, dependencies)
	project.Tasks[name] = task
	return task
}
Exemplo n.º 3
0
// Task1 adds a simple task to the project.
func (project *Project) Task1(name string, handler func(*Context)) *Task {
	task := NewTask(name, project.contextArgm)

	if handler == nil {
		util.Panic("godo", "Task %s requires a dependency or handler\n", name)
	}

	task.Handler = HandlerFunc(handler)

	project.Tasks[name] = task
	return task
}
Exemplo n.º 4
0
func runAndWatch(godoFile string) {
	done := make(chan bool, 1)
	run := func(forceBuild bool) (*exec.Cmd, string) {
		cmd, exe := buildCommand(godoFile, forceBuild)
		cmd.Start()
		go func() {
			err := cmd.Wait()
			done <- true
			if err != nil {
				if isVerbose {
					util.Debug("godo", "godo process killed\n")
				}
			}
		}()
		return cmd, exe
	}

	bufferSize := 2048
	watchr, err := watcher.NewWatcher(bufferSize)
	if err != nil {
		util.Panic("project", "%v\n", err)
	}
	godoDir := filepath.Dir(godoFile)
	watchr.WatchRecursive(godoDir)
	watchr.ErrorHandler = func(err error) {
		util.Error("godo", "Watcher error %v\n", err)
	}

	cmd, exe := run(false)
	// this function will block forever, Ctrl+C to quit app
	// var lastHappenedTime int64
	watchr.Start()
	util.Info("godo", "watching %s ...\n", godoDir)

	<-time.After(godo.GetWatchDelay() + (300 * time.Millisecond))

	// forloop:
	for {
		select {
		case event := <-watchr.Event:
			if event.Path == exe {
				continue
			}
			util.Debug("watchmain", "%+v\n", event)
			syscall.Kill(cmd.Process.Pid, syscall.SIGQUIT)
			cmd.Process.Kill()
			<-done
			cmd, _ = run(true)
		}
	}

}
Exemplo n.º 5
0
func (project *Project) watchTask(task *Task, root string, logName string, handler func(e *watcher.FileEvent)) {
	ignorePathFn := func(p string) bool {
		return watcher.DefaultIgnorePathFn(p) || !task.isWatchedFile(p)
	}

	// if len(task.EffectiveWatchRegexps) == 0 {
	// 	util.Error("godo", "EffectiveWatchRegexps should not be zero")
	// } else {
	// 	ignorePathFn = func(p string) bool {
	// 		return watcher.DefaultIgnorePathFn(p) || !task.isWatchedFile(p)
	// 	}
	// }

	bufferSize := 2048
	watchr, err := watcher.NewWatcher(bufferSize)
	if err != nil {
		util.Panic("project", "%v\n", err)
	}
	watchr.IgnorePathFn = ignorePathFn
	watchr.ErrorHandler = func(err error) {
		util.Error("project", "Watcher error %v\n", err)
	}
	watchr.WatchRecursive(root)

	// this function will block forever, Ctrl+C to quit app
	// var lastHappenedTime int64
	util.Info(logName, "watching %s ...\n", root)

	// not sure why this need to be unbuffered, but it was blocking
	// on cquit <- true
	cquit := make(chan bool, 1)
	project.Lock()
	project.cwatchTasks[cquit] = true
	project.Unlock()
	watchr.Start()
forloop:
	for {
		select {
		case event := <-watchr.Event:
			//util.Debug("DBG", "handling watchr.Event %+v\n", event)
			handler(event)
		case <-cquit:
			watchr.Stop()
			break forloop
		}
	}
}
Exemplo n.º 6
0
// Task adds a task to the project with dependencies and handler.
func (project *Project) Task(name string, dependencies Dependency, handler func(*Context)) *Task {
	task := NewTask(name, project.contextArgm)

	if handler == nil && dependencies == nil {
		util.Panic("godo", "Task %s requires a dependency or handler\n", name)
	}

	if handler != nil {
		task.Handler = HandlerFunc(handler)
	}
	if dependencies != nil {
		task.dependencies = append(task.dependencies, dependencies)
	}

	project.Tasks[name] = task
	return task
}