Work in progress

This commit is contained in:
Alex Yatskov 2015-07-20 15:25:22 +09:00
parent d31e9ab103
commit ef14501478
5 changed files with 64 additions and 49 deletions

View File

@ -33,8 +33,7 @@ import (
type command []string type command []string
func (c command) process(dir string, flags int) error { func (c command) process(dir string, flags int) error {
var args []string args := appendExpEnv(nil, c)
args = appendExpEnv(args, c)
var cmd *exec.Cmd var cmd *exec.Cmd
switch { switch {

View File

@ -22,8 +22,6 @@
package main package main
import "fmt"
type config struct { type config struct {
Tasks map[string]taskDef Tasks map[string]taskDef
Macros map[string]macroDef Macros map[string]macroDef
@ -32,11 +30,5 @@ type config struct {
func (c *config) process(srcDir, dstDir, taskName string, flags int) error { func (c *config) process(srcDir, dstDir, taskName string, flags int) error {
c.tasksHandled = make(map[string]bool) c.tasksHandled = make(map[string]bool)
return task(taskName).process(srcDir, dstDir, c, flags)
task, ok := c.Tasks[taskName]
if !ok {
return fmt.Errorf("task not found %s", taskName)
}
return task.process(taskName, srcDir, dstDir, c, flags)
} }

View File

@ -23,23 +23,37 @@
package main package main
import ( import (
"fmt"
"log"
"os" "os"
"strings" "strings"
) )
type env []string type env []string
func (e env) process() { func (e env) process(flags int) error {
items := appendExpEnv(nil, e) items := appendExpEnv(nil, e)
var value string
switch { switch {
case len(items) == 0: case len(items) == 0:
return return fmt.Errorf("enviornment element is invalid")
case len(items) == 1: case len(items) == 1:
os.Unsetenv(items[0]) if flags&flagVerbose == flagVerbose {
case len(items) == 2: log.Printf("unsetting variable %s", items[0])
os.Setenv(items[0], items[1])
default:
os.Setenv(items[0], strings.Join(items[1:], ","))
} }
os.Unsetenv(items[0])
return nil
case len(items) == 2:
value = items[1]
default:
value = strings.Join(items[1:], ",")
}
if flags&flagVerbose == flagVerbose {
log.Printf("setting variable %s to %s", items[0], value)
}
os.Setenv(items[0], value)
return nil
} }

View File

@ -27,8 +27,7 @@ type macroDef struct {
} }
func (m macroDef) process(dir string, params []string, flags int) error { func (m macroDef) process(dir string, params []string, flags int) error {
var args []string args := appendExpEnv(nil, m.Prefix)
args = appendExpEnv(args, m.Prefix)
args = appendExpEnv(args, params) args = appendExpEnv(args, params)
args = appendExpEnv(args, m.Suffix) args = appendExpEnv(args, m.Suffix)
@ -53,3 +52,18 @@ func (m macroDef) process(dir string, params []string, flags int) error {
return cmd.Run() return cmd.Run()
} }
type macro []string
func (m macro) process(dir string, conf *config, flags int) error {
if len(m) == 0 {
return fmt.Errorf("macro element is invalid")
}
macro, ok := conf.Macros[m[0]]
if !ok {
return fmt.Errorf("macro dependency not found %s", m[0])
}
return macro.process(dir, m[1:], flags)
}

60
task.go
View File

@ -24,54 +24,30 @@ package main
import "fmt" import "fmt"
type macro []string
type taskDef struct { type taskDef struct {
Deps []string Deps []task
Links []link Links []link
Cmds []command Cmds []command
Macros []macro Macros []macro
Envs []env Envs []env
} }
func (t *taskDef) process(taskName, srcDir, dstDir string, conf *config, flags int) error { func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error {
handled, ok := conf.tasksHandled[taskName] for _, currTask := range t.Deps {
if ok && handled { if err := currTask.process(srcDir, dstDir, conf, flags); err != nil {
return nil
}
conf.tasksHandled[taskName] = true
for _, depName := range t.Deps {
depTask, ok := conf.Tasks[depName]
if !ok {
return fmt.Errorf("task dependency not found %s", depName)
}
if err := depTask.process(depName, srcDir, dstDir, conf, flags); err != nil {
return err return err
} }
} }
for _, currEnv := range t.Envs { for _, currEnv := range t.Envs {
currEnv.process() if err := currEnv.process(flags); err != nil {
return err
}
} }
if flags&flagNoMacro == 0 { if flags&flagNoMacro == 0 {
for _, currMacro := range t.Macros { for _, currMacro := range t.Macros {
if len(currMacro) == 0 { if err := currMacro.process(dstDir, conf, flags); err != nil {
continue
}
macroName := currMacro[0]
macroParams := currMacro[1:]
depMacro, ok := conf.Macros[macroName]
if !ok {
return fmt.Errorf("macro dependency not found %s", macroName)
}
if err := depMacro.process(dstDir, macroParams, flags); err != nil {
return err return err
} }
} }
@ -95,3 +71,23 @@ func (t *taskDef) process(taskName, srcDir, dstDir string, conf *config, flags i
return nil return nil
} }
type task string
func (t task) process(srcDir, dstDir string, conf *config, flags int) error {
taskName := string(t)
handled, ok := conf.tasksHandled[taskName]
if ok && handled {
return nil
}
conf.tasksHandled[taskName] = true
task, ok := conf.Tasks[taskName]
if !ok {
return fmt.Errorf("task not found %s", t)
}
return task.process(srcDir, dstDir, conf, flags)
}