-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_sign_mapping.go
118 lines (96 loc) · 4.16 KB
/
code_sign_mapping.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
package utils
import (
"fmt"
"sort"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-xcode/certificateutil"
"github.com/bitrise-io/go-xcode/exportoptions"
"github.com/bitrise-io/go-xcode/profileutil"
"github.com/ryanuber/go-glob"
)
// CodeSignGroupItem ...
type CodeSignGroupItem struct {
Certificate certificateutil.CertificateInfoModel
BundleIDProfileMap map[string]profileutil.ProvisioningProfileInfoModel
}
func isCertificateInstalled(installedCertificates []certificateutil.CertificateInfoModel, certificate certificateutil.CertificateInfoModel) bool {
installed := false
for _, installedCertificate := range installedCertificates {
if certificate.Serial == installedCertificate.Serial {
installed = true
break
}
}
if installed {
log.Printf("certificate: %s [%s] is installed", certificate.CommonName, certificate.Serial)
}
return installed
}
func createCertificateProfilesMapping(profiles []profileutil.ProvisioningProfileInfoModel, certificates []certificateutil.CertificateInfoModel) map[string][]profileutil.ProvisioningProfileInfoModel {
createCertificateProfilesMap := map[string][]profileutil.ProvisioningProfileInfoModel{}
for _, profile := range profiles {
for _, embeddedCert := range profile.DeveloperCertificates {
if !isCertificateInstalled(certificates, embeddedCert) {
continue
}
if _, ok := createCertificateProfilesMap[embeddedCert.Serial]; !ok {
createCertificateProfilesMap[embeddedCert.Serial] = []profileutil.ProvisioningProfileInfoModel{}
}
createCertificateProfilesMap[embeddedCert.Serial] = append(createCertificateProfilesMap[embeddedCert.Serial], profile)
}
}
fmt.Println()
for subject, profiles := range createCertificateProfilesMap {
log.Printf("certificate: %s included in profiles:", subject)
for _, profile := range profiles {
log.Printf("- %s", profile.Name)
}
fmt.Println()
}
return createCertificateProfilesMap
}
func createCodeSignGroups(profileGroups map[string][]profileutil.ProvisioningProfileInfoModel, bundleIDs []string, exportMethod exportoptions.Method) []CodeSignGroupItem {
filteredCodeSignGroupItems := []CodeSignGroupItem{}
for groupItemCertificateSerial, profiles := range profileGroups {
log.Printf("checking certificate (%s) group:", groupItemCertificateSerial)
sort.Sort(ByBundleIDLength(profiles))
bundleIDProfileMap := map[string]profileutil.ProvisioningProfileInfoModel{}
for _, bundleID := range bundleIDs {
for _, profile := range profiles {
if profile.ExportType != exportMethod {
log.Printf("profile (%s) export method (%s) is not the desired (%s)", profile.Name, profile.ExportType, exportMethod)
continue
}
if !glob.Glob(profile.BundleID, bundleID) {
log.Printf("profile (%s) does not provision bundle id: %s", profile.Name, profile.BundleID)
continue
}
log.Printf("profile (%s) MATCHES for bundle id (%s) and export method (%s)", profile.Name, bundleID, exportMethod)
bundleIDProfileMap[bundleID] = profile
break
}
}
log.Printf("matching profiles: %d should be: %d", len(bundleIDProfileMap), len(bundleIDs))
fmt.Println()
if len(bundleIDProfileMap) == len(bundleIDs) {
groupItemCertificate := certificateutil.CertificateInfoModel{}
for _, profile := range bundleIDProfileMap {
for _, certificate := range profile.DeveloperCertificates {
if groupItemCertificateSerial == certificate.Serial {
groupItemCertificate = certificate
}
}
}
filteredCodeSignGroupItems = append(filteredCodeSignGroupItems, CodeSignGroupItem{Certificate: groupItemCertificate, BundleIDProfileMap: bundleIDProfileMap})
}
}
return filteredCodeSignGroupItems
}
// ResolveCodeSignGroupItems ...
func ResolveCodeSignGroupItems(bundleIDs []string, exportMethod exportoptions.Method, profiles []profileutil.ProvisioningProfileInfoModel, certificates []certificateutil.CertificateInfoModel) []CodeSignGroupItem {
log.Printf("Creating certificate profiles mapping...")
certificateProfilesMapping := createCertificateProfilesMapping(profiles, certificates)
log.Printf("Creating CodeSignGroups...")
groups := createCodeSignGroups(certificateProfilesMapping, bundleIDs, exportMethod)
return groups
}