From f8baa2051f034d7cd9e24a1cb89a380b1896c958 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 6 Sep 2015 19:40:22 +0900 Subject: [PATCH] Adding support for deps on macros --- .gitignore | 1 - command.go | 71 ++++++++++++++++++++++++++++++++---------------------- task.go | 14 ++++++++++- 3 files changed, 55 insertions(+), 31 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index daf9c6e..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -homemaker diff --git a/command.go b/command.go index 493951f..73b0dae 100644 --- a/command.go +++ b/command.go @@ -31,29 +31,51 @@ import ( ) type macro struct { + Deps []string Prefix []string Suffix []string } -func processCmdMacro(macroName string, args []string, conf *config) error { - for _, mn := range makeVariantNames(macroName, conf.variant) { - m, ok := conf.Macros[mn] - if !ok { - continue +func findCmdMacro(macroName string, conf *config) (*macro, string) { + if strings.HasPrefix(macroName, "@") { + mn := strings.TrimPrefix(macroName, "@") + for _, mn := range makeVariantNames(mn, conf.variant) { + if m, ok := conf.Macros[mn]; ok { + return &m, mn + } } - - margs := appendExpEnv(nil, m.Prefix) - margs = appendExpEnv(margs, args) - margs = appendExpEnv(margs, m.Suffix) - - if conf.flags&flagVerbose != 0 { - log.Printf("expanding macro: %s", mn) - } - - return processCmd(margs, conf) } - return fmt.Errorf("macro or variant not found: %s", macroName) + return nil, "" +} + +func findCmdDeps(params []string, conf *config) []string { + if len(params) == 0 { + return nil + } + + if m, _ := findCmdMacro(params[0], conf); m != nil { + return m.Deps + } + + return nil +} + +func processCmdMacro(macroName string, args []string, conf *config) error { + m, mn := findCmdMacro(macroName, conf) + if m == nil { + return fmt.Errorf("macro or variant not found: %s", macroName) + } + + margs := appendExpEnv(nil, m.Prefix) + margs = appendExpEnv(margs, args) + margs = appendExpEnv(margs, m.Suffix) + + if conf.flags&flagVerbose != 0 { + log.Printf("expanding macro: %s", mn) + } + + return processCmd(margs, conf) } func processCmd(params []string, conf *config) error { @@ -63,24 +85,15 @@ func processCmd(params []string, conf *config) error { } cmdName := args[0] - if strings.HasPrefix(cmdName, "@") { - var ( - macroName = strings.TrimPrefix(cmdName, "@") - macroArgs []string - ) - - if len(args) > 1 { - macroArgs = args[1:] - } - - return processCmdMacro(macroName, macroArgs, conf) - } - var cmdArgs []string if len(args) > 1 { cmdArgs = args[1:] } + if strings.HasPrefix(cmdName, "@") { + return processCmdMacro(cmdName, cmdArgs, conf) + } + if conf.flags&flagVerbose != 0 { log.Printf("executing command: %s %s", cmdName, strings.Join(cmdArgs, " ")) } diff --git a/task.go b/task.go index 0a5a776..5bc91a8 100644 --- a/task.go +++ b/task.go @@ -34,8 +34,20 @@ type task struct { Envs [][]string } +func (t *task) deps(conf *config) []string { + deps := t.Deps + + if conf.flags&flagNoCmds == 0 { + for _, currCmd := range t.Cmds { + deps = append(deps, findCmdDeps(currCmd, conf)...) + } + } + + return deps +} + func (t *task) process(conf *config) error { - for _, currTask := range t.Deps { + for _, currTask := range t.deps(conf) { if err := processTask(currTask, conf); err != nil { return err }