summaryrefslogtreecommitdiff
path: root/model/multi.go
diff options
context:
space:
mode:
Diffstat (limited to 'model/multi.go')
-rw-r--r--model/multi.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/model/multi.go b/model/multi.go
new file mode 100644
index 0000000..35b792f
--- /dev/null
+++ b/model/multi.go
@@ -0,0 +1,73 @@
+package model
+
+import (
+ "errors"
+ "io/fs"
+ "path/filepath"
+ "slices"
+ "strings"
+)
+
+func Include(root fs.FS, st *State, data map[string]*SourceData, indexers []Indexer, runners []Runner) error {
+ for _, s := range st.Sources {
+ if data[s.Path] == nil || data[s.Path].Includes == nil {
+ continue
+ }
+ for _, w := range data[s.Path].Includes {
+ if IsRelative(w) {
+ p, err := filepath.Abs(filepath.Join(s.Path, w))
+ if err != nil {
+ return err
+ }
+
+ // check if source is already included
+ for _, maybeSource := range st.Sources {
+ if maybeSource.Path == p {
+ continue
+ }
+ }
+
+ src, errs := NewSource(root, p, indexers, runners)
+ if len(errs) > 0 {
+ return errors.Join(errs...)
+ }
+ s.Targets = append(s.Targets, src.Targets...)
+ }
+ }
+ }
+ return nil
+}
+
+func Wants(root fs.FS, st *State, data map[string]*SourceData, indexers []Indexer, runners []Runner) error {
+ for i, s := range st.Sources {
+ if data[s.Path] == nil || data[s.Path].Wants == nil {
+ continue
+ }
+ for _, w := range data[s.Path].Wants {
+ if IsRelative(w) {
+ p, err := filepath.Abs(filepath.Join(s.Path, w))
+ if err != nil {
+ return err
+ }
+
+ // check if source is already included
+ for _, maybeSource := range st.Sources {
+ if maybeSource.Path == p {
+ continue
+ }
+ }
+
+ src, errs := NewSource(root, p, indexers, runners)
+ if len(errs) > 0 {
+ return errors.Join(errs...)
+ }
+ st.Sources = slices.Insert(st.Sources, i+1, src)
+ }
+ }
+ }
+ return nil
+}
+
+func IsRelative(path string) bool {
+ return strings.HasPrefix(path, ".")
+}