Adding support for deps on macros

This commit is contained in:
Alex Yatskov 2015-09-06 19:40:22 +09:00
parent 67a97669ca
commit f8baa2051f
3 changed files with 55 additions and 31 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
homemaker

View File

@ -31,15 +31,40 @@ import (
) )
type macro struct { type macro struct {
Deps []string
Prefix []string Prefix []string
Suffix []string Suffix []string
} }
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
}
}
}
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 { func processCmdMacro(macroName string, args []string, conf *config) error {
for _, mn := range makeVariantNames(macroName, conf.variant) { m, mn := findCmdMacro(macroName, conf)
m, ok := conf.Macros[mn] if m == nil {
if !ok { return fmt.Errorf("macro or variant not found: %s", macroName)
continue
} }
margs := appendExpEnv(nil, m.Prefix) margs := appendExpEnv(nil, m.Prefix)
@ -53,9 +78,6 @@ func processCmdMacro(macroName string, args []string, conf *config) error {
return processCmd(margs, conf) return processCmd(margs, conf)
} }
return fmt.Errorf("macro or variant not found: %s", macroName)
}
func processCmd(params []string, conf *config) error { func processCmd(params []string, conf *config) error {
args := appendExpEnv(nil, params) args := appendExpEnv(nil, params)
if len(args) == 0 { if len(args) == 0 {
@ -63,24 +85,15 @@ func processCmd(params []string, conf *config) error {
} }
cmdName := args[0] 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 var cmdArgs []string
if len(args) > 1 { if len(args) > 1 {
cmdArgs = args[1:] cmdArgs = args[1:]
} }
if strings.HasPrefix(cmdName, "@") {
return processCmdMacro(cmdName, cmdArgs, 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, " "))
} }

14
task.go
View File

@ -34,8 +34,20 @@ type task struct {
Envs [][]string 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 { 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 { if err := processTask(currTask, conf); err != nil {
return err return err
} }