This commit is contained in:
Alex Yatskov 2015-07-20 19:42:08 +09:00
parent 20602f0a96
commit 2eb2dceedf
6 changed files with 56 additions and 114 deletions

View File

@ -30,26 +30,45 @@ import (
"strings" "strings"
) )
func processCmd(params []string, dir string, flags int) error { type macro struct {
args := appendExpEnv(nil, params) Prefix []string
Suffix []string
}
var cmd *exec.Cmd func processCmd(params []string, dir string, conf *config, flags int) error {
switch { args := appendExpEnv(nil, params)
case len(args) == 0: if len(args) == 0 {
return fmt.Errorf("command element is invalid") return fmt.Errorf("command element is invalid")
case len(args) == 1:
cmd = exec.Command(args[0])
default:
cmd = exec.Command(args[0], args[1:]...)
} }
cmdName := args[0]
var cmdArgs []string
if strings.HasPrefix(cmdName, "@") {
macroName := strings.TrimPrefix(cmdName, "@")
m, ok := conf.Macros[macroName]
if !ok {
return fmt.Errorf("macro dependency not found %s", macroName)
}
cmdArgs = appendExpEnv(cmdArgs, m.Prefix)
if len(args) > 1 {
cmdArgs = appendExpEnv(cmdArgs, args[1:])
}
cmdArgs = appendExpEnv(cmdArgs, m.Suffix)
} else if len(args) > 1 {
cmdArgs = args[1:]
}
cmd := exec.Command(cmdName, cmdArgs...)
cmd.Dir = dir cmd.Dir = dir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
if flags&flagVerbose == flagVerbose { if flags&flagVerbose == flagVerbose {
log.Printf("executing command %s", strings.Join(args, " ")) log.Printf("executing command %s %s", cmdName, strings.Join(cmdArgs, " "))
} }
return cmd.Run() return cmd.Run()

View File

@ -23,8 +23,8 @@
package main package main
type config struct { type config struct {
Tasks map[string]taskDef Tasks map[string]task
Macros map[string]macroDef Macros map[string]macro
tasksHandled map[string]bool tasksHandled map[string]bool
} }

View File

@ -31,7 +31,6 @@ import (
"os" "os"
"os/user" "os/user"
"path" "path"
"path/filepath"
"github.com/naoina/toml" "github.com/naoina/toml"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -46,7 +45,7 @@ const (
flagNoMacro flagNoMacro
) )
func parse(filename string) (*config, error) { func parseCfg(filename string) (*config, error) {
bytes, err := ioutil.ReadFile(filename) bytes, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
@ -80,15 +79,6 @@ func usage() {
flag.PrintDefaults() flag.PrintDefaults()
} }
func makeAbsPath(path string) string {
path, err := filepath.Abs(path)
if err != nil {
log.Fatal(err)
}
return path
}
func main() { func main() {
currUsr, err := user.Current() currUsr, err := user.Current()
if err != nil { if err != nil {
@ -102,7 +92,6 @@ func main() {
verbose := flag.Bool("verbose", false, "verbose output") verbose := flag.Bool("verbose", false, "verbose output")
nocmd := flag.Bool("nocmd", false, "don't execute commands") nocmd := flag.Bool("nocmd", false, "don't execute commands")
nolink := flag.Bool("nolink", false, "don't create links") nolink := flag.Bool("nolink", false, "don't create links")
nomacro := flag.Bool("nomacro", false, "don't execute macros")
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
@ -123,9 +112,6 @@ func main() {
if *nolink { if *nolink {
flags |= flagNoLink flags |= flagNoLink
} }
if *nomacro {
flags |= flagNoMacro
}
if flag.NArg() == 2 { if flag.NArg() == 2 {
confDirAbs := makeAbsPath(flag.Arg(0)) confDirAbs := makeAbsPath(flag.Arg(0))
@ -137,7 +123,7 @@ func main() {
os.Setenv("HM_SRC", srcDirAbs) os.Setenv("HM_SRC", srcDirAbs)
os.Setenv("HM_DEST", dstDirAbs) os.Setenv("HM_DEST", dstDirAbs)
conf, err := parse(confDirAbs) conf, err := parseCfg(confDirAbs)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -1,67 +0,0 @@
/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
)
type macroDef struct {
Prefix []string
Suffix []string
}
func (m macroDef) process(dir string, params []string, flags int) error {
args := appendExpEnv(nil, m.Prefix)
args = appendExpEnv(args, params)
args = appendExpEnv(args, m.Suffix)
var cmd *exec.Cmd
switch {
case len(args) == 0:
return fmt.Errorf("macro element is invalid")
case len(args) == 1:
cmd = exec.Command(args[0])
default:
cmd = exec.Command(args[0], args[1:]...)
}
cmd.Dir = dir
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
if flags&flagVerbose == flagVerbose {
log.Printf("executing macro %s", strings.Join(args, " "))
}
return cmd.Run()
}
func processMacro(args []string, dir string, conf *config, flags int) error {
if len(args) == 0 {
return fmt.Errorf("macro element is invalid")
}
macro, ok := conf.Macros[args[0]]
if !ok {
return fmt.Errorf("macro dependency not found %s", args[0])
}
return macro.process(dir, args[1:], flags)
}

27
task.go
View File

@ -24,15 +24,14 @@ package main
import "fmt" import "fmt"
type taskDef struct { type task struct {
Deps []string Deps []string
Links [][]string Links [][]string
Cmds [][]string Cmds [][]string
Macros [][]string Envs [][]string
Envs [][]string
} }
func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error { func (t *task) process(srcDir, dstDir string, conf *config, flags int) error {
for _, currTask := range t.Deps { for _, currTask := range t.Deps {
if err := processTask(currTask, srcDir, dstDir, conf, flags); err != nil { if err := processTask(currTask, srcDir, dstDir, conf, flags); err != nil {
return err return err
@ -45,14 +44,6 @@ func (t *taskDef) process(srcDir, dstDir string, conf *config, flags int) error
} }
} }
if flags&flagNoMacro == 0 {
for _, currMacro := range t.Macros {
if err := processMacro(currMacro, dstDir, conf, flags); err != nil {
return err
}
}
}
if flags&flagNoLink == 0 { if flags&flagNoLink == 0 {
for _, currLink := range t.Links { for _, currLink := range t.Links {
if err := processLink(currLink, srcDir, dstDir, flags); err != nil { if err := processLink(currLink, srcDir, dstDir, flags); err != nil {
@ -63,7 +54,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 := processCmd(currCmd, dstDir, flags); err != nil { if err := processCmd(currCmd, dstDir, conf, flags); err != nil {
return err return err
} }
} }
@ -80,10 +71,10 @@ func processTask(taskName, srcDir, dstDir string, conf *config, flags int) error
conf.tasksHandled[taskName] = true conf.tasksHandled[taskName] = true
task, ok := conf.Tasks[taskName] t, ok := conf.Tasks[taskName]
if !ok { if !ok {
return fmt.Errorf("task not found %s", taskName) return fmt.Errorf("task not found %s", taskName)
} }
return task.process(srcDir, dstDir, conf, flags) return t.process(srcDir, dstDir, conf, flags)
} }

15
util.go
View File

@ -22,7 +22,11 @@
package main package main
import "os" import (
"log"
"os"
"path/filepath"
)
func appendExpEnv(dst, src []string) []string { func appendExpEnv(dst, src []string) []string {
for _, value := range src { for _, value := range src {
@ -31,3 +35,12 @@ func appendExpEnv(dst, src []string) []string {
return dst return dst
} }
func makeAbsPath(path string) string {
path, err := filepath.Abs(path)
if err != nil {
log.Fatal(err)
}
return path
}