前提
预先做好模版,我这里做了四个模板,分别对应四个渠道
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
import os import sys import shutil import logging import plistlib import argparse import subprocess import coloredlogs from PIL import Image from pbxproj import XcodeProject from pbxproj.pbxextensions.ProjectFiles import FileOptions parser = argparse.ArgumentParser(description='argpase') parser.add_argument('--project-folder', '-p', type=str, required=True, help="Xcode工程目录") parser.add_argument('--replace-assets', '-s', type=int, required=False, default=1, help='替换资源[0:不替换, 1:替换]') parser.add_argument('--pack-ipa', '-i', type=int, required=False, default=0, help='构建ipa[0:不构建, 1:release&develop, 2:release, 3:develop]') args = parser.parse_args() def group_add_files(project, group, folder, file_options): for item in os.listdir(folder): full_item = os.path.join(folder, item) if os.path.isdir(full_item): sub_group = project.get_or_create_group(item, parent=group) group_add_files(project, sub_group, full_item, file_options) # 递归复制子目录 else: try: project.add_file(full_item, parent=group, force=True, target_name='UnityFramework', file_options=file_options) except: logging.info('ERROR添加文件引用失败: ' + full_item) ## 替换资源 def replace_assets(area, sdk_folder, project_folder, tproject_folder): logging.info('替换资源: ' + area) logging.info('sdk_folder: ' + sdk_folder) logging.info('project_folder: ' + project_folder) logging.info('tproject_folder: ' + tproject_folder) ## 清理构建 clean_cmd = ('xcodebuild clean -project %s/Unity-iPhone.xcodeproj -configuration Debug' %(tproject_folder)) process = subprocess.Popen(clean_cmd, shell=True, stdin=subprocess.PIPE, stderr=sys.stderr) process.communicate() if process.returncode != 0: logging.error('%s clean failed!' %(area)) return clean_cmd = ('xcodebuild clean -project %s/Unity-iPhone.xcodeproj -configuration Release' %(tproject_folder)) process = subprocess.Popen(clean_cmd, shell=True, stdin=subprocess.PIPE, stderr=sys.stderr) process.communicate() if process.returncode != 0: logging.error('%s clean failed!' %(area)) return logging.info('拷贝plist前缓存并写入正确的版本号') bundle_version_string = '' bundle_version_number = '' with open(os.path.join(project_folder, 'Info.plist'), 'rb') as f: plist_data = plistlib.load(f) bundle_version_number = plist_data['CFBundleVersion'] bundle_version_string = plist_data['CFBundleShortVersionString'] plist_data with open(os.path.join(tproject_folder, 'Info.plist'), 'rb') as f0: plist_data = plistlib.load(f0) plist_data['CFBundleVersion'] = bundle_version_number plist_data['CFBundleShortVersionString'] = bundle_version_string with open(os.path.join(tproject_folder, 'Info.plist'), 'wb') as f1: plistlib.dump(plist_data, f1) logging.info('bundle_version_number: ' + bundle_version_number) logging.info('bundle_version_string: ' + bundle_version_string) logging.info('拷贝Data目录') shutil.rmtree(os.path.join(tproject_folder, 'Data')) shutil.copytree(os.path.join(project_folder, 'Data'), os.path.join(tproject_folder, 'Data')) logging.info('拷贝Classes目录') shutil.rmtree(os.path.join(tproject_folder, 'Classes')) shutil.copytree(os.path.join(project_folder, 'Classes'), os.path.join(tproject_folder, 'Classes')) logging.info('拷贝Libraries目录') shutil.rmtree(os.path.join(tproject_folder, 'Libraries')) shutil.copytree(os.path.join(project_folder, 'Libraries'), os.path.join(tproject_folder, 'Libraries')) project = XcodeProject.load(os.path.join(tproject_folder, 'Unity-iPhone.xcodeproj/project.pbxproj')) logging.info('移除Data目录引用') project.remove_group_by_name('Data') logging.info('移除Classes目录引用') project.remove_group_by_name('Classes') logging.info('移除Libraries目录引用') project.remove_group_by_name('Libraries') file_options = FileOptions(weak=True, ignore_unknown_type=True) logging.info('添加Classes目录引用') project.add_folder(os.path.join(tproject_folder, 'Data'), create_groups=False, target_name='Unity-iPhone') logging.info('添加Classes目录引用') classes_group = project.get_or_create_group('Classes', make_relative=True) group_add_files(project, classes_group, os.path.join(tproject_folder, 'Classes'), file_options) logging.info('添加Libraries目录引用') libraries_group = project.get_or_create_group('Libraries', make_relative=True) group_add_files(project, libraries_group, os.path.join(tproject_folder, 'Libraries'), file_options) project.remove_group_by_name('libil2cpp') project.save() ## 写入SDK初始化参数(替换37及个推参数) logging.info('写入37SDK和个推初始化参数到Classes/UnityAppController.mm') SQSecret = '' GTAppId = '' GTAppKey = '' GTAppSecret = '' with open(os.path.join(sdk_folder, '备份文件/sdk-params.plist'), 'rb') as f: params = plistlib.load(f) if not 'SQSecret' in params: logging.error('本地化SDK替换失败: 未读取到有效的`SQSecret`,请检查`备份文件/sdk-params.plist`文件的有效性') exit() else: SQSecret = params['SQSecret'].strip() if not 'GTAppId' in params: logging.error('本地化SDK替换失败: 未读取到有效的`GTAppId`,请检查`备份文件/sdk-params.plist`文件的有效性') exit() else: GTAppId = params['GTAppId'].strip() if not 'GTAppKey' in params: logging.error('本地化SDK替换失败: 未读取到有效的`GTAppKey`,请检查`备份文件/sdk-params.plist`文件的有效性') exit() else: GTAppKey = params['GTAppKey'].strip() if not 'GTAppSecret' in params: logging.error('本地化SDK替换失败: 未读取到有效的`GTAppSecret`,请检查`备份文件/sdk-params.plist`文件的有效性') exit() else: GTAppSecret = params['GTAppSecret'].strip() with open(os.path.join(tproject_folder, 'Classes/UnityAppController.mm'), 'r+') as f: lines = f.readlines() index = -1 for line in lines: index = index + 1 if line.find('#define SQSecret') != -1: lines[index] = '#define SQSecret @"' + SQSecret + '"\n' if line.find('#define GTAppId') != -1: lines[index] = '#define GTAppId @"' + GTAppId + '"\n' if line.find('#define GTAppKey') != -1: lines[index] = '#define GTAppKey @"' + GTAppKey + '"\n' if line.find('#define GTAppSecret') != -1: lines[index] = '#define GTAppSecret @"' + GTAppSecret + '"\n' '''if area == 'en' and line.find('#import <RVSDK_global/RVSDK_global.h>') != -1: lines[index] = '#import <RVSDK/RVSDK.h> \n' # 如果写入的字符数少于原来的字符数,会在文件末尾写入多余的行 ''' f.seek(0) f.writelines(lines) def pack(area, project_folder, is_release): area = area.lower() profiles = ({ 'en':'b7b8ca4f-6ed5-4ac9-8bd9-2faf01e06802', 'kr':'1b0cad1c-7ba7-4143-9024-ed2c58fef657', 'tw':'b84438ca-6a91-46d0-9cf1-3b6b5f821109', 'vn':'42122714-bb8c-4641-a93a-3c5e132395fd', }) if is_release: profiles = ({ 'en':'2355228b-4248-404f-b578-f80e6888e59c', 'kr':'5cd009f4-ea3f-4459-b386-967f8a5577c7', 'tw':'8d3a705b-c5c6-4728-a028-aea00601b437', 'vn':'067011a7-3e7e-4799-8436-60ea80dd7157', }) if area not in profiles.keys(): logging.error('未定义的地区类型:' + area) return logging.info('构建:' + area) bundle_version_string = '' bundle_version_number = '' with open(os.path.join(project_folder, 'Info.plist'), 'rb') as f: plist_data = plistlib.load(f) bundle_version_number = plist_data['CFBundleVersion'] bundle_version_string = plist_data['CFBundleShortVersionString'] archive_folder = '/Volumes/Data/Xcode/Archives/temp/' archive_path = os.path.join(archive_folder, 'Archive.xcarchive') exportOptions = os.path.join(os.getcwd(), ('ExportOptions/ExportOptions-%s-dev.plist' %(area))) output_path = os.path.join('/Volumes/Data/IPA_DEV', '%s[%s][%s]' %(area.upper(), bundle_version_string, bundle_version_number)) if is_release: exportOptions = os.path.join(os.getcwd(), ('ExportOptions/ExportOptions-%s.plist' %(area))) output_path = os.path.join('/Volumes/Data/IPA', '%s[%s][%s]' %(area.upper(), bundle_version_string, bundle_version_number)) if os.path.exists(archive_folder): shutil.rmtree(archive_folder, ignore_errors=True) if os.path.exists(output_path): shutil.rmtree(output_path, ignore_errors=True) ## 生成Archive cmd1 = ('xcodebuild archive -project %s/Unity-iPhone.xcodeproj -scheme Unity-iPhone -configuration Release -archivePath %s PROVISIONING_PROFILE=%s -destination \'generic/platform=iOS\'' %(project_folder, archive_path, profiles[area])) ## 导出ipa cmd2 = ('xcodebuild -exportArchive -archivePath %s -exportPath %s -exportOptionsPlist %s PROVISIONING_PROFILE=%s' %(archive_path, output_path, exportOptions, profiles[area])) process = subprocess.Popen(cmd1, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout, stderr=sys.stderr, close_fds=True, universal_newlines=True, bufsize=1) process.communicate() if process.returncode == 0: logging.info('%s archive successful!' %(area)) else: logging.error('%s archive failed!' %(area)) return process = subprocess.Popen(cmd2, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout, stderr=sys.stderr, close_fds=True, universal_newlines=True, bufsize=1) process.communicate() if process.returncode == 0: logging.info('%s pack ipa successful!' %(area)) else: logging.error('%s pack ipa failed!' %(area)) return def main(): coloredlogs.install(level='INFO', fmt='%(asctime)s %(levelname)s %(message)s') sdk_folders = ({ 'en':'/Volumes/Data/SDK/37本地化SDK/全球版', 'kr':'/Volumes/Data/SDK/37本地化SDK/日韩', 'tw':'/Volumes/Data/SDK/37本地化SDK/台湾', 'vn':'/Volumes/Data/SDK/37本地化SDK/越南', }) tproject_folders = ({ 'en':'/Volumes/Data/Project/PNC#TMPL#EN', 'kr':'/Volumes/Data/Project/PNC#TMPL#KR', 'tw':'/Volumes/Data/Project/PNC#TMPL#TW', 'vn':'/Volumes/Data/Project/PNC#TMPL#VN', }) ##测试代码 # replace_assets('en', sdk_folders['en'], args.project_folder, tproject_folders['en']) # return if args.replace_assets == 1: for key, value in sdk_folders.items(): replace_assets(key, value, args.project_folder, tproject_folders[key]) logging.info('pack:' + str(args.pack_ipa)) if args.pack_ipa == 1: for key, value in sdk_folders.items(): pack(key, tproject_folders[key], False) for key, value in sdk_folders.items(): pack(key, tproject_folders[key], True) elif args.pack_ipa == 2: for key, value in sdk_folders.items(): pack(key, tproject_folders[key], True) elif args.pack_ipa == 3: for key, value in sdk_folders.items(): pack(key, tproject_folders[key], False) if __name__ == '__main__': main() |
- 本文固定链接: http://www.u3d8.com/?p=2834
- 转载请注明: 网虫虫 在 u3d8.com 发表过