mk/recipe.go

174 lines
3.1 KiB
Go
Raw Normal View History

2013-03-03 17:50:00 -08:00
// Various function for dealing with recipes.
2013-02-25 23:52:08 -08:00
package main
import (
2013-03-03 17:51:00 -08:00
"bufio"
"fmt"
2013-02-26 11:33:07 -08:00
"io"
"log"
"os"
"os/exec"
2013-03-03 17:51:00 -08:00
"strings"
2013-03-03 18:57:14 -08:00
"unicode/utf8"
2013-02-25 23:52:08 -08:00
)
2013-03-03 17:50:00 -08:00
// Try to unindent a recipe, so that it begins an column 0. (This is mainly for
// recipes in python, or other indentation-significant languages.)
2013-03-03 18:57:14 -08:00
func stripIndentation(s string, mincol int) string {
// trim leading whitespace
reader := bufio.NewReader(strings.NewReader(s))
output := ""
for {
line, err := reader.ReadString('\n')
col := 0
i := 0
for i < len(line) && col < mincol {
c, w := utf8.DecodeRuneInString(line[i:])
if strings.IndexRune(" \t\n", c) >= 0 {
col += 1
i += w
} else {
break
}
}
output += line[i:]
if err != nil {
break
}
}
2013-03-03 17:50:00 -08:00
2013-03-03 18:57:14 -08:00
return output
}
2013-03-03 17:50:00 -08:00
// Indent each line of a recipe.
2013-03-03 18:57:14 -08:00
func printIndented(out io.Writer, s string, ind int) {
indentation := strings.Repeat(" ", ind)
2013-03-03 17:51:00 -08:00
reader := bufio.NewReader(strings.NewReader(s))
2013-03-03 18:57:14 -08:00
firstline := true
2013-03-03 17:51:00 -08:00
for {
line, err := reader.ReadString('\n')
if len(line) > 0 {
2013-03-03 18:57:14 -08:00
if !firstline {
io.WriteString(out, indentation)
}
2013-03-03 17:51:00 -08:00
io.WriteString(out, line)
}
if err != nil {
break
}
2013-03-03 18:57:14 -08:00
firstline = false
2013-03-03 17:51:00 -08:00
}
2013-03-03 17:50:00 -08:00
}
// Execute a recipe.
func dorecipe(target string, u *node, e *edge) bool {
2013-03-03 17:51:00 -08:00
vars := make(map[string][]string)
vars["target"] = []string{target}
if e.r.ismeta {
if e.r.attributes.regex {
for i := range e.matches {
vars[fmt.Sprintf("stem%d", i)] = e.matches[i : i+1]
}
} else {
vars["stem"] = []string{e.stem}
}
}
// TODO: other variables to set
// alltargets
// newprereq
prereqs := make([]string, 0)
for i := range u.prereqs {
if u.prereqs[i].r == e.r && u.prereqs[i].v != nil {
prereqs = append(prereqs, u.prereqs[i].v.name)
}
}
2013-03-09 20:54:13 -08:00
vars["prereq"] = prereqs
2013-03-03 17:51:00 -08:00
input := expandRecipeSigils(e.r.recipe, vars)
sh := "sh"
args := []string{}
if len(e.r.shell) > 0 {
sh = e.r.shell[0]
args = e.r.shell[1:]
}
if !e.r.attributes.quiet {
2013-03-03 18:57:14 -08:00
mkPrintRecipe(target, input)
2013-03-03 17:51:00 -08:00
}
2013-03-03 17:50:00 -08:00
2013-03-03 17:51:00 -08:00
if dryrun {
return true
}
_, success := subprocess(
sh,
args,
input,
true,
true,
false)
return success
}
2013-03-03 17:50:00 -08:00
// A monolithic function for executing subprocesses
func subprocess(program string,
2013-02-26 11:33:07 -08:00
args []string,
input string,
echo_out bool,
echo_err bool,
2013-03-03 17:50:00 -08:00
capture_out bool) (string, bool) {
2013-02-26 11:33:07 -08:00
cmd := exec.Command(program, args...)
if echo_out {
cmdout, err := cmd.StdoutPipe()
2013-03-03 17:50:00 -08:00
if err == nil {
2013-02-26 11:33:07 -08:00
go io.Copy(os.Stdout, cmdout)
}
}
if echo_err {
2013-03-03 17:50:00 -08:00
cmderr, err := cmd.StderrPipe()
if err == nil {
2013-02-26 11:33:07 -08:00
go io.Copy(os.Stderr, cmderr)
}
}
if len(input) > 0 {
cmdin, err := cmd.StdinPipe()
2013-02-26 22:41:25 -08:00
if err == nil {
go func() { cmdin.Write([]byte(input)); cmdin.Close() }()
2013-02-26 11:33:07 -08:00
}
}
output := ""
var err error
if capture_out {
var outbytes []byte
outbytes, err = cmd.Output()
output = string(outbytes)
2013-03-10 00:34:42 -08:00
if len(output) > 0 && output[len(output)-1] == '\n' {
2013-02-26 22:41:25 -08:00
output = output[:len(output)-1]
}
2013-02-26 11:33:07 -08:00
} else {
err = cmd.Run()
}
2013-03-03 17:51:00 -08:00
success := true
2013-02-26 11:33:07 -08:00
if err != nil {
2013-03-03 17:51:00 -08:00
exiterr, ok := err.(*exec.ExitError)
if ok {
success = exiterr.ProcessState.Success()
} else {
log.Fatal(err)
}
2013-02-26 11:33:07 -08:00
}
2013-03-03 17:50:00 -08:00
return output, success
2013-02-25 23:52:08 -08:00
}