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
}
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 {
args := appendExpEnv(nil, params)
if len(args) == 0 {
@ -43,24 +69,16 @@ func processCmd(params []string, conf *config) error {
cmdName := args[0]
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 {
margs = appendExpEnv(margs, args[1:])
}
margs = appendExpEnv(margs, m.Suffix)
if conf.flags&flagVerbose != 0 {
log.Printf("using macro: %s", macroName)
macroArgs = args[1:]
}
return processCmd(margs, conf)
return processCmdMacro(macroName, macroArgs, conf)
}
var cmdArgs []string

23
task.go
View File

@ -37,6 +37,12 @@ type task struct {
}
func (t *task) process(conf *config) error {
if t.handled {
return nil
}
t.handled = true
for _, currTask := range t.Deps {
if err := processTask(currTask, conf); err != nil {
return err
@ -69,31 +75,22 @@ func (t *task) process(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
for _, tn := range taskNames {
for _, tn := range makeVariantNames(taskName, conf.variant) {
t, ok := conf.Tasks[tn]
if !ok {
continue
}
if t.handled {
found = true
continue
}
if !t.handled {
if conf.flags&flagVerbose != 0 {
log.Printf("executing task: %s", tn)
log.Printf("processing task: %s", tn)
}
t.handled = true
if err := t.process(conf); err != nil {
return err
}
}
found = true
}

10
util.go
View File

@ -47,6 +47,16 @@ func makeAbsPath(path string) string {
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 {
for {
fmt.Printf("%s: [y]es, [n]o? ", strings.Join(prompts, " "))