forked from nekocode/create-android-kotlin-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_creator.py
executable file
·264 lines (197 loc) · 8.35 KB
/
project_creator.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
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
#!/usr/bin/env python
# coding:utf-8
import os
import shutil
import zipfile
try:
import requests
except ImportError:
requests = None
print 'Project Creator depends on the "requests" lib.'
def download_lastest_src():
print 'Fetching the lastest version from github...'
r = requests.get('https://github.com/nekocode/kotgo/releases/latest', allow_redirects=False)
lastest_tag = r.headers['Location'].split('/')[-1]
zipfile_name = 'src-%s.zip' % lastest_tag
if os.path.exists(zipfile_name):
print 'Already downloaded [%s].' % zipfile_name
return zipfile_name, lastest_tag
print 'Downloading the lastest release [%s]...' % zipfile_name
url = 'https://github.com/nekocode/kotgo/archive/%s.zip' % lastest_tag
r = requests.get(url)
with open(zipfile_name, 'wb') as data:
data.write(r.content)
print 'Download finished.'
return zipfile_name, lastest_tag
def unzip_src_package(zipfile_name):
print 'Unziping [%s]...' % zipfile_name
zfile = zipfile.ZipFile(zipfile_name, 'r')
names = zfile.namelist()
root_dir = names[0]
if os.path.exists(root_dir):
print 'Already unzipped.'
return root_dir
for filename in names:
path = os.path.join('./', filename)
if path.endswith('/'):
if not os.path.exists(os.path.dirname(path)):
os.mkdir(os.path.dirname(path))
else:
file(path, 'wb').write(zfile.read(filename))
print 'Unzip finished.'
return root_dir
class TextProcesser:
def __init__(self, file_path):
self.file_path = file_path
self.commands = []
def rm_line_has_text(self, text):
self.commands.append(('rm_line', text))
return self
def replace_all_text(self, src, dst):
self.commands.append(('replace', src, dst))
return self
def replace_header(self, src, dst):
self.commands.append(('replace_header', src, dst))
return self
def remove_comment(self):
self.commands.append(('rm_comment', None))
return self
def recreate(self, text):
self.commands = []
self.commands.append(('recreate', text))
return self
def finish(self):
with open(self.file_path, 'r') as src_file, open(self.file_path + '.new', 'w') as new_file:
for line in src_file.readlines():
need_write = True
need_recreate = None
for cmd in self.commands:
if cmd[0] == 'rm_line' and cmd[1] in line:
need_write = False
break
elif cmd[0] == 'rm_comment' and \
(line.startswith('/**') or line.startswith(' * ') or line.startswith(' */')):
need_write = False
break
elif cmd[0] == 'recreate':
need_recreate = cmd[1]
break
elif cmd[0] == 'replace':
line = line.replace(cmd[1], cmd[2])
elif cmd[0] == 'replace_header' and (line.startswith('package') or line.startswith('import')):
line = line.replace(cmd[1], cmd[2])
if need_recreate is not None:
new_file.write(need_recreate)
break
if need_write:
new_file.write(line)
shutil.move(self.file_path + '.new', self.file_path)
class ProjectFactory:
def __init__(self, template_zip, version):
self.template_zip = template_zip
self.version = version
def create_project(self, project_name, package_name):
template_dir = unzip_src_package(self.template_zip)
if os.path.exists(project_name):
shutil.rmtree(project_name)
shutil.move(template_dir, project_name)
os.chdir(project_name)
shutil.move('sample', 'app')
print 'Creating project [%s]...' % project_name
self.process(project_name, package_name)
print 'Creat finished.'
def process(self, project_name, package_name):
# =================
# Root
# =================
# build.gradle
TextProcesser('build.gradle').rm_line_has_text('android-maven').finish()
# settings.gradle
TextProcesser('settings.gradle').recreate("include ':app', ':data'").finish()
# rm unnessary files
os.remove('README.md')
os.remove('README_CN.md')
shutil.rmtree('art')
if os.path.exists('project_creator.py'):
os.remove('project_creator.py')
shutil.rmtree('component')
# =================
# app
# =================
# build.gradle
TextProcesser('app/build.gradle') \
.replace_all_text('cn.nekocode.kotgo.sample', package_name) \
.replace_all_text('compile project(":component")',
'compile "com.github.nekocode:kotgo:%s"' % self.version) \
.finish()
# build.gradle
TextProcesser('app/proguard-rules.pro') \
.replace_all_text('cn.nekocode.kotgo.sample', package_name) \
.finish()
# AndroidManifest.xml
TextProcesser('app/src/main/AndroidManifest.xml') \
.replace_all_text('cn.nekocode.kotgo.sample', package_name) \
.finish()
# strings.xml
TextProcesser('app/src/main/res/values/strings.xml') \
.replace_all_text('Kotgo', project_name) \
.finish()
# move package
package_dir_postfix = package_name.replace('.', '/')
tmp_package_path = 'app/src/main/javaTmp/' + package_dir_postfix + '/'
old_package_path = 'app/src/main/java/cn/nekocode/kotgo/sample/'
os.makedirs(tmp_package_path)
for f in os.listdir(old_package_path):
shutil.move(old_package_path + f, tmp_package_path)
shutil.rmtree('app/src/main/java')
os.renames('app/src/main/javaTmp', 'app/src/main/java')
new_package_path = 'app/src/main/java/' + package_dir_postfix + '/'
# src files
def process_all_src(path):
for p in os.listdir(path):
if os.path.isdir(path + p):
process_all_src(path + p + '/')
elif p.endswith('.kt') or p.endswith('.java'):
TextProcesser(path + p) \
.remove_comment() \
.replace_header('cn.nekocode.kotgo.sample', package_name) \
.finish()
process_all_src(new_package_path)
# =================
# data
# =================
package_name += '.data'
# AndroidManifest.xml
TextProcesser('data/src/main/AndroidManifest.xml') \
.replace_all_text('cn.nekocode.kotgo.sample.data', package_name) \
.finish()
# move package
package_dir_postfix = package_name.replace('.', '/')
tmp_package_path = 'data/src/main/javaTmp/' + package_dir_postfix + '/'
old_package_path = 'data/src/main/java/cn/nekocode/kotgo/sample/data/'
os.makedirs(tmp_package_path)
for f in os.listdir(old_package_path):
shutil.move(old_package_path + f, tmp_package_path)
shutil.rmtree('data/src/main/java')
os.renames('data/src/main/javaTmp', 'data/src/main/java')
new_package_path = 'data/src/main/java/' + package_dir_postfix + '/'
# src files
def process_all_src(path):
for p in os.listdir(path):
if os.path.isdir(path + p):
process_all_src(path + p + '/')
elif p.endswith('.kt') or p.endswith('.java'):
TextProcesser(path + p) \
.remove_comment() \
.replace_header('cn.nekocode.kotgo.sample.data', package_name) \
.finish()
process_all_src(new_package_path)
return self
def main():
project_name = raw_input('Input new project name: ')
package_path = raw_input('Input the full package path (such as com.company.test): ')
template_zip, version = download_lastest_src()
factory = ProjectFactory(template_zip, version)
factory.create_project(project_name, package_path)
if __name__ == '__main__' and requests is not None:
main()