mirror of
https://github.com/go-task/task.git
synced 2025-08-08 22:36:57 +02:00
Merge branch 'variables'
This commit is contained in:
60
task.go
60
task.go
@@ -95,10 +95,16 @@ func RunTask(ctx context.Context, name string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !Force && t.isUpToDate() {
|
if !Force {
|
||||||
|
upToDate, err := t.isUpToDate()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if upToDate {
|
||||||
log.Printf(`task: Task "%s" is up to date`, name)
|
log.Printf(`task: Task "%s" is up to date`, name)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for i := range t.Cmds {
|
for i := range t.Cmds {
|
||||||
if err := t.runCommand(ctx, i); err != nil {
|
if err := t.runCommand(ctx, i); err != nil {
|
||||||
@@ -109,18 +115,13 @@ func RunTask(ctx context.Context, name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) runDeps(ctx context.Context) error {
|
func (t *Task) runDeps(ctx context.Context) error {
|
||||||
vars, err := t.handleVariables()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
g, ctx := errgroup.WithContext(ctx)
|
g, ctx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
for _, d := range t.Deps {
|
for _, d := range t.Deps {
|
||||||
dep := d
|
dep := d
|
||||||
|
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
dep, err := ReplaceVariables(dep, vars)
|
dep, err := t.ReplaceVariables(dep)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -132,36 +133,41 @@ func (t *Task) runDeps(ctx context.Context) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = g.Wait(); err != nil {
|
if err := g.Wait(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) isUpToDate() bool {
|
func (t *Task) isUpToDate() (bool, error) {
|
||||||
if len(t.Sources) == 0 || len(t.Generates) == 0 {
|
if len(t.Sources) == 0 || len(t.Generates) == 0 {
|
||||||
return false
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
sourcesMaxTime, err := getPatternsMaxTime(t.Sources)
|
sources, err := t.ReplaceSliceVariables(t.Sources)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
generates, err := t.ReplaceSliceVariables(t.Generates)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sourcesMaxTime, err := getPatternsMaxTime(sources)
|
||||||
if err != nil || sourcesMaxTime.IsZero() {
|
if err != nil || sourcesMaxTime.IsZero() {
|
||||||
return false
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
generatesMinTime, err := getPatternsMinTime(t.Generates)
|
generatesMinTime, err := getPatternsMinTime(generates)
|
||||||
if err != nil || generatesMinTime.IsZero() {
|
if err != nil || generatesMinTime.IsZero() {
|
||||||
return false
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return generatesMinTime.After(sourcesMaxTime)
|
return generatesMinTime.After(sourcesMaxTime), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) runCommand(ctx context.Context, i int) error {
|
func (t *Task) runCommand(ctx context.Context, i int) error {
|
||||||
vars, err := t.handleVariables()
|
c, err := t.ReplaceVariables(t.Cmds[i])
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
c, err := ReplaceVariables(t.Cmds[i], vars)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -174,12 +180,12 @@ func (t *Task) runCommand(ctx context.Context, i int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
dir, err := ReplaceVariables(t.Dir, vars)
|
dir, err := t.ReplaceVariables(t.Dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
envs, err := t.getEnviron(vars)
|
envs, err := t.getEnviron()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -209,7 +215,7 @@ func (t *Task) runCommand(ctx context.Context, i int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) getEnviron(vars map[string]string) ([]string, error) {
|
func (t *Task) getEnviron() ([]string, error) {
|
||||||
if t.Env == nil {
|
if t.Env == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -217,15 +223,11 @@ func (t *Task) getEnviron(vars map[string]string) ([]string, error) {
|
|||||||
envs := os.Environ()
|
envs := os.Environ()
|
||||||
|
|
||||||
for k, v := range t.Env {
|
for k, v := range t.Env {
|
||||||
replacedValue, err := ReplaceVariables(v, vars)
|
env, err := t.ReplaceVariables(fmt.Sprintf("%s=%s", k, v))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
replacedKey, err := ReplaceVariables(k, vars)
|
envs = append(envs, env)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
envs = append(envs, fmt.Sprintf("%s=%s", replacedKey, replacedValue))
|
|
||||||
}
|
}
|
||||||
return envs, nil
|
return envs, nil
|
||||||
}
|
}
|
||||||
|
@@ -25,15 +25,10 @@ var (
|
|||||||
ErrMultilineResultCmd = errors.New("Got multiline result from command")
|
ErrMultilineResultCmd = errors.New("Got multiline result from command")
|
||||||
)
|
)
|
||||||
|
|
||||||
var varCmds = make(map[string]string)
|
|
||||||
|
|
||||||
func handleDynamicVariableContent(value string) (string, error) {
|
func handleDynamicVariableContent(value string) (string, error) {
|
||||||
if !strings.HasPrefix(value, "$") {
|
if !strings.HasPrefix(value, "$") {
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
if result, ok := varCmds[value]; ok {
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
buff := bytes.NewBuffer(nil)
|
buff := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
@@ -53,11 +48,10 @@ func handleDynamicVariableContent(value string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result = strings.TrimSpace(result)
|
result = strings.TrimSpace(result)
|
||||||
varCmds[value] = result
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) handleVariables() (map[string]string, error) {
|
func (t *Task) getVariables() (map[string]string, error) {
|
||||||
localVariables := make(map[string]string)
|
localVariables := make(map[string]string)
|
||||||
for key, value := range t.Vars {
|
for key, value := range t.Vars {
|
||||||
val, err := handleDynamicVariableContent(value)
|
val, err := handleDynamicVariableContent(value)
|
||||||
@@ -105,14 +99,33 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReplaceSliceVariables writes vars into initial string slice
|
||||||
|
func (t *Task) ReplaceSliceVariables(initials []string) ([]string, error) {
|
||||||
|
result := make([]string, len(initials))
|
||||||
|
for i, s := range initials {
|
||||||
|
var err error
|
||||||
|
result[i], err = t.ReplaceVariables(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ReplaceVariables writes vars into initial string
|
// ReplaceVariables writes vars into initial string
|
||||||
func ReplaceVariables(initial string, vars map[string]string) (string, error) {
|
func (t *Task) ReplaceVariables(initial string) (string, error) {
|
||||||
t, err := template.New("").Funcs(templateFuncs).Parse(initial)
|
vars, err := t.getVariables()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
templ, err := template.New("").Funcs(templateFuncs).Parse(initial)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
b := bytes.NewBuffer(nil)
|
b := bytes.NewBuffer(nil)
|
||||||
if err = t.Execute(b, vars); err != nil {
|
if err = templ.Execute(b, vars); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return b.String(), nil
|
return b.String(), nil
|
||||||
|
Reference in New Issue
Block a user