issue #1725 project creator script works ok for iOS and Android on Mac OSX env

This commit is contained in:
walzer 2013-02-18 22:09:42 +08:00
parent cb25e65a9c
commit 0a4700e59b
8 changed files with 249 additions and 0 deletions

View File

@ -0,0 +1,119 @@
#!/usr/bin/python
# create_project.py
# Create cross-platform cocos2d-x project
# Copyright (c) 2012 cocos2d-x.org
# Author: WangZhe
# define global variables
context = {
# "modules_all" : ["extensions", "box2d", "chipmunk"], # const
# "modules_reserved" : [],
# "modules_to_remove" : [],
"language" : "cpp", # default value is "cpp", can be "lua" or "javascript"
"src_project_name" : "undefined", # it depends on "language"
"src_package_name" : "undefined", # it depends on "language"
"dst_project_name" : "undeifned",
"dst_package_name" : "undefined",
"scr_project_path" : "undefined",
"dst_project_path" : "undefined",
"script_dir" : "undefined",
}
# begin
import sys
import os, os.path
import shutil # for copy folders and files
def dumpUsage():
print "Usage: create-project.py [options]"
print "Options:"
print " -project PROJECT_NAME Project name, e.g. MyGame"
print " -package PACKAGE_NAME Package name, e.g. org.cocos2d-x.MyAwesomeGame"
print " -language SCRIPT_LANGUAGE Script lanauge you want to used, should be cpp | lua | javascript"
print " If this param is nil, we will create a cpp project for you"
# print " -modules MODULE_LIST Extenal moduels you want to added, should be extensions | chipmunk | box2d"
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"
def checkParams(context):
# generate our internal params
context["script_dir"] = os.getcwd() + "/"
# invalid invoke, tell users how to input params
if len(sys.argv) < 3:
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]
# elif "-modules" == sys.argv[i]:
# read the left params as module
# for j in range(i+1, len(sys.argv)):
# context["modules_reserved"].append(sys.argv[j])
# fill in src_project_name and src_package_name according to "language"
if ("cpp" == context["language"]):
context["src_project_name"] = "HelloCpp"
# context["src_package_name"] = "org.cocos2d-x.hellocpp"
context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-cpp"
print "Creating multi-platform cpp projects haven't been supported in this python script yet"
sys.exit()
elif ("lua" == context["language"]):
context["src_project_name"] = "HelloLua"
# context["src_package_name"] = "org.cocos2d-x.hellolua"
context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-lua"
print "Creating multi-platform lua projects haven't been supported in this python script yet"
sys.exit()
elif ("javascript" == context["language"]):
context["src_project_name"] = "HelloJavascript"
# context["src_package_name"] = "org.cocos2d-x.hellojavascript"
context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-js"
# decide the modules to remove
# context["modules_to_remove"] = list(set(context["modules_all"]) - set(context["modules_reserved"]))
#end of checkParams(context) function
# -------------- main --------------
# dump argvs
print sys.argv
# prepare valid "context" dictionary
checkParams(context)
import pprint
pprint.pprint(context)
# copy "lauguage"(cpp/lua/javascript) platform.proj into cocos2d-x/projects/<project_name>/folder
if (os.path.exists(context["dst_project_path"]) == True):
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)
# call process_proj from each platform's script folder
platforms_list = ["ios",
"android",
# "win32",
# "mac",
# "linux",
# "blackberry"
]
for platform in platforms_list:
exec "import creator_%s.handle_project_files" % (platform)
exec "creator_%s.handle_project_files.handle_project_files(context)" % (platform)
print "New project is created in this path: " + context["dst_project_path"]
print "Have Fun!"

View File

@ -0,0 +1,2 @@
# This empty file indicates this folder as a module in python
# Don't remove me!

View File

@ -0,0 +1,50 @@
#!/usr/bin/python
# handle_project_files.py for Android
# Copyright (c) 2012 cocos2d-x.org
# Author: WangZhe
# Android
import os
def handle_project_files(context):
# determine proj_path
proj_path = context["dst_project_path"] + "/proj.android/"
# determine src_package_name & dst_package_name
if ("cpp" == context["language"]):
context["src_package_name"] = "org.cocos2dx.hellocpp"
elif ("lua" == context["language"]):
context["src_package_name"] = "org.cocos2dx.hellolua"
elif ("javascript" == context["language"]):
context["src_package_name"] = "org.cocos2dx.hellojavascript"
# rename files and folders
package_name_list = context["dst_package_name"].split('.')
os.rename(proj_path + "src/org/cocos2dx/HelloJavascript/HelloJavascript.java",
proj_path + "src/" +
package_name_list[0] + "/" + package_name_list[1] + "/" + package_name_list[2] + "/" +
context["dst_project_name"] + ".java")
# remove useless files.
os.popen("rm -rf " + proj_path + "assets")
os.popen("rm -rf " + proj_path + "bin")
os.popen("rm -rf " + proj_path + "gen")
os.popen("rm -rf " + proj_path + "obj")
# replaceString function is implemented in ../create-project.py
import replaces
# package_name should be replaced at first. Don't change this sequence
replaces.replaceString(proj_path + "AndroidManifest.xml", context["src_package_name"], context["dst_package_name"])
replaces.replaceString(proj_path + "ndkgdb.sh", context["src_package_name"], context["dst_package_name"])
replaces.replaceString(proj_path + "src/org/cocos2dx/hellojavascript/HelloJavascript.java", context["src_package_name"], context["dst_package_name"])
replaces.replaceString(proj_path + ".project", context["src_project_name"], context["dst_project_name"])
replaces.replaceString(proj_path + "AndroidManifest.xml", context["src_project_name"], context["dst_project_name"])
replaces.replaceString(proj_path + "build.xml", context["src_project_name"], context["dst_project_name"])
replaces.replaceString(proj_path + "build_native.sh", context["src_project_name"], context["dst_project_name"])
replaces.replaceString(proj_path + "ndkgdb.sh", context["src_project_name"], context["dst_project_name"])
replaces.replaceString(proj_path + "res/values/strings.xml",context["src_project_name"], context["dst_project_name"])
replaces.replaceString(proj_path + "src/org/cocos2dx/hellojavascript/HelloJavascript.java", context["src_project_name"], context["dst_project_name"])
# done!
print "proj.android : Done!"

View File

@ -0,0 +1,2 @@
# This empty file indicates this folder as a module in python
# Don't remove me!

View File

@ -0,0 +1,42 @@
#!/usr/bin/python
# handle_project_files.py for iOS
# Copyright (c) 2012 cocos2d-x.org
# Author: WangZhe
# iOS
import os
def handle_project_files(context):
# determine proj_path
proj_path = context["dst_project_path"] + "/proj.ios/"
# determine src_package_name & dst_package_name
if ("cpp" == context["language"]):
context["src_package_name"] = "org.cocos2d-x.HelloCpp"
elif ("lua" == context["language"]):
context["src_package_name"] = "org.cocos2d-x.HelloLua"
elif ("javascript" == context["language"]):
context["src_package_name"] = "org.cocos2d-x.HelloJavascript"
# rename files and folders
os.rename(proj_path + context["src_project_name"] + ".xcodeproj",
proj_path + context["dst_project_name"] + ".xcodeproj" )
# remove useless files.
os.popen("rm -rf " + proj_path + context["dst_project_name"] + ".xcodeproj/project.xcworkspace")
os.popen("rm -rf " + proj_path + context["dst_project_name"] + ".xcodeproj/xcuserdata" )
os.popen("rm -rf " + proj_path + "build" )
# replaceString function is implemented in ../create-project.py
import replaces
# package_name should be replaced at first. Don't change this sequence
replaces.replaceString(proj_path + "Info.plist",
context["src_package_name"], context["dst_package_name"])
replaces.replaceString(proj_path + context["dst_project_name"] + ".xcodeproj/project.pbxproj",
context["src_project_name"], context["dst_project_name"])
replaces.replaceString(proj_path + "Info.plist",
context["src_project_name"], context["dst_project_name"])
# done!
print "proj.ios : Done!"

View File

@ -0,0 +1,2 @@
# This empty file indicates this folder as a module in python
# Don't remove me!

View File

@ -0,0 +1,13 @@
#!/usr/bin/python
# process_proj.py
# Copyright (c) 2012 cocos2d-x.org
# Author: WangZhe
import os
def process_proj(context):
proj_path = context["project_path"] + context["dst_project_name"] + "/proj.win32/"
# remove useless files.
os.popen("rm -rf " + proj_path)
print "proj.win32 : TBD, Python script for win32 hasn't been implemented yet. Call Microsoft for help :)"

View File

@ -0,0 +1,19 @@
#!/usr/bin/python
# replaces.py
# Copyright (c) 2012 cocos2d-x.org
# Author: WangZhe
def replaceString(filepath, src_string, dst_string):
content = ""
f1 = open(filepath, "r")
for line in f1:
if src_string in line:
content += line.replace(src_string, dst_string)
else:
content += line
f1.close()
f2 = open(filepath, "w")
f2.write(content)
f2.close()
# end of replaceString(filepath, src, dst) function