mirror of
https://github.com/go-task/task.git
synced 2025-08-08 22:36:57 +02:00
separe preconditions and precondition
This commit is contained in:
@ -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,
|
||||
|
@ -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
|
||||
}
|
||||
|
61
taskfile/ast/preconditions.go
Normal file
61
taskfile/ast/preconditions.go
Normal file
@ -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
|
||||
}
|
Reference in New Issue
Block a user