-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.go
103 lines (94 loc) · 2.34 KB
/
build.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
package dpkg
import (
"os"
"path"
)
func BuildIndices(rootDir string, buildFn BuildFunc, indices []IndicesFile) (string, error) {
err := downloadIndicesContent(rootDir, indices)
if err != nil {
return "", err
}
var fpaths []string
var hashs []string
for _, index := range indices {
fpaths = append(fpaths, path.Join(rootDir, index.CachePath))
hashs = append(hashs, index.Hash)
}
cacheFile := path.Join(rootDir, "db", HashArrayString(hashs))
err = EnsureDirectory(path.Dir(cacheFile))
if err != nil {
return "", err
}
_, err = os.Stat(cacheFile)
if err == nil {
return cacheFile, nil
}
cache, err := buildFn(fpaths...)
if err != nil {
return "", err
}
return cacheFile, storeGOB(cacheFile, cache)
}
func downloadIndicesContent(rootDir string, indices []IndicesFile) error {
for _, index := range indices {
local := path.Join(rootDir, index.CachePath)
hash, _ := HashFile(local)
if hash == index.Hash {
DebugPrintf("%q is cached\n", local)
continue
}
err := DownloadToFile(
index.Url,
path.Join(rootDir, index.CachePath),
)
if err != nil {
return err
}
}
return nil
}
type BuildFunc func(files ...string) (interface{}, error)
func loadOrBuildContent(baseDir string, files []IndicesFile) (map[string][]string, error) {
cache := make(map[string][]string)
cacheFile, err := BuildIndices(baseDir, BuildContent, files)
if err != nil {
return nil, err
}
err = loadGOB(cacheFile, &cache)
return cache, err
}
func loadOrBuildArchive(baseDir string, arch string, files []IndicesFile) (Archive, error) {
cache := NewArchive(arch)
cacheFile, err := BuildIndices(baseDir, BuildArchive, files)
if err != nil {
return cache, err
}
err = loadGOB(cacheFile, &cache)
return cache, err
}
func BuildArchive(files ...string) (interface{}, error) {
cache := NewArchive("")
for _, fpath := range files {
cfs, err := LoadPackages(fpath)
if err != nil {
return cache, err
}
for _, cf := range cfs {
name := cf.Get("Package")
cache.Packages[name] = cf
for _, p := range parseProvides(cf.Get("provides")) {
cache.Virtuals[p] = append(cache.Virtuals[p], name)
}
}
}
return cache, nil
}
func BuildContent(files ...string) (interface{}, error) {
ret := make(map[string][]string)
for _, f := range files {
for name, ps := range parseContentIndices(f) {
ret[name] = ps
}
}
return ret, nil
}