Skip to content

Commit

Permalink
Added task execution
Browse files Browse the repository at this point in the history
  • Loading branch information
rdsubhas committed Nov 14, 2016
1 parent d1904d5 commit d0b3570
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 11 deletions.
8 changes: 6 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ func List() {
for _, p := range w.Projects {
tasks := []string{}
for t, _ := range p.Tasks {
tasks = append(tasks, t)
if !strings.HasPrefix(t, "_") {
tasks = append(tasks, t)
}
}
if len(tasks) > 0 {
table.Append([]string{p.Name, strings.Join(p.Tags, ", "), strings.Join(tasks, ", ")})
}
table.Append([]string{p.Name, strings.Join(p.Tags, ", "), strings.Join(tasks, ", ")})
}

table.Render()
Expand Down
6 changes: 3 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func Run(qs []string) {

w := loadWorkspace()
for _, q := range queries {
ms := q.Search(&w)
for _, m := range ms {
core.Execute(&w, &m.Project, &m.Task)
err := core.ExecuteQuery(&w, q)
if err != nil {
log.Fatal(err)
}
}
}
87 changes: 84 additions & 3 deletions core/execution.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,90 @@
package core

import (
"log"
"fmt"
"time"
"os"
"os/exec"
)

func Execute(w *Workspace, p *Project, t *Task) {
log.Printf("Running: %v/%v", p.Name, t.Name)
type Execution struct {
Workspace *Workspace
Project *Project
Task *Task
}

func ExecuteQuery(w *Workspace, q Query) error {
matches := q.Search(w)
for _, match := range matches {
e := Execution{Workspace:w, Project:&match.Project, Task:&match.Task}
err := e.Execute()
if err != nil {
return err
}
}
return nil
}

func (e *Execution) Execute() error {
start := time.Now()
fmt.Printf("%v/%v: Running\n", e.Project.Name, e.Task.Name)

err := e.ExecuteDependent(e.Task.Before)
if err == nil {
err = e.ExecuteSelf()
if err == nil {
err = e.ExecuteDependent(e.Task.After)
}
}

elapsed := time.Since(start)
if err != nil {
fmt.Printf("%v/%v: Failed, Took: %v\n", e.Project.Name, e.Task.Name, elapsed)
} else {
fmt.Printf("%v/%v: Completed, Took: %v\n", e.Project.Name, e.Task.Name, elapsed)
}

return err
}

func (e *Execution) ExecuteSelf() error {
return e.Cmd().Run()
}

func (e *Execution) ExecuteDependent(qs []string) error {
for _, q := range qs {
query, err := ParseQuery(q)
if err != nil {
return err
}
if len(query.Tags) == 0 {
query.Tags = []string{e.Project.Name}
}
if !query.Match(e.Project, e.Task) {
err = ExecuteQuery(e.Workspace, query)
if err != nil {
return err
}
}
}
return nil
}

func (e *Execution) Cmd() *exec.Cmd {
cmd := exec.Command("sh", "-exc", e.Task.Cmd)
cmd.Dir = e.Project.Cwd
cmd.Env = envList(e.Project.Env)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}

func envList(env map[string]string) []string {
env["PATH"] = env["PATH"] + string(os.PathListSeparator) + os.Getenv("PATH")
envList := make([]string, len(env))
for k, v := range env {
envList = append(envList, fmt.Sprintf("%v=%v", k, v))
}
return envList
}
9 changes: 7 additions & 2 deletions core/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func ParseProject(path string) (Project, error) {
if info, err := os.Stat(src); err != nil {
return Project{}, err
} else if info.IsDir() {
src = filepath.Join(src, "myke.yml")
if mp, err := ParseProject(filepath.Join(src, "myke.yml")); err != nil {
return ParseProject(filepath.Join(src, "s2do.yml"))
} else {
return mp, err
}
}

p, err := loadProjectYaml(src)
Expand All @@ -45,7 +49,8 @@ func ParseProject(path string) (Project, error) {
for _, epath := range p.EnvFiles {
p.Env = mergeEnv(p.Env, loadEnvFile(epath))
}
// TODO: Merge OsEnv()

p.Env = mergeEnv(p.Env, loadOsEnv())
p.Env["PATH"] = normalizePaths(p.Cwd, p.Env["PATH"])

for _, epath := range p.Extends {
Expand Down
4 changes: 4 additions & 0 deletions core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ func loadTaskJson(name string, json gjson.Result) Task {

if j := json.Get("desc"); j.Exists() {
t.Desc = j.String()
} else {
t.Desc = ""
}
if j := json.Get("cmd"); j.Exists() {
t.Cmd = j.String()
} else {
t.Cmd = ""
}
if j := json.Get("before"); j.Exists() {
for _, s := range j.Array() {
Expand Down
5 changes: 4 additions & 1 deletion core/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func loadOsEnv() (map[string]string) {
env := make(map[string]string)
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
env[pair[0]] = pair[1]
if pair[0] != "PATH" {
// PATH is handled as a special case, so lets skip it
env[pair[0]] = pair[1]
}
}
return env
}
Expand Down

0 comments on commit d0b3570

Please sign in to comment.