Skip to content

Commit f704a06

Browse files
committed
refactor: extract node content loading to separate method
1 parent 1804a50 commit f704a06

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

taskfile/reader.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,43 @@ func (r *Reader) include(node Node) error {
184184
}
185185

186186
func (r *Reader) readNode(node Node) (*ast.Taskfile, error) {
187+
b, err := r.loadNodeContent(node)
188+
if err != nil {
189+
return nil, err
190+
}
191+
192+
var tf ast.Taskfile
193+
if err := yaml.Unmarshal(b, &tf); err != nil {
194+
// Decode the taskfile and add the file info the any errors
195+
taskfileInvalidErr := &errors.TaskfileDecodeError{}
196+
if errors.As(err, &taskfileInvalidErr) {
197+
return nil, taskfileInvalidErr.WithFileInfo(node.Location(), b, 2)
198+
}
199+
return nil, &errors.TaskfileInvalidError{URI: filepathext.TryAbsToRel(node.Location()), Err: err}
200+
}
201+
202+
// Check that the Taskfile is set and has a schema version
203+
if tf.Version == nil {
204+
return nil, &errors.TaskfileVersionCheckError{URI: node.Location()}
205+
}
206+
207+
// Set the taskfile/task's locations
208+
tf.Location = node.Location()
209+
for _, task := range tf.Tasks.Values() {
210+
// If the task is not defined, create a new one
211+
if task == nil {
212+
task = &ast.Task{}
213+
}
214+
// Set the location of the taskfile for each task
215+
if task.Location.Taskfile == "" {
216+
task.Location.Taskfile = tf.Location
217+
}
218+
}
219+
220+
return &tf, nil
221+
}
222+
223+
func (r *Reader) loadNodeContent(node Node) ([]byte, error) {
187224
var b []byte
188225
var err error
189226
var cache *Cache
@@ -272,33 +309,5 @@ func (r *Reader) readNode(node Node) (*ast.Taskfile, error) {
272309
}
273310
}
274311

275-
var tf ast.Taskfile
276-
if err := yaml.Unmarshal(b, &tf); err != nil {
277-
// Decode the taskfile and add the file info the any errors
278-
taskfileInvalidErr := &errors.TaskfileDecodeError{}
279-
if errors.As(err, &taskfileInvalidErr) {
280-
return nil, taskfileInvalidErr.WithFileInfo(node.Location(), b, 2)
281-
}
282-
return nil, &errors.TaskfileInvalidError{URI: filepathext.TryAbsToRel(node.Location()), Err: err}
283-
}
284-
285-
// Check that the Taskfile is set and has a schema version
286-
if tf.Version == nil {
287-
return nil, &errors.TaskfileVersionCheckError{URI: node.Location()}
288-
}
289-
290-
// Set the taskfile/task's locations
291-
tf.Location = node.Location()
292-
for _, task := range tf.Tasks.Values() {
293-
// If the task is not defined, create a new one
294-
if task == nil {
295-
task = &ast.Task{}
296-
}
297-
// Set the location of the taskfile for each task
298-
if task.Location.Taskfile == "" {
299-
task.Location.Taskfile = tf.Location
300-
}
301-
}
302-
303-
return &tf, nil
312+
return b, nil
304313
}

0 commit comments

Comments
 (0)