-
Notifications
You must be signed in to change notification settings - Fork 110
/
update.go
96 lines (78 loc) · 1.94 KB
/
update.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
gx "github.com/whyrusleeping/gx/gxutil"
log "github.com/whyrusleeping/stump"
)
func RecursiveDepUpdate(pkg *gx.Package, from, to string) error {
log.Log("recursively updating %s to %s", from, to)
todo := map[string]string{
from: to,
}
cwd, err := os.Getwd()
if err != nil {
return err
}
checked := make(map[string]bool)
_, err = cascadingUpdate(pkg, cwd, todo, checked)
return err
}
func cascadingUpdate(cur *gx.Package, dir string, updates map[string]string, checked map[string]bool) (bool, error) {
log.Log("cascading update of package %s in %s", cur.Name, dir)
var changed bool
err := cur.ForEachDep(func(dep *gx.Dependency, child *gx.Package) error {
if checked[dep.Hash] {
return nil
}
log.Log(" - processing %s...", dep.Name)
if to, ok := updates[dep.Hash]; ok {
log.Log(" ==> updating dep %s on %s", dep.Name, cur.Name)
dep.Hash = to
dep.Version = child.Version
changed = true
} else {
nchild, err := fetchAndUpdate(dep.Hash, updates, checked)
if err != nil {
return err
}
if nchild != "" {
updates[dep.Hash] = nchild
dep.Hash = nchild
changed = true
} else {
checked[dep.Hash] = true
}
}
return nil
})
if err != nil {
return false, err
}
return changed, nil
}
func fetchAndUpdate(tofetch string, updates map[string]string, checked map[string]bool) (string, error) {
log.Log("fetch and update: %s", tofetch)
dir, err := ioutil.TempDir("", "gx-update")
if err != nil {
return "", err
}
dir = filepath.Join(dir, tofetch)
pkg, err := pm.GetPackageTo(tofetch, dir)
if err != nil {
return "", err
}
changed, err := cascadingUpdate(pkg, dir, updates, checked)
if err != nil {
return "", err
}
if changed {
err := gx.SavePackageFile(pkg, filepath.Join(dir, pkg.Name, gx.PkgFileName))
if err != nil {
return "", err
}
return pm.PublishPackage(dir, &pkg.PackageBase)
}
return "", nil
}