include.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package ast
  2. import (
  3. "gopkg.in/yaml.v3"
  4. "github.com/go-task/task/v3/errors"
  5. omap "github.com/go-task/task/v3/internal/omap"
  6. )
  7. // Include represents information about included taskfiles
  8. type Include struct {
  9. Namespace string
  10. Taskfile string
  11. Dir string
  12. Optional bool
  13. Internal bool
  14. Aliases []string
  15. AdvancedImport bool
  16. Vars *Vars
  17. Flatten bool
  18. }
  19. // Includes represents information about included tasksfiles
  20. type Includes struct {
  21. omap.OrderedMap[string, *Include]
  22. }
  23. // UnmarshalYAML implements the yaml.Unmarshaler interface.
  24. func (includes *Includes) UnmarshalYAML(node *yaml.Node) error {
  25. switch node.Kind {
  26. case yaml.MappingNode:
  27. // NOTE(@andreynering): on this style of custom unmarshalling,
  28. // even number contains the keys, while odd numbers contains
  29. // the values.
  30. for i := 0; i < len(node.Content); i += 2 {
  31. keyNode := node.Content[i]
  32. valueNode := node.Content[i+1]
  33. var v Include
  34. if err := valueNode.Decode(&v); err != nil {
  35. return errors.NewTaskfileDecodeError(err, node)
  36. }
  37. v.Namespace = keyNode.Value
  38. includes.Set(keyNode.Value, &v)
  39. }
  40. return nil
  41. }
  42. return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("includes")
  43. }
  44. // Len returns the length of the map
  45. func (includes *Includes) Len() int {
  46. if includes == nil {
  47. return 0
  48. }
  49. return includes.OrderedMap.Len()
  50. }
  51. // Wrapper around OrderedMap.Set to ensure we don't get nil pointer errors
  52. func (includes *Includes) Range(f func(k string, v *Include) error) error {
  53. if includes == nil {
  54. return nil
  55. }
  56. return includes.OrderedMap.Range(f)
  57. }
  58. func (include *Include) UnmarshalYAML(node *yaml.Node) error {
  59. switch node.Kind {
  60. case yaml.ScalarNode:
  61. var str string
  62. if err := node.Decode(&str); err != nil {
  63. return errors.NewTaskfileDecodeError(err, node)
  64. }
  65. include.Taskfile = str
  66. return nil
  67. case yaml.MappingNode:
  68. var includedTaskfile struct {
  69. Taskfile string
  70. Dir string
  71. Optional bool
  72. Internal bool
  73. Flatten bool
  74. Aliases []string
  75. Vars *Vars
  76. }
  77. if err := node.Decode(&includedTaskfile); err != nil {
  78. return errors.NewTaskfileDecodeError(err, node)
  79. }
  80. include.Taskfile = includedTaskfile.Taskfile
  81. include.Dir = includedTaskfile.Dir
  82. include.Optional = includedTaskfile.Optional
  83. include.Internal = includedTaskfile.Internal
  84. include.Aliases = includedTaskfile.Aliases
  85. include.AdvancedImport = true
  86. include.Vars = includedTaskfile.Vars
  87. include.Flatten = includedTaskfile.Flatten
  88. return nil
  89. }
  90. return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("include")
  91. }
  92. // DeepCopy creates a new instance of IncludedTaskfile and copies
  93. // data by value from the source struct.
  94. func (include *Include) DeepCopy() *Include {
  95. if include == nil {
  96. return nil
  97. }
  98. return &Include{
  99. Namespace: include.Namespace,
  100. Taskfile: include.Taskfile,
  101. Dir: include.Dir,
  102. Optional: include.Optional,
  103. Internal: include.Internal,
  104. AdvancedImport: include.AdvancedImport,
  105. Vars: include.Vars.DeepCopy(),
  106. Flatten: include.Flatten,
  107. }
  108. }