2017-03-02 11:46:20 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2017-03-05 21:49:44 +02:00
|
|
|
"bytes"
|
2017-03-09 00:03:17 +02:00
|
|
|
"errors"
|
2017-03-02 11:46:20 +02:00
|
|
|
"os"
|
2017-04-16 22:16:56 +02:00
|
|
|
"path/filepath"
|
2017-03-05 21:49:44 +02:00
|
|
|
"runtime"
|
2017-03-02 11:46:20 +02:00
|
|
|
"strings"
|
2017-03-05 21:49:44 +02:00
|
|
|
"text/template"
|
2017-03-02 12:28:34 +02:00
|
|
|
|
2017-03-12 22:18:59 +02:00
|
|
|
"github.com/go-task/task/execext"
|
|
|
|
|
2017-04-16 21:53:11 +02:00
|
|
|
"github.com/Masterminds/sprig"
|
2017-03-02 12:28:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-03-05 21:21:22 +02:00
|
|
|
// TaskvarsFilePath file containing additional variables
|
|
|
|
TaskvarsFilePath = "Taskvars"
|
2017-03-09 00:03:17 +02:00
|
|
|
// ErrMultilineResultCmd is returned when a command returns multiline result
|
|
|
|
ErrMultilineResultCmd = errors.New("Got multiline result from command")
|
2017-03-02 11:46:20 +02:00
|
|
|
)
|
|
|
|
|
2017-07-15 20:28:59 +02:00
|
|
|
// Vars is a string[string] variables map
|
|
|
|
type Vars map[string]Var
|
|
|
|
|
|
|
|
// Var represents either a static or dynamic variable
|
|
|
|
type Var struct {
|
|
|
|
Static string
|
|
|
|
Sh string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs Vars) toStringMap() (m map[string]string) {
|
|
|
|
m = make(map[string]string, len(vs))
|
|
|
|
for k, v := range vs {
|
2017-07-20 09:05:37 +02:00
|
|
|
if v.Sh != "" {
|
|
|
|
// Dynamic variable is not yet resolved; trigger
|
|
|
|
// <no value> to be used in templates.
|
|
|
|
continue
|
|
|
|
}
|
2017-07-15 20:28:59 +02:00
|
|
|
m[k] = v.Static
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrCantUnmarshalVar is returned for invalid var YAML
|
|
|
|
ErrCantUnmarshalVar = errors.New("task: can't unmarshal var value")
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler interface
|
|
|
|
func (v *Var) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var str string
|
|
|
|
if err := unmarshal(&str); err == nil {
|
|
|
|
if strings.HasPrefix(str, "$") {
|
|
|
|
v.Sh = strings.TrimPrefix(str, "$")
|
|
|
|
} else {
|
|
|
|
v.Static = str
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var sh struct {
|
|
|
|
Sh string
|
|
|
|
}
|
|
|
|
if err := unmarshal(&sh); err == nil {
|
|
|
|
v.Sh = sh.Sh
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrCantUnmarshalVar
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:00:17 +02:00
|
|
|
var (
|
|
|
|
templateFuncs template.FuncMap
|
|
|
|
)
|
2017-04-16 21:53:11 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
taskFuncs := template.FuncMap{
|
|
|
|
"OS": func() string { return runtime.GOOS },
|
|
|
|
"ARCH": func() string { return runtime.GOARCH },
|
2017-04-22 20:46:29 +02:00
|
|
|
// historical reasons
|
|
|
|
"IsSH": func() bool { return true },
|
2017-04-16 22:16:56 +02:00
|
|
|
"FromSlash": func(path string) string {
|
|
|
|
return filepath.FromSlash(path)
|
|
|
|
},
|
|
|
|
"ToSlash": func(path string) string {
|
|
|
|
return filepath.ToSlash(path)
|
|
|
|
},
|
2017-05-27 15:52:22 +02:00
|
|
|
"ExeExt": func() string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return ".exe"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
},
|
2017-04-16 21:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
templateFuncs = sprig.TxtFuncMap()
|
|
|
|
for k, v := range taskFuncs {
|
|
|
|
templateFuncs[k] = v
|
|
|
|
}
|
2017-03-05 21:49:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-20 09:05:37 +02:00
|
|
|
// getVariables returns fully resolved variables following the priorty order:
|
|
|
|
// 1. Call variables (should already have been resolved)
|
|
|
|
// 2. Environment (should not need to be resolved)
|
|
|
|
// 3. Task variables, resolved with access to:
|
|
|
|
// - call, taskvars and environement variables
|
|
|
|
// 4. Taskvars variables, resolved with access to:
|
|
|
|
// - environment variables
|
|
|
|
func (e *Executor) getVariables(call Call) (Vars, error) {
|
|
|
|
t, ok := e.Tasks[call.Task]
|
|
|
|
if !ok {
|
|
|
|
return nil, &taskNotFoundError{call.Task}
|
|
|
|
}
|
2017-07-08 21:00:17 +02:00
|
|
|
|
2017-07-20 09:05:37 +02:00
|
|
|
merge := func(dest Vars, srcs ...Vars) {
|
|
|
|
for _, src := range srcs {
|
|
|
|
for k, v := range src {
|
|
|
|
dest[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
varsKeys := func(srcs ...Vars) []string {
|
|
|
|
m := make(map[string]struct{})
|
|
|
|
for _, src := range srcs {
|
|
|
|
for k := range src {
|
|
|
|
m[k] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lst := make([]string, 0, len(m))
|
|
|
|
for k := range m {
|
|
|
|
lst = append(lst, k)
|
|
|
|
}
|
|
|
|
return lst
|
|
|
|
}
|
|
|
|
replaceVars := func(dest Vars, keys []string) error {
|
|
|
|
r := varReplacer{vars: dest}
|
|
|
|
for _, k := range keys {
|
|
|
|
v := dest[k]
|
|
|
|
dest[k] = Var{
|
|
|
|
Static: r.replace(v.Static),
|
|
|
|
Sh: r.replace(v.Sh),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.err
|
|
|
|
}
|
|
|
|
resolveShell := func(dest Vars, keys []string) error {
|
|
|
|
for _, k := range keys {
|
|
|
|
v := dest[k]
|
2017-07-31 01:36:35 +02:00
|
|
|
static, err := e.handleShVar(v)
|
2017-07-08 20:13:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-20 09:05:37 +02:00
|
|
|
dest[k] = Var{Static: static}
|
2017-07-08 20:13:27 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-20 09:05:37 +02:00
|
|
|
update := func(dest Vars, srcs ...Vars) error {
|
|
|
|
merge(dest, srcs...)
|
|
|
|
// updatedKeys ensures template evaluation is run only once.
|
|
|
|
updatedKeys := varsKeys(srcs...)
|
|
|
|
if err := replaceVars(dest, updatedKeys); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return resolveShell(dest, updatedKeys)
|
2017-07-08 20:13:27 +02:00
|
|
|
}
|
2017-07-20 09:05:37 +02:00
|
|
|
|
|
|
|
// Resolve taskvars variables to "result" with environment override variables.
|
|
|
|
override := getEnvironmentVariables()
|
|
|
|
result := make(Vars, len(e.taskvars)+len(t.Vars)+len(override))
|
|
|
|
if err := update(result, e.taskvars, override); err != nil {
|
2017-07-08 20:13:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-20 09:05:37 +02:00
|
|
|
// Resolve task variables to "result" with environment and call override variables.
|
|
|
|
merge(override, call.Vars)
|
|
|
|
if err := update(result, t.Vars, override); err != nil {
|
2017-07-08 20:13:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2017-07-08 19:46:49 +02:00
|
|
|
func getEnvironmentVariables() Vars {
|
2017-03-25 16:01:44 +02:00
|
|
|
var (
|
|
|
|
env = os.Environ()
|
2017-07-08 19:46:49 +02:00
|
|
|
m = make(Vars, len(env))
|
2017-03-25 16:01:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, e := range env {
|
|
|
|
keyVal := strings.SplitN(e, "=", 2)
|
|
|
|
key, val := keyVal[0], keyVal[1]
|
2017-07-15 20:28:59 +02:00
|
|
|
m[key] = Var{Static: val}
|
2017-03-02 11:46:20 +02:00
|
|
|
}
|
2017-03-25 16:01:44 +02:00
|
|
|
return m
|
2017-03-02 11:46:20 +02:00
|
|
|
}
|
2017-07-08 20:13:27 +02:00
|
|
|
|
2017-07-31 01:36:35 +02:00
|
|
|
func (e *Executor) handleShVar(v Var) (string, error) {
|
2017-07-15 20:28:59 +02:00
|
|
|
if v.Static != "" {
|
|
|
|
return v.Static, nil
|
2017-07-08 20:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
e.muDynamicCache.Lock()
|
|
|
|
defer e.muDynamicCache.Unlock()
|
2017-07-15 20:28:59 +02:00
|
|
|
if result, ok := e.dynamicCache[v.Sh]; ok {
|
2017-07-08 20:13:27 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
opts := &execext.RunCommandOptions{
|
2017-07-15 20:28:59 +02:00
|
|
|
Command: v.Sh,
|
2017-07-08 20:13:27 +02:00
|
|
|
Dir: e.Dir,
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: e.Stderr,
|
|
|
|
}
|
|
|
|
if err := execext.RunCommand(opts); err != nil {
|
|
|
|
return "", &dynamicVarError{cause: err, cmd: opts.Command}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := strings.TrimSuffix(stdout.String(), "\n")
|
|
|
|
if strings.ContainsRune(result, '\n') {
|
|
|
|
return "", ErrMultilineResultCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
result = strings.TrimSpace(result)
|
2017-07-15 20:28:59 +02:00
|
|
|
e.verbosePrintfln(`task: dynamic variable: "%s", result: "%s"`, v.Sh, result)
|
|
|
|
e.dynamicCache[v.Sh] = result
|
2017-07-08 20:13:27 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
2017-07-16 21:09:55 +02:00
|
|
|
|
2017-07-31 00:34:28 +02:00
|
|
|
// CompiledTask returns a copy of a task, but replacing
|
2017-07-16 21:09:55 +02:00
|
|
|
// variables in almost all properties using the Go template package
|
2017-07-31 00:45:01 +02:00
|
|
|
func (t *Task) CompiledTask(dir string, vars Vars) (*Task, error) {
|
2017-07-16 21:09:55 +02:00
|
|
|
r := varReplacer{vars: vars}
|
2017-07-20 09:05:37 +02:00
|
|
|
|
2017-07-16 21:09:55 +02:00
|
|
|
new := Task{
|
|
|
|
Desc: r.replace(t.Desc),
|
|
|
|
Sources: r.replaceSlice(t.Sources),
|
|
|
|
Generates: r.replaceSlice(t.Generates),
|
|
|
|
Status: r.replaceSlice(t.Status),
|
|
|
|
Dir: r.replace(t.Dir),
|
2017-07-20 09:05:37 +02:00
|
|
|
Vars: nil,
|
2017-07-16 21:09:55 +02:00
|
|
|
Set: r.replace(t.Set),
|
|
|
|
Env: r.replaceVars(t.Env),
|
2017-07-20 01:20:24 +02:00
|
|
|
Silent: t.Silent,
|
2017-07-16 21:09:55 +02:00
|
|
|
}
|
2017-07-31 00:45:01 +02:00
|
|
|
if dir != "" && !filepath.IsAbs(new.Dir) {
|
|
|
|
new.Dir = filepath.Join(dir, new.Dir)
|
|
|
|
}
|
2017-07-16 21:09:55 +02:00
|
|
|
|
|
|
|
if len(t.Cmds) > 0 {
|
|
|
|
new.Cmds = make([]*Cmd, len(t.Cmds))
|
|
|
|
for i, cmd := range t.Cmds {
|
|
|
|
new.Cmds[i] = &Cmd{
|
2017-07-20 01:20:24 +02:00
|
|
|
Task: r.replace(cmd.Task),
|
|
|
|
Silent: cmd.Silent,
|
|
|
|
Cmd: r.replace(cmd.Cmd),
|
|
|
|
Vars: r.replaceVars(cmd.Vars),
|
2017-07-16 21:09:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(t.Deps) > 0 {
|
|
|
|
new.Deps = make([]*Dep, len(t.Deps))
|
|
|
|
for i, dep := range t.Deps {
|
|
|
|
new.Deps[i] = &Dep{
|
|
|
|
Task: r.replace(dep.Task),
|
|
|
|
Vars: r.replaceVars(dep.Vars),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &new, r.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// varReplacer is a help struct that allow us to call "replaceX" funcs multiple
|
|
|
|
// times, without having to check for error each time.
|
|
|
|
// The first error that happen will be assigned to r.err, and consecutive
|
|
|
|
// calls to funcs will just return the zero value.
|
|
|
|
type varReplacer struct {
|
|
|
|
vars Vars
|
|
|
|
strMap map[string]string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *varReplacer) replace(str string) string {
|
|
|
|
if r.err != nil || str == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
templ, err := template.New("").Funcs(templateFuncs).Parse(str)
|
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.strMap == nil {
|
|
|
|
r.strMap = r.vars.toStringMap()
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err = templ.Execute(&b, r.strMap); err != nil {
|
|
|
|
r.err = err
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *varReplacer) replaceSlice(strs []string) []string {
|
|
|
|
if r.err != nil || len(strs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
new := make([]string, len(strs))
|
|
|
|
for i, str := range strs {
|
|
|
|
new[i] = r.replace(str)
|
|
|
|
}
|
|
|
|
return new
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *varReplacer) replaceVars(vars Vars) Vars {
|
|
|
|
if r.err != nil || len(vars) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
new := make(Vars, len(vars))
|
|
|
|
for k, v := range vars {
|
|
|
|
new[k] = Var{
|
|
|
|
Static: r.replace(v.Static),
|
|
|
|
Sh: r.replace(v.Sh),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new
|
|
|
|
}
|