forked from brackeen/glfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_project.py
executable file
·192 lines (166 loc) · 5.9 KB
/
new_project.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
#!/usr/bin/python
import getpass, os, re, shutil, sys
print ""
print "Create a new GLFM project"
print ""
#
# Gets a line of input. The input is stripped of spaces.
# If the input is empty, default_value is returned
#
def get_input(prompt, default_value):
value = raw_input(prompt + " [" + default_value + "]: ")
if not value:
return default_value
else:
value = value.strip()
if len(value) == 0:
return default_value
else:
return value
def name_safe(value):
return re.match(r'^[a-zA-Z\d_]*$', value) is not None
def package_safe(value):
# From http://stackoverflow.com/questions/5205339/regular-expression-matching-fully-qualified-java-classes
return re.match(r'^([a-zA-Z_$][a-zA-Z\d_$]*\.)*[a-zA-Z_$][a-zA-Z\d_$]*$', value) is not None
#
# Read EMSCRIPTEN_ROOT from ~/.emscripten
#
dot_emscripten = os.path.expanduser("~/.emscripten")
if os.path.exists(dot_emscripten):
exec(open(dot_emscripten, 'r').read())
if 'EMSCRIPTEN_ROOT' in globals():
emsdk_path = os.path.dirname(os.path.dirname(EMSCRIPTEN_ROOT))
else:
print "Warning: Emscripten does not appear to be installed"
emsdk_path = "~/emsdk_portable"
#
# Get project variables
#
while True:
app_name = get_input("App name (without spaces)", "GLFMApp")
if name_safe(app_name):
break
else:
print "Illegal name! The app name can only contain letters, numbers, and an underscore."
while True:
package_name = get_input("App package name", "com." + getpass.getuser() + "." + app_name)
if package_safe(package_name):
break
else:
print "Illegal package name! The app name can only contain letters and numbers,"
print "and each component must start with a letter."
emsdk_path = get_input("Emscripten emsdk path", emsdk_path)
#
# Find a default output dir that doesn't already exist (so that nothing is overridden)
#
output_dir = "../" + app_name
output_dir_n = 1
while os.path.exists(output_dir):
output_dir_n += 1
output_dir = "../" + app_name + `output_dir_n`
output_dir = get_input("Project path", output_dir)
print ""
if os.path.exists(output_dir):
print "Project path '" + output_dir + "' already exists. Exiting."
exit(1)
#
# Confirm creation
#
print "Project summary:"
print " App name:", app_name
print " App package name:", package_name
print " Emscripten emsdk path:", emsdk_path
print " Project path:", output_dir
confirm = get_input("Create (y/n)?", "Y")
if confirm != "Y":
print ""
print "Project creation canceled"
exit(1)
####################################################################################################
ignored_files = (".DS_Store", "Thumbs.db", "Desktop.ini")
ignored_paths = (
"example/platform/android/build",
"example/platform/android/.gradle",
"example/platform/android/.idea",
"example/platform/emscripten/bin",
"example/platform/ios/GLFMExample.xcodeproj/project.xcworkspace",
"example/platform/ios/GLFMExample.xcodeproj/xcuserdata",
)
def do_name_replace(s):
s = s.replace("GLFMExample", app_name)
return s
def do_replace(s):
s = s.replace("GLFMExample", app_name)
s = s.replace("com.brackeen.glfmexample", package_name)
s = s.replace("com.brackeen.${PRODUCT_NAME:rfc1034identifier}", package_name)
return s
def copy_android_buildfile(src_file, dst_file):
with open(dst_file, "wt") as fout:
with open(src_file, "rt") as fin:
for line in fin:
line = line.replace("../../../include", "../../glfm/include");
line = line.replace("../../../src", "../../glfm/src");
fout.write(do_replace(line))
def copy_emscripten_makefile(src_file, dst_file):
with open(dst_file, "wt") as fout:
with open(src_file, "rt") as fin:
for line in fin:
if line.startswith("GLFM_ROOT :="):
fout.write("GLFM_ROOT := ../../glfm\n")
elif line.startswith("APP_ROOT :="):
fout.write("APP_ROOT := ../..\n")
else:
fout.write(do_replace(line))
def copy_ios_project_file(src_file, dst_file):
with open(dst_file, "wt") as fout:
with open(src_file, "rt") as fin:
for line in fin:
line = line.replace("path = ../../..;", "path = ../../glfm;")
fout.write(do_replace(line))
def copy_generic_project_file(src_file, dst_file):
with open(dst_file, "wt") as fout:
with open(src_file, "rt") as fin:
for line in fin:
fout.write(do_replace(line))
def copy_template(src_dir, dst_dir):
if src_dir in ignored_paths:
return
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
for name in os.listdir(src_dir):
if name in ignored_files:
continue
src = os.path.join(src_dir, name)
dst = os.path.join(dst_dir, do_name_replace(name))
if os.path.isfile(src):
if name == "Makefile":
copy_emscripten_makefile(src, dst)
elif name == "build.gradle":
copy_android_buildfile(src, dst)
elif name == "project.pbxproj":
copy_ios_project_file(src, dst)
elif (name == "AndroidManifest.xml" or name == "strings.xml" or name.endswith(".plist")):
copy_generic_project_file(src, dst)
else:
shutil.copy2(src, dst)
elif os.path.isdir(src):
copy_template(src, dst)
os.makedirs(output_dir)
# Copy GLFM
shutil.copytree("include", output_dir + "/glfm/include")
shutil.copytree("src", output_dir + "/glfm/src")
# Copy example
shutil.copytree("example/src", output_dir + "/src")
shutil.copytree("example/assets", output_dir + "/assets")
# Copy project files
copy_template("example/platform/android", output_dir + "/platform/android");
copy_template("example/platform/emscripten", output_dir + "/platform/emscripten");
# iOS: package names require dash instead of underscore
package_name = package_name.replace("_", "-")
copy_template("example/platform/ios", output_dir + "/platform/ios");
# Special case: create a Makefile.local for emscripten
with open(output_dir + "/platform/emscripten/Makefile.local", "wt") as fout:
fout.write("EMSCRIPTEN_PATH = " + emsdk_path)
# Woop!
print ""
print "Done."