Initial pass for true and false conditionals for tasks

This commit is contained in:
Alex Yatskov 2016-02-13 11:56:01 -08:00
parent 4bda542bd5
commit 29f25892d8
2 changed files with 44 additions and 16 deletions

View File

@ -61,7 +61,7 @@ func findCmdDeps(params []string, conf *config) []string {
return nil return nil
} }
func processCmdMacro(macroName string, args []string, conf *config) error { func processCmdMacro(macroName string, args []string, interact bool, conf *config) error {
m, mn := findCmdMacro(macroName, conf) m, mn := findCmdMacro(macroName, conf)
if m == nil { if m == nil {
return fmt.Errorf("macro or variant not found: %s", macroName) return fmt.Errorf("macro or variant not found: %s", macroName)
@ -75,10 +75,10 @@ func processCmdMacro(macroName string, args []string, conf *config) error {
log.Printf("expanding macro: %s", mn) log.Printf("expanding macro: %s", mn)
} }
return processCmd(margs, conf) return processCmd(margs, interact, conf)
} }
func processCmd(params []string, conf *config) error { func processCmd(params []string, interact bool, conf *config) error {
args := appendExpEnv(nil, params) args := appendExpEnv(nil, params)
if len(args) == 0 { if len(args) == 0 {
return fmt.Errorf("invalid command statement") return fmt.Errorf("invalid command statement")
@ -91,19 +91,28 @@ func processCmd(params []string, conf *config) error {
} }
if strings.HasPrefix(cmdName, "@") { if strings.HasPrefix(cmdName, "@") {
return processCmdMacro(cmdName, cmdArgs, conf) return processCmdMacro(cmdName, cmdArgs, interact, conf)
} }
if conf.flags&flagVerbose != 0 { if conf.flags&flagVerbose != 0 {
log.Printf("executing command: %s %s", cmdName, strings.Join(cmdArgs, " ")) log.Printf("executing command: %s %s", cmdName, strings.Join(cmdArgs, " "))
} }
return try(func() error { exec := func() error {
cmd := exec.Command(cmdName, cmdArgs...) cmd := exec.Command(cmdName, cmdArgs...)
cmd.Dir = conf.dstDir cmd.Dir = conf.dstDir
if interact {
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
}
return cmd.Run() return cmd.Run()
}) }
if interact {
return try(exec)
}
return exec()
} }

21
task.go
View File

@ -32,6 +32,8 @@ type task struct {
Links [][]string Links [][]string
Cmds [][]string Cmds [][]string
Envs [][]string Envs [][]string
IfTrue [][]string
IfFalse [][]string
} }
func (t *task) deps(conf *config) []string { func (t *task) deps(conf *config) []string {
@ -60,11 +62,28 @@ func (t *task) process(conf *config) error {
} }
if conf.flags&flagNoCmds == 0 { if conf.flags&flagNoCmds == 0 {
CmdLoop:
for {
for _, currCnd := range t.IfTrue {
if err := processCmd(currCnd, false, conf); err != nil {
break CmdLoop
}
}
for _, currCnd := range t.IfFalse {
if err := processCmd(currCnd, false, conf); err == nil {
break CmdLoop
}
}
for _, currCmd := range t.Cmds { for _, currCmd := range t.Cmds {
if err := processCmd(currCmd, conf); err != nil { if err := processCmd(currCmd, true, conf); err != nil {
return err return err
} }
} }
break
}
} }
if conf.flags&flagNoLinks == 0 { if conf.flags&flagNoLinks == 0 {