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

来源:互联网 发布:程序员的自我修养图片 编辑:程序博客网 时间:2024/05/24 07:28


之前写了个篇博文运用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-8import sysimport stringimport osprint("这个命令是用来跑cocos2dx-lua项目的")reverXieGang = "\\"xieGang = "/"def formatDir(str):str = str.replace(reverXieGang, xieGang)if str[len(str) - 1] == '/':return str[0:len(str)-1]return strdef getParentDir(str):index = str.rfind(xieGang,0, len(str))if index > -1:ret = str[0:index]return retreturn strdef getLastDir(str):index = str.rfind(xieGang,0, len(str))if index > -1:return str[index + 1: len(str)]return strdef getProjectInfo(luaFilePath):luaFilePath = formatDir(luaFilePath)#print "入口文件为", luaFilePathparentDir = getParentDir(luaFilePath)#print "父目录为", parentDirlastDir = getLastDir(luaFilePath)#print "当前目录名为", lastDirwhile lastDir != "src":luaFilePath = parentDirparentDir = getParentDir(luaFilePath)lastDir = getLastDir(luaFilePath)luaFilePath = parentDirparentDir = getParentDir(luaFilePath)lastDir = getLastDir(luaFilePath)projectName = lastDir#print "当前工程名为:", projectNamereturn (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 + projectNamesimFilePath = projectDir + xieGang + "simulator/win32/" + projectName+".exe -workdir " + projectDirprint "模拟器路径为", simFilePathos.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-8import sysimport stringimport osimport reimport shutilprint("这个命令是用来跑cocos2dx-js项目的")reverXieGang = "\\"xieGang = "/"def formatDir(str):str = str.replace(reverXieGang, xieGang)if str[len(str) - 1] == '/':return str[0:len(str)-1]return strdef getParentDir(str):index = str.rfind(xieGang,0, len(str))if index > -1:ret = str[0:index]return retreturn strdef getLastDir(str):index = str.rfind(xieGang,0, len(str))if index > -1:return str[index + 1: len(str)]return strdef getProjectInfo(luaFilePath):luaFilePath = formatDir(luaFilePath)#print "入口文件为", luaFilePathparentDir = getParentDir(luaFilePath)#print "父目录为", parentDirlastDir = getLastDir(luaFilePath)#print "当前目录名为", lastDirisFoundPro = 1while lastDir != "src":luaFilePath = parentDirparentDir = getParentDir(luaFilePath)lastDir = getLastDir(luaFilePath)if parentDir == luaFilePath:isFoundPro = 0breakif isFoundPro == 0:return 0, 0luaFilePath = parentDirparentDir = getParentDir(luaFilePath)lastDir = getLastDir(luaFilePath)projectName = lastDir#print "当前工程名为:", projectNamereturn (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:    returndef 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 1return 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 "创建目录", dirNameos.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 + projectNametargetDir = 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文件,请重新编译工程,或者切换编译版本..."returnshellCmd = targetDir + xieGang + exeFileNameprint "命令行:", shellCmdos.system(shellCmd)#-workdir ${folder}#main方法if __name__ == "__main__":startFuc()



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









 

原创粉丝点击