summaryrefslogtreecommitdiff
path: root/model/multi.go
diff options
context:
space:
mode:
authorewy <ewy0@protonmail.com>2026-06-02 15:07:59 +0200
committerewy <ewy0@protonmail.com>2026-06-02 15:07:59 +0200
commitf77fc4ef5f2df96c6bba3cd6c7d88a77d61a5a80 (patch)
treebdead2d2b245407aaa20e71c433d7c8f757d7049 /model/multi.go
parent6d607f114f63184a43237a3ff80aee622401ee23 (diff)
add .wants and .includesHEADmaster
.wants adds potential out-of-tree sources to your state (for monorepos, for example) .includes adds targets from another source to this one, adding inheritance
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, ".")
+}