2013-02-18 22:09:42 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
# create_project.py
|
|
|
|
# Create cross-platform cocos2d-x project
|
|
|
|
# Copyright (c) 2012 cocos2d-x.org
|
|
|
|
# Author: WangZhe
|
|
|
|
|
|
|
|
# define global variables
|
2013-07-23 13:37:17 +08:00
|
|
|
PLATFORMS = {
|
2013-07-24 17:27:29 +08:00
|
|
|
"cpp" : ["ios", "android", "win32", "mac", "linux"],
|
|
|
|
"lua" : ["ios", "android", "win32", "linux"],
|
2013-07-23 13:37:17 +08:00
|
|
|
"javascript" : ["ios", "android", "win32"]
|
|
|
|
}
|
|
|
|
|
2013-02-18 22:09:42 +08:00
|
|
|
|
|
|
|
# begin
|
|
|
|
import sys
|
|
|
|
import os, os.path
|
2013-02-22 16:32:12 +08:00
|
|
|
import json
|
|
|
|
import shutil
|
2013-02-18 22:09:42 +08:00
|
|
|
|
2013-07-23 13:37:17 +08:00
|
|
|
def checkParams():
|
2013-06-28 10:12:55 +08:00
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
# set the parser to parse input params
|
|
|
|
# the correspond variable name of "-x, --xxx" is parser.xxx
|
2013-07-01 14:10:14 +08:00
|
|
|
parser = OptionParser(usage="Usage: ./%prog -p <PROJECT_NAME> -k <PACKAGE_NAME> -l <cpp|lua|javascript>\nSample: ./%prog -p MyGame -k com.MyCompany.AwesomeGame -l javascript")
|
2013-06-28 10:12:55 +08:00
|
|
|
parser.add_option("-p", "--project", metavar="PROJECT_NAME", help="Set a project name")
|
|
|
|
parser.add_option("-k", "--package", metavar="PACKAGE_NAME", help="Set a package name for project")
|
|
|
|
parser.add_option("-l", "--language",
|
|
|
|
metavar="PROGRAMMING_NAME",
|
|
|
|
type="choice",
|
|
|
|
choices=["cpp", "lua", "javascript"],
|
2013-07-23 13:37:17 +08:00
|
|
|
help="Major programing language you want to used, should be [cpp | lua | javascript]")
|
2013-06-28 10:12:55 +08:00
|
|
|
|
|
|
|
#parse the params
|
|
|
|
(opts, args) = parser.parse_args()
|
|
|
|
|
2013-02-18 22:09:42 +08:00
|
|
|
# generate our internal params
|
2013-07-23 13:37:17 +08:00
|
|
|
context = {}.fromkeys(("language", "src_project_name", "src_package_name", "dst_project_name", "dst_package_name", "src_project_path", "dst_project_path", "script_dir"))
|
|
|
|
platforms_list = []
|
|
|
|
|
|
|
|
context["script_dir"] = os.path.abspath(os.path.dirname(__file__))
|
2013-02-18 22:09:42 +08:00
|
|
|
|
2013-06-28 10:12:55 +08:00
|
|
|
if opts.project:
|
|
|
|
context["dst_project_name"] = opts.project
|
2013-07-23 13:37:17 +08:00
|
|
|
context["dst_project_path"] = os.path.abspath(os.path.join(context["script_dir"], "..", "..", "projects", context["dst_project_name"]))
|
2013-06-28 10:12:55 +08:00
|
|
|
else:
|
|
|
|
parser.error("-p or --project is not specified")
|
|
|
|
|
|
|
|
if opts.package:
|
|
|
|
context["dst_package_name"] = opts.package
|
|
|
|
else:
|
|
|
|
parser.error("-k or --package is not specified")
|
|
|
|
|
|
|
|
if opts.language:
|
|
|
|
context["language"] = opts.language
|
|
|
|
else:
|
2013-07-01 01:04:01 +08:00
|
|
|
parser.error("-l or --language is not specified")
|
2013-02-21 16:51:22 +08:00
|
|
|
|
2013-02-18 22:09:42 +08:00
|
|
|
# fill in src_project_name and src_package_name according to "language"
|
2013-07-23 13:37:17 +08:00
|
|
|
template_dir = os.path.abspath(os.path.join(context["script_dir"], "..", "..", "template"))
|
2013-02-18 22:09:42 +08:00
|
|
|
if ("cpp" == context["language"]):
|
|
|
|
context["src_project_name"] = "HelloCpp"
|
2013-02-20 23:15:48 +08:00
|
|
|
context["src_package_name"] = "org.cocos2dx.hellocpp"
|
2013-07-23 13:37:17 +08:00
|
|
|
context["src_project_path"] = os.path.join(template_dir, "multi-platform-cpp")
|
2013-02-18 22:09:42 +08:00
|
|
|
elif ("lua" == context["language"]):
|
|
|
|
context["src_project_name"] = "HelloLua"
|
2013-02-20 23:15:48 +08:00
|
|
|
context["src_package_name"] = "org.cocos2dx.hellolua"
|
2013-07-23 13:37:17 +08:00
|
|
|
context["src_project_path"] = os.path.join(template_dir, "multi-platform-lua")
|
2013-02-18 22:09:42 +08:00
|
|
|
elif ("javascript" == context["language"]):
|
|
|
|
context["src_project_name"] = "HelloJavascript"
|
2013-02-20 23:15:48 +08:00
|
|
|
context["src_package_name"] = "org.cocos2dx.hellojavascript"
|
2013-07-23 13:37:17 +08:00
|
|
|
context["src_project_path"] = os.path.join(template_dir, "multi-platform-js")
|
|
|
|
platforms_list = PLATFORMS.get(context["language"], [])
|
|
|
|
return context, platforms_list
|
2013-02-20 23:15:48 +08:00
|
|
|
# end of checkParams(context) function
|
2013-02-18 22:09:42 +08:00
|
|
|
|
2013-02-22 16:32:12 +08:00
|
|
|
def replaceString(filepath, src_string, dst_string):
|
2013-07-26 10:50:37 +08:00
|
|
|
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()
|
2013-02-22 16:32:12 +08:00
|
|
|
# end of replaceString
|
|
|
|
|
2013-07-23 13:37:17 +08:00
|
|
|
def processPlatformProjects(context, platform):
|
2013-02-22 16:32:12 +08:00
|
|
|
# determine proj_path
|
2013-07-23 13:37:17 +08:00
|
|
|
proj_path = os.path.join(context["dst_project_path"], "proj." + platform)
|
2013-02-22 16:32:12 +08:00
|
|
|
java_package_path = ""
|
|
|
|
|
2013-07-23 13:37:17 +08:00
|
|
|
# read json config file or the current platform
|
2013-02-22 16:32:12 +08:00
|
|
|
f = open("%s.json" % platform)
|
|
|
|
data = json.load(f)
|
|
|
|
|
|
|
|
# rename package path, like "org.cocos2dx.hello" to "com.company.game". This is a special process for android
|
2013-07-23 13:37:17 +08:00
|
|
|
if platform == "android":
|
2013-02-22 16:32:12 +08:00
|
|
|
src_pkg = context["src_package_name"].split('.')
|
|
|
|
dst_pkg = context["dst_package_name"].split('.')
|
2013-07-23 13:37:17 +08:00
|
|
|
|
|
|
|
src_dir = os.path.join(proj_path, "src", *src_pkg)
|
|
|
|
dst_dir = os.path.join(proj_path, "src", *dst_pkg)
|
|
|
|
os.renames(src_dir, dst_dir)
|
|
|
|
|
|
|
|
java_package_path = os.path.join(*dst_pkg)
|
2013-02-22 16:32:12 +08:00
|
|
|
|
|
|
|
# rename files and folders
|
2013-07-23 13:37:17 +08:00
|
|
|
for item in data["rename"]:
|
|
|
|
tmp = item.replace("PACKAGE_PATH", java_package_path)
|
2013-02-22 16:32:12 +08:00
|
|
|
src = tmp.replace("PROJECT_NAME", context["src_project_name"])
|
|
|
|
dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
|
2013-07-23 13:37:17 +08:00
|
|
|
if os.path.exists(os.path.join(proj_path, src)):
|
|
|
|
os.rename(os.path.join(proj_path, src), os.path.join(proj_path, dst))
|
2013-02-22 16:32:12 +08:00
|
|
|
|
|
|
|
# remove useless files and folders
|
2013-07-23 13:37:17 +08:00
|
|
|
for item in data["remove"]:
|
|
|
|
dst = item.replace("PROJECT_NAME", context["dst_project_name"])
|
|
|
|
if os.path.exists(os.path.join(proj_path, dst)):
|
|
|
|
shutil.rmtree(os.path.join(proj_path, dst))
|
2013-02-22 16:32:12 +08:00
|
|
|
|
|
|
|
# rename package_name. This should be replaced at first. Don't change this sequence
|
2013-07-23 13:37:17 +08:00
|
|
|
for item in data["replace_package_name"]:
|
|
|
|
tmp = item.replace("PACKAGE_PATH", java_package_path)
|
2013-02-22 16:32:12 +08:00
|
|
|
dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
|
2013-07-23 13:37:17 +08:00
|
|
|
if os.path.exists(os.path.join(proj_path, dst)):
|
|
|
|
replaceString(os.path.join(proj_path, dst), context["src_package_name"], context["dst_package_name"])
|
2013-02-22 16:32:12 +08:00
|
|
|
|
|
|
|
# rename project_name
|
2013-07-23 13:37:17 +08:00
|
|
|
for item in data["replace_project_name"]:
|
|
|
|
tmp = item.replace("PACKAGE_PATH", java_package_path)
|
2013-02-22 16:32:12 +08:00
|
|
|
dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
|
2013-07-23 13:37:17 +08:00
|
|
|
if os.path.exists(os.path.join(proj_path, dst)):
|
|
|
|
replaceString(os.path.join(proj_path, dst), context["src_project_name"], context["dst_project_name"])
|
2013-02-22 16:32:12 +08:00
|
|
|
|
|
|
|
# done!
|
2013-07-26 10:50:37 +08:00
|
|
|
print ("proj.%s\t\t: Done!" % platform)
|
2013-02-22 16:32:12 +08:00
|
|
|
# end of processPlatformProjects
|
|
|
|
|
2013-07-26 10:50:37 +08:00
|
|
|
def createPlatformProjects():
|
2013-07-23 13:37:17 +08:00
|
|
|
# prepare valid "context" dictionary
|
|
|
|
context, platforms_list = checkParams()
|
|
|
|
# print context, platforms_list
|
|
|
|
|
|
|
|
# copy "lauguage"(cpp/lua/javascript) platform.proj into cocos2d-x/projects/<project_name>/folder
|
|
|
|
if os.path.exists(context["dst_project_path"]):
|
2013-07-26 10:50:37 +08:00
|
|
|
print ("Error:" + context["dst_project_path"] + " folder is already existing")
|
|
|
|
print ("Please remove the old project or choose a new PROJECT_NAME in -project parameter")
|
2013-07-23 13:37:17 +08:00
|
|
|
sys.exit()
|
|
|
|
else:
|
|
|
|
shutil.copytree(context["src_project_path"], context["dst_project_path"], True)
|
|
|
|
|
|
|
|
# call process_proj from each platform's script folder
|
|
|
|
for platform in platforms_list:
|
|
|
|
processPlatformProjects(context, platform)
|
|
|
|
# exec "import %s.handle_project_files" % (platform)
|
|
|
|
# exec "%s.handle_project_files.handle_project_files(context)" % (platform)
|
2013-02-18 22:09:42 +08:00
|
|
|
|
2013-07-26 10:50:37 +08:00
|
|
|
print ("New project has been created in this path: " + context["dst_project_path"].replace("/tools/project-creator/../..", ""))
|
|
|
|
print ("Have Fun!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -------------- main --------------
|
|
|
|
# dump argvs
|
|
|
|
# print sys.argv
|
|
|
|
if __name__ == '__main__':
|
|
|
|
createPlatformProjects()
|
2013-02-18 22:09:42 +08:00
|
|
|
|