# encoding: utf-8 import os import sys import stat from pathlib import Path import time import re import socket import task import tools import log_tail import getpass class CIProcess(): def __init__(self, unity_exe_path, buld_player_name, build_type, local_name): self.unity_exe_path = unity_exe_path self.buld_player_name = buld_player_name self.build_type = build_type self.local_name = local_name self.unity_method_name = 'H3DBuild.ContentPipeline.BuildPlayerCmd' self.unity_log_path = os.getcwd() + "/build-ios-output/archive_log.txt" self.xcode_log_path = os.getcwd() + "/build-ios-output/archive_log.txt" self.unity_proj_path = os.getcwd() + "/.." self.build_path = self.unity_proj_path + "/XcodeProject" self.build_ipa_py = "ipaExportor.py" # 编译unity生成xcode工程 def BuildUnity(self): os.system("rm -Rf %s" % "../Library/ScriptAssemblies") os.system("rm -Rf %s" % "../H3DLog") export_file = os.getcwd() + "/build-ios-output/framw-unity.ipa" if Path(export_file).is_file(): print("%s exist, delete" % export_file) os.remove(export_file) # 结束正在运行的unity进程; TODO: 暂屏蔽 # os.system('pkill -9 Unity') # os.system('pkill -9 Xcode') os.system("rm -Rf %s" % self.unity_log_path) f=open(self.unity_log_path,'wb+') os.chdir("../..") run_cmd = '"{}" -quit -batchmode -logFile {} -projectPath {} -executeMethod {} -buildName {} -buildTarget iOS'\ .format(self.unity_exe_path, self.unity_log_path, self.unity_proj_path, self.unity_method_name, self.buld_player_name) print(run_cmd) sys.stdout.flush() ret = os.system(run_cmd) time.sleep(1) content = f.read() content = content.decode("utf8","ignore") print(content) f.close() if re.search("Build Failed", content): print("build failed!") sys.stdout.flush() ret = -1 if ret < 0: ret = -ret return ret # 编译unity生成ipa包 def BuildXcode(self): if os.path.exists(self.build_path) is False: print("build_path not exist, %s" % self.build_path) return -1 print(self.build_path) os.chdir(self.build_path) print(os.getcwd()) if os.path.exists(self.build_ipa_py) is False: print("build_ipa_py not exist, %s" % self.build_ipa_py) return -2 if (self.build_type != "Debug") and (self.build_type != "Release"): print("build_type not Debug or Relaeae, %s" % self.build_type) return -3 os.system("chmod +x ./%s" % self.build_ipa_py) ret = os.system("./%s %s %s > %s" % (self.build_ipa_py, self.build_type, self.local_name, self.xcode_log_path)) f=open(self.xcode_log_path,'wb+') content = f.read() content = content.decode("utf8","ignore") print(content) f.close() return ret # 清理工作目录 def CleanDir(self): os.system("rm -Rf %s" % self.build_path) user=getpass.getuser() cache="/Users/{}/Library/Developer/Xcode/DerivedData/Unity-iPhone-*".format(user) os.system("rm -Rf %s" % cache) def Run(self): self.CleanDir() ret = self.BuildUnity() if (0 != ret): print("BuildUnity fail, ret = ", ret) sys.stdout.flush() return ret return self.BuildXcode() if __name__ == '__main__': if (5 > len(sys.argv)): print("args err! not unity_exe_path and buld_player_name") os._exit(-1) unity_exe_path = sys.argv[1] buld_player_name = sys.argv[2] build_type = sys.argv[3] local_name = sys.argv[4] print("unity_exe_path : %s" % unity_exe_path) print("buld_player_name : %s" % buld_player_name) print("build_type : %s" % build_type) print("local_name : %s" % local_name) if False == Path(unity_exe_path).is_file(): print("unity_exe_path is not exist, %s" % unity_exe_path) os._exit(-2) process = CIProcess(unity_exe_path, buld_player_name, build_type, local_name) ret = process.Run() print("ret:%d" % ret) sys.stdout.flush() os._exit(ret)