This commit is contained in:
Alex Yatskov 2015-09-04 13:34:30 +09:00
parent ce68f7324a
commit 241cd0c5ea
3 changed files with 56 additions and 31 deletions

View File

@ -35,6 +35,32 @@ type macro struct {
Suffix []string Suffix []string
} }
func processCmdMacro(macroName string, args []string, conf *config) error {
var found bool
for _, mn := range makeVariantNames(macroName, conf.variant) {
m, ok := conf.Macros[mn]
if !ok {
continue
}
margs := appendExpEnv(nil, m.Prefix)
margs = appendExpEnv(margs, args)
margs = appendExpEnv(margs, m.Suffix)
if conf.flags&flagVerbose != 0 {
log.Printf("using macro: %s", mn)
}
return processCmd(margs, conf)
}
if !found {
return fmt.Errorf("macro or variant not found: %s", macroName)
}
return nil
}
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 {
@ -43,24 +69,16 @@ func processCmd(params []string, conf *config) error {
cmdName := args[0] cmdName := args[0]
if strings.HasPrefix(cmdName, "@") { if strings.HasPrefix(cmdName, "@") {
macroName := strings.TrimPrefix(cmdName, "@") var (
macroName = strings.TrimPrefix(cmdName, "@")
macroArgs []string
)
m, ok := conf.Macros[macroName]
if !ok {
return fmt.Errorf("macro not found: %s", macroName)
}
margs := appendExpEnv(nil, m.Prefix)
if len(args) > 1 { if len(args) > 1 {
margs = appendExpEnv(margs, args[1:]) macroArgs = args[1:]
}
margs = appendExpEnv(margs, m.Suffix)
if conf.flags&flagVerbose != 0 {
log.Printf("using macro: %s", macroName)
} }
return processCmd(margs, conf) return processCmdMacro(macroName, macroArgs, conf)
} }
var cmdArgs []string var cmdArgs []string

31
task.go
View File

@ -37,6 +37,12 @@ type task struct {
} }
func (t *task) process(conf *config) error { func (t *task) process(conf *config) error {
if t.handled {
return nil
}
t.handled = true
for _, currTask := range t.Deps { for _, currTask := range t.Deps {
if err := processTask(currTask, conf); err != nil { if err := processTask(currTask, conf); err != nil {
return err return err
@ -69,30 +75,21 @@ func (t *task) process(conf *config) error {
} }
func processTask(taskName string, conf *config) error { func processTask(taskName string, conf *config) error {
taskNames := []string{taskName}
if len(conf.variant) > 0 {
taskNames = append(taskNames, fmt.Sprint(taskName, "%", conf.variant))
}
var found bool var found bool
for _, tn := range taskNames { for _, tn := range makeVariantNames(taskName, conf.variant) {
t, ok := conf.Tasks[tn] t, ok := conf.Tasks[tn]
if !ok { if !ok {
continue continue
} }
if t.handled { if !t.handled {
found = true if conf.flags&flagVerbose != 0 {
continue log.Printf("processing task: %s", tn)
} }
if conf.flags&flagVerbose != 0 { if err := t.process(conf); err != nil {
log.Printf("executing task: %s", tn) return err
} }
t.handled = true
if err := t.process(conf); err != nil {
return err
} }
found = true found = true

10
util.go
View File

@ -47,6 +47,16 @@ func makeAbsPath(path string) string {
return path return path
} }
func makeVariantNames(name, variant string) []string {
names := []string{name}
if len(variant) > 0 {
names = append(names, fmt.Sprint(name, "%", variant))
}
return names
}
func prompt(prompts ...string) bool { func prompt(prompts ...string) bool {
for { for {
fmt.Printf("%s: [y]es, [n]o? ", strings.Join(prompts, " ")) fmt.Printf("%s: [y]es, [n]o? ", strings.Join(prompts, " "))