summaryrefslogtreecommitdiff
path: root/model/multi.go
blob: 35b792f64667efe46fb712594dcc64527f239bc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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, ".")
}