-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsb3.py
172 lines (140 loc) · 4.13 KB
/
jsb3.py
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
#!/usr/bin/env python
#coding=utf-8
'''
@author: stonelee
@email: [email protected]
@blog: http://stonelee.info
@description: create jsb3 file in Ext JS 4
将app下的js文件按照model, store, view, controller的顺序来创建jsb3。
如果view中使用requires加载其它view,则按照依赖顺序先行加载。
'''
import os
import parse
import sort
APP_NAME = 'CIIS'
APP_PATH = '../app/'
APPJS_PATH = '../app.js'
LICENSE_TEXT = 'Copyright(c)'
def js_loop(path):
for root, dirs, files in os.walk(path):
for filename in files:
#只解析js
if os.path.splitext(filename)[1] == '.js':
yield root,filename
def simple_loop(path):
for root,filename in js_loop(path):
i = root.index('app/')
path = root[i:]
yield '{"path": "%s/","name": "%s"},' %(path,filename)
def parse_view_name(path,appName):
'''
>> parse_view_name('../app/view/coalbusinesslicense/List.js','CIIS')
'CIIS.view.coalbusinesslicense.List'
'''
i = path.index('/view/')
s = path[i:]
s = s.replace('/','.')
s = os.path.splitext(s)[0]
s = appName+s
return s
def get_view_names(path):
views = {}
for root,filename in js_loop(path):
filepath = os.path.join(root,filename)
viewName = parse_view_name(filepath,APP_NAME)
f = open(filepath)
data = f.read()
requires = parse.parse_requires(data)
if requires:
views[viewName] = requires
else:
views[viewName] = []
f.close()
return views
def parse_jsb_view(viewName):
'''
>> parse_jsb_view('CIIS.view.coalbusinesslicense.List')
{"path": "app/view/coalbusinesslicense/","name": "List.js"}
'''
names = viewName.split('.')
i = names.index('view')
path_list = names[i:-1]
path_list.insert(0,'app')
path = '/'.join(path_list)
name = names[-1] + '.js'
return '{"path": "%s/","name": "%s"},' %(path,name)
def view_loop(path):
views = get_view_names(path)
views = sort.sort_by_require(views)
for view in views:
yield parse_jsb_view(view)
def skeleton(files,before,after):
return '''
{
"projectName": "%s",
"licenseText": "%s",
"builds": [
{
"name": "All Classes",
"target": "all-debug.js",
"options": {
"debug": true
},
"files": [%s,%s%s]
},
{
"name": "Application - Production",
"target": "all.js",
"compress": true,
"files": [
{
"path": "",
"name": "all-debug.js"
}
]
}
],
"resources": []
}
''' %(APP_NAME,LICENSE_TEXT,before,files,after)
def get_files():
model_path = APP_PATH+'model'
store_path = APP_PATH+'store'
view_path = APP_PATH+'view'
controller_path = APP_PATH+'controller'
files = ''
for path in [model_path,store_path]:
for afile in simple_loop(path):
files += afile
for afile in view_loop(view_path):
files += afile
for afile in simple_loop(controller_path):
files += afile
return files
def get_jsb(before_name,after_name):
def get_file_name(name):
return name[name.rindex('/')+1:]
before = '{"path": "","name": "%s"}' %get_file_name(before_name)
after = '{"path": "","name": "%s"}' %get_file_name(after_name)
files = get_files()
return skeleton(files,before,after)
def create_before_after(appjs,before_name,after_name):
f = open(appjs)
data = f.read()
data = parse.close_autoload(data)
before, after = parse.split_appjs(data)
f.close()
f = open(before_name,'w')
f.write(before)
f.close()
f = open(after_name,'w')
f.write(after)
f.close()
def create_jsb(filename,before_name,after_name):
create_before_after(APPJS_PATH,before_name,after_name)
jsb = get_jsb(before_name,after_name)
f = open(filename,'w')
f.write(jsb)
f.close()
if __name__ == '__main__':
pass