-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
356 lines (290 loc) · 11.1 KB
/
utils.go
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-xcode/certificateutil"
"github.com/bitrise-io/go-xcode/export"
"github.com/bitrise-io/go-xcode/exportoptions"
"github.com/bitrise-io/go-xcode/profileutil"
"github.com/bitrise-io/go-xcode/v2/xcarchive"
)
const (
// ExportProductApp ...
ExportProductApp ExportProduct = "app"
// ExportProductAppClip ...
ExportProductAppClip ExportProduct = "app-clip"
)
// ExportProduct ...
type ExportProduct string
// ParseExportProduct ...
func ParseExportProduct(product string) (ExportProduct, error) {
switch product {
case "app":
return ExportProductApp, nil
case "app-clip":
return ExportProductAppClip, nil
default:
return "", fmt.Errorf("unkown method (%s)", product)
}
}
func findIDEDistrubutionLogsPath(output string) (string, error) {
pattern := `IDEDistribution: -\[IDEDistributionLogging _createLoggingBundleAtPath:\]: Created bundle at path '(?P<log_path>.*)'`
re := regexp.MustCompile(pattern)
scanner := bufio.NewScanner(strings.NewReader(output))
for scanner.Scan() {
line := scanner.Text()
if match := re.FindStringSubmatch(line); len(match) == 2 {
return match[1], nil
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", nil
}
func generateExportOptionsPlist(exportProduct ExportProduct, exportMethodStr, teamID string, uploadBitcode, compileBitcode bool, xcodebuildMajorVersion int64, archive xcarchive.IosArchive, manageVersionAndBuildNumber bool) (string, error) {
log.Printf("Generating export options")
var productBundleID string
var exportMethod exportoptions.Method
exportTeamID := ""
exportCodeSignIdentity := ""
exportProfileMapping := map[string]string{}
exportCodeSignStyle := ""
switch exportProduct {
case ExportProductApp:
productBundleID = archive.Application.BundleIdentifier()
case ExportProductAppClip:
productBundleID = archive.Application.ClipApplication.BundleIdentifier()
}
parsedMethod, err := exportoptions.ParseMethod(exportMethodStr)
if err != nil {
return "", fmt.Errorf("failed to parse export options, error: %s", err)
}
exportMethod = parsedMethod
log.Printf("export-method specified: %s", exportMethodStr)
if xcodebuildMajorVersion >= 9 {
log.Printf("xcode major version > 9, generating provisioningProfiles node")
fmt.Println()
log.Printf("Target Bundle ID - Entitlements map")
var bundleIDs []string
for bundleID, entitlements := range archive.BundleIDEntitlementsMap() {
bundleIDs = append(bundleIDs, bundleID)
entitlementKeys := []string{}
for key := range entitlements {
entitlementKeys = append(entitlementKeys, key)
}
log.Printf("%s: %s", bundleID, entitlementKeys)
}
fmt.Println()
log.Printf("Resolving CodeSignGroups...")
certs, err := certificateutil.InstalledCodesigningCertificateInfos()
if err != nil {
return "", fmt.Errorf("failed to get installed certificates, error: %s", err)
}
certs = certificateutil.FilterValidCertificateInfos(certs).ValidCertificates
log.Debugf("Installed certificates:")
for _, certInfo := range certs {
log.Debugf(certInfo.String())
}
profs, err := profileutil.InstalledProvisioningProfileInfos(profileutil.ProfileTypeIos)
if err != nil {
return "", fmt.Errorf("failed to get installed provisioning profiles, error: %s", err)
}
log.Debugf("Installed profiles:")
for _, profileInfo := range profs {
log.Debugf(profileInfo.String(certs...))
}
log.Printf("Resolving CodeSignGroups...")
codeSignGroups := export.CreateSelectableCodeSignGroups(certs, profs, bundleIDs)
if len(codeSignGroups) == 0 {
log.Errorf("Failed to find code signing groups for specified export method (%s)", exportMethod)
}
log.Debugf("\nGroups:")
for _, group := range codeSignGroups {
log.Debugf(group.String())
}
bundleIDEntitlementsMap := archive.BundleIDEntitlementsMap()
for bundleID := range bundleIDEntitlementsMap {
bundleIDs = append(bundleIDs, bundleID)
}
if len(bundleIDEntitlementsMap) > 0 {
log.Warnf("Filtering CodeSignInfo groups for target capabilities")
codeSignGroups = export.FilterSelectableCodeSignGroups(codeSignGroups, export.CreateEntitlementsSelectableCodeSignGroupFilter(bundleIDEntitlementsMap))
log.Debugf("\nGroups after filtering for target capabilities:")
for _, group := range codeSignGroups {
log.Debugf(group.String())
}
}
log.Warnf("Filtering CodeSignInfo groups for export method")
codeSignGroups = export.FilterSelectableCodeSignGroups(codeSignGroups, export.CreateExportMethodSelectableCodeSignGroupFilter(exportMethod))
log.Debugf("\nGroups after filtering for export method:")
for _, group := range codeSignGroups {
log.Debugf(group.String())
}
if teamID != "" {
log.Warnf("Export TeamID specified: %s, filtering CodeSignInfo groups...", teamID)
codeSignGroups = export.FilterSelectableCodeSignGroups(codeSignGroups, export.CreateTeamSelectableCodeSignGroupFilter(teamID))
log.Debugf("\nGroups after filtering for team ID:")
for _, group := range codeSignGroups {
log.Debugf(group.String())
}
}
if !archive.Application.ProvisioningProfile.IsXcodeManaged() {
log.Warnf("App was signed with NON xcode managed profile when archiving,\n" +
"only NOT xcode managed profiles are allowed to sign when exporting the archive.\n" +
"Removing xcode managed CodeSignInfo groups")
codeSignGroups = export.FilterSelectableCodeSignGroups(codeSignGroups, export.CreateNotXcodeManagedSelectableCodeSignGroupFilter())
log.Debugf("\nGroups after filtering for NOT Xcode managed profiles:")
for _, group := range codeSignGroups {
log.Debugf(group.String())
}
}
defaultProfileURL := os.Getenv("BITRISE_DEFAULT_PROVISION_URL")
if teamID == "" && defaultProfileURL != "" {
if defaultProfile, err := getDefaultProvisioningProfile(); err == nil {
log.Debugf("\ndefault profile: %v\n", defaultProfile)
filteredCodeSignGroups := export.FilterSelectableCodeSignGroups(codeSignGroups,
export.CreateExcludeProfileNameSelectableCodeSignGroupFilter(defaultProfile.Name))
if len(filteredCodeSignGroups) > 0 {
codeSignGroups = filteredCodeSignGroups
log.Debugf("\nGroups after removing default profile:")
for _, group := range codeSignGroups {
log.Debugf(group.String())
}
}
}
}
var iosCodeSignGroups []export.IosCodeSignGroup
for _, selectable := range codeSignGroups {
bundleIDProfileMap := map[string]profileutil.ProvisioningProfileInfoModel{}
for bundleID, profiles := range selectable.BundleIDProfilesMap {
if len(profiles) > 0 {
bundleIDProfileMap[bundleID] = profiles[0]
} else {
log.Warnf("No profile available to sign (%s) target!", bundleID)
}
}
iosCodeSignGroups = append(iosCodeSignGroups, *export.NewIOSGroup(selectable.Certificate, bundleIDProfileMap))
}
log.Debugf("\nFiltered groups:")
for i, group := range iosCodeSignGroups {
log.Debugf("Group #%d:", i)
for bundleID, profile := range group.BundleIDProfileMap() {
log.Debugf(" - %s: %s (%s)", bundleID, profile.Name, profile.UUID)
}
}
if len(iosCodeSignGroups) > 0 {
codeSignGroup := export.IosCodeSignGroup{}
if len(iosCodeSignGroups) >= 1 {
codeSignGroup = iosCodeSignGroups[0]
}
if len(iosCodeSignGroups) > 1 {
log.Warnf("Multiple code signing groups found! Using the first code signing group")
}
exportTeamID = codeSignGroup.Certificate().TeamID
exportCodeSignIdentity = codeSignGroup.Certificate().CommonName
for bundleID, profileInfo := range codeSignGroup.BundleIDProfileMap() {
exportProfileMapping[bundleID] = profileInfo.Name
isXcodeManaged := profileutil.IsXcodeManaged(profileInfo.Name)
if isXcodeManaged {
if exportCodeSignStyle != "" && exportCodeSignStyle != "automatic" {
log.Errorf("Both xcode managed and NON xcode managed profiles in code signing group")
}
exportCodeSignStyle = "automatic"
} else {
if exportCodeSignStyle != "" && exportCodeSignStyle != "manual" {
log.Errorf("Both xcode managed and NON xcode managed profiles in code signing group")
}
exportCodeSignStyle = "manual"
}
}
} else {
log.Errorf("Failed to find Codesign Groups")
}
}
var exportOpts exportoptions.ExportOptions
if exportMethod == exportoptions.MethodAppStore {
options := exportoptions.NewAppStoreOptions()
options.UploadBitcode = uploadBitcode
if xcodebuildMajorVersion >= 9 {
options.BundleIDProvisioningProfileMapping = exportProfileMapping
options.SigningCertificate = exportCodeSignIdentity
options.TeamID = exportTeamID
if archive.Application.ProvisioningProfile.IsXcodeManaged() && exportCodeSignStyle == "manual" {
log.Warnf("App was signed with xcode managed profile when archiving,")
log.Warnf("ipa export uses manual code singing.")
log.Warnf(`Setting "signingStyle" to "manual"`)
options.SigningStyle = "manual"
}
}
if xcodebuildMajorVersion >= 13 {
log.Debugf("Setting flag for managing app version and build number")
options.ManageAppVersion = manageVersionAndBuildNumber
}
exportOpts = options
} else {
options := exportoptions.NewNonAppStoreOptions(exportMethod)
options.CompileBitcode = compileBitcode
if xcodebuildMajorVersion >= 12 {
options.DistributionBundleIdentifier = productBundleID
}
if xcodebuildMajorVersion >= 9 {
options.BundleIDProvisioningProfileMapping = exportProfileMapping
options.SigningCertificate = exportCodeSignIdentity
options.TeamID = exportTeamID
if archive.Application.ProvisioningProfile.IsXcodeManaged() && exportCodeSignStyle == "manual" {
log.Warnf("App was signed with xcode managed profile when archiving,")
log.Warnf("ipa export uses manual code singing.")
log.Warnf(`Setting "signingStyle" to "manual"`)
options.SigningStyle = "manual"
}
}
exportOpts = options
}
return exportOpts.String()
}
func getDefaultProvisioningProfile() (profileutil.ProvisioningProfileInfoModel, error) {
defaultProfileURL := os.Getenv("BITRISE_DEFAULT_PROVISION_URL")
if defaultProfileURL == "" {
return profileutil.ProvisioningProfileInfoModel{}, nil
}
tmpDir, err := pathutil.NormalizedOSTempDirPath("tmp_default_profile")
if err != nil {
return profileutil.ProvisioningProfileInfoModel{}, err
}
tmpDst := filepath.Join(tmpDir, "default.mobileprovision")
tmpDstFile, err := os.Create(tmpDst)
if err != nil {
return profileutil.ProvisioningProfileInfoModel{}, err
}
defer func() {
if err := tmpDstFile.Close(); err != nil {
log.Errorf("Failed to close file (%s), error: %s", tmpDst, err)
}
}()
response, err := http.Get(defaultProfileURL)
if err != nil {
return profileutil.ProvisioningProfileInfoModel{}, err
}
defer func() {
if err := response.Body.Close(); err != nil {
log.Errorf("Failed to close response body, error: %s", err)
}
}()
if _, err := io.Copy(tmpDstFile, response.Body); err != nil {
return profileutil.ProvisioningProfileInfoModel{}, err
}
defaultProfile, err := profileutil.NewProvisioningProfileInfoFromFile(tmpDst)
if err != nil {
return profileutil.ProvisioningProfileInfoModel{}, err
}
return defaultProfile, nil
}