-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
402 lines (341 loc) · 9.53 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package main
import (
"bytes"
"encoding/xml"
"fmt"
"github.com/alexflint/go-arg"
"io/ioutil"
"log"
"os"
"path"
"strings"
"text/template"
"unicode"
)
//todo: maybe could use <include /> the way that if it has id, then look at that layout and instead of inline
//find it's ids ,just use the name of layout to it class in the X class (must not be merge layout)
//maybe this is overkill and better to use compound view in this case
type argsConf struct {
PACKAGE string `arg:"positional,-p,help:app package name (ex: com.example.hello)"`
APP_DIR string `arg:"-a,help:android application project app path (default: current directory)"`
XML_DIR string `arg:"-x,help:app xml layout directory (default example 'src/main/res/layout')"`
OUT_DIR string `arg:"-o,help:directory to flush generated output (default example 'src/main/java/com/example/hello')"`
OUT_CLASS_NAME string `arg:"-n,help:class name of generated output (default X)"`
}
type FieldView struct {
ViewClass string
Id string `xml:"id,attr"`
Layout string `xml:"layout,attr"`
XMLName xml.Name
Content []byte `xml:",innerxml"`
Childes []*FieldView `xml:",any"`
ShouldSet bool
}
type CellView struct {
FileName string
ClassName string
RootField FieldView
Fields []*FieldView
IsMergeLayout bool
RootClass string
}
type GenFile struct {
Cells []*CellView
Imports *Imports
PackageName string
ClassName string
OutClass string
}
type Imports struct {
AndroidViews map[string]bool
OtherViews map[string]bool
}
var genFile *GenFile
//there are many of Android views in android.view and android.widget for now just mass import them ("*")
//see: https://developer.android.com/reference/android/view/package-summary.html
//https://developer.android.com/reference/android/widget/package-summary.html
var notWidgets = map[string]bool{
"include": true,
"merge": true,
"view": true,
"ViewGroup": true,
"fragment": true,
"requestFocus": true,
}
var args *argsConf
func main() {
parseArgs()
genFile = &GenFile{
Imports: &Imports{},
ClassName: args.OUT_CLASS_NAME,
PackageName: args.PACKAGE,
}
xmls, err := ioutil.ReadDir(args.XML_DIR)
if err != nil {
panic(err)
}
for _, xml := range xmls {
transformNewXmlFile(xml)
}
genFile.Gen()
}
func parseArgs() {
args = &argsConf{}
arg.MustParse(args)
if args.PACKAGE == "" {
log.Fatal("PACKAGE could not be empty. The first command line paramter is package name")
}
if args.APP_DIR == "" {
wd, err := os.Getwd()
noErr(err)
args.APP_DIR = wd
}
if args.XML_DIR == "" {
args.XML_DIR = path.Join(args.APP_DIR, "/src/main/res/layout/")
}
if args.OUT_DIR == "" {
args.OUT_DIR = path.Join(args.APP_DIR, "/src/main/java", strings.Replace(args.PACKAGE, ".", "/", -1))
}
if args.OUT_CLASS_NAME == "" {
args.OUT_CLASS_NAME = "X"
}
}
func transformNewXmlFile(xmlF os.FileInfo) {
data, err := ioutil.ReadFile(path.Join(args.XML_DIR, xmlF.Name()))
noErr(err)
buf := bytes.NewBuffer(data)
dec := xml.NewDecoder(buf)
var root FieldView
err = dec.Decode(&root)
noErr(err)
fileName := strings.Replace(xmlF.Name(), ".xml", "", -1)
cell := &CellView{
FileName: fileName,
ClassName: ToCamel(fileName),
RootField: root,
}
root.walkDown(cell, false)
if len(cell.Fields) > 0 { //againest <merge/> tag
if cell.IsMergeLayout {
cell.RootClass = "ViewGroup"
} else {
cell.RootClass = cell.Fields[0].ViewClass
}
rootCls := cell.Fields[0].ViewClass
if len(rootCls) > 0 { //not <merge /> xmls
genFile.Cells = append(genFile.Cells, cell)
}
}
}
func (field *FieldView) walkDown(cell *CellView, isIncludeCall bool) {
field.extractClassName()
if unicode.IsLower(rune(field.ViewClass[0])) { //not <include /> tag
//just we support include and merge tag of no View tags
if field.XMLName.Local != "include" && field.XMLName.Local != "merge" {
return
}
if field.XMLName.Local == "include" {
addIncludeTag(field, cell)
}
}
if field.XMLName.Local == "merge" && isIncludeCall == false {
cell.IsMergeLayout = true
}
field.addFieldViewToCellView(cell)
for _, child := range field.Childes {
child.walkDown(cell, isIncludeCall)
}
}
func (field *FieldView) addFieldViewToCellView(cell *CellView) {
if field.XMLName.Local == "include" || field.XMLName.Local == "merge" {
return
}
cell.Fields = append(cell.Fields, field)
genFile.Imports.Add(field.XMLName.Local)
if len(field.Id) > 0 {
arr := strings.Split(field.Id, "/")
field.Id = arr[1]
field.ShouldSet = true
}
return
}
//todo extract xml reader
func addIncludeTag(include *FieldView, cell *CellView) {
layout := stripRef(include.Layout)
if layout == "" {
return
}
data, err := ioutil.ReadFile(path.Join(args.XML_DIR, layout+".xml"))
noErr(err)
buf := bytes.NewBuffer(data)
dec := xml.NewDecoder(buf)
var root FieldView
err = dec.Decode(&root)
noErr(err)
root.walkDown(cell, true)
}
//get Class name - ex: com.example.com.Avatar -> Avatar
func (field *FieldView) extractClassName() {
clsDotIndex := strings.LastIndex(field.XMLName.Local, ".")
if clsDotIndex == -1 {
field.ViewClass = field.XMLName.Local
} else {
field.ViewClass = field.XMLName.Local[clsDotIndex+1:]
}
}
func (i *Imports) Add(cls string) {
index := strings.Index(cls, ".")
if index > 0 {
if i.OtherViews == nil {
i.OtherViews = make(map[string]bool)
}
i.OtherViews[cls] = true
} else {
if i.AndroidViews == nil {
i.AndroidViews = make(map[string]bool)
}
if _, ok := notWidgets[cls]; !ok {
i.AndroidViews[cls] = true
}
}
}
func (g *GenFile) Gen() {
for _, cell := range g.Cells {
out := bytes.NewBufferString("")
tmpl, err := template.New("t").Parse(TMPL_CELL)
if err != nil {
panic(err)
}
err = tmpl.Execute(out, cell)
noErr(err)
g.OutClass += out.String()
}
outFileBody := bytes.NewBufferString("")
tmpl2, err := template.New("t2").Parse(TMPL_FILE)
noErr(err)
err = tmpl2.Execute(outFileBody, g)
noErr(err)
outJava := path.Join(args.OUT_DIR, args.OUT_CLASS_NAME+".java")
//an optimization for taking advantage of faster build time
oldWrite, err := ioutil.ReadFile(outJava)
if err != nil || !bytes.Equal(outFileBody.Bytes(), oldWrite) {
ioutil.WriteFile(outJava, outFileBody.Bytes(), 0666)
}
}
//ex: @layout/titlebar => titlebar or @+id/my_text => my_text
func stripRef(ref string) string {
arr := strings.Split(ref, "/")
return arr[1]
}
func ToCamel(s string) string {
s = strings.Trim(s, " ")
s = strings.Replace(s, "__", "+", -1)
strOut := ""
capNext := true
for _, char := range s {
switch {
case (char >= 'A' && char <= 'Z'):
strOut += string(char)
capNext = false
case char >= 'a' && char <= 'z':
if capNext {
strOut += strings.ToUpper(string(char))
} else {
strOut += string(char)
}
capNext = false
case char == '_' || char == ' ':
capNext = true
case char == '+':
strOut += string(char)
capNext = true
case char >= '0' && char <= '9':
strOut += string(char) //for numbers,...
capNext = false
default:
capNext = false
}
}
strOut = strings.Replace(strOut, "+", "_", -1)
return strOut
}
func noErr(err error) {
if err != nil {
panic(err)
}
}
//old
func (field *FieldView) String() string {
return fmt.Sprintf("%v %v %v \n", field.XMLName.Local, field.Id, field.ViewClass)
}
func (cell *CellView) String() string {
s := fmt.Sprintf("%d \n", len(cell.Fields))
for _, f := range cell.Fields {
s += f.String()
}
return s
}
const TMPL_CELL = `
{{- $rootClass := .RootClass }}
public static class {{ .ClassName }} {
public {{ $rootClass }} root;
{{- range .Fields -}}
{{- if .ShouldSet}}
public {{ .ViewClass }} {{ .Id -}};
{{- end}}
{{- end}}
public {{ .ClassName }}(Context context,ViewGroup parent) {
this(context,parent, R.layout.{{ .FileName }} );
}
public {{ .ClassName }}(Context context,ViewGroup parent, int layout) {
{{- if eq .IsMergeLayout true}}
root = ({{ $rootClass }}) LayoutInflater.from(context).inflate(layout,parent,true);//for Compound Views
{{ else }}
root = ({{ $rootClass }}) LayoutInflater.from(context).inflate(layout,parent ,false);
{{- end}}
{{- range .Fields -}}
{{- if .ShouldSet}}
{{ .Id }} = ( {{- .ViewClass -}} ) root.findViewById( R.id. {{- .Id -}} );
{{- end}}
{{- end}}
}
public {{ .ClassName }}(Context context) {
this(context ,null);
}
public {{ .ClassName }}(ViewGroup parent) {
this(parent.getContext() ,parent);
}
public static class IDS {
public static int LAYOUT = R.layout.{{ .FileName }};
{{- range .Fields -}}
{{- if .ShouldSet}}
public static int {{.Id}} = R.id.{{- .Id -}};
{{- end}}
{{- end}}
}
}
`
const TMPL_FILE =
`//DON'T EDIT, THIS FILE IS AUTO GENERATED AND ALWAYS WILL BE SYNCED WITH 'layout/*.xml' FILES
//TO SYNC JUST REBUILD THE APP
//SEE THIS TOOL AT: https://github.com/jozn/xml2java
package {{.PackageName}};
import android.widget.*;
import android.view.*;
import android.webkit.WebView;
import android.view.LayoutInflater;
import android.content.Context;
{{- range $key, $val := .Imports.OtherViews }}
import {{ $key }};
{{- end }}
import {{.PackageName}}.R;
public class {{.ClassName}} {
{{.OutClass}}
}
/////////////// manual imports /////////////////
/*
{{- range $key, $val := .Imports.AndroidViews }}
import android.widget.{{ $key }};
{{- end }}
*/
`