-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgir.go
82 lines (65 loc) · 1.63 KB
/
gir.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
package generate
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
type Gir struct {
repo *Repository
addendaRepo *Repository
}
func FromRoot(name, version string, noFormat bool, addToTotal func(n int), oneDone func()) {
girsMap := map[string]*Gir{}
gir := girNew(name, version, girsMap)
// create a map of namespaces
nn := make(map[string]*Namespace)
for _, g := range girsMap {
ns := g.repo.Namespace
ns.repo = g.repo
nn[ns.Name] = ns
}
// make all namespaces available all namespaces
for _, ns := range nn {
ns.namespaces = nn
}
// initialise each namespace
for _, ns := range nn {
ns.repo.Init()
}
gir.repo.Namespace.noFormat = noFormat
if gir.repo.Namespace.Name == name && gir.repo.Namespace.Version == version {
gir.repo.Generate(addToTotal, oneDone)
}
}
func girNew(name string, version string, girs map[string]*Gir) *Gir {
fullname := name + "-" + version
if gir, haveGir := girs[fullname]; haveGir {
return gir
}
g := &Gir{}
g.repo = g.LoadFile(fullname+".gir", true)
g.addendaRepo = g.LoadFile(fullname+"-addenda.gir", false)
g.repo.MergeAddenda(g.addendaRepo)
girs[fullname] = g
for _, i := range g.repo.Includes {
girNew(i.Name, i.Version, girs)
}
return g
}
func (g *Gir) LoadFile(filename string, required bool) *Repository {
filepath := projectFilepath("gir-files", filename)
source, err := ioutil.ReadFile(filepath)
if err != nil {
if os.IsNotExist(err) && !required {
return &Repository{}
}
panic(err)
}
girRepo := &Repository{}
err = xml.Unmarshal(source, girRepo)
if err != nil {
panic(fmt.Errorf("Failed to parse %s : %s", filepath, err))
}
return girRepo
}