11package codesigndoc
22
33import (
4+ "errors"
45 "fmt"
56 "os"
67
7- "github.com/bitrise-io/go-utils/colorstring"
8-
98 "github.com/bitrise-io/go-utils/command"
109 "github.com/bitrise-io/go-utils/log"
1110 "github.com/bitrise-io/goinp/goinp"
1211 "github.com/bitrise-tools/codesigndoc/bitriseio"
1312 "github.com/bitrise-tools/go-xcode/certificateutil"
13+ "github.com/bitrise-tools/go-xcode/export"
1414 "github.com/bitrise-tools/go-xcode/profileutil"
1515 "github.com/bitrise-tools/go-xcode/xcarchive"
1616)
@@ -19,72 +19,63 @@ const collectCodesigningFilesInfo = `To collect available code sign files, we se
1919- which has installed Codesign Identity in your Keychain"
2020- which can provision your application target's bundle ids"
2121- which has the project defined Capabilities set"
22- - which matches to the selected ipa export method"
22+ - which matches to the selected export method"
2323`
2424
2525// ExportCodesignFiles exports the codesigning files required to create an xcode archive
2626// and exports the codesigning files for the specified export method
2727func ExportCodesignFiles (archivePath , outputDirPath string , certificatesOnly bool , askForPassword bool ) (bool , bool , error ) {
28- certificates , err := installedCertificates (IOSCertificate )
28+ // Find out the XcArchive type
29+ isMacOs , err := xcarchive .IsMacOS (archivePath )
2930 if err != nil {
30- return false , false , fmt . Errorf ( "failed to list installed code signing identities, error: %s" , err )
31+ return false , false , err
3132 }
3233
33- profiles , err := profileutil .InstalledProvisioningProfileInfos (profileutil .ProfileTypeIos )
34- if err != nil {
35- return false , false , fmt .Errorf ("failed to list installed provisioning profiles, error: %s" , err )
34+ // Set up the XcArchive type for certs and profiles.
35+ certificateType := IOSCertificate
36+ profileType := profileutil .ProfileTypeIos
37+ if isMacOs {
38+ certificateType = MacOSCertificate
39+ profileType = profileutil .ProfileTypeMacOs
3640 }
3741
38- // archive code sign settings
39- archive , err := xcarchive . NewIosArchive ( archivePath )
42+ // Certificates
43+ certificates , err := installedCertificates ( certificateType )
4044 if err != nil {
41- return false , false , fmt .Errorf ("failed to analyze archive , error: %s" , err )
45+ return false , false , fmt .Errorf ("failed to list installed code signing identities , error: %s" , err )
4246 }
4347
44- archiveCodeSignGroup , err := analyzeArchive ( archive , certificates )
48+ installerCertificates , err := certificateutil . InstalledInstallerCertificateInfos ( )
4549 if err != nil {
46- return false , false , fmt .Errorf ("failed to analyze the archive , error: %s" , err )
50+ return false , false , fmt .Errorf ("failed to list installed code signing identities , error: %s" , err )
4751 }
4852
49- fmt .Println ()
50- log .Infof ("Codesign settings used for archive:" )
51- fmt .Println ()
52- printCodesignGroup (archiveCodeSignGroup )
53-
54- // ipa export code sign settings
55- fmt .Println ()
56- fmt .Println ()
57- log .Printf ("🔦 Analyzing the archive, to get ipa export code signing settings..." )
58-
59- certificatesToExport := []certificateutil.CertificateInfoModel {}
60- profilesToExport := []profileutil.ProvisioningProfileInfoModel {}
61-
62- if certificatesOnly {
63- ipaExportCertificate , err := collectIpaExportCertificate (archiveCodeSignGroup .Certificate , certificates )
64- if err != nil {
65- return false , false , err
66- }
53+ log .Debugf ("Installed certificates:" )
54+ for _ , installedCertificate := range certificates {
55+ log .Debugf (installedCertificate .String ())
56+ }
6757
68- certificatesToExport = append (certificatesToExport , archiveCodeSignGroup .Certificate , ipaExportCertificate )
69- } else {
70- ipaExportCodeSignGroups , err := collectIpaExportCodeSignGroups (archive , certificates , profiles )
71- if err != nil {
72- return false , false , err
73- }
58+ // Profiles
59+ profiles , err := profileutil .InstalledProvisioningProfileInfos (profileType )
60+ if err != nil {
61+ return false , false , fmt .Errorf ("failed to list installed provisioning profiles, error: %s" , err )
62+ }
7463
75- if len (ipaExportCodeSignGroups ) == 0 {
76- errorString := "\n 🚨 " + colorstring .Red ("Failed to collect codesigning files for the selected distribution type.\n " ) +
77- colorstring .Yellow ("Export an ipa with the same export method which code signing files you want to collect (e.g app-store if you want to collect the code signing files for app-store distribution) in your local xcode and run codesigndoc again.\n " ) +
78- colorstring .Yellow ("If the tool fails please report the issue with the codesigndoc log and the local ipa exportOptions.plist" )
79- return false , false , fmt .Errorf (errorString )
80- }
64+ log .Debugf ("Installed profiles:" )
65+ for _ , profileInfo := range profiles {
66+ log .Debugf (profileInfo .String (certificates ... ))
67+ }
8168
82- codeSignGroups := append (ipaExportCodeSignGroups , archiveCodeSignGroup )
83- certificates , profiles := extractCertificatesAndProfiles (codeSignGroups ... )
84- certificatesToExport = append (certificatesToExport , certificates ... )
85- profilesToExport = append (profilesToExport , profiles ... )
69+ certificatesToExport , profilesToExport , err := getFilesToExport (archivePath , certificates , installerCertificates , profiles , certificatesOnly )
70+ if err != nil {
71+ return false , false , err
8672 }
8773
74+ // export code sign settings
75+ fmt .Println ()
76+ fmt .Println ()
77+ log .Printf ("🔦 Analyzing the archive, to get export code signing settings..." )
78+
8879 if err := collectAndExportIdentities (certificatesToExport , outputDirPath , askForPassword ); err != nil {
8980 return false , false , err
9081 }
@@ -122,3 +113,87 @@ func ExportCodesignFiles(archivePath, outputDirPath string, certificatesOnly boo
122113
123114 return certsUploaded , provProfilesUploaded , nil
124115}
116+
117+ func getFilesToExport (archivePath string , installedCertificates []certificateutil.CertificateInfoModel , installedInstallerCertificates []certificateutil.CertificateInfoModel , installedProfiles []profileutil.ProvisioningProfileInfoModel , certificatesOnly bool ) ([]certificateutil.CertificateInfoModel , []profileutil.ProvisioningProfileInfoModel , error ) {
118+ macOS , err := xcarchive .IsMacOS (archivePath )
119+ if err != nil {
120+ return nil , nil , err
121+ }
122+
123+ var certificate certificateutil.CertificateInfoModel
124+ var archive Archive
125+ var achiveCodeSignGroup export.CodeSignGroup
126+
127+ if macOS {
128+ archive , achiveCodeSignGroup , err = getMacOSCodeSignGroup (archivePath , installedCertificates )
129+ if err != nil {
130+ return nil , nil , err
131+ }
132+ certificate = achiveCodeSignGroup .Certificate ()
133+ } else {
134+ archive , achiveCodeSignGroup , err = getIOSCodeSignGroup (archivePath , installedCertificates )
135+ if err != nil {
136+ return nil , nil , err
137+ }
138+ certificate = achiveCodeSignGroup .Certificate ()
139+ }
140+
141+ certificatesToExport := []certificateutil.CertificateInfoModel {}
142+ profilesToExport := []profileutil.ProvisioningProfileInfoModel {}
143+
144+ if certificatesOnly {
145+ exportCertificate , err := collectExportCertificate (macOS , certificate , installedCertificates , installedInstallerCertificates )
146+ if err != nil {
147+ return nil , nil , err
148+ }
149+
150+ certificatesToExport = append (certificatesToExport , certificate )
151+ certificatesToExport = append (certificatesToExport , exportCertificate ... )
152+ } else {
153+ certificatesToExport , profilesToExport , err = collectCertificatesAndProfiles (archive , certificate , installedCertificates , installedProfiles , certificatesToExport , profilesToExport , achiveCodeSignGroup )
154+ if err != nil {
155+ return nil , nil , err
156+ }
157+ }
158+
159+ return certificatesToExport , profilesToExport , nil
160+ }
161+
162+ func collectCertificatesAndProfiles (archive Archive , certificate certificateutil.CertificateInfoModel ,
163+ installedCertificates []certificateutil.CertificateInfoModel , installedProfiles []profileutil.ProvisioningProfileInfoModel ,
164+ certificatesToExport []certificateutil.CertificateInfoModel , profilesToExport []profileutil.ProvisioningProfileInfoModel ,
165+ achiveCodeSignGroup export.CodeSignGroup ) ([]certificateutil.CertificateInfoModel , []profileutil.ProvisioningProfileInfoModel , error ) {
166+
167+ _ , macOS := archive .(xcarchive.MacosArchive )
168+
169+ groups , err := collectExportCodeSignGroups (archive , installedCertificates , installedProfiles )
170+ if err != nil {
171+ return nil , nil , err
172+ }
173+
174+ var exportCodeSignGroups []export.CodeSignGroup
175+ for _ , group := range groups {
176+ if macOS {
177+ exportCodeSignGroup , ok := group .(* export.MacCodeSignGroup )
178+ if ok {
179+ exportCodeSignGroups = append (exportCodeSignGroups , exportCodeSignGroup )
180+ }
181+ } else {
182+ exportCodeSignGroup , ok := group .(* export.IosCodeSignGroup )
183+ if ok {
184+ exportCodeSignGroups = append (exportCodeSignGroups , exportCodeSignGroup )
185+ }
186+ }
187+ }
188+
189+ if len (exportCodeSignGroups ) == 0 {
190+ return nil , nil , errors .New ("no export code sign groups collected" )
191+ }
192+
193+ codeSignGroups := append (exportCodeSignGroups , achiveCodeSignGroup )
194+ certificates , profiles := extractCertificatesAndProfiles (codeSignGroups ... )
195+ certificatesToExport = append (certificatesToExport , certificates ... )
196+ profilesToExport = append (profilesToExport , profiles ... )
197+
198+ return certificatesToExport , profilesToExport , nil
199+ }
0 commit comments