-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplateGenerator.py
200 lines (161 loc) · 7.83 KB
/
TemplateGenerator.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
#!/usr/bin/env python3
import os
import shutil
from distutils.dir_util import copy_tree
import re
class TemplateGenerator:
default_package = "id.example.mvp"
template_pkg = "id.example.mvp"
template_pkg_dir = template_pkg.replace(".", "/")
def __init__(self, pkg_id, app_name, template_dir_name):
self.app_name = app_name
self.package_id = pkg_id
self.output_dir = "{}/{}".format("output", app_name)
self.template_dir_name = template_dir_name
def do_generate(self):
package_path = self.package_id.replace(".", "/")
my_dir = os.getcwd()
self.make_dir(self.package_id)
os.chdir(my_dir)
self.copy_template_files(self.package_id)
self.copy_template_project(package_path)
self.copy_template_res()
os.chdir(my_dir)
print("change package name to {}".format(self.package_id))
self.replace_package_id(self.default_package, self.package_id)
self.replace_package_build_gradle(self.default_package, self.package_id)
def replace_package_build_gradle(self, default_pkg, new_pkg):
file_input = "{}/{}".format(self.output_dir, "app/build.gradle")
filedata = None
print("replace package id")
x = "change {} to {}".format(default_pkg, new_pkg)
print(x)
with open(file_input, 'r') as file:
filedata = file.read()
filedata = filedata.replace(self.default_package, self.package_id)
with open(file_input, 'w') as file:
file.write(filedata)
def replace_package_id(self, def_pkg, new_pkg):
path = "{}/{}".format(os.getcwd(), self.output_dir)
print("path: {}".format(path))
patterns = [".kt", ".xml"]
matching_file_list = [os.path.join(dp, f)
for dp, dn, filenames in os.walk(path)
for f in filenames
if os.path.splitext(f)[1] in patterns]
print('Files found matching patterns: ' + str(len(matching_file_list)))
fileCount = 0
filesReplaced = 0
for currentFile in matching_file_list:
fileCount += 1
fileReplaced = self.replace_package_in_file(currentFile, def_pkg, new_pkg)
if fileReplaced:
filesReplaced += 1
print("Total Files Searched : " + str(fileCount))
print("Total Files Replaced/Updated : " + str(filesReplaced))
def replace_package_in_file(self, file_name, def_pkg, new_pkg):
# print("change package id in file {}".format(file_name))
file_updated = False
with open(file_name, 'r') as f:
new_lines = []
for line in f.readlines():
if def_pkg in line:
file_updated = True
line = line.replace(def_pkg, new_pkg)
new_lines.append(line)
if file_updated:
print("String found and update file {}".format(file_name))
try:
with open(file_name, 'w') as f:
for line in new_lines:
f.write(line)
except:
print("ERROR! Can't access file {}".format(file_name))
return file_updated
def make_dir(self, pkg_id):
path = pkg_id.replace(".", "/")
app_dir = "app/src"
android_test_dir = "{}/{}".format("androidTest/java", path)
main_dir = "{}/{}".format("main/java", path)
test_dir = "{}/{}".format("test/java", path)
res_dir = "main/res"
print("path: {}".format(path))
if not os.path.exists(self.output_dir):
os.mkdir(self.output_dir)
else:
print("path {} already exists".format(self.output_dir))
exit()
# gradle directory
if not os.path.exists("gradle/wrapper"):
os.chdir(self.output_dir)
os.makedirs("gradle/wrapper")
print(os.getcwd())
app = "{}/{}".format(self.output_dir, app_dir)
if not os.path.exists(app):
os.makedirs(app_dir)
if not os.path.exists(main_dir):
os.chdir(app_dir)
print(os.getcwd())
os.makedirs(main_dir)
os.makedirs(res_dir)
os.makedirs(android_test_dir)
os.makedirs(test_dir)
else:
print("{} already created".format(main_dir))
def copy_template_files(self, pkgId):
# copy gradle stuff
shutil.copy("template/{}/build.gradle".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "build.gradle"))
shutil.copy("template/{}/gradle.properties".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "gradle.properties"))
shutil.copy("template/{}/gradlew".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "gradlew"))
shutil.copy("template/{}/gradlew.bat".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "gradlew.bat"))
shutil.copy("template/{}/settings.gradle".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "settings.gradle"))
# copy wrapper
shutil.copy("template/{}/gradle/wrapper/gradle-wrapper.jar".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "gradle/wrapper/gradle-wrapper.jar"))
shutil.copy("template/{}/gradle/wrapper/gradle-wrapper.properties".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "gradle/wrapper/gradle-wrapper.properties"))
# copy gradle app
shutil.copy("template/{}/app/build.gradle".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "app/build.gradle"))
shutil.copy("template/{}/app/dependencies.gradle".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "app/dependencies.gradle"))
shutil.copy("template/{}/app/proguard-rules.pro".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "app/proguard-rules.pro"))
shutil.copy("template/{}/app/src/main/AndroidManifest.xml".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "app/src/main/AndroidManifest.xml"))
def copy_template_project(self, pkg_path):
copy_tree(
"template/{}/app/src/androidTest/java/{}".format(self.template_dir_name, self.template_pkg_dir),
"{}/{}/{}".format(self.output_dir, "app/src/androidTest/java", pkg_path)
)
copy_tree(
"template/{}/app/src/main/java/{}".format(self.template_dir_name, self.template_pkg_dir),
"{}/{}/{}".format(self.output_dir, "app/src/main/java", pkg_path)
)
copy_tree(
"template/{}/app/src/test/java/{}".format(self.template_dir_name, self.template_pkg_dir),
"{}/{}/{}".format(self.output_dir, "app/src/test/java", pkg_path)
)
def copy_template_res(self):
copy_tree(
"template/{}/app/src/main/res".format(self.template_dir_name),
"{}/{}".format(self.output_dir, "app/src/main/res")
)
if __name__ == "__main__":
my_app_name = input("Nama Aplikasi (default MyApp)? ")
my_app_name = my_app_name.replace(" ", "")
if my_app_name == "":
my_app_name = "MyApp"
my_pkg_id = "1.1"
while not re.match("^([a-z_]{1}[a-z0-9_]*(\\.[a-z_]{1}[a-z0-9_]*)*)$", my_pkg_id):
my_pkg_id = input("ID nama paket (contoh: com.example)? ")
print("Format paket salah") #TODO: masih dipanggil meski format sudah benar
template_dir_name = input("direktori nama template (di direktori template/namanya): ")
print("nama: {}, id: {}".format(my_app_name, my_pkg_id))
generator = TemplateGenerator(pkg_id=my_pkg_id, app_name=my_app_name, template_dir_name=template_dir_name)
generator.do_generate()