运用Python运行cocos2dx-lua/js项目


之前写了个篇博文运用Sublime Text开发cocos2dx-lua项目的lua部分,其中的运行命令是这么做的

{  
    "working_dir": "${project_path:${folder}}",  
    "shell_cmd": "${folder}/simulator/win32/game1.exe -workdir ${folder}"  
}  

这个buildSystem有缺陷:${folder}可能会指向其他工程目录,模拟器game1.exe文件不知道该怎么动态表达, 新的项目必须要修改它; 所以就想做个一劳永逸的build system.

 python脚本就很适合啦。

{
	"working_dir": "${project_path:${folder}",
	"shell_cmd": "D:\\soft\\python2.7_32\\python.exe D:\\soft\\cocos\\workspace\\luapro_run.py ${file}"
}

运行luapro_run.py脚本, 参数传入项目中的lua文件 ${file}

# -*- coding: UTF-8 -*-
# coding=UTF-8
import sys
import string
import os
print("这个命令是用来跑cocos2dx-lua项目的")
reverXieGang = "\\"
xieGang = "/"
def formatDir(str):
	str = str.replace(reverXieGang, xieGang)
	if str[len(str) - 1] == '/':
		return str[0:len(str)-1]
	return str
def getParentDir(str):
	index = str.rfind(xieGang,0, len(str))
	if index > -1:
		ret = str[0:index]
		return ret
	return str
def getLastDir(str):
	index = str.rfind(xieGang,0, len(str))
	if index > -1:
		return str[index + 1: len(str)]
	return str
def getProjectInfo(luaFilePath):
		luaFilePath = formatDir(luaFilePath)
		#print "入口文件为", luaFilePath
		parentDir = getParentDir(luaFilePath)
		#print "父目录为", parentDir
		lastDir = getLastDir(luaFilePath)
		#print "当前目录名为", lastDir
		while lastDir != "src":
			luaFilePath = parentDir
			parentDir = getParentDir(luaFilePath)
			lastDir = getLastDir(luaFilePath)
		luaFilePath = parentDir
		parentDir = getParentDir(luaFilePath)
		lastDir = getLastDir(luaFilePath)
		projectName = lastDir
		#print "当前工程名为:", projectName
		return (parentDir, projectName)
def startFuc():
	argsLen = len(sys.argv)
	if argsLen < 2:
		print "参数错误, 要加入要当前项目的lua文件路径"
	else:
		luaFilePath = sys.argv[1]
		print "入口文件为", luaFilePath
		(projectParentDir, projectName) = getProjectInfo(luaFilePath)
		#print "当前工程名为:", projectName
		#print "工程父目录为", projectParentDir
		#模拟器路径
		projectDir = projectParentDir + xieGang + projectName
		simFilePath = projectDir + xieGang + "simulator/win32/" + projectName+".exe -workdir " + projectDir
		print "模拟器路径为", simFilePath
		os.system(simFilePath)

#-workdir ${folder}
#main方法
if __name__ == "__main__":
	startFuc()



同理,cocos2dx-js项目也可以这么做,做它比lua复杂些,它需要把main.js, src等目录/文件 复制到win32的debug或release目录下。

{
	"working_dir": "${project_path:${folder}}",
	"shell_cmd": "D:\\soft\\python2.7_32\\python.exe D:\\soft\\cocos\\workspace\\cocosJS_pro_run.py ${file}"
}

cocosJS_pro_run.py

# -*- coding: UTF-8 -*-
# coding=UTF-8
import sys
import string
import os
import re
import shutil
print("这个命令是用来跑cocos2dx-js项目的")
reverXieGang = "\\"
xieGang = "/"
def formatDir(str):
	str = str.replace(reverXieGang, xieGang)
	if str[len(str) - 1] == '/':
		return str[0:len(str)-1]
	return str
def getParentDir(str):
	index = str.rfind(xieGang,0, len(str))
	if index > -1:
		ret = str[0:index]
		return ret
	return str
def getLastDir(str):
	index = str.rfind(xieGang,0, len(str))
	if index > -1:
		return str[index + 1: len(str)]
	return str
def getProjectInfo(luaFilePath):
	luaFilePath = formatDir(luaFilePath)
	#print "入口文件为", luaFilePath
	parentDir = getParentDir(luaFilePath)
	#print "父目录为", parentDir
	lastDir = getLastDir(luaFilePath)
	#print "当前目录名为", lastDir
	isFoundPro = 1
	while lastDir != "src":
		luaFilePath = parentDir
		parentDir = getParentDir(luaFilePath)
		lastDir = getLastDir(luaFilePath)
		if parentDir == luaFilePath:
			isFoundPro = 0
			break
	if isFoundPro == 0:
		return 0, 0
	luaFilePath = parentDir
	parentDir = getParentDir(luaFilePath)
	lastDir = getLastDir(luaFilePath)
	projectName = lastDir
	#print "当前工程名为:", projectName
	return (parentDir, projectName)
def myCpFile(filePath, destDir):
	fileName = getLastDir(filePath)
	destDir = formatDir(destDir)
	destFile = destDir + xieGang + fileName
	#print "复制文件%s到%s" % (filePath, destFile)
	shutil.copyfile(filePath, destFile)
	return
#复制文件
def cpFilesByRegrex(path, fileRegrex, flag, destDir):
	for dirpath,dirnames,filenames in os.walk(path):
	    for file in filenames:
	    	if dirpath == path:
	    		if re.match(fileRegrex, file, flag):
	    			myCpFile(dirpath + xieGang + file, destDir)
	    	else:
	    		return
def cpFilesByName(dirPath, fileName, distDir):
	myCpFile(dirPath + xieGang + fileName, distDir)
def  findDir(path, dirName):
	for dirpath,dirnames,filenames in os.walk(path):
	    for d in dirnames:
	    	if d == dirName:
	    		return 1
	return 0
#根据正则匹配取得目录或文件名称
def getDirOrFileNameByRegrex(dirPath, regrex, flag):
	for dirpath,dirnames,filenames in os.walk(dirPath):
	    if dirpath == dirPath:
	    	for dirName in dirnames:
	    		if re.match(regrex, dirName, flag):
	    			return dirName
	    	for fileName in filenames:
	    		if re.match(regrex, fileName, flag):
	    			return fileName
	    else:
	    	return ""	
	    	


#拷贝目录
def cpDir(dirPath, destDir):
	#print "复制目录%s到%s" % (dirPath, destDir)
	dirName = getLastDir(dirPath)
	if os.path.exists(destDir + xieGang + dirName) == False:
		os.chdir(destDir)
		#print "创建目录", dirName
		os.mkdir(dirName)
	for dirpath,dirnames,filenames in os.walk(dirPath):
		if dirpath == dirPath:
		    for file in filenames:
		    	cpFilesByName(dirPath, file, destDir + xieGang + dirName)
		    for subDir in dirnames:
		    	#print "子目录名:", subDir
		    	cpDir(dirpath + xieGang + subDir, destDir + xieGang + dirName)

def startFuc():
	argsLen = len(sys.argv)
	if argsLen < 2:
		print "参数错误, 要加入要当前项目的lua文件路径"
	else:
		luaFilePath = sys.argv[1]
		print "入口文件为", luaFilePath
		(projectParentDir, projectName) = getProjectInfo(luaFilePath)
		if projectParentDir == 0 :
			luaFilePath = formatDir(luaFilePath)
			if findDir(".", "src"):
				print "当前目录就是工程目录"
				curDir = getParentDir(luaFilePath)
				projectParentDir = getParentDir(curDir)
				projectName = getLastDir(curDir)
			else:
				print "未找到工程目录."
				return
		#print "当前工程名为:", projectName
		#print "工程父目录为", projectParentDir
		#模拟器路径
		projectDir = projectParentDir + xieGang + projectName
		targetDir = projectDir + xieGang +"frameworks/runtime-src/proj.win32/Debug.win32"
		cpFilesByRegrex(projectDir, r'.*[.]js$', 0, targetDir)
		cpFilesByRegrex(projectDir, r'.*[.]json$',0, targetDir)
		cpDir(projectDir + xieGang + "src", targetDir)
		#取得exe文件名称
		exeFileName = getDirOrFileNameByRegrex(targetDir, r'.*[.]exe$', 0)
		if exeFileName == "":
			print "还没有生成exe文件,请重新编译工程,或者切换编译版本..."
			return
		shellCmd = targetDir + xieGang + exeFileName
		print "命令行:", shellCmd
		os.system(shellCmd)
#-workdir ${folder}
#main方法
if __name__ == "__main__":
	startFuc()



呵呵,不再老是打开庞大的VS2013编译项目啦,感觉写python脚本挺爽的!









 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值