This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.go
127 lines (105 loc) · 3.33 KB
/
generator.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
package main
import (
"encoding/xml"
"io/ioutil"
"path/filepath"
)
type Generator struct {
outputDir string
PackageName string `xml:"package-name"`
GirPath string `xml:"gir-path"`
ExternalPackages []*ExternalPackage `xml:"external-package"`
Includes []string `xml:"include"`
PkgConfigs []string `xml:"pkg-config"`
FunctionIgnorePatterns []string `xml:"function-ignore-patterns>entry"`
FunctionRenames []*Rename `xml:"function-rename>rename"`
ConstantIgnorePatterns []string `xml:"constant-ignore-patterns>entry"`
TypesIgnorePatterns []string `xml:"type-ignore-patterns>entry"`
ParamDirectionEntries []*Entry `xml:"param-direction>entry"`
ParamDirections map[string]string
TypeMappingEntries []*Entry `xml:"type-mapping>entry"`
InParamMarshalEntries []*Entry `xml:"in-param-marshal>entry"`
OutParamMarshalEntries []*Entry `xml:"out-param-marshal>entry"`
InParamMarshals map[string]*Entry
OutParamMarshals map[string]*Entry
SignalParamTypeMappings map[string]string
}
type ExternalPackage struct {
Name string `xml:"name,attr"`
GirPath string `xml:"gir-path"`
Import string `xml:"import"`
Repo *Repository
}
type Rename struct {
From string `xml:"from,attr"`
To string `xml:",chardata"`
}
type Entry struct {
Text string `xml:",chardata"`
Spec string `xml:"spec,attr"`
Param string `xml:"param"`
Before string `xml:"before"`
After string `xml:"after"`
}
func Gen(outputDir string) {
// read build file
buildFileContent, err := ioutil.ReadFile(filepath.Join(outputDir, "build.xml"))
checkError(err)
generator := &Generator{
ParamDirections: make(map[string]string),
InParamMarshals: make(map[string]*Entry),
OutParamMarshals: make(map[string]*Entry),
SignalParamTypeMappings: make(map[string]string),
}
err = xml.Unmarshal(buildFileContent, generator)
checkError(err)
generator.outputDir = outputDir
// prepares
for _, entry := range generator.ParamDirectionEntries {
generator.ParamDirections[entry.Spec] = entry.Text
}
for _, entry := range generator.TypeMappingEntries {
TypeMapping[entry.Spec] = entry.Text
}
for _, entry := range generator.InParamMarshalEntries {
generator.InParamMarshals[entry.Spec] = entry
}
for _, entry := range generator.OutParamMarshalEntries {
generator.OutParamMarshals[entry.Spec] = entry
}
// parse gir
contents, err := ioutil.ReadFile(generator.GirPath)
checkError(err)
repo := generator.Parse(contents)
// parse external gir
for _, ext := range generator.ExternalPackages {
contents, err := ioutil.ReadFile(ext.GirPath)
checkError(err)
ext.Repo = generator.Parse(contents)
}
// add includes
generator.Includes = append(generator.Includes, "stdlib.h")
// namespace
ns := repo.Namespace
p("=> %s\n", ns.Name)
// struct types
generator.GenStructTypes(ns)
// class types
generator.GenClassTypes(ns)
// functions
generator.GenFunctions(ns)
// constants
generator.GenConstants(ns)
// flags
generator.GenFlags(ns)
// enums
generator.GenEnums(ns)
// structs
generator.GenStructs(ns)
// class traits
generator.GenTraits(ns)
// class constructors
generator.GenClassConstructors(ns)
// signals
// generator.GenSignals(ns)
}