1
0
mirror of https://github.com/go-task/task.git synced 2025-08-08 22:36:57 +02:00
This commit is contained in:
Valentin Maerten
2025-01-03 14:03:16 +01:00
parent 10d7123f60
commit 08bd0d982a
3 changed files with 15 additions and 10 deletions

View File

@@ -14,7 +14,7 @@ import (
var ErrPreconditionFailed = errors.New("task: precondition not met") var ErrPreconditionFailed = errors.New("task: precondition not met")
func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *ast.Task) (bool, error) { func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *ast.Task) (bool, error) {
for _, p := range t.Preconditions { for _, p := range append(t.Preconditions, e.Taskfile.Preconditions.Preconditions...) {
err := execext.RunCommand(ctx, &execext.RunCommandOptions{ err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: p.Sh, Command: p.Sh,
Dir: t.Dir, Dir: t.Dir,

View File

@@ -2,9 +2,11 @@ package ast
import ( import (
"fmt" "fmt"
"github.com/go-task/task/v3/internal/deepcopy"
"sync" "sync"
"github.com/go-task/task/v3/internal/deepcopy"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"github.com/go-task/task/v3/errors" "github.com/go-task/task/v3/errors"
@@ -13,7 +15,7 @@ import (
// Precondition represents a precondition necessary for a task to run // Precondition represents a precondition necessary for a task to run
type ( type (
Preconditions struct { Preconditions struct {
preconditions []*Precondition Preconditions []*Precondition
mutex sync.RWMutex mutex sync.RWMutex
} }
@@ -30,7 +32,7 @@ func (p *Preconditions) DeepCopy() *Preconditions {
defer p.mutex.RUnlock() defer p.mutex.RUnlock()
p.mutex.RLock() p.mutex.RLock()
return &Preconditions{ return &Preconditions{
preconditions: deepcopy.Slice(p.preconditions), Preconditions: deepcopy.Slice(p.Preconditions),
} }
} }
@@ -46,7 +48,7 @@ func (p *Precondition) DeepCopy() *Precondition {
func NewPreconditions() *Preconditions { func NewPreconditions() *Preconditions {
return &Preconditions{ return &Preconditions{
preconditions: make([]*Precondition, 0), Preconditions: make([]*Precondition, 0),
} }
} }
@@ -83,13 +85,12 @@ func (p *Precondition) UnmarshalYAML(node *yaml.Node) error {
} }
func (p *Preconditions) UnmarshalYAML(node *yaml.Node) error { func (p *Preconditions) UnmarshalYAML(node *yaml.Node) error {
if p == nil || p.Preconditions == nil {
if p == nil || p.preconditions == nil {
*p = *NewPreconditions() *p = *NewPreconditions()
} }
if err := node.Decode(&p.preconditions); err != nil { if err := node.Decode(&p.Preconditions); err != nil {
return errors.NewTaskfileDecodeError(err, node).WithTypeMessage("precondition") return errors.NewTaskfileDecodeError(err, node).WithTypeMessage("preconditions")
} }
return nil return nil

View File

@@ -60,8 +60,12 @@ func (t1 *Taskfile) Merge(t2 *Taskfile, include *Include) error {
if t1.Tasks == nil { if t1.Tasks == nil {
t1.Tasks = NewTasks() t1.Tasks = NewTasks()
} }
if t1.Preconditions == nil {
t1.Preconditions = NewPreconditions()
}
t1.Vars.Merge(t2.Vars, include) t1.Vars.Merge(t2.Vars, include)
t1.Env.Merge(t2.Env, include) t1.Env.Merge(t2.Env, include)
// TODO:@vmaerten Merge precondition
return t1.Tasks.Merge(t2.Tasks, include, t1.Vars) return t1.Tasks.Merge(t2.Tasks, include, t1.Vars)
} }
@@ -100,6 +104,7 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
tf.Dotenv = taskfile.Dotenv tf.Dotenv = taskfile.Dotenv
tf.Run = taskfile.Run tf.Run = taskfile.Run
tf.Interval = taskfile.Interval tf.Interval = taskfile.Interval
tf.Preconditions = taskfile.Preconditions
if tf.Includes == nil { if tf.Includes == nil {
tf.Includes = NewIncludes() tf.Includes = NewIncludes()
} }
@@ -112,7 +117,6 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
if tf.Tasks == nil { if tf.Tasks == nil {
tf.Tasks = NewTasks() tf.Tasks = NewTasks()
} }
if tf.Preconditions == nil { if tf.Preconditions == nil {
tf.Preconditions = NewPreconditions() tf.Preconditions = NewPreconditions()
} }