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
|
|
|
|
context = {
|
2013-02-21 16:51:22 +08:00
|
|
|
"language" : "undefined",
|
|
|
|
"src_project_name" : "undefined",
|
|
|
|
"src_package_name" : "undefined",
|
2013-02-18 22:09:42 +08:00
|
|
|
"dst_project_name" : "undeifned",
|
|
|
|
"dst_package_name" : "undefined",
|
2013-02-18 23:34:31 +08:00
|
|
|
"src_project_path" : "undefined",
|
2013-02-18 22:09:42 +08:00
|
|
|
"dst_project_path" : "undefined",
|
|
|
|
"script_dir" : "undefined",
|
|
|
|
}
|
2013-02-21 16:51:22 +08:00
|
|
|
platforms_list = []
|
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
|
|
|
|
|
|
|
def dumpUsage():
|
2013-02-21 16:51:22 +08:00
|
|
|
print "Usage: create-project.py -project PROJECT_NAME -package PACKAGE_NAME -language PROGRAMING_LANGUAGE"
|
2013-02-18 22:09:42 +08:00
|
|
|
print "Options:"
|
2013-02-18 23:34:31 +08:00
|
|
|
print " -project PROJECT_NAME Project name, for example: MyGame"
|
|
|
|
print " -package PACKAGE_NAME Package name, for example: com.MyCompany.MyAwesomeGame"
|
2013-02-21 16:51:22 +08:00
|
|
|
print " -language PROGRAMING_LANGUAGE Major programing lanauge you want to used, should be [cpp | lua | javascript]"
|
2013-02-20 23:42:48 +08:00
|
|
|
print ""
|
|
|
|
print "Sample 1: ./create_project.py -project MyGame -package com.MyCompany.AwesomeGame"
|
|
|
|
print "Sample 2: ./create_project.py -project MyGame -package com.MyCompany.AwesomeGame -language javascript"
|
|
|
|
print ""
|
2013-02-18 22:09:42 +08:00
|
|
|
|
|
|
|
def checkParams(context):
|
|
|
|
# generate our internal params
|
|
|
|
context["script_dir"] = os.getcwd() + "/"
|
2013-02-21 16:51:22 +08:00
|
|
|
global platforms_list
|
2013-02-18 22:09:42 +08:00
|
|
|
|
|
|
|
# invalid invoke, tell users how to input params
|
2013-02-21 16:51:22 +08:00
|
|
|
if len(sys.argv) < 7:
|
2013-02-18 22:09:42 +08:00
|
|
|
dumpUsage()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
# find our params
|
|
|
|
for i in range(1, len(sys.argv)):
|
|
|
|
if "-project" == sys.argv[i]:
|
|
|
|
# read the next param as project_name
|
|
|
|
context["dst_project_name"] = sys.argv[i+1]
|
|
|
|
context["dst_project_path"] = os.getcwd() + "/../../projects/" + context["dst_project_name"]
|
|
|
|
elif "-package" == sys.argv[i]:
|
|
|
|
# read the next param as g_PackageName
|
|
|
|
context["dst_package_name"] = sys.argv[i+1]
|
|
|
|
elif "-language" == sys.argv[i]:
|
|
|
|
# choose a scripting language
|
|
|
|
context["language"] = sys.argv[i+1]
|
2013-02-21 16:51:22 +08:00
|
|
|
|
|
|
|
# pinrt error log our required paramters are not ready
|
|
|
|
raise_error = False
|
|
|
|
if context["dst_project_name"] == "undefined":
|
|
|
|
print "Invalid -project parameter"
|
|
|
|
raise_error = True
|
|
|
|
if context["dst_package_name"] == "undefined":
|
|
|
|
print "Invalid -package parameter"
|
|
|
|
raise_error = True
|
|
|
|
if context["language"] == "undefined":
|
|
|
|
print "Invalid -language parameter"
|
|
|
|
raise_error = True
|
|
|
|
if raise_error != False:
|
|
|
|
sys.exit()
|
|
|
|
|
2013-02-18 22:09:42 +08:00
|
|
|
# fill in src_project_name and src_package_name according to "language"
|
|
|
|
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-02-18 22:09:42 +08:00
|
|
|
context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-cpp"
|
2013-02-21 16:51:22 +08:00
|
|
|
platforms_list = ["ios",
|
|
|
|
"android",
|
|
|
|
"win32",
|
|
|
|
"mac",
|
|
|
|
"blackberry",
|
|
|
|
"linux",
|
|
|
|
"marmalade"]
|
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-02-18 22:09:42 +08:00
|
|
|
context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-lua"
|
2013-02-21 16:51:22 +08:00
|
|
|
platforms_list = ["ios",
|
|
|
|
"android",
|
2013-02-21 17:11:11 +08:00
|
|
|
"win32",
|
|
|
|
"blackberry",
|
|
|
|
"linux",
|
|
|
|
"marmalade"]
|
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-02-21 16:51:22 +08:00
|
|
|
context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-js"
|
|
|
|
platforms_list = ["ios",
|
|
|
|
"android",
|
|
|
|
"win32"]
|
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):
|
|
|
|
content = ""
|
|
|
|
f1 = open(filepath, "rb")
|
|
|
|
for line in f1:
|
|
|
|
if src_string in line:
|
|
|
|
content += line.replace(src_string, dst_string)
|
|
|
|
else:
|
|
|
|
content += line
|
|
|
|
f1.close()
|
|
|
|
f2 = open(filepath, "wb")
|
|
|
|
f2.write(content)
|
|
|
|
f2.close()
|
|
|
|
# end of replaceString
|
|
|
|
|
|
|
|
def processPlatformProjects(platform):
|
|
|
|
# determine proj_path
|
|
|
|
proj_path = context["dst_project_path"] + "/proj.%s/" % platform
|
|
|
|
java_package_path = ""
|
|
|
|
|
|
|
|
# read josn config file or the current platform
|
|
|
|
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
|
|
|
|
if (platform == "android"):
|
|
|
|
src_pkg = context["src_package_name"].split('.')
|
|
|
|
dst_pkg = context["dst_package_name"].split('.')
|
|
|
|
os.rename(proj_path + "src/" + src_pkg[0],
|
|
|
|
proj_path + "src/" + dst_pkg[0])
|
|
|
|
os.rename(proj_path + "src/" + dst_pkg[0] + "/" + src_pkg[1],
|
|
|
|
proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1])
|
|
|
|
os.rename(proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1] + "/" + src_pkg[2],
|
|
|
|
proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1] + "/" + dst_pkg[2])
|
|
|
|
java_package_path = dst_pkg[0] + "/" + dst_pkg[1] + "/" + dst_pkg[2]
|
|
|
|
|
|
|
|
# rename files and folders
|
|
|
|
for i in range(0, len(data["rename"])):
|
|
|
|
tmp = data["rename"][i].replace("PACKAGE_PATH", java_package_path)
|
|
|
|
src = tmp.replace("PROJECT_NAME", context["src_project_name"])
|
|
|
|
dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
|
|
|
|
if (os.path.exists(proj_path + src) == True):
|
|
|
|
os.rename(proj_path + src, proj_path + dst)
|
|
|
|
|
|
|
|
# remove useless files and folders
|
|
|
|
for i in range(0, len(data["remove"])):
|
|
|
|
dst = data["remove"][i].replace("PROJECT_NAME", context["dst_project_name"])
|
|
|
|
if (os.path.exists(proj_path + dst) == True):
|
|
|
|
shutil.rmtree(proj_path + dst)
|
|
|
|
|
|
|
|
# rename package_name. This should be replaced at first. Don't change this sequence
|
|
|
|
for i in range(0, len(data["replace_package_name"])):
|
|
|
|
tmp = data["replace_package_name"][i].replace("PACKAGE_PATH", java_package_path)
|
|
|
|
dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
|
|
|
|
if (os.path.exists(proj_path + dst) == True):
|
|
|
|
replaceString(proj_path + dst, context["src_package_name"], context["dst_package_name"])
|
|
|
|
|
|
|
|
# rename project_name
|
|
|
|
for i in range(0, len(data["replace_project_name"])):
|
|
|
|
tmp = data["replace_project_name"][i].replace("PACKAGE_PATH", java_package_path)
|
|
|
|
dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
|
|
|
|
if (os.path.exists(proj_path + dst) == True):
|
|
|
|
replaceString(proj_path + dst, context["src_project_name"], context["dst_project_name"])
|
|
|
|
|
|
|
|
# done!
|
|
|
|
print "proj.%s\t\t: Done!" % platform
|
|
|
|
# end of processPlatformProjects
|
|
|
|
|
|
|
|
|
2013-02-18 22:09:42 +08:00
|
|
|
|
|
|
|
# -------------- main --------------
|
|
|
|
# dump argvs
|
2013-02-18 23:34:31 +08:00
|
|
|
# print sys.argv
|
2013-02-18 22:09:42 +08:00
|
|
|
|
|
|
|
# prepare valid "context" dictionary
|
|
|
|
checkParams(context)
|
2013-02-18 23:34:31 +08:00
|
|
|
# import pprint
|
|
|
|
# pprint.pprint(context)
|
2013-02-18 22:09:42 +08:00
|
|
|
|
|
|
|
# copy "lauguage"(cpp/lua/javascript) platform.proj into cocos2d-x/projects/<project_name>/folder
|
|
|
|
if (os.path.exists(context["dst_project_path"]) == True):
|
2013-02-22 16:32:12 +08:00
|
|
|
shutil.rmtree(context["dst_project_path"])
|
|
|
|
#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"
|
|
|
|
#sys.exit()
|
|
|
|
#else:
|
|
|
|
|
|
|
|
shutil.copytree(context["src_project_path"], context["dst_project_path"], True)
|
2013-02-18 22:09:42 +08:00
|
|
|
|
2013-02-21 16:51:22 +08:00
|
|
|
# call process_proj from each platform's script folder
|
2013-02-18 22:09:42 +08:00
|
|
|
for platform in platforms_list:
|
2013-02-22 16:32:12 +08:00
|
|
|
processPlatformProjects(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-02-20 13:26:41 +08:00
|
|
|
print "New project has been created in this path: " + context["dst_project_path"].replace("/tools/project-creator/../..", "")
|
2013-02-18 22:09:42 +08:00
|
|
|
print "Have Fun!"
|
|
|
|
|