Cocos creator打包工程化(py)
背景:之前打包一直手动执行各种脚本,公司发布几个平台(不同服务器),然后上周测试服更新,连续几个版本出现更新问题,搞得人精疲力竭,所以还是交给脚本来执行比较可靠。
当前方式:编译之后将build/jsb-link目录下src,res,subpackages三个文件按照不同的脚本来编译生成版本号的配置文件(原生的是.manifest文件,但是我们支持大厅子游戏更新,所以自定义引擎使用的是.u3d文件作为配置文件),每次都要去改对应的版本号,生成好之后再合并上传。
优化点:1,直接读取项目内版本文件来生成版本号配置文件。
2,不同平台读取配置文件来读取链接,而不用每个平台创建一个bat
3,读取项目版本配置中在线服务器标致校验是否是对应版本(避免人为出错)
4,不开编辑器指令打包
5,合并压缩
环境:
cocos creator 2.2,python2.7
配置文件config.ini
[test]
file=version_generator.js
subFile=version_generator_sub_new_test.js
url=https://xxx.ccc.net/client/update/[game]
file=version_generator.js
subFile=version_generator_sub_new.js
url=https://yyy.dddd.net/client_vn/update/[india]
file=version_generator.js
subFile=version_generator_sub_new.js
url=https://zzz.ddd.net/game_app/
file是用来生成版本配置文件的js文件:就是官网的js文件
subFile是用来处理subpackages的版本配置文件的js文件
url是将要上传的远程文件地址,
bat文件:
C:/CocosCreator_2.2.0/CocosCreator.exe --path %cd%/../ --build "platform=android;debug=false;md5Cache=false;template=link"
pause
python ./createWebZip.py native game
pause
第一行就是生成native的执行语句,不用打开编辑器。
第二行执行python文件生成版本配置和压缩相关。
python文件:
# -*- coding: UTF-8 -*-
import shutil
import os
import random
import string
import zipfile
import base64
import ConfigParser
import sysimport sys reload(sys)
sys.setdefaultencoding('utf-8')#压缩文件
def zipDir(zipFilePath, dirName, curCwd):gameAPath = curCwd + os.sep + dirNameprint('Start zip A game:', gameAPath)with zipfile.ZipFile(zipFilePath, "w", compression=zipfile.ZIP_DEFLATED) as zf:for dirpath, dirnames, filenames in os.walk(gameAPath):fpath = dirpath.replace(gameAPath, '')fpath = fpath and fpath + os.sep or ''for filename in filenames:zf.write(os.path.join(dirpath, filename), fpath+filename)zf.close()#运维配置的jinkens多了一层目录,这里处理的是将web-mobile先复制到tmp文件夹中,再压缩重命名成web-mobile.zip
def createZipFile(dirName):sPath = os.getcwd()zipFilePath = sPath + os.sep + dirName + ".zip"if os.path.isdir("tmp"):shutil.rmtree(sPath + os.sep + "tmp")os.mkdir("tmp")shutil.move(sPath + os.sep + dirName, sPath + os.sep + "tmp")if not os.path.exists(sPath):print "先编译生成web-mobile"else:zipDir(zipFilePath, "tmp", sPath)if os.path.isdir("tmp"):shutil.rmtree(sPath + os.sep + "tmp")#获取出版本号和是否在线信息,每个项目不同,获取的方式也不同,
def getGameInfo(uuu3dPath):import refileObj = open(uuu3dPath, "rb+")fileContent = fileObj.read()version = re.findall(r'\d+\.\d+\.\d+', fileContent)online = re.findall(r'online_switch[\s]*\:[\s]*([a-z]+)', fileContent)fileObj.close()if len(version) == 1 and len(online) == 1:if online[0] == 'true':print "线上发布,请谨慎操作……"print "发布版本号:", version[0]return version[0], online[0] == 'true'else:print "版本号获取失败,请检查uuu3d.js配置文件:" + uuu3dPathreturn None, False#执行js文件
def createMainManifest(fileName, url, version):os.system("node " + fileName + " -v " + version + " -u " + url + version + "/ -s ./" + version + "/ -d " + version + "/")def createSubManifest(fileName, url, version):os.system("node " + fileName + " -u " + url + version + "/game_res -s ./" + version + "/game_res/ -d " + version + "/game_res/")#将src和res先移出来生成版本文件
def dealUpdateFile(sPath, srcName, version):versionDir = sPath + os.sep + versionoriginSrc = sPath + os.sep + srcName + os.sep + "src"originRes = sPath + os.sep + srcName + os.sep + "res"shutil.move(originSrc, versionDir)shutil.move(originRes, versionDir)#将subpackages移动到对应目录然后执行js文件生成版本文件
def dealSubUpdateFile(sPath, srcName, version):versionDir = sPath + os.sep + versionoriginSub = sPath + os.sep + srcName + os.sep + "subpackages"shutil.move(originSub, versionDir)os.rename(versionDir + os.sep + "subpackages", versionDir + os.sep + "game_res")def createWebZipFile(dirName):createZipFile(dirName)#生成native更新文件{game_res:子包,src:代码,res:资源,config.u3d, version.u3d}
def createNativeZipFile(dirName, platform):import timesPath = os.getcwd()#读取项目内配置文件地址,来获取版本号version和是否在线的信息onlineuuu3dPath = sPath + "/../assets/Framework/Unity3d/uuu3d.js"version, online = getGameInfo(uuu3dPath)zipFilePath = sPath + os.sep + platform + "_" + version + ".zip"if(not online and platform != 'test' or online and platform == 'test'):print "当前平台和uuu3d配置文件不匹配,请检查校正后再打包……", platformreturnif os.path.isfile(zipFilePath):#TODO:这里有问题,不能直接相加,文件命名冲突想做成随机名字的zipFilePath = zipFilePath.replace('.zip', "_" + time.time() + ".zip")print "当前版本号已经存在压缩包:", zipFilePath," 重命名为:" + zipFilePathfileName, subFile, url = getConfig(platform)if version:versionDir = sPath + os.sep + versionif os.path.exists(versionDir):shutil.rmtree(versionDir)os.mkdir(versionDir)dealUpdateFile(sPath, dirName, version)createMainManifest(fileName, url, version)dealSubUpdateFile(sPath, dirName, version)createSubManifest(subFile, url, version)zipDir(zipFilePath, version, sPath)if os.path.isdir(version):shutil.rmtree(sPath + os.sep + version)#读取ini配置文件
def getConfig(platform):cf=ConfigParser.ConfigParser()cf.read("config.ini")fileName = cf.get(platform, "file")subFile = cf.get(platform, "subFile")url = cf.get(platform, "url")return fileName, subFile, urlif __name__ == '__main__':#web平台发布:用编辑器发布之后,再执行指令,因为cocos creator 2.2执行web发布md5Cache无效if len(sys.argv) > 0 and sys.argv[1] == 'web-mobile':createWebZipFile(sys.argv[1])elif len(sys.argv) > 0 and sys.argv[1] == 'native':platform = sys.argv[2]print "发布平台:", platformcreateNativeZipFile("jsb-link", platform)