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