This commit is contained in:
Alex Yatskov 2015-07-20 18:53:42 +09:00
parent d1dcb0a2f4
commit 20602f0a96
7 changed files with 49 additions and 52 deletions

View File

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

View File

@ -30,5 +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) return processTask(taskName, srcDir, dstDir, c, flags)
} }

View File

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

16
link.go
View File

@ -30,8 +30,6 @@ import (
"strconv" "strconv"
) )
type link []string
func cleanPath(loc string, flags int) error { func cleanPath(loc string, flags int) error {
if info, _ := os.Lstat(loc); info != nil { if info, _ := os.Lstat(loc); info != nil {
if info.Mode()&os.ModeSymlink == os.ModeSymlink { if info.Mode()&os.ModeSymlink == os.ModeSymlink {
@ -72,8 +70,8 @@ func createPath(loc string, flags int, mode os.FileMode) error {
return nil return nil
} }
func (l link) parse() (srcPath string, dstPath string, mode os.FileMode, err error) { func parseLink(params []string) (srcPath, dstPath string, mode os.FileMode, err error) {
length := len(l) length := len(params)
if length < 1 || length > 3 { if length < 1 || length > 3 {
err = fmt.Errorf("link element is invalid") err = fmt.Errorf("link element is invalid")
return return
@ -81,7 +79,7 @@ func (l link) parse() (srcPath string, dstPath string, mode os.FileMode, err err
if length > 2 { if length > 2 {
var parsed uint64 var parsed uint64
parsed, err = strconv.ParseUint(l[2], 0, 64) parsed, err = strconv.ParseUint(params[2], 0, 64)
if err != nil { if err != nil {
return return
} }
@ -91,17 +89,17 @@ func (l link) parse() (srcPath string, dstPath string, mode os.FileMode, err err
mode = 0755 mode = 0755
} }
dstPath = os.ExpandEnv(l[0]) dstPath = os.ExpandEnv(params[0])
srcPath = dstPath srcPath = dstPath
if length > 1 { if length > 1 {
srcPath = os.ExpandEnv(l[1]) srcPath = os.ExpandEnv(params[1])
} }
return return
} }
func (l link) process(srcDir, dstDir string, flags int) error { func processLink(params []string, srcDir, dstDir string, flags int) error {
srcPath, dstPath, mode, err := l.parse() srcPath, dstPath, mode, err := parseLink(params)
if err != nil { if err != nil {
return err return err
} }

View File

@ -53,17 +53,15 @@ func (m macroDef) process(dir string, params []string, flags int) error {
return cmd.Run() return cmd.Run()
} }
type macro []string func processMacro(args []string, dir string, conf *config, flags int) error {
if len(args) == 0 {
func (m macro) process(dir string, conf *config, flags int) error {
if len(m) == 0 {
return fmt.Errorf("macro element is invalid") return fmt.Errorf("macro element is invalid")
} }
macro, ok := conf.Macros[m[0]] macro, ok := conf.Macros[args[0]]
if !ok { if !ok {
return fmt.Errorf("macro dependency not found %s", m[0]) return fmt.Errorf("macro dependency not found %s", args[0])
} }
return macro.process(dir, m[1:], flags) return macro.process(dir, args[1:], flags)
} }

28
task.go
View File

@ -25,29 +25,29 @@ package main
import "fmt" import "fmt"
type taskDef struct { type taskDef struct {
Deps []task `json:",string"` Deps []string
Links []link Links [][]string
Cmds []command Cmds [][]string
Macros []macro Macros [][]string
Envs []env Envs [][]string
} }
func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error { func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error {
for _, currTask := range t.Deps { for _, currTask := range t.Deps {
if err := currTask.process(srcDir, dstDir, conf, flags); err != nil { if err := processTask(currTask, srcDir, dstDir, conf, flags); err != nil {
return err return err
} }
} }
for _, currEnv := range t.Envs { for _, currEnv := range t.Envs {
if err := currEnv.process(flags); err != nil { if err := processEnv(currEnv, flags); err != nil {
return err return err
} }
} }
if flags&flagNoMacro == 0 { if flags&flagNoMacro == 0 {
for _, currMacro := range t.Macros { for _, currMacro := range t.Macros {
if err := currMacro.process(dstDir, conf, flags); err != nil { if err := processMacro(currMacro, dstDir, conf, flags); err != nil {
return err return err
} }
} }
@ -55,7 +55,7 @@ func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error
if flags&flagNoLink == 0 { if flags&flagNoLink == 0 {
for _, currLink := range t.Links { for _, currLink := range t.Links {
if err := currLink.process(srcDir, dstDir, flags); err != nil { if err := processLink(currLink, srcDir, dstDir, flags); err != nil {
return err return err
} }
} }
@ -63,7 +63,7 @@ func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error
if flags&flagNoCmd == 0 { if flags&flagNoCmd == 0 {
for _, currCmd := range t.Cmds { for _, currCmd := range t.Cmds {
if err := currCmd.process(dstDir, flags); err != nil { if err := processCmd(currCmd, dstDir, flags); err != nil {
return err return err
} }
} }
@ -72,11 +72,7 @@ func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error
return nil return nil
} }
type task string func processTask(taskName, srcDir, dstDir string, conf *config, flags int) error {
func (t task) process(srcDir, dstDir string, conf *config, flags int) error {
taskName := string(t)
handled, ok := conf.tasksHandled[taskName] handled, ok := conf.tasksHandled[taskName]
if ok && handled { if ok && handled {
return nil return nil
@ -86,7 +82,7 @@ func (t task) process(srcDir, dstDir string, conf *config, flags int) error {
task, ok := conf.Tasks[taskName] task, ok := conf.Tasks[taskName]
if !ok { if !ok {
return fmt.Errorf("task not found %s", t) return fmt.Errorf("task not found %s", taskName)
} }
return task.process(srcDir, dstDir, conf, flags) return task.process(srcDir, dstDir, conf, flags)

11
util.go
View File

@ -1,6 +1,15 @@
/* /*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net> * Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
all * Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software. * copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR