From f77fc4ef5f2df96c6bba3cd6c7d88a77d61a5a80 Mon Sep 17 00:00:00 2001 From: ewy Date: Tue, 2 Jun 2026 15:07:59 +0200 Subject: add .wants and .includes .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 --- model/multi.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 model/multi.go (limited to 'model/multi.go') 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, ".") +} -- cgit v1.3.1