Cleanup
This commit is contained in:
parent
20602f0a96
commit
2eb2dceedf
39
command.go
39
command.go
@ -30,26 +30,45 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func processCmd(params []string, dir string, flags int) error {
|
||||
args := appendExpEnv(nil, params)
|
||||
type macro struct {
|
||||
Prefix []string
|
||||
Suffix []string
|
||||
}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
switch {
|
||||
case len(args) == 0:
|
||||
func processCmd(params []string, dir string, conf *config, flags int) error {
|
||||
args := appendExpEnv(nil, params)
|
||||
if len(args) == 0 {
|
||||
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.Stderr = os.Stderr
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stdin = os.Stdin
|
||||
|
||||
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()
|
||||
|
@ -23,8 +23,8 @@
|
||||
package main
|
||||
|
||||
type config struct {
|
||||
Tasks map[string]taskDef
|
||||
Macros map[string]macroDef
|
||||
Tasks map[string]task
|
||||
Macros map[string]macro
|
||||
tasksHandled map[string]bool
|
||||
}
|
||||
|
||||
|
18
homemaker.go
18
homemaker.go
@ -31,7 +31,6 @@ import (
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/naoina/toml"
|
||||
"gopkg.in/yaml.v2"
|
||||
@ -46,7 +45,7 @@ const (
|
||||
flagNoMacro
|
||||
)
|
||||
|
||||
func parse(filename string) (*config, error) {
|
||||
func parseCfg(filename string) (*config, error) {
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -80,15 +79,6 @@ func usage() {
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
func makeAbsPath(path string) string {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
func main() {
|
||||
currUsr, err := user.Current()
|
||||
if err != nil {
|
||||
@ -102,7 +92,6 @@ func main() {
|
||||
verbose := flag.Bool("verbose", false, "verbose output")
|
||||
nocmd := flag.Bool("nocmd", false, "don't execute commands")
|
||||
nolink := flag.Bool("nolink", false, "don't create links")
|
||||
nomacro := flag.Bool("nomacro", false, "don't execute macros")
|
||||
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
@ -123,9 +112,6 @@ func main() {
|
||||
if *nolink {
|
||||
flags |= flagNoLink
|
||||
}
|
||||
if *nomacro {
|
||||
flags |= flagNoMacro
|
||||
}
|
||||
|
||||
if flag.NArg() == 2 {
|
||||
confDirAbs := makeAbsPath(flag.Arg(0))
|
||||
@ -137,7 +123,7 @@ func main() {
|
||||
os.Setenv("HM_SRC", srcDirAbs)
|
||||
os.Setenv("HM_DEST", dstDirAbs)
|
||||
|
||||
conf, err := parse(confDirAbs)
|
||||
conf, err := parseCfg(confDirAbs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
67
macro.go
67
macro.go
@ -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
27
task.go
@ -24,15 +24,14 @@ package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type taskDef struct {
|
||||
Deps []string
|
||||
Links [][]string
|
||||
Cmds [][]string
|
||||
Macros [][]string
|
||||
Envs [][]string
|
||||
type task struct {
|
||||
Deps []string
|
||||
Links [][]string
|
||||
Cmds [][]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 {
|
||||
if err := processTask(currTask, srcDir, dstDir, conf, flags); err != nil {
|
||||
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 {
|
||||
for _, currLink := range t.Links {
|
||||
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 {
|
||||
for _, currCmd := range t.Cmds {
|
||||
if err := processCmd(currCmd, dstDir, flags); err != nil {
|
||||
if err := processCmd(currCmd, dstDir, conf, flags); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -80,10 +71,10 @@ func processTask(taskName, srcDir, dstDir string, conf *config, flags int) error
|
||||
|
||||
conf.tasksHandled[taskName] = true
|
||||
|
||||
task, ok := conf.Tasks[taskName]
|
||||
t, ok := conf.Tasks[taskName]
|
||||
if !ok {
|
||||
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
15
util.go
@ -22,7 +22,11 @@
|
||||
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func appendExpEnv(dst, src []string) []string {
|
||||
for _, value := range src {
|
||||
@ -31,3 +35,12 @@ func appendExpEnv(dst, src []string) []string {
|
||||
|
||||
return dst
|
||||
}
|
||||
|
||||
func makeAbsPath(path string) string {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user