需求: 實現自動化打包exe
項目說明:
引擎cocos-js 3.16版
打包流程說明:
初始化項目Sln -> 編譯C++工程 -> 壓縮圖片資源 -> js轉化jsc -> 刪除VS生成多余文件 -> 加密 -> 刪除jsc -> 生成exe安裝包
環境變量:
因為需要使用vs工具的環境變量初始化批處理。這里配置vs安裝路徑(具體到Tools)到環境變量path(D:\xxx\Common7\Tools)
(或者python自己獲取)
依賴工具:
InnoSetup
具體實現(主要)代碼:
class PackagePc(object): def __init__(self, args): self.workPath = WORK_PATH self.vsDevCmd = "VsDevCmd.bat" self.compileCommand = "" self.compilePath = "" self.issPath = "" self.packConfig = utils.parse_json(os.path.join(self.workPath, "package.json")) self.projectName = self.packConfig.get("projectName") self.projectSlnPath = utils.flat_path(os.path.join(self.workPath, '../../../frameworks/runtime-src/proj.win32/')) self.compilePath = "{}{}".format(self.packConfig.get("compileType"), ".win32") self.assetsPath = "{}{}{}".format(self.projectSlnPath, "\\", self.compilePath) self.useLessFileList = ['.pdb', '.ilk', '.lib', '.log', '.obj', '.exp', '.js', '.idb', ".res"] def buildPcPack(self): #初始化項目Sln self.initProjectSln() #編譯C++工程 self.compileCPlus() #壓縮圖片資源 self.doPngQuant() #js轉化jsc self.compileJsFile() #刪除VS生成多余文件 self.removeUselessFile(self.useLessFileList) #加密 self.doEncrypt() #刪除jsc self.removeUselessFile(['.jsc']) #生成exe安裝包 self.setUpISS() # 初始化編譯命令 def initProjectSln(self): slnName = self.packConfig.get("sln") if not slnName: raise_known_error('沒有配置項目sln名', KnownError.ERROR_WRONG_CONFIG) self.compileCommand = "{}{}{}{}{}{}".format("devenv ", self.projectSlnPath , "\\" , slnName, ".sln", " /rebuild release") # 編譯C++工程 def compileCPlus(self): print(u"----------開始編譯C++工程----------") utils.run_shell("%s & %s" % (self.vsDevCmd, self.compileCommand)) # 壓縮圖片資源 def doPngQuant(self): print(u"----------開始壓縮圖片資源----------") excludeDict = {"exclude_files": ["gg_s9_res"], "exclude_folders": ["games", "src/libs/images/native/native_s9_res"]} quant = png_quant.PngQuant(self.assetsPath, excludeDict) quant.compress_images_in_path() #壓縮js文件 def compileJsFile(self): print(u"----------開始js轉化jsc----------") if not os.path.exists(self.assetsPath): print("compileJsFile path not found =" + self.assetsPath) return compilePath = utils.flat_path(os.path.join(self.workPath, '../../../frameworks/cocos2d-x/tools/cocos2d-console/bin/cocos')) compileCmd = "%s jscompile -s %s -d %s" % (compilePath, self.assetsPath, self.assetsPath) utils.run_shell(compileCmd) #資源加密 def doEncrypt(self): print(u"----------開始資源加密----------") ASSETS_ENCRYPT_EXCLUDE_CFG = ["games/mj_common/images/card_packer/*.png"] encrypt = ResEncrypt(self.assetsPath, self.assetsPath, False, ASSETS_ENCRYPT_EXCLUDE_CFG, True, False) encrypt.do_encrypt() #刪除項目中無用的文件 def removeUselessFile(self, fileList): print(u"----------開始刪除VS生成多余文件----------")for parent, dirNames, fileNames in os.walk(self.assetsPath): for fileFullName in fileNames: if os.path.splitext(fileFullName)[1] in fileList: print("file %s"%(os.path.join(parent, fileFullName))) os.remove(os.path.join(parent, fileFullName)) #生成exe安裝包 def setUpISS(self): print(u"----------開始生成exe安裝包----------") issPath = utils.flat_path(os.path.join(self.workPath, '../../../tools/hall/PackPC/')) devCmd = "{}{}".format("cd ", os.path.join(issPath, "InnoSetup")) issCmd = "{}{}".format("iscc ", os.path.join(issPath, "build.iss")) self.issPath = os.path.join(issPath, "build.iss") if not os.path.exists(self.issPath): raise_known_error("找不到ISS文件", KnownError.ERROR_OTHERS) self.modifyISS(issPath, False) utils.run_shell("%s & %s & %s" % (devCmd, "ISCC.exe", issCmd)) self.modifyISS(issPath, True) #修改、還原Inno Setup打包配置 def modifyISS(self, issPath, isReduction): with open(self.issPath, "rb") as f: data = f.read() f.close() cfgVersion = self.packConfig.get("version").decode('utf8').encode('gb2312') if isReduction: data = data.replace(cfgVersion, "GameVersion") data = data.replace(self.assetsPath, "PCProjectPath") data = data.replace(issPath, "IcoPath") data = data.replace("xxxxxPC" + cfgVersion + "-Setup", "OutputFilename") else: data = data.replace("GameVersion", cfgVersion)#修改版本號 data = data.replace("PCProjectPath", self.assetsPath)#修改項目路徑名 data = data.replace("IcoPath", issPath)#修改ICO路徑 data = data.replace("OutputFilename","xxxxxPC" + cfgVersion + "-Setup") with open(self.issPath, "wb") as f: f.write(data) f.close()
(python新手,如有改進之處,還麻煩指出)