def run_command(cmd, cwd=None, is_print=True, encrypt=None): if encrypt: cmd_str = ' '.join(cmd).replace(encrypt, "*******") else: cmd_str = ' '.join(cmd) if is_print: print('=================================================') print("========== [Run Command]: {} ==========".format(cmd_str)) print('=================================================') process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd) while True: out = process.stdout.read(1) if out == '' and process.poll() is not None: break if out != '': sys.stdout.write(out) sys.stdout.flush() exit_code = process.poll() if exit_code != 0: log_tool.show_error('Run Command Error for {}, Error code: {}'.format( cmd_str, exit_code)) exit(exit_code) log_tool.show_info('Done!')
def run_command_write_result_to_file(cmd, result_file, is_print=True, cwd=None): if is_print: print '=================================================' print "========== [Run Command]: {} ==========".format(' '.join(cmd)) print '=================================================' print "========== [Result file]: {} ==========".format(result_file) print '=================================================' with open(result_file, 'w') as outfile: process = subprocess.Popen(cmd, stdout=outfile, stderr=outfile, cwd=cwd) process.wait() exit_code = process.poll() if exit_code != 0: time.sleep(2) log_tool.show_error( '[ERROR]: Run Command Error for {}, Error code: {}'.format( ' '.join(cmd), exit_code)) exit(exit_code) log_tool.show_info('Run Command is done!')
def svn_revert(local_path): """ svn revert :param local_path: :return: """ cmd = ['svn', 'revert', local_path, '--depth', 'infinity'] result = common.run_command(cmd) log_tool.show_info("The local path is {}.Revert status: {}".format(local_path, result))
def ClearBuildInfo(): unity_package_manager_path = os.path.join(workspace.unity_project_path, 'UnityPackageManager') if os.path.isdir(unity_package_manager_path): shutil.rmtree(unity_package_manager_path, ignore_errors=True) log_tool.show_info('[INFO]: delete' + unity_package_manager_path) # 清除ios工程目录 if os.path.isdir(IOS_PRJ_DIR): shutil.rmtree(IOS_PRJ_DIR, ignore_errors=True) log_tool.show_info('[INFO]: delete' + IOS_PRJ_DIR)
def BuildAssetBundle(): # 先更新到最新 if LOCAL_OP is False: UpdateProduct() log_tool.show_info('[REBUILD] --> {}'.format(IS_REBUILD)) ab_path = os.path.join(workspace.unity_project_path, 'Bundles') platform_bundle_path = os.path.join(ab_path, PLATFORM) if IS_REBUILD is True: # 如果是Rebuild,需要删除库里的Bundles目录 log_tool.show_info('[AutoBuild] Rebuild AssetBundle, Delete Bundle Direcotory for {}'.format(PLATFORM)) if os.path.isdir(platform_bundle_path): if LOCAL_OP is False: git_tool.git_delete(workspace.product_path, platform_bundle_path) git_tool.git_commit(workspace.product_path, '[AutoBuild] Rebuild AssetBundle, Delete Bundle Direcotory for {}'.format(PLATFORM)) else: shutil.rmtree(platform_bundle_path) build_function = 'KGame.KAutoBuilderAB.ReBuildAssetBundles{}'.format(PLATFORM) else: build_function = 'KGame.KAutoBuilderAB.BuildAssetBundles{}'.format(PLATFORM) log_path = os.path.join(workspace.unity_project_path, "build_ab_log.txt") unity_tool.build(build_function, UNITY_PATH, workspace.unity_project_path, log_path) # 判断平台manifest文件是否存在,是则构建成功,否则失败 if PLATFORM == 'Android': android_manifest_file = os.path.join(workspace.unity_project_path, 'Bundles/Android/Android.manifest') if not os.path.exists(android_manifest_file): log_tool.show_error("Build failed! Can't find the file of {}, Platform: {}".format(android_manifest_file, PLATFORM)) exit(1) elif PLATFORM == 'iOS': ios_manifest_file = os.path.join(workspace.unity_project_path, 'Bundles/iOS/iOS.manifest') if not os.path.exists(ios_manifest_file): log_tool.show_error("Build failed! Can't find the file of {}, Platform: {}".format(ios_manifest_file, PLATFORM)) exit(1) else: pass if not os.path.isdir(platform_bundle_path): log_tool.show_error("Can't find the path of {}".format(platform_bundle_path)) exit(1) # 提交AB相关修改文件 if LOCAL_OP is False: ab_setting_path = os.path.join(workspace.unity_project_path, 'Setting/Base/FileAbInfoSetting_{}.txt'.format(PLATFORM)) git_tool.git_add(workspace.product_path, ab_setting_path) git_tool.git_add(workspace.product_path, platform_bundle_path) git_tool.git_commit(workspace.product_path, '[AutoBuild]: Asset Bundles Rebuild: {} Commit.'.format(IS_REBUILD))
def build(method, unity_path, project_path, log_path): """ call unity to build a project :param method: :param unity_path: :param project_path: :param log_path: :return: """ build_cmd = [unity_path, '-batchmode', '-projectPath', project_path, '-nographics', '-executeMethod', method, '-logFile', log_path, '-quit'] log_tool.show_info('Start Unity...') if os.path.exists(log_path): os .remove(log_path) log_tool.show_info('Delete the old logfile!') thread.start_new_thread(common.start_thread_to_tail, (log_path,)) log_tool.show_info('Run Unity Command: {}'.format(' '.join(build_cmd))) result_file = 'result.txt' common.run_command_write_result_to_file(build_cmd, result_file, is_print=True, cwd=project_path) log_tool.show_info('Run Command is done!')
def _PreHandleIosPrj_Sign(xcode_project): global developmentTeam global code_sign_identity global provisioning_profile_specifier global bundle_id if SIGN_TYPE == 'Inhouse': if PROFILE_TYPE == 'Development': # 暂时没有development类型的pp log_tool.show_error('Have not Inhouse Development Provioning Profile') exit(1) elif PROFILE_TYPE == 'Distribution': developmentTeam = globalvalue_tool.get_value("development_team_inhouse") code_sign_identity = globalvalue_tool.get_value("code_sign_identity_inhouse_distribution") provisioning_profile_specifier = globalvalue_tool.get_value("provisioning_profile_specifier_inhouse_distribuion") bundle_id = globalvalue_tool.get_value("bundle_id_ios_inhouse") elif SIGN_TYPE == 'AppStore': if PROFILE_TYPE == 'Development': # 暂时没有development类型的pp log_tool.show_error('Have not AppStore Development Provioning Profile') exit(1) elif PROFILE_TYPE == 'Distribution': developmentTeam = globalvalue_tool.get_value("development_team_appstore") code_sign_identity = globalvalue_tool.get_value("code_sign_identity_appstore_distribution") provisioning_profile_specifier = globalvalue_tool.get_value("provisioning_profile_specifier_appstore_distribuion") bundle_id = globalvalue_tool.get_value("bundle_id_ios_appstore") else: log_tool.show_error("Invalid SignType {}".format(SIGN_TYPE)) exit(1) # 设置手动签名 xcode_project.set_flags('CODE_SIGN_STYLE', 'Manual') for st in xcode_project.get_build_phases_by_name('PBXProject'): for t in xcode_project.get_build_phases_by_name('PBXNativeTarget'): log_tool.show_info("Handling target[{}], Set 'ProvisioningStyle = Manual'".format(t.name)) st.set_provisioning_style('Manual', t) st.attributes.TargetAttributes[u'1D6058900D05DD3D006BFB54'].parse({'DevelopmentTeam': developmentTeam}) st.attributes.TargetAttributes[u'1D6058900D05DD3D006BFB54'].SystemCapabilities.parse( {'com.apple.Push': {'enabled': '1'}}) st.attributes.TargetAttributes[u'1D6058900D05DD3D006BFB54'].SystemCapabilities.parse( {'com.apple.Keychain': {'enabled': '0'}}) xcode_project.set_flags('DEVELOPMENT_TEAM', developmentTeam, target_name='Unity-iPhone') xcode_project.set_flags('CODE_SIGN_IDENTITY', code_sign_identity) xcode_project.set_flags('CODE_SIGN_IDENTITY[sdk=iphoneos*]', code_sign_identity) xcode_project.set_flags('PROVISIONING_PROFILE_SPECIFIER', provisioning_profile_specifier, target_name='Unity-iPhone')
def BuildIosPrj(): scheme = 'Unity-iPhone' info_plist_path = 'Info.plist' package_name = 'Unity-iPhone.ipa' if (VERSION_TYPE == 'Alpha'): configuration = 'Debug' else: configuration = 'Release' # 切换到工程目录 os.chdir(IOS_PRJ_DIR) # Archive archive_cmd = [ XCODE_BUILD_PATH, '-archivePath', 'Unity-iPhone', '-project', 'Unity-iPhone.xcodeproj', '-configuration', configuration, '-scheme', scheme, 'archive', '-verbose', 'CODE_SIGN_IDENTITY={}'.format(code_sign_identity), 'PROVISIONING_PROFILE_SPECIFIER={}'.format(provisioning_profile_specifier), '-sdk', 'iphoneos' ] common.run_command(archive_cmd) # Export export_cmd = [ XCODE_BUILD_PATH, '-exportArchive', '-archivePath', 'Unity-iPhone.xcarchive', '-exportPath', 'Unity-iPhone-resigned-dist', '-exportOptionsPlist', info_plist_path ] common.run_command(export_cmd) # Copy ipa build_date = time.strftime("%Y.%m.%d.%H.%M.%S", time.localtime()) ipa_dir = GetIpaDir() source_ipa_path = os.path.join(IOS_PRJ_DIR, os.path.join('Unity-iPhone-resigned-dist', package_name)) target_ipa_path = os.path.join(ipa_dir, '{}_{}_{}.{}.{}.{}_{}_{}.ipa'.format( PRODUCT_NAME, BRANCH_NAME, MAJOR_NUMBER, MINOR_NUMBER, PATCH_NUMBER, BUILD_NUMBER, VERSION_TYPE, build_date)) shutil.copyfile(source_ipa_path, target_ipa_path) log_tool.show_info('Build Ios Ipa is done!')
def delete_all(path, ignore_paths): """ delete file or directory :param path: :param ignore_paths: :return: """ path = path.replace('\\', '/') if delete_check(path, ignore_paths): return False if not os.path.isdir(path) or is_link(path): log_tool.show_info('Delete file or link: {}'.format(path)) delete_link(path) return True log_tool.show_info('Delete directory: {}'.format(path)) shutil.rmtree(path) return True
def BuildIpa(): if not common.is_macos(): log_tool.show_error('Only mac can build ios ipa') exit(1) # if LOCAL_OP is False: # UpdateProduct() # 清除上次构建残留 ClearBuildInfo() # 删除不需要打包的资源 ClearResNoNeedBuild() # 导出Ios工程 ExportIosPrj() # # 分析日志行,确保unity是否构建成功 # time.sleep(1) # log_path = os.path.join(workspace.unity_project_path, "unity_build_ios_ipa_log.txt") # if os.path.isfile(log_path): # for line in fileinput.input(log_path, 1024): # if "is an incorrect path for a scene file" in line: # log_tool.show_error(u'[ERROR]: 场景文件路径不正确,请检查EditorBuildSettings') # fileinput.close() # exit(1) # if "Compilation failed:" in line: # log_tool.show_error('Build iOS Ipa Failed!') # fileinput.close() # exit(1) # Ios工程编译前处理 PreHandleIosPrj() if ONLY_EXPORT is True: log_tool.show_info('Only Export XcodeProject, Excape BuildIosPrj!') exit(0) # 编译Ios工程,生成ipa BuildIosPrj()
def BuildApk(): if LOCAL_OP is False: UpdateProduct() # 清除上次构建残留 ClearBuildInfo() # 删除不需要打包的资源 ClearResNoNeedBuild() # 导出Android工程 ExportAndroidPrj() # # 分析日志行,确保unity是否构建成功 # time.sleep(1) # log_path = os.path.join(workspace.unity_project_path, "unity_build_android_apk_log.txt") # if os.path.isfile(log_path): # for line in fileinput.input(log_path, 1024): # if "is an incorrect path for a scene file" in line: # log_tool.show_error(u'[ERROR]: 场景文件路径不正确,请检查EditorBuildSettings') # fileinput.close() # exit(1) # if "Compilation failed:" in line: # log_tool.show_error('Build Android Apk Failed!') # fileinput.close() # exit(1) # if "OBB Builder Failed!" in line: # log_tool.show_error('OBB Builder Failed!') # fileinput.close() # exit(1) # Android工程编译前处理 PreHandleAndroidPrj() # 编译Android工程,生成Apk BuildAndroidPrj() log_tool.show_info('Build Android Apk is done!')
def DeleteUpdatePackage(pack_dir): hot_package_dir = _GetHotPackageDir() hot_package_dir = os.path.join(hot_package_dir, pack_dir) if not os.path.isdir(hot_package_dir): log_tool.show_info('HotPackageDir {} not Exist!'.format(hot_package_dir)) return # 获取最新版本号 resource_version_path = os.path.join(hot_package_dir, '{}.{}.resource_version.txt'.format(PACKAGE_NAME_PREFIX, MAIN_VERSION)) last_version_number = _GetLastVersionNumber(resource_version_path) if last_version_number == 0: log_tool.show_info('last_version_number = {}, no update_version to delete!'.format(last_version_number)) return global DELETE_VERSION_NUMBER if DELETE_VERSION_NUMBER == -1: DELETE_VERSION_NUMBER = last_version_number if DELETE_VERSION_NUMBER > last_version_number: log_tool.show_error('DELETE_VERSION_NUMBER {} > last_version_number {}', DELETE_VERSION_NUMBER, last_version_number) return delete_package_list = Queue.Queue() delete_version_number = last_version_number while delete_version_number >= DELETE_VERSION_NUMBER: for i in range(0, delete_version_number): zipfile_name = '{}.{}.{}-{}.zip'.format(PACKAGE_NAME_PREFIX, MAIN_VERSION, i, delete_version_number) zipfile_path = os.path.join(hot_package_dir, zipfile_name) zipfile_md5_path = '{}.md5'.format(zipfile_path) zipfile_info_path = '{}.info'.format(zipfile_path) if os.path.isfile(zipfile_path): delete_package_list.put(zipfile_path) if os.path.isfile(zipfile_md5_path): delete_package_list.put(zipfile_md5_path) if os.path.isfile(zipfile_info_path): delete_package_list.put(zipfile_info_path) version_zipfile_manifest_path = os.path.join(hot_package_dir, '{}.{}.{}.zip.manifest'.format(PACKAGE_NAME_PREFIX, MAIN_VERSION, delete_version_number)) version_zipfile_info_path = os.path.join(hot_package_dir, '{}.{}.{}.zip.info'.format(PACKAGE_NAME_PREFIX, MAIN_VERSION, delete_version_number)) if os.path.isfile(version_zipfile_manifest_path): delete_package_list.put(version_zipfile_manifest_path) if os.path.isfile(version_zipfile_info_path): delete_package_list.put(version_zipfile_info_path) delete_version_number = delete_version_number - 1 # 删除热更版本文件 while not delete_package_list.empty(): source_file_path = delete_package_list.get() os.remove(source_file_path) # 更新热更版本号 _SetLastVersionNumber(resource_version_path, DELETE_VERSION_NUMBER - 1) log_tool.show_info('auto_delete_update_package Done!')
def PreHandleIosPrj(): # 工程文件预修改 xcode_project = XcodeProject().load(IOS_PRJ_PATH) _PreHandleIosPrj_Base(xcode_project) _PreHandleIosPrj_Sign(xcode_project) _PreHandle_ProductBundleIdentifier(xcode_project) xcode_project.save() log_tool.show_info('XcodeProject PreHandle Done!') # plist文件修改 _PreHandle_plist() log_tool.show_info('plist.info PreHandle Done!') log_tool.show_info('PreHandleIosPrj Done!')
def up_product(self, version_code=None): """ 只更新products库 :return: """ if version_code: log_tool.show_info( 'Update Products To Version:Newest, Working Dir: {}'.format( self.product_path)) else: log_tool.show_info( 'Update Products To Version:{}, Working Dir: {}'.format( version_code, self.product_path)) if not os.path.isdir(self.product_path): log_tool.show_info('Checkout new products...') git_tool.git_clone(self.product_url, self.product_path) git_tool.git_cleanup(self.product_path) git_tool.git_switch(self.product_path, self.branch_name) git_tool.git_up(self.product_path, version_code)
def PreHandleAndroidPrj(): # 回滚列表,以下文件需要回滚 build_gradle_path = os.path.join( workspace.unity_project_path, 'AndroidPrj/Version_{}/{}/build.gradle'.format(LANGUAGE, PRODUCT_NAME)) string_xml_path = os.path.join( workspace.unity_project_path, 'AndroidPrj/Version_{}/{}/src/main/res/values/strings.xml'.format( LANGUAGE, PRODUCT_NAME)) android_manifest = os.path.join( workspace.unity_project_path, 'AndroidPrj/Version_{}/{}/src/main/AndroidManifest.xml'.format( LANGUAGE, PRODUCT_NAME)) revert_list = [build_gradle_path, string_xml_path, android_manifest] for revert_file in revert_list: git_tool.git_revert(workspace.unity_project_path, revert_list) # 修改string.xml,修改app_name common.replace_text( string_xml_path, '<string name="app_name">{}</string>'.format(PRODUCT_NAME), '<string name="app_name">{}</string>'.format(APP_SHOW_NAME)) # 修改AndroidManifest.xml version_name = "{}.{}.{}".format(MAJOR_NUMBER, MINOR_NUMBER, BUILD_NUMBER) common.replace_text(android_manifest, 'android:versionName="0.0.0"', 'android:versionName="%s"' % version_name) common.replace_text(android_manifest, 'android:versionCode="1"', 'android:versionCode="%s"' % BUILD_NUMBER) # Obb相关修改 if IS_OBB_VERSION is True: #Obb文件改名 obb_src_path = os.path.join( workspace.unity_project_path, 'AndroidPrj/Version_{}/{}/{}.main.obb'.format( LANGUAGE, PRODUCT_NAME, PRODUCT_NAME)) obb_dst_path = os.path.join( workspace.unity_project_path, 'AndroidPrj/Version_{}/{}/main.{}.{}.obb'.format( LANGUAGE, PRODUCT_NAME, BUILD_NUMBER, BUNDLE_ID)) os.rename(obb_src_path, obb_dst_path) # 修改AppConfig appconfig_path = os.path.join( workspace.unity_project_path, 'AndroidPrj/Version_{}/{}/src/main/assets/AppConfigs.txt'.format( LANGUAGE, PRODUCT_NAME)) common.replace_text(appconfig_path, 'IsObbVersion = 0', 'IsObbVersion = 1') common.replace_text( appconfig_path, 'ObbFileName = ', 'ObbFileName = main.{}.{}.obb'.format(BUILD_NUMBER, BUNDLE_ID)) if NEED_DOWNLOAD_OBB is True: common.replace_text(appconfig_path, 'IsNeedDownloadObb = 0', 'IsNeedDownloadObb = 1') # 获取Obb文件内的MD5只,修改AndroidManifest.xml obb_zipfile = zipfile.ZipFile(obb_dst_path) for file_path in obb_zipfile.namelist(): file_path = file_path.encode('utf-8') if file_path.startswith('assets/') and len( file_path.split('/')) == 2: if len(file_path.split('/') [-1]) > 20 and '.' not in file_path.split('/')[-1]: obb_md5 = os.path.basename(file_path) # 替换 AndroidManifest.xml MD5 值 common.replace_text( android_manifest, '10015bb9-866e-406b-bc8d-feff9e1f6045', obb_md5) # 拷贝到Apk目录去 apk_dir = GetApkDir() shutil.copy(obb_dst_path, apk_dir) # 如果需要cdn下载Obb,需要再生成一个md5文件 if NEED_DOWNLOAD_OBB is True: md5_file_path = '%s.md5' % obb_dst_path log_tool.show_info( 'Calculate the MD5 value and write it to file: {}'.format( md5_file_path)) md5_obj = hashlib.md5() with open(obb_dst_path, 'rb') as tmp_f: md5_obj.update(tmp_f.read()) md5 = md5_obj.hexdigest() md5_file = open(md5_file_path, 'w') md5_file.write(md5) md5_file.flush() # 拷贝到Apk目录去 shutil.copy(md5_file_path, apk_dir)
def is_exists(path): if os.path.exists(path): log_tool.show_info('{} exists !'.format(path)) return path else: return is_exists(os.path.dirname(path))
def _SetLastVersionNumber(resource_version_path, version_number): with open(resource_version_path, 'wb+') as wf: wf.write(str(version_number).zfill(0)) log_tool.show_info('SetLastVersionNumber {} Done!'.format(version_number))
def ProduceUploadPackage(pack_dir): temp_dir = _GetTempDir() if os.path.isdir(temp_dir): shutil.rmtree(temp_dir) hot_package_dir = _GetHotPackageDir() hot_package_dir = os.path.join(hot_package_dir, pack_dir) if not os.path.isdir(hot_package_dir): log_tool.show_error('HotPackageDir {} not Exist!'.format(hot_package_dir)) return upload_package_list = Queue.Queue() # 拷贝resource_version resource_version_path = os.path.join(hot_package_dir, '{}.{}.resource_version.txt'.format(PACKAGE_NAME_PREFIX, MAIN_VERSION)) upload_package_list.put(resource_version_path) global UPDATE_VERSION_NUMBER if UPDATE_VERSION_NUMBER == -1: UPDATE_VERSION_NUMBER = _GetLastVersionNumber(resource_version_path) for i in range(0, UPDATE_VERSION_NUMBER): if i == 0 or UPDATE_VERSION_NUMBER - i <= MAX_VERSION_SPAN: zipfile_name = '{}.{}.{}-{}.zip'.format(PACKAGE_NAME_PREFIX, MAIN_VERSION, i, UPDATE_VERSION_NUMBER) zipfile_path = os.path.join(hot_package_dir, zipfile_name) zipfile_md5_path = '{}.md5'.format(zipfile_path) zipfile_info_path = '{}.info'.format(zipfile_path) upload_package_list.put(zipfile_path) upload_package_list.put(zipfile_md5_path) upload_package_list.put(zipfile_info_path) temp_dir = _GetTempDir() branch_name = BRANCH_NAME if '/' not in BRANCH_NAME else BRANCH_NAME.replace('/', '_') copy_dir = os.path.join(temp_dir, '{}/{}/{}/{}'.format(branch_name, MAIN_VERSION, PLATFORM, pack_dir)) if not os.path.isdir(copy_dir): os.makedirs(copy_dir) while not upload_package_list.empty(): source_file_path = upload_package_list.get() target_file_path = os.path.join(copy_dir, os.path.basename(source_file_path)) if os.path.isfile(source_file_path): log_tool.show_info('=================== Copy {} to {} ======================'.format(source_file_path, target_file_path)) shutil.copyfile(source_file_path, target_file_path) else: log_tool.show_error("Can't find the file {}".format(source_file_path)) return # zip file os.chdir(temp_dir) upload_package_zipfile = '{}.{}.{}-{}.zip'.format(PACKAGE_NAME_PREFIX, PLATFORM, MAIN_VERSION, UPDATE_VERSION_NUMBER) zip_cmd = ['zip', '-r', upload_package_zipfile, branch_name] common.run_command(zip_cmd) # 拷贝到热更包目录 upload_dir = _GetUploadDir() upload_dir = os.path.join(upload_dir, pack_dir) if not os.path.isdir(upload_dir): os.makedirs(upload_dir) target_file_path = os.path.join(upload_dir, os.path.basename(upload_package_zipfile)) shutil.copyfile(upload_package_zipfile, target_file_path) # 删除临时目录 shutil.rmtree(temp_dir) log_tool.show_info('auto_build_upload_hotpackage Done!')
def Pack(package_setting, is_packbase): # FilePacker path if common.is_windows(): filepackerpath = os.path.join(workspace.unity_project_path, 'FilePacker.exe') else: filepackerpath = os.path.join(workspace.unity_project_path, 'FilePacker') # 获取执行权限 os.chdir(workspace.unity_project_path) chmod = ['chmod', '755', 'FilePacker'] common.run_command(chmod) UPDATER_PACK_PATH = os.path.join(CURRENT_PATH, 'updater_pack.py') UPDATER_CMD = ['python', UPDATER_PACK_PATH, BRANCH_NAME, VERSION_NUMBER, MAIN_VERSION, PLATFORM, workspace.unity_project_path, PROJECT_NAME, package_setting.package_dir_name, '{}'.format(is_packbase), IS_REBOOT, IS_BACKGROUND_UPDATE] # 生成Patch包 for pack_dir in package_setting.pack_dirs: pack_idx_path = os.path.join(workspace.unity_project_path, 'Assets/StreamingAssets/Res/{}/pack.idx'.format(pack_dir)) pack_dat_path = os.path.join(workspace.unity_project_path, 'Assets/StreamingAssets/Res/{}/pack0.dat'.format(pack_dir)) if not os.path.isfile(pack_idx_path) or not os.path.isfile(pack_dat_path): log_tool.show_error("Can't find {} and {}".format(pack_idx_path, pack_dat_path)) exit(1) if not is_packbase: pack_settings = workspace.get_pack_setting() if not pack_settings: log_tool.show_error("GetPackSetting Failed") exit(1) if not pack_settings.has_key(pack_dir): log_tool.show_error('{} not in PackSetting'.format(pack_dir)) exit(1) patch_srcfile_path = os.path.join(workspace.unity_project_path, 'Assets/StreamingAssets/Res/{}/pack.patch'.format(pack_dir)) patch_path = os.path.join(workspace.unity_project_path, 'Patch/{}'.format(pack_dir)) patch_idx_path = os.path.join(patch_path, 'pack.idx') patch_dstfile_path = os.path.join(patch_path, 'pack.patch') if os.path.isdir(patch_path): shutil.rmtree(patch_path) os.makedirs(patch_path) # 修改pack配置文件 pack_ini_file = os.path.join(workspace.unity_project_path, 'FilePacker.ini') pack_setting = pack_settings[pack_dir] # outdir config_content = '[FilePacker]\n' config_content = '{}DstPath=Assets/StreamingAssets/Res/{}\n'.format(config_content, pack_dir) # folders folder_index = 1 for folder in pack_setting.folders: config_content = '{}Folder{}={}\n'.format(config_content, folder_index, folder) folder_index = folder_index + 1 # files file_index = 1 for file in pack_setting.files: config_content = '{}File{}={}\n'.format(config_content, file_index, file) file_index = file_index + 1 file = open(pack_ini_file, 'w') file.write(config_content) file.close() log_tool.show_info('Run FilePacker --patch') common.run_command([filepackerpath, '--patch']) log_tool.show_info('Run FilePacker --patch is done!') if os.path.isfile(pack_idx_path) and os.path.isfile(patch_srcfile_path): log_tool.show_info('Copy {} --> {}'.format(pack_idx_path, patch_idx_path)) shutil.copyfile(pack_idx_path, patch_idx_path) log_tool.show_info('Copy {} --> {}'.format(patch_srcfile_path, patch_dstfile_path)) shutil.copyfile(patch_srcfile_path, patch_dstfile_path) else: log_tool.show_info("Can't find {}! No Change!".format(patch_srcfile_path)) # 生成热更包 log_tool.show_info("========== Run updater_pack.py ==========") common.run_command(UPDATER_CMD)
def BuildPack(): # 先更新到最新 if LOCAL_OP is False: UpdateProduct() # 生成所有脚本路径的配置表 # 现在不需要生成,可以通过加载目录的方式 #CreateAllLuaFilePath() sleep(1) # 获取所有需要Pack的目录 pack_settings = workspace.get_pack_setting() if not pack_settings: exit(1) # FilePacker path if common.is_windows(): filepackercpath = os.path.join(workspace.unity_project_path, 'FilePacker.exe') elif common.is_macos(): filepackercpath = os.path.join(workspace.unity_project_path, 'FilePacker') # 获取执行权限 os.chdir(workspace.unity_project_path) chmod = ['chmod', '755', 'FilePacker'] common.run_command(chmod) if not os.path.isfile(filepackercpath): log_tool.show_error("Can't find the FilePacker.exe: {}".format(filepackercpath)) exit(1) # 需要打包的目录 pack_dirs = [] if not PACK_DIRS or PACK_DIRS == '': for setting in pack_settings.values(): pack_dirs.append(setting.pack_dir_name) else: dirs = PACK_DIRS.split(',') for dir_name in dirs: pack_dirs.append(dir_name.strip()) # 删除需要打包的目录 root_dir = os.path.join(workspace.unity_project_path, 'Assets/StreamingAssets/Res') for pack_dir in pack_dirs: pack_forlder = os.path.join(root_dir, pack_dir) if os.path.isdir(pack_forlder): shutil.rmtree(pack_forlder) # 生成Pack文件 pack_ini_file = os.path.join(workspace.unity_project_path, 'FilePacker.ini') for pack_dir in pack_dirs: # 修改pack配置文件 if not pack_settings.has_key(pack_dir): log_tool.show_error('{} not in PackSetting'.format(pack_dir)) exit(1) pack_setting = pack_settings[pack_dir] # outdir config_content = '[FilePacker]\n' config_content = '{}DstPath=Assets/StreamingAssets/Res/{}\n'.format(config_content, pack_dir) # folders folder_index = 1 for folder in pack_setting.folders: config_content = '{}Folder{}={}\n'.format(config_content, folder_index, folder) folder_index = folder_index + 1 # files file_index = 1 for file in pack_setting.files: config_content = '{}File{}={}\n'.format(config_content, file_index, file) file_index = file_index + 1 file = open(pack_ini_file, 'w') file.write(config_content) file.close() # 生成Pack目录 dir_path = os.path.join(workspace.unity_project_path, 'Assets/StreamingAssets/Res/{}'.format(pack_dir)) if not os.path.isdir(dir_path): os.makedirs(dir_path) common.run_command(filepackercpath) # 打开一下Unity,自动生成一下pack的meta文件 if LOCAL_OP is False: log_tool.show_info('Wait For Generator MetaFiles') build_function = 'KGame.KAutoBuilderPack.GenerMetaFiles' log_path = os.path.join(workspace.unity_project_path, 'generator_pack_metafiles_log.txt') unity_tool.build(build_function, UNITY_PATH, workspace.unity_project_path, log_path) log_tool.show_info('Generator MetaFiles Finished')