diff --git a/precondition.go b/precondition.go index babe250f..e22dc215 100644 --- a/precondition.go +++ b/precondition.go @@ -15,7 +15,7 @@ import ( var ErrPreconditionFailed = errors.New("task: precondition not met") func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *ast.Task) (bool, error) { - for _, p := range slices.Concat(e.Taskfile.Preconditions.Preconditions, t.Preconditions) { + for _, p := range slices.Concat(e.Taskfile.Preconditions.Values, t.Preconditions) { err := execext.RunCommand(ctx, &execext.RunCommandOptions{ Command: p.Sh, Dir: t.Dir, diff --git a/taskfile/ast/precondition.go b/taskfile/ast/precondition.go index 69ca5f00..2a25396b 100644 --- a/taskfile/ast/precondition.go +++ b/taskfile/ast/precondition.go @@ -2,58 +2,18 @@ package ast import ( "fmt" - "sync" - "github.com/go-task/task/v3/errors" - "github.com/go-task/task/v3/internal/deepcopy" - "gopkg.in/yaml.v3" ) // Precondition represents a precondition necessary for a task to run type ( - Preconditions struct { - Preconditions []*Precondition - mutex sync.RWMutex - } - Precondition struct { Sh string Msg string } ) -func NewPreconditions() *Preconditions { - return &Preconditions{ - Preconditions: make([]*Precondition, 0), - } -} - -func (p *Preconditions) DeepCopy() *Preconditions { - if p == nil { - return nil - } - defer p.mutex.RUnlock() - p.mutex.RLock() - return &Preconditions{ - Preconditions: deepcopy.Slice(p.Preconditions), - } -} - -func (p *Preconditions) Merge(other *Preconditions) { - if p == nil || p.Preconditions == nil || other == nil { - return - } - - p.mutex.Lock() - defer p.mutex.Unlock() - - other.mutex.RLock() - defer other.mutex.RUnlock() - - p.Preconditions = append(p.Preconditions, deepcopy.Slice(other.Preconditions)...) -} - func (p *Precondition) DeepCopy() *Precondition { if p == nil { return nil @@ -95,15 +55,3 @@ func (p *Precondition) UnmarshalYAML(node *yaml.Node) error { return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("precondition") } - -func (p *Preconditions) UnmarshalYAML(node *yaml.Node) error { - if p == nil || p.Preconditions == nil { - *p = *NewPreconditions() - } - - if err := node.Decode(&p.Preconditions); err != nil { - return errors.NewTaskfileDecodeError(err, node).WithTypeMessage("preconditions") - } - - return nil -} diff --git a/taskfile/ast/preconditions.go b/taskfile/ast/preconditions.go new file mode 100644 index 00000000..5afa0412 --- /dev/null +++ b/taskfile/ast/preconditions.go @@ -0,0 +1,61 @@ +package ast + +import ( + "sync" + + "github.com/go-task/task/v3/errors" + "github.com/go-task/task/v3/internal/deepcopy" + + "gopkg.in/yaml.v3" +) + +// Precondition represents a precondition necessary for a task to run +type ( + Preconditions struct { + Values []*Precondition + mutex sync.RWMutex + } +) + +func NewPreconditions() *Preconditions { + return &Preconditions{ + Values: make([]*Precondition, 0), + } +} + +func (p *Preconditions) DeepCopy() *Preconditions { + if p == nil { + return nil + } + defer p.mutex.RUnlock() + p.mutex.RLock() + return &Preconditions{ + Values: deepcopy.Slice(p.Values), + } +} + +func (p *Preconditions) Merge(other *Preconditions) { + if p == nil || p.Values == nil || other == nil { + return + } + + p.mutex.Lock() + defer p.mutex.Unlock() + + other.mutex.RLock() + defer other.mutex.RUnlock() + + p.Values = append(p.Values, deepcopy.Slice(other.Values)...) +} + +func (p *Preconditions) UnmarshalYAML(node *yaml.Node) error { + if p == nil || p.Values == nil { + *p = *NewPreconditions() + } + + if err := node.Decode(&p.Values); err != nil { + return errors.NewTaskfileDecodeError(err, node).WithTypeMessage("preconditions") + } + + return nil +}