-
-
Notifications
You must be signed in to change notification settings - Fork 649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add global precondition #1993
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,56 @@ package ast | |
|
||
import ( | ||
"fmt" | ||
|
||
"gopkg.in/yaml.v3" | ||
"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 Precondition struct { | ||
Sh string | ||
Msg string | ||
type ( | ||
Preconditions struct { | ||
Preconditions []*Precondition | ||
mutex sync.RWMutex | ||
} | ||
|
||
Precondition struct { | ||
Sh string | ||
Msg string | ||
} | ||
Comment on lines
+20
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've moved |
||
) | ||
|
||
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 { | ||
|
@@ -55,3 +95,15 @@ 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -59,28 +60,33 @@ func (t1 *Taskfile) Merge(t2 *Taskfile, include *Include) error { | |
if t1.Tasks == nil { | ||
t1.Tasks = NewTasks() | ||
} | ||
if t1.Preconditions == nil { | ||
t1.Preconditions = NewPreconditions() | ||
} | ||
t1.Vars.Merge(t2.Vars, include) | ||
t1.Env.Merge(t2.Env, include) | ||
t1.Preconditions.Merge(t2.Preconditions) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we don't have scoping, this is going to lead to issues where global preconditions in included Taskfiles are going to impact tasks in parent files. I think this is going to be pretty unintuitive to users at the moment. For example: # Taskfile.yml
version: '3'
includes:
include: include.yml
tasks:
default:
cmds:
- echo "default task" # include.yml
version: '3'
preconditions:
- test -f foo.txt
We could:
I think 1 is pretty reckless. We're essentially intentionally adding behaviour that we know is confusing and will raise questions. 3 is very conservative while 2 is a good compromise. It depends how urgently we want to roll out this functionality. If we don't think its urgent then maybe we wait. The question is how long will scoping actually take? I've already taken a stab at it and I can tell you - it's significant piece of work. |
||
return t1.Tasks.Merge(t2.Tasks, include, t1.Vars) | ||
} | ||
|
||
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) | ||
|
@@ -98,6 +104,7 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error { | |
tf.Dotenv = taskfile.Dotenv | ||
tf.Run = taskfile.Run | ||
tf.Interval = taskfile.Interval | ||
tf.Preconditions = taskfile.Preconditions | ||
if tf.Includes == nil { | ||
tf.Includes = NewIncludes() | ||
} | ||
|
@@ -110,6 +117,9 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error { | |
if tf.Tasks == nil { | ||
tf.Tasks = NewTasks() | ||
} | ||
if tf.Preconditions == nil { | ||
tf.Preconditions = NewPreconditions() | ||
} | ||
return nil | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
version: '3' | ||
|
||
preconditions: | ||
- sh: "[ 1 = 0 ]" | ||
msg: "1 != 0 obviously!" | ||
|
||
tasks: | ||
impossible: | ||
cmd: echo "won't run" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: '3' | ||
|
||
preconditions: | ||
- test -f foo.txt | ||
|
||
tasks: | ||
foo: | ||
|
||
impossible: | ||
preconditions: | ||
- sh: "[ 1 = 0 ]" | ||
msg: "1 != 0 obviously!" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect the global
preconditions
to run first. For example:Assuming that neither file exists, I would expect to see
Also prefer
slices.Concat
overappend
and...
now that its in the std lib.