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 (
"fmt"
"github.com/go-task/task/v3/internal/deepcopy"
"sync"
"gopkg.in/yaml.v3"
@ -9,9 +11,27 @@ import (
)
// Precondition represents a precondition necessary for a task to run
type Precondition struct {
Sh string
Msg string
type (
Preconditions struct {
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 {
@ -24,6 +44,12 @@ func (p *Precondition) DeepCopy() *Precondition {
}
}
func NewPreconditions() *Preconditions {
return &Preconditions{
preconditions: make([]*Precondition, 0),
}
}
// UnmarshalYAML implements yaml.Unmarshaler interface.
func (p *Precondition) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind {
@ -55,3 +81,16 @@ 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("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
type Taskfile struct {
Location string
Version *semver.Version
Output Output
Method string
Includes *Includes
Set []string
Shopt []string
Vars *Vars
Env *Vars
Tasks *Tasks
Silent bool
Dotenv []string
Run string
Interval time.Duration
Location string
Version *semver.Version
Output Output
Method string
Includes *Includes
Set []string
Shopt []string
Vars *Vars
Env *Vars
Preconditions *Preconditions
Tasks *Tasks
Silent bool
Dotenv []string
Run string
Interval time.Duration
}
// Merge merges the second Taskfile into the first
@ -68,19 +69,20 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind {
case yaml.MappingNode:
var taskfile struct {
Version *semver.Version
Output Output
Method string
Includes *Includes
Set []string
Shopt []string
Vars *Vars
Env *Vars
Tasks *Tasks
Silent bool
Dotenv []string
Run string
Interval time.Duration
Version *semver.Version
Output Output
Method string
Includes *Includes
Preconditions *Preconditions
Set []string
Shopt []string
Vars *Vars
Env *Vars
Tasks *Tasks
Silent bool
Dotenv []string
Run string
Interval time.Duration
}
if err := node.Decode(&taskfile); err != nil {
return errors.NewTaskfileDecodeError(err, node)
@ -110,6 +112,10 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
if tf.Tasks == nil {
tf.Tasks = NewTasks()
}
if tf.Preconditions == nil {
tf.Preconditions = NewPreconditions()
}
return nil
}