運用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腳本挺爽的!









 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章