-
Notifications
You must be signed in to change notification settings - Fork 2
/
deps.go
156 lines (143 loc) · 3.37 KB
/
deps.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"sort"
)
func (pkgs Packages) ByName(name string) *PkgInfo {
for _, pkg := range pkgs {
if pkg.Name == name {
return pkg
}
}
return nil
}
func (pkgs Packages) Names(hashes []Hash) []string {
names := make([]string, len(hashes))
for i, hash := range hashes {
names[i] = pkgs[hash].Name
}
sort.Strings(names)
return names
}
func (pkgs Packages) Dump() {
for hash, pkg := range pkgs {
fmt.Printf("%s %s ::\n", hash, pkg.Name)
for dephash, dep := range pkg.Deps {
fmt.Printf(" %s %s \n", dephash, dep.Name)
}
fmt.Printf("\n")
}
}
func GatherDeps(pkgs Packages, root Hash, pkgDir string) (*PkgInfo, error) {
if pkgs[root] != nil {
return pkgs[root], nil // already processed
}
jsonPkg, err := ReadPackage(pkgDir)
if err != nil {
return nil, err
}
pkg := &PkgInfo{
Hash: root,
Name: jsonPkg.Name,
Path: jsonPkg.Gx.Dvcsimport,
Deps: Packages{},
DirectDeps: Packages{},
}
for _, dep := range jsonPkg.GxDependencies {
depPkg, err := GatherDeps(pkgs, dep.Hash, GxDir(dep.Hash, dep.Name))
if err != nil {
return nil, err
}
pkg.Deps[dep.Hash] = depPkg
pkg.DirectDeps[dep.Hash] = depPkg
// collect any extra dependencies (performs closure)
for _, subdep := range depPkg.Deps {
pkg.Deps[subdep.Hash] = subdep
}
}
pkgs[root] = pkg
return pkg, nil
}
func (pkgs Packages) RevDeps(hash Hash) DepSet {
revDeps := DepSet{}
for dephash, dep := range pkgs {
if dep.Deps[hash] != nil {
revDeps.Add(dephash)
}
}
return revDeps
}
func (pkgs Packages) Intersect(deps DepSet) DepSet {
intersect := DepSet{}
for dephash, _ := range pkgs {
if deps.Has(dephash) {
intersect.Add(dephash)
}
}
return intersect
}
type RevDep struct {
Hash Hash
Level int
DirectDeps []Hash // Deps to update that depend on previous level
AlsoUpdate []Hash // Deps that also need to be updated
IndirectDeps []Hash //
}
func BubbleList(pkgs Packages, hash Hash) []RevDep {
// Start by getting the rev deps for the hash
lst := []RevDep{}
deps := pkgs.RevDeps(hash)
deps.Add(hash)
// Now determine which of those packages depends on each other
depMap := map[Hash]DepSet{}
fullDeps := map[Hash]DepSet{}
for dephash, _ := range deps {
depMap[dephash] = pkgs[dephash].Deps.Intersect(deps)
fullDeps[dephash] = depMap[dephash].Clone()
}
level := 0
iterate := func(directDeps ...Hash) []Hash {
res := []Hash{}
for dephash, deps := range depMap {
pruned := []Hash{}
for _, toDel := range directDeps {
if deps.Has(toDel) {
deps.Del(toDel)
pruned = append(pruned, toDel)
}
}
if deps.Len() > 0 {
continue
}
if len(directDeps) > 0 && len(pruned) == 0 {
panic("pruned list empty")
}
delete(depMap, dephash)
toUpdate := pkgs[dephash].DirectDeps.Intersect(fullDeps[dephash])
indirect := fullDeps[dephash]
delete(fullDeps, dephash)
indirect.Del(pruned...)
c := toUpdate.Del(pruned...)
if c != len(pruned) {
panic("direct deps not in package.json")
}
alsoUpdate := toUpdate.Elms()
indirect.Del(alsoUpdate...)
res = append(res, dephash)
lst = append(lst, RevDep{
Hash: dephash,
Level: level,
DirectDeps: pruned,
AlsoUpdate: alsoUpdate,
IndirectDeps: indirect.Elms(),
})
}
level += 1
return res
}
next := iterate()
for len(next) > 0 {
next = iterate(next...)
}
return lst
}