-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_dependency_chains.go
76 lines (64 loc) · 1.43 KB
/
compute_dependency_chains.go
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
74
75
76
package main
import (
"context"
"slices"
"sync"
)
func computeDependencyChains(
ctx context.Context,
goModGraph map[ModuleName][]ModuleName,
rootModule ModuleName,
isModuleMatching ModuleMatchingFunc,
) (<-chan DependencyChain, error) {
var (
chains = make(chan DependencyChain)
wg = new(sync.WaitGroup)
)
moduleToParents := make(map[ModuleName][]ModuleName)
for mod, deps := range goModGraph {
for _, dep := range deps {
moduleToParents[dep] = append(moduleToParents[dep], mod)
}
}
for module, parents := range moduleToParents {
if isModuleMatching(module) {
for _, parent := range parents {
wg.Add(1)
go computeDependencyChain(ctx, wg, moduleToParents, rootModule, parent, NewDependencyChain(module), chains)
}
}
}
go func() {
wg.Wait()
close(chains)
}()
return chains, nil
}
func computeDependencyChain(
ctx context.Context,
wg *sync.WaitGroup,
moduleToParents map[ModuleName][]ModuleName,
rootModule ModuleName,
module ModuleName,
chain DependencyChain,
chains chan<- DependencyChain,
) {
defer wg.Done()
if isContextDone(ctx) {
return
}
// Prevent circular dependencies
if chain.Has(module) {
return
}
chain = chain.Add(module)
if module == rootModule {
slices.Reverse(chain.chain)
chains <- chain
return
}
for _, parent := range moduleToParents[module] {
wg.Add(1)
go computeDependencyChain(ctx, wg, moduleToParents, rootModule, parent, chain, chains)
}
}