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-02 22:18:25 +01:00
parent 8558e0c48a
commit 10d7123f60
2 changed files with 75 additions and 30 deletions

View File

@@ -2,6 +2,8 @@ package ast
import ( import (
"fmt" "fmt"
"github.com/go-task/task/v3/internal/deepcopy"
"sync"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@@ -9,9 +11,27 @@ import (
) )
// Precondition represents a precondition necessary for a task to run // Precondition represents a precondition necessary for a task to run
type Precondition struct { type (
Sh string Preconditions struct {
Msg string preconditions []*Precondition
mutex sync.RWMutex
}
Precondition struct {
Sh string
Msg string
}
)
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 *Precondition) DeepCopy() *Precondition { func (p *Precondition) DeepCopy() *Precondition {
@@ -24,6 +44,12 @@ func (p *Precondition) DeepCopy() *Precondition {
} }
} }
func NewPreconditions() *Preconditions {
return &Preconditions{
preconditions: make([]*Precondition, 0),
}
}
// UnmarshalYAML implements yaml.Unmarshaler interface. // UnmarshalYAML implements yaml.Unmarshaler interface.
func (p *Precondition) UnmarshalYAML(node *yaml.Node) error { func (p *Precondition) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind { switch node.Kind {
@@ -55,3 +81,16 @@ func (p *Precondition) UnmarshalYAML(node *yaml.Node) error {
return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("precondition") 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("precondition")
}
return nil
}

View File

@@ -20,20 +20,21 @@ var ErrIncludedTaskfilesCantHaveDotenvs = errors.New("task: Included Taskfiles c
// Taskfile is the abstract syntax tree for a Taskfile // Taskfile is the abstract syntax tree for a Taskfile
type Taskfile struct { type Taskfile struct {
Location string Location string
Version *semver.Version Version *semver.Version
Output Output Output Output
Method string Method string
Includes *Includes Includes *Includes
Set []string Set []string
Shopt []string Shopt []string
Vars *Vars Vars *Vars
Env *Vars Env *Vars
Tasks *Tasks Preconditions *Preconditions
Silent bool Tasks *Tasks
Dotenv []string Silent bool
Run string Dotenv []string
Interval time.Duration Run string
Interval time.Duration
} }
// Merge merges the second Taskfile into the first // Merge merges the second Taskfile into the first
@@ -68,19 +69,20 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind { switch node.Kind {
case yaml.MappingNode: case yaml.MappingNode:
var taskfile struct { var taskfile struct {
Version *semver.Version Version *semver.Version
Output Output Output Output
Method string Method string
Includes *Includes Includes *Includes
Set []string Preconditions *Preconditions
Shopt []string Set []string
Vars *Vars Shopt []string
Env *Vars Vars *Vars
Tasks *Tasks Env *Vars
Silent bool Tasks *Tasks
Dotenv []string Silent bool
Run string Dotenv []string
Interval time.Duration Run string
Interval time.Duration
} }
if err := node.Decode(&taskfile); err != nil { if err := node.Decode(&taskfile); err != nil {
return errors.NewTaskfileDecodeError(err, node) return errors.NewTaskfileDecodeError(err, node)
@@ -110,6 +112,10 @@ 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 {
tf.Preconditions = NewPreconditions()
}
return nil return nil
} }