-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXcodebuild.py
174 lines (156 loc) · 6.16 KB
/
Xcodebuild.py
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
# -*- coding: utf-8 -*-
import os,sys
import time
import re
class Xcodebuild(object):
packMethod = 'development'
def __init__(self):
self.archivePath = ''
self.outPutPath = ''
self.appName = ''
self.ipaPath = ''
@property
def existWorkspace(self):
workspace = os.popen('find . -maxdepth 1 -name *.xcworkspace -print')
workspaceName = workspace.read()
if workspaceName:
return True
else:
return False
@property
def projectName(self):
workspace = os.popen('find . -maxdepth 1 -name *.xcworkspace -print')
workspaceName = workspace.read()[2:]
if not workspaceName:
project = os.popen('find . -maxdepth 1 -name *.xcodeproj -print')
projectName = project.read()[2:]
if not projectName:
print ('\033[7;31m' + '脚本所在目录没有xcode项目,请检查脚本所在目录。' + '\033[0m')
quit()
else:
return projectName
else:
return workspaceName
# 打包
def archiveAndExportProject(self):
os.system('xcodebuild clean')
name = ''
archiveSucess = False
if self.existWorkspace:
name = self.projectName[:-13]
archiveSucess = self.__archiveWithWorkspace(name)
else:
name = self.projectName[:-11]
archiveSucess = self.__archiveWithProject(name)
self.appName = name
if archiveSucess:
self.exportByZipApp()
else:
print ('\033[7;31m' + '归档失败,请检查证书配置.' + '\033[0m')
# 通过zip打包归档文件内的.app文件的方式导出ipa
def exportByZipApp(self):
payloadPath = 'Payload'
appPath = os.path.join(self.archivePath,'Products/Applications/%s.app' % self.appName)
ipaPath = self.appName + '.ipa'
cmd = 'mkdir %s&&cp -r %s %s&&zip -r %s %s' %(payloadPath, appPath, payloadPath, ipaPath, payloadPath)
print('cmd = ' + cmd)
success = os.system(cmd)
if success == 0:
os.system('mkdir %s' % self.outPutPath)
cpCmd = 'cp -r %s %s %s' % (self.archivePath, ipaPath, self.outPutPath)
print('cpCmd = ' + cpCmd)
os.system(cpCmd)
os.system('open ' + self.outPutPath)
self.ipaPath = os.path.join(self.outPutPath, ipaPath)
print('\033[7;32m' + '打包完成,请到%s获取ipa文件' % self.outPutPath + '\033[0m')
else:
print ('\033[7;31m' + '打包失败,请检查证书配置.' + '\033[0m')
# 删除打包产生的文件和文件夹
deleteCmd = 'rm -rf build %s %s %s' % (ipaPath, self.archivePath, payloadPath)
print('deleteCmd = ' + deleteCmd)
os.system(deleteCmd)
# 用xcodebuild -exportArchive讲归档文件打包成ipa文件
def exportByXcodebuild(self):
ipaPath = self.appName + '.ipa'
self.prepareExportPlist()
exportCmd = 'xcodebuild -exportArchive -archivePath %s -exportPath ./ -exportOptionsPlist export.plist' % (self.archivePath)
print('exportCmd = ' + exportCmd)
exportSuccess = os.system(exportCmd)
if exportSuccess == 0:
os.system('mkdir %s' % self.outPutPath)
cpCmd = 'cp -r %s %s %s' % (self.archivePath, ipaPath, self.outPutPath)
print('cpCmd = ' + cpCmd)
os.system(cpCmd)
os.system('open ' + self.outPutPath)
self.ipaPath = os.path.join(self.outPutPath, ipaPath)
print('\033[7;32m' + '打包完成,请到%s获取ipa文件' % self.outPutPath + '\033[0m')
else:
print ('\033[7;31m' + '打包失败,请检查证书配置.' + '\033[0m')
# 删除打包产生的文件和文件夹
deleteCmd = 'rm -rf build %s %s' % (ipaPath, self.archivePath)
print('deleteCmd = ' + deleteCmd)
os.system(deleteCmd)
# 归档.xcworkspace文件
def __archiveWithWorkspace(self,name):
print('hehe')
# 归档.xcodeproj文件
def __archiveWithProject(self,name):
self.outPutPath = self.getOutPutPath(name)
archivePath = name + '.xcarchive'
self.archivePath = archivePath
archiveCmd = 'xcodebuild archive -scheme %s -configuration "Release" -archivePath %s' % (name, archivePath)
print(archiveCmd)
archiveSuccess = os.system(archiveCmd)
if archiveSuccess == 0:
return True
else:
return False
# 生产ipa和归档文件路径,用于完成后存放
def getOutPutPath(self,name):
if not self.outPutPath:
outPutPath = os.environ['HOME']
outPutPath = os.path.join(outPutPath, 'Desktop')
outPutPath = os.path.join(outPutPath, name + self.getCurrentTime())
self.outPutPath = outPutPath
return self.outPutPath
# 获取当前时间
def getCurrentTime(self):
return time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
def prepareExportPlist(self):
# 若使用exportByXcodebuild方法导出ipa需配置packMethod,可选配置有: app-store, ad-hoc, package, enterprise, development, developer-id
packMethod = 'development'
content = '''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>compileBitcode</key>
<false/>
<key>embedOnDemandResourcesAssetPacksInBundle</key>
<true/>
<key>iCloudContainerEnvironment</key>
<string>com.apple.developer.icloud-container-environment</string>
<key>manifest</key>
<dict>
<key>appURL</key>
<string>www.apple.com</string>
<key>displayImageURL</key>
<string>www.apple.com</string>
<key>fullSizeImageURL</key>
<string>www.apple.com</string>
<key>assetPackManifestURL</key>
<string>www.apple.com</string>
</dict>
<key>method</key>
<string>%s</string>
<key>thinning</key>
<string><none></string>
<key>uploadBitcode</key>
<false/>
<key>uploadSymbols</key>
<false/>
</dict>
</plist>'''% packMethod
f = open('export.plist','w')
f.write(content)
f.close()