-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuilder.go
More file actions
220 lines (189 loc) · 5.8 KB
/
builder.go
File metadata and controls
220 lines (189 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package taskgraph
import (
"errors"
"fmt"
)
// TaskBuilder helps construct taskgraph Tasks with a fluent API.
type TaskBuilder[T any] struct {
name string
resultKey Key[T]
depends []any
fn any
condition Condition
defaultVal T
defaultSet bool
defaultBindings []Binding
}
// NewTaskBuilder creates a new builder for a task that produces a result of type T.
func NewTaskBuilder[T any](name string, key Key[T]) *TaskBuilder[T] {
return &TaskBuilder[T]{
name: name,
resultKey: key,
}
}
// DependsOn adds dependencies to the task.
func (b *TaskBuilder[T]) DependsOn(deps ...any) *TaskBuilder[T] {
b.depends = append(b.depends, deps...)
return b
}
// Run sets the function to execute. The function signature must match the dependencies.
func (b *TaskBuilder[T]) Run(fn any) *TaskBuilder[T] {
b.fn = fn
return b
}
// RunIf sets a condition for the task execution.
func (b *TaskBuilder[T]) RunIf(cond Condition) *TaskBuilder[T] {
b.condition = cond
return b
}
// RunIfAll sets a ConditionAnd (logical AND) for the task execution using the provided keys.
func (b *TaskBuilder[T]) RunIfAll(keys ...ReadOnlyKey[bool]) *TaskBuilder[T] {
b.condition = ConditionAnd(keys)
return b
}
// RunIfAny sets a ConditionOr (logical OR) for the task execution using the provided keys.
func (b *TaskBuilder[T]) RunIfAny(keys ...ReadOnlyKey[bool]) *TaskBuilder[T] {
b.condition = ConditionOr(keys)
return b
}
// Default sets the default value for the result key if the condition is false.
func (b *TaskBuilder[T]) Default(val T) *TaskBuilder[T] {
b.defaultVal = val
b.defaultSet = true
return b
}
// WithDefaultBindings adds arbitrary default bindings if the condition is false.
func (b *TaskBuilder[T]) WithDefaultBindings(bindings ...Binding) *TaskBuilder[T] {
b.defaultBindings = append(b.defaultBindings, bindings...)
return b
}
// Build constructs and returns the Task.
func (b *TaskBuilder[T]) Build() (TaskSet, error) {
reflect := Reflect[T]{
Name: b.name,
ResultKey: b.resultKey,
Depends: b.depends,
Fn: b.fn,
}
reflect.location = getLocation(2)
// Eagerly build to validate and get the underlying task
task, err := reflect.Build()
if err != nil {
return nil, err
}
var ts TaskSet = task
if b.condition != nil {
conditional := Conditional{
Wrapped: ts,
Condition: b.condition,
}
if b.defaultSet {
conditional.DefaultBindings = append(
conditional.DefaultBindings,
b.resultKey.Bind(b.defaultVal),
)
}
conditional.DefaultBindings = append(conditional.DefaultBindings, b.defaultBindings...)
conditional.location = getLocation(2)
ts = conditional
}
return ts, nil
}
// Tasks satisfies the TaskSet interface to avoid the need to call Build(). It is equivalent to
// calling Must(Build()).Tasks().
func (b *TaskBuilder[T]) Tasks() []Task {
return Must(b.Build()).Tasks()
}
// MultiTaskBuilder helps construct taskgraph Tasks that provide multiple outputs or perform side effects.
type MultiTaskBuilder struct {
name string
depends []any
fn any
provides []ID
condition Condition
defaultBindings []Binding
errors []error
}
// NewMultiTaskBuilder creates a new builder for a multi-output or side-effect task.
func NewMultiTaskBuilder(name string) *MultiTaskBuilder {
return &MultiTaskBuilder{
name: name,
}
}
// DependsOn adds dependencies to the task.
func (b *MultiTaskBuilder) DependsOn(deps ...any) *MultiTaskBuilder {
b.depends = append(b.depends, deps...)
return b
}
// Provides declares the keys that this task provides.
func (b *MultiTaskBuilder) Provides(keys ...any) *MultiTaskBuilder {
for _, k := range keys {
rk, err := newReflectKey(k)
if err != nil {
b.errors = append(b.errors, fmt.Errorf("invalid key passed to Provides: %w", err))
continue
}
id, err := rk.ID()
if err != nil {
b.errors = append(b.errors, fmt.Errorf("invalid key ID in Provides: %w", err))
continue
}
b.provides = append(b.provides, id)
}
return b
}
// Run sets the function to execute. The function signature must match the dependencies.
// Fn must return []Binding or ([]Binding, error).
func (b *MultiTaskBuilder) Run(fn any) *MultiTaskBuilder {
b.fn = fn
return b
}
// RunIf sets a condition for the task execution.
func (b *MultiTaskBuilder) RunIf(cond Condition) *MultiTaskBuilder {
b.condition = cond
return b
}
// RunIfAll sets a ConditionAnd (logical AND) for the task execution using the provided keys.
func (b *MultiTaskBuilder) RunIfAll(keys ...ReadOnlyKey[bool]) *MultiTaskBuilder {
b.condition = ConditionAnd(keys)
return b
}
// RunIfAny sets a ConditionOr (logical OR) for the task execution using the provided keys.
func (b *MultiTaskBuilder) RunIfAny(keys ...ReadOnlyKey[bool]) *MultiTaskBuilder {
b.condition = ConditionOr(keys)
return b
}
// WithDefaultBindings adds arbitrary default bindings if the condition is false.
func (b *MultiTaskBuilder) WithDefaultBindings(bindings ...Binding) *MultiTaskBuilder {
b.defaultBindings = append(b.defaultBindings, bindings...)
return b
}
// Build constructs and returns the Task.
func (b *MultiTaskBuilder) Build() (TaskSet, error) {
if len(b.errors) > 0 {
return nil, errors.Join(b.errors...)
}
reflect := ReflectMulti{
Name: b.name,
Depends: b.depends,
Fn: b.fn,
Provides: b.provides,
}
reflect.location = getLocation(2)
var task TaskSet = reflect
if b.condition != nil {
conditional := Conditional{
Wrapped: task,
Condition: b.condition,
DefaultBindings: b.defaultBindings,
}
conditional.location = getLocation(2)
task = conditional
}
return task, nil
}
// Tasks satisfies the TaskSet interface to avoid the need to call Build(). It is equivalent to
// calling Must(Build()).Tasks().
func (b *MultiTaskBuilder) Tasks() []Task {
return Must(b.Build()).Tasks()
}