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
func (c command) process(dir string, flags int) error {
var args []string
args = appendExpEnv(args, c)
args := appendExpEnv(nil, c)
var cmd *exec.Cmd
switch {

View File

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

View File

@ -23,23 +23,37 @@
package main
import (
"fmt"
"log"
"os"
"strings"
)
type env []string
func (e env) process() {
func (e env) process(flags int) error {
items := appendExpEnv(nil, e)
var value string
switch {
case len(items) == 0:
return
return fmt.Errorf("enviornment element is invalid")
case len(items) == 1:
if flags&flagVerbose == flagVerbose {
log.Printf("unsetting variable %s", items[0])
}
os.Unsetenv(items[0])
return nil
case len(items) == 2:
os.Setenv(items[0], items[1])
value = items[1]
default:
os.Setenv(items[0], strings.Join(items[1:], ","))
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 {
var args []string
args = appendExpEnv(args, m.Prefix)
args := appendExpEnv(nil, m.Prefix)
args = appendExpEnv(args, params)
args = appendExpEnv(args, m.Suffix)
@ -53,3 +52,18 @@ func (m macroDef) process(dir string, params []string, flags int) error {
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"
type macro []string
type taskDef struct {
Deps []string
Deps []task
Links []link
Cmds []command
Macros []macro
Envs []env
}
func (t *taskDef) process(taskName, srcDir, dstDir string, conf *config, flags int) error {
handled, ok := conf.tasksHandled[taskName]
if ok && handled {
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 {
func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error {
for _, currTask := range t.Deps {
if err := currTask.process(srcDir, dstDir, conf, flags); err != nil {
return err
}
}
for _, currEnv := range t.Envs {
currEnv.process()
if err := currEnv.process(flags); err != nil {
return err
}
}
if flags&flagNoMacro == 0 {
for _, currMacro := range t.Macros {
if len(currMacro) == 0 {
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 {
if err := currMacro.process(dstDir, conf, flags); err != nil {
return err
}
}
@ -95,3 +71,23 @@ func (t *taskDef) process(taskName, srcDir, dstDir string, conf *config, flags i
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)
}