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,29 +31,51 @@ import (
) )
type macro struct { type macro struct {
Deps []string
Prefix []string Prefix []string
Suffix []string Suffix []string
} }
func processCmdMacro(macroName string, args []string, conf *config) error { func findCmdMacro(macroName string, conf *config) (*macro, string) {
for _, mn := range makeVariantNames(macroName, conf.variant) { if strings.HasPrefix(macroName, "@") {
m, ok := conf.Macros[mn] mn := strings.TrimPrefix(macroName, "@")
if !ok { for _, mn := range makeVariantNames(mn, conf.variant) {
continue 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 { func processCmd(params []string, conf *config) error {
@ -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
} }