From 10d7123f6034bed5417c91a668454283ee601700 Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Thu, 2 Jan 2025 22:18:25 +0100 Subject: [PATCH] wip --- taskfile/ast/precondition.go | 45 +++++++++++++++++++++++++-- taskfile/ast/taskfile.go | 60 ++++++++++++++++++++---------------- 2 files changed, 75 insertions(+), 30 deletions(-) diff --git a/taskfile/ast/precondition.go b/taskfile/ast/precondition.go index 275144c9..07663f2b 100644 --- a/taskfile/ast/precondition.go +++ b/taskfile/ast/precondition.go @@ -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 +} diff --git a/taskfile/ast/taskfile.go b/taskfile/ast/taskfile.go index 4aad932d..f75d8761 100644 --- a/taskfile/ast/taskfile.go +++ b/taskfile/ast/taskfile.go @@ -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 }