forked from chukong/CocosVR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createNewProject.py
executable file
·205 lines (167 loc) · 6.42 KB
/
createNewProject.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
#!/usr/bin/python
import sys
import json
import os, os.path
import shutil
from optparse import OptionParser
def os_is_win32():
return sys.platform == 'win32'
def add_path_prefix(path_str):
if not os_is_win32():
return path_str
if path_str.startswith("\\\\?\\"):
return path_str
ret = "\\\\?\\" + os.path.abspath(path_str)
ret = ret.replace("/", "\\")
return ret
def replace_string(filepath, src_string, dst_string):
""" From file's content replace specified string
Arg:
filepath: Specify a file contains the path
src_string: old string
dst_string: new string
"""
if src_string is None or dst_string is None:
raise TypeError
content = ""
f1 = open(filepath, "rb")
for line in f1:
strline = line.decode('utf8')
if src_string in strline:
content += strline.replace(src_string, dst_string)
else:
content += strline
f1.close()
f2 = open(filepath, "wb")
f2.write(content.encode('utf8'))
f2.close()
# end of replace_string
def project_rename(dir, name, v):
dst_project_dir = dir
dst_project_name = name
src_project_name = v['src_project_name']
if dst_project_name == src_project_name:
return
files = v['files']
for f in files:
src = f.replace("PROJECT_NAME", src_project_name)
dst = f.replace("PROJECT_NAME", dst_project_name)
src_file_path = os.path.join(dst_project_dir, src)
dst_file_path = os.path.join(dst_project_dir, dst)
if os.path.exists(src_file_path):
if dst_project_name.lower() == src_project_name.lower():
temp_file_path = "%s-temp" % src_file_path
os.rename(src_file_path, temp_file_path)
os.rename(temp_file_path, dst_file_path)
else:
if os.path.exists(dst_file_path):
os.remove(dst_file_path)
os.rename(src_file_path, dst_file_path)
def project_replace_project_name(dir, name, v):
dst_project_dir = dir
dst_project_name = name
src_project_name = v['src_project_name']
if dst_project_name == src_project_name:
return
files = v['files']
for f in files:
dst = f.replace("PROJECT_NAME", dst_project_name)
if os.path.exists(os.path.join(dst_project_dir, dst)):
replace_string(
os.path.join(dst_project_dir, dst), src_project_name, dst_project_name)
def project_replace_package_name(dir, name, v):
dst_project_dir = dir
dst_project_name = name
src_package_name = v['src_package_name']
dst_package_name = 'org.cocos2dx.' + name
if dst_package_name == src_package_name:
return
files = v['files']
for f in files:
dst = f.replace("PROJECT_NAME", dst_project_name)
if os.path.exists(os.path.join(dst_project_dir, dst)):
replace_string(
os.path.join(dst_project_dir, dst), src_package_name, dst_package_name)
def project_replace_mac_bundleid(dir, name, v):
dst_project_dir = dir
dst_project_name = name
src_bundleid = v['src_bundle_id']
dst_bundleid = 'org.cocos2dx.' + name
if src_bundleid == dst_bundleid:
return
files = v['files']
for f in files:
dst = f.replace("PROJECT_NAME", dst_project_name)
if os.path.exists(os.path.join(dst_project_dir, dst)):
replace_string(
os.path.join(dst_project_dir, dst), src_bundleid, dst_bundleid)
def project_replace_ios_bundleid(dir, name, v):
dst_project_dir = dir
dst_project_name = name
src_bundleid = v['src_bundle_id']
dst_bundleid = 'org.cocos2dx.' + name
if src_bundleid == dst_bundleid:
return
files = v['files']
for f in files:
dst = f.replace("PROJECT_NAME", dst_project_name)
if os.path.exists(os.path.join(dst_project_dir, dst)):
replace_string(
os.path.join(dst_project_dir, dst), src_bundleid, dst_bundleid)
def append_x_engine(src_dir, dst_dir, v):
src = os.path.join(src_dir, v['from'])
dst = os.path.join(dst_dir, v['to'])
cocosx_files_json = os.path.join(src, 'templates', 'cocos2dx_files.json')
f = open(cocosx_files_json)
data = json.load(f)
f.close()
fileList = data['common']
# begin copy engine
for index in range(len(fileList)):
srcfile = os.path.join(src, fileList[index])
dstfile = os.path.join(dst, fileList[index])
srcfile = add_path_prefix(srcfile)
dstfile = add_path_prefix(dstfile)
if not os.path.exists(os.path.dirname(dstfile)):
os.makedirs(add_path_prefix(os.path.dirname(dstfile)))
# copy file or folder
if os.path.exists(srcfile):
if os.path.isdir(srcfile):
if os.path.exists(dstfile):
shutil.rmtree(dstfile)
shutil.copytree(srcfile, dstfile)
else:
if os.path.exists(dstfile):
os.remove(dstfile)
shutil.copy2(srcfile, dstfile)
# -------------- main --------------
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-n", "--name", dest="proj_name", help='the name of project')
parser.add_option("-p", "--path", dest="proj_path", help='the path of project')
(opts, args) = parser.parse_args()
if opts.proj_name is None:
name = 'HelloVR'
else:
name = opts.proj_name
if opts.proj_path is None:
path = './'
else:
path = opts.proj_path
new_path = os.path.join(path, name)
shutil.copytree("./templates", new_path)
vr_path = os.path.join(new_path, 'cocosvr')
shutil.copytree("./cocosvr", vr_path)
ext_path = os.path.join(new_path, 'external')
shutil.copytree("./external", ext_path)
f = open('cocos-project-template.json')
data = json.load(f)
f.close()
do_default = data['do_default']
#cocos_path = os.path.join(new_path, 'cocos2d')
append_x_engine('', new_path, do_default['append_x_engine'])
project_rename(new_path, name, do_default['project_rename'])
project_replace_project_name(new_path, name, do_default['project_replace_project_name'])
project_replace_package_name(new_path, name, do_default['project_replace_package_name'])
project_replace_mac_bundleid(new_path, name, do_default['project_replace_mac_bundleid'])
project_replace_ios_bundleid(new_path, name, do_default['project_replace_ios_bundleid'])