forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve_dependencies.go
61 lines (50 loc) · 1.29 KB
/
resolve_dependencies.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
package pkg
import (
birelpkg "github.com/cloudfoundry/bosh-cli/release/pkg"
)
func ResolveDependencies(pkg birelpkg.Compilable) []birelpkg.Compilable {
return reverse(resolveInner(pkg, []birelpkg.Compilable{}))
}
func resolveInner(pkg birelpkg.Compilable, noFollow []birelpkg.Compilable) []birelpkg.Compilable {
all := []birelpkg.Compilable{}
for _, depPkg := range pkg.Deps() {
if !contains(all, depPkg) && !contains(noFollow, depPkg) {
all = append(all, depPkg)
tDeps := resolveInner(depPkg, joinUnique(all, noFollow))
for _, tDepPkg := range tDeps {
all = append(all, tDepPkg)
}
}
}
for i, el := range all {
if el == pkg {
all = append(all[:i], all[i+1:]...)
}
}
return all
}
func contains(list []birelpkg.Compilable, element birelpkg.Compilable) bool {
for _, pkg := range list {
if element == pkg {
return true
}
}
return false
}
func joinUnique(a []birelpkg.Compilable, b []birelpkg.Compilable) []birelpkg.Compilable {
joined := []birelpkg.Compilable{}
joined = append(joined, a...)
for _, pkg := range b {
if !contains(a, pkg) {
joined = append(joined, pkg)
}
}
return joined
}
func reverse(a []birelpkg.Compilable) []birelpkg.Compilable {
for i := len(a)/2 - 1; i >= 0; i-- {
opp := len(a) - 1 - i
a[i], a[opp] = a[opp], a[i]
}
return a
}