Skip to content

Commit 55553b0

Browse files
authored
Exported functions (#105)
* Create exported functions to use codesigndoc from package * Move to function ExportCodesigningFiles export logic * Improve logging * Move log file writing to a closure * Rename GenerateXcodeArchive to BuildXcodeArchive
1 parent c82fde6 commit 55553b0

File tree

5 files changed

+153
-97
lines changed

5 files changed

+153
-97
lines changed

cmd/xamarin.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,34 +161,31 @@ func scanXamarinProject(cmd *cobra.Command, args []string) error {
161161
fmt.Println()
162162
fmt.Println()
163163
log.Printf(`🔦 Running a Build, to get all the required code signing settings...`)
164-
var isLogFileWritten bool
165164
logOutputFilePath := filepath.Join(absExportOutputDirPath, "xamarin-build-output.log")
166165

167166
archivePath, logOutput, err := xamarinCmd.GenerateArchive()
168-
if writeFiles == codesign.WriteFilesAlways ||
169-
writeFiles == codesign.WriteFilesFallback && err != nil { // save the xamarin output into a debug log file
167+
if writeFiles == codesign.WriteFilesAlways || writeFiles == codesign.WriteFilesFallback && err != nil { // save the xamarin output into a debug log file
170168
if err := os.MkdirAll(absExportOutputDirPath, 0700); err != nil {
171169
return fmt.Errorf("failed to create output directory, error: %s", err)
172170
}
171+
173172
log.Infof("💡 "+colorstring.Yellow("Saving xamarin output into file")+": %s", logOutputFilePath)
174173
if err := fileutil.WriteStringToFile(logOutputFilePath, logOutput); err != nil {
175174
log.Errorf("Failed to save xamarin build output into file (%s), error: %s", logOutputFilePath, err)
176-
} else {
177-
isLogFileWritten = true
178175
}
179176
}
180177
if err != nil {
181-
log.Warnf("Last lines of build log:")
178+
log.Warnf("Last lines of the build log:")
182179
fmt.Println(stringutil.LastNLines(logOutput, 15))
180+
181+
log.Infof(colorstring.Yellow("Please check the build log to see what caused the error."))
183182
fmt.Println()
184-
if isLogFileWritten {
185-
log.Warnf("Please check the logfile (%s) to see what caused the error", logOutputFilePath)
186-
log.Warnf(`and make sure that you can "Archive for Publishing" this project from Xamarin!`)
187-
fmt.Println()
188-
log.Infof("Open the project: %s", xamarinCmd.SolutionFilePath)
189-
log.Infof(`And do "Archive for Publishing", after selecting the Configuration+Platform: %s|%s`, xamarinCmd.Configuration, xamarinCmd.Platform)
190-
fmt.Println()
191-
}
183+
184+
log.Errorf("Build failed.")
185+
log.Infof(colorstring.Yellow("Open the project: ")+"%s", xamarinCmd.SolutionFilePath)
186+
log.Infof(colorstring.Yellow(`And do "Archive for Publishing", after selecting the Configuration+Platform: `)+"%s|%s", xamarinCmd.Configuration, xamarinCmd.Platform)
187+
fmt.Println()
188+
192189
return ArchiveError{toolXamarin, "failed to run xamarin build command: " + err.Error()}
193190
}
194191

@@ -197,9 +194,14 @@ func scanXamarinProject(cmd *cobra.Command, args []string) error {
197194
if err != nil {
198195
return err
199196
}
200-
exoprtResult, err := codesign.UploadAndWriteCodesignFiles(certificatesToExport,
201-
profilesToExport,
202-
isAskForPassword,
197+
198+
certificates, profiles, err := codesign.ExportCodesigningFiles(certificatesToExport, profilesToExport, isAskForPassword)
199+
if err != nil {
200+
return err
201+
}
202+
203+
exportResult, err := codesign.UploadAndWriteCodesignFiles(certificates,
204+
profiles,
203205
codesign.WriteFilesConfig{
204206
WriteFiles: writeFiles,
205207
AbsOutputDirPath: absExportOutputDirPath,
@@ -212,6 +214,6 @@ func scanXamarinProject(cmd *cobra.Command, args []string) error {
212214
return err
213215
}
214216

215-
printFinished(exoprtResult, absExportOutputDirPath)
217+
printFinished(exportResult, absExportOutputDirPath)
216218
return nil
217219
}

cmd/xcode.go

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
"github.com/bitrise-io/go-utils/fileutil"
1414
"github.com/bitrise-io/go-utils/log"
1515
"github.com/bitrise-io/go-utils/pathutil"
16-
"github.com/bitrise-io/go-utils/stringutil"
17-
"github.com/bitrise-io/go-xcode/utility"
1816
"github.com/bitrise-io/goinp/goinp"
1917
"github.com/spf13/cobra"
2018
)
@@ -54,29 +52,19 @@ func absOutputDir() (string, error) {
5452
return absExportOutputDirPath, nil
5553
}
5654

57-
func scanXcodeProject(cmd *cobra.Command, args []string) error {
55+
func scanXcodeProject(_ *cobra.Command, _ []string) error {
5856
absExportOutputDirPath, err := absOutputDir()
5957
if err != nil {
6058
return err
6159
}
6260

63-
// Output tools versions
64-
xcodebuildVersion, err := utility.GetXcodeVersion()
65-
if err != nil {
66-
return fmt.Errorf("failed to get Xcode (xcodebuild) version, error: %s", err)
67-
}
68-
fmt.Println()
69-
log.Infof("%s: %s (%s)", colorstring.Green("Xcode (xcodebuild) version"), xcodebuildVersion.Version, xcodebuildVersion.BuildVersion)
70-
fmt.Println()
71-
7261
xcodeCmd := xcode.CommandModel{}
7362

7463
projectPath := paramXcodeProjectFilePath
7564
if projectPath == "" {
7665
log.Infof("Scan the directory for project files")
7766
log.Warnf("You can specify the Xcode project/workscape file to scan with the --file flag.")
7867

79-
//
8068
// Scan the directory for Xcode Project (.xcworkspace / .xcodeproject) file first
8169
// If can't find any, ask the user to drag-and-drop the file
8270
projpth, err := findXcodeProject()
@@ -120,47 +108,33 @@ func scanXcodeProject(cmd *cobra.Command, args []string) error {
120108
xcodeCmd.SDK = paramXcodebuildSDK
121109
}
122110

123-
fmt.Println()
124-
log.Printf("🔦 Running an Xcode Archive, to get all the required code signing settings...")
125-
var isLogFileWritten bool
126-
xcodebuildOutputFilePath := filepath.Join(absExportOutputDirPath, "xcodebuild-output.log")
127-
archivePath, xcodebuildOutput, err := xcodeCmd.GenerateArchive()
111+
writeBuildLogs := func(xcodebuildOutput string) error {
112+
if writeFiles == codesign.WriteFilesAlways || writeFiles == codesign.WriteFilesFallback && err != nil { // save the xcodebuild output into a debug log file
113+
xcodebuildOutputFilePath := filepath.Join(absExportOutputDirPath, "xcodebuild-output.log")
114+
if err := os.MkdirAll(absExportOutputDirPath, 0700); err != nil {
115+
return fmt.Errorf("failed to create output directory, error: %s", err)
116+
}
128117

129-
if writeFiles == codesign.WriteFilesAlways ||
130-
writeFiles == codesign.WriteFilesFallback && err != nil { // save the xcodebuild output into a debug log file
131-
if err := os.MkdirAll(absExportOutputDirPath, 0700); err != nil {
132-
return fmt.Errorf("failed to create output directory, error: %s", err)
133-
}
134-
log.Infof("💡 "+colorstring.Yellow("Saving xcodebuild output into file")+": %s", xcodebuildOutputFilePath)
135-
if err := fileutil.WriteStringToFile(xcodebuildOutputFilePath, xcodebuildOutput); err != nil {
136-
log.Errorf("Failed to save xcodebuild output into file (%s), error: %s", xcodebuildOutputFilePath, err)
137-
} else {
138-
isLogFileWritten = true
118+
log.Infof("💡 "+colorstring.Yellow("Saving xcodebuild output into file")+": %s", xcodebuildOutputFilePath)
119+
if err := fileutil.WriteStringToFile(xcodebuildOutputFilePath, xcodebuildOutput); err != nil {
120+
return fmt.Errorf("Failed to save xcodebuild output into file (%s), error: %s", xcodebuildOutputFilePath, err)
121+
}
139122
}
123+
return nil
140124
}
125+
126+
archivePath, err := codesigndoc.BuildXcodeArchive(xcodeCmd, writeBuildLogs)
141127
if err != nil {
142-
log.Warnf("Last lines of build log:")
143-
fmt.Println(stringutil.LastNLines(xcodebuildOutput, 15))
144-
fmt.Println()
145-
if isLogFileWritten {
146-
log.Warnf("Please check the logfile (%s) to see what caused the error", xcodebuildOutputFilePath)
147-
log.Warnf("and make sure that you can Archive this project from Xcode!")
148-
fmt.Println()
149-
log.Printf("Open the project: %s", xcodeCmd.ProjectFilePath)
150-
log.Printf("and Archive, using the Scheme: %s", xcodeCmd.Scheme)
151-
fmt.Println()
152-
}
153128
return ArchiveError{toolXcode, err.Error()}
154129
}
155130

156-
// If certificatesOnly is set, CollectCodesignFiles returns an empty slice for profiles
157-
certificatesToExport, profilesToExport, err := codesigndoc.CollectCodesignFiles(archivePath, certificatesOnly)
131+
certificates, profiles, err := codesigndoc.CodesigningFilesForXCodeProject(archivePath, certificatesOnly, isAskForPassword)
158132
if err != nil {
159133
return err
160134
}
161-
exoprtResult, err := codesign.UploadAndWriteCodesignFiles(certificatesToExport,
162-
profilesToExport,
163-
isAskForPassword,
135+
136+
exportResult, err := codesign.UploadAndWriteCodesignFiles(certificates,
137+
profiles,
164138
codesign.WriteFilesConfig{
165139
WriteFiles: writeFiles,
166140
AbsOutputDirPath: absExportOutputDirPath,
@@ -173,6 +147,6 @@ func scanXcodeProject(cmd *cobra.Command, args []string) error {
173147
return err
174148
}
175149

176-
printFinished(exoprtResult, absExportOutputDirPath)
150+
printFinished(exportResult, absExportOutputDirPath)
177151
return nil
178152
}

cmd/xcodeUITests.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,35 +114,34 @@ func scanXcodeUITestsProject(cmd *cobra.Command, args []string) error {
114114
xcodeUITestsCmd.SDK = paramXcodebuildSDK
115115
}
116116

117+
fmt.Println()
117118
fmt.Println()
118119
log.Printf("🔦 Running an Xcode build-for-testing, to get all the required code signing settings...")
119-
var isLogFileWritten bool
120120
xcodebuildOutputFilePath := filepath.Join(absExportOutputDirPath, "xcodebuild-output.log")
121121

122122
buildForTestingPath, xcodebuildOutput, err := xcodeUITestsCmd.RunBuildForTesting()
123-
if writeFiles == codesign.WriteFilesAlways ||
124-
writeFiles == codesign.WriteFilesFallback && err != nil { // save the xcodebuild output into a debug log file
123+
if writeFiles == codesign.WriteFilesAlways || writeFiles == codesign.WriteFilesFallback && err != nil { // save the xcodebuild output into a debug log file
125124
if err := os.MkdirAll(absExportOutputDirPath, 0700); err != nil {
126125
return fmt.Errorf("failed to create output directory, error: %s", err)
127126
}
127+
128128
log.Infof("💡 "+colorstring.Yellow("Saving xcodebuild output into file")+": %s", xcodebuildOutputFilePath)
129129
if err := fileutil.WriteStringToFile(xcodebuildOutputFilePath, xcodebuildOutput); err != nil {
130130
log.Errorf("Failed to save xcodebuild output into file (%s), error: %s", xcodebuildOutputFilePath, err)
131-
} else {
132-
isLogFileWritten = true
133131
}
134132
}
135133
if err != nil {
136-
log.Warnf("Last lines of build log:")
134+
log.Warnf("Last lines of the build log:")
137135
fmt.Println(stringutil.LastNLines(xcodebuildOutput, 15))
136+
137+
log.Infof(colorstring.Yellow("Please check the build log to see what caused the error."))
138138
fmt.Println()
139-
if isLogFileWritten {
140-
log.Warnf("Please check the logfile (%s) to see what caused the error", xcodebuildOutputFilePath)
141-
log.Warnf("and make sure that you can run Build for testing against the project from Xcode!")
142-
fmt.Println()
143-
log.Printf("Open the project: %s", xcodeUITestsCmd.ProjectFilePath)
144-
fmt.Println()
145-
}
139+
140+
log.Errorf("Xcode Build For Testing failed.")
141+
log.Infof(colorstring.Yellow("Open the project: ")+"%s", xcodeUITestsCmd.ProjectFilePath)
142+
log.Infof(colorstring.Yellow("and make sure that you can run Build For Testing, with the scheme: ")+"%s", xcodeUITestsCmd.Scheme)
143+
fmt.Println()
144+
146145
return BuildForTestingError{toolXcode, err.Error()}
147146
}
148147

@@ -151,9 +150,14 @@ func scanXcodeUITestsProject(cmd *cobra.Command, args []string) error {
151150
if err != nil {
152151
return err
153152
}
154-
exportResult, err := codesign.UploadAndWriteCodesignFiles(certificatesToExport,
155-
profilesToExport,
156-
isAskForPassword,
153+
154+
certificates, profiles, err := codesign.ExportCodesigningFiles(certificatesToExport, profilesToExport, isAskForPassword)
155+
if err != nil {
156+
return err
157+
}
158+
159+
exportResult, err := codesign.UploadAndWriteCodesignFiles(certificates,
160+
profiles,
157161
codesign.WriteFilesConfig{
158162
WriteFiles: writeFiles,
159163
AbsOutputDirPath: absExportOutputDirPath,

codesign/export.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package codesign
22

33
import (
4-
"bytes"
54
"errors"
65
"fmt"
76
"io/ioutil"
@@ -53,38 +52,49 @@ type ExportReport struct {
5352
CodesignFilesWritten bool
5453
}
5554

56-
// UploadAndWriteCodesignFiles exports then uploads codesign files to bitrise.io and saves them to output folder
57-
func UploadAndWriteCodesignFiles(certificates []certificateutil.CertificateInfoModel, profiles []profileutil.ProvisioningProfileInfoModel, askForPassword bool, writeFilesConfig WriteFilesConfig, uploadConfig UploadConfig) (ExportReport, error) {
58-
identities, err := collectAndExportIdentities(certificates, askForPassword)
55+
// ExportCodesigningFiles exports certificates from the Keychain and provisoining profiles from their directory
56+
func ExportCodesigningFiles(certificatesRequired []certificateutil.CertificateInfoModel, profilesRequired []profileutil.ProvisioningProfileInfoModel, askForPassword bool) (models.Certificates, []models.ProvisioningProfile, error) {
57+
certificates, err := exportIdentities(certificatesRequired, askForPassword)
5958
if err != nil {
60-
return ExportReport{}, err
59+
return models.Certificates{}, nil, err
6160
}
62-
provisioningProfiles, err := collectAndExportProvisioningProfiles(profiles)
61+
62+
profiles, err := exportProvisioningProfiles(profilesRequired)
6363
if err != nil {
64-
return ExportReport{}, err
64+
return models.Certificates{}, nil, err
6565
}
6666

67+
return certificates, profiles, nil
68+
}
69+
70+
// UploadAndWriteCodesignFiles exports then uploads codesign files to bitrise.io and saves them to output folder
71+
func UploadAndWriteCodesignFiles(certificates models.Certificates, provisioningProfiles []models.ProvisioningProfile, writeFilesConfig WriteFilesConfig, uploadConfig UploadConfig) (ExportReport, error) {
6772
var client *bitrise.Client
6873
// both or none CLI flags are required
6974
if uploadConfig.PersonalAccessToken != "" && uploadConfig.AppSlug != "" {
7075
// Upload automatically if token is provided as CLI paramter, do not export to filesystem
7176
// Used to upload artifacts as part of an other CLI tool
77+
var err error
7278
client, err = bitrise.NewClient(uploadConfig.PersonalAccessToken)
7379
if err != nil {
7480
return ExportReport{}, err
7581
}
82+
7683
client.SetSelectedAppSlug(uploadConfig.AppSlug)
7784
}
85+
7886
if client == nil {
7987
uploadConfirmMsg := "Do you want to upload the provisioning profiles and certificates to Bitrise?"
8088
if len(provisioningProfiles) == 0 {
8189
uploadConfirmMsg = "Do you want to upload the certificates to Bitrise?"
8290
}
8391
fmt.Println()
92+
8493
shouldUpload, err := goinp.AskForBoolFromReader(uploadConfirmMsg, os.Stdin)
8594
if err != nil {
8695
return ExportReport{}, err
8796
}
97+
8898
if shouldUpload {
8999
if client, err = bitriseio.GetInteractiveConfigClient(); err != nil {
90100
return ExportReport{}, err
@@ -95,21 +105,21 @@ func UploadAndWriteCodesignFiles(certificates []certificateutil.CertificateInfoM
95105
var filesWritten bool
96106
if writeFilesConfig.WriteFiles == WriteFilesAlways ||
97107
writeFilesConfig.WriteFiles == WriteFilesFallback && client == nil {
98-
if err := writeFiles(identities, provisioningProfiles, writeFilesConfig); err != nil {
108+
if err := writeFiles(certificates, provisioningProfiles, writeFilesConfig); err != nil {
99109
return ExportReport{}, err
100110
}
101111
filesWritten = true
102112
}
103113

104114
if client == nil {
105115
return ExportReport{
106-
CertificatesUploaded: len(certificates) == 0,
107-
ProvisioningProfilesUploaded: len(profiles) == 0,
116+
CertificatesUploaded: len(certificates.Info) == 0,
117+
ProvisioningProfilesUploaded: len(provisioningProfiles) == 0,
108118
CodesignFilesWritten: filesWritten,
109119
}, nil
110120
}
111121

112-
certificatesUploaded, profilesUploaded, err := bitriseio.UploadCodesigningFiles(client, identities, provisioningProfiles)
122+
certificatesUploaded, profilesUploaded, err := bitriseio.UploadCodesigningFiles(client, certificates, provisioningProfiles)
113123
return ExportReport{
114124
CertificatesUploaded: certificatesUploaded,
115125
ProvisioningProfilesUploaded: profilesUploaded,
@@ -147,8 +157,8 @@ func writeFiles(identities models.Certificates, provisioningProfiles []models.Pr
147157
return nil
148158
}
149159

150-
// collectAndExportIdentities exports the given certificates merged in a single .p12 file
151-
func collectAndExportIdentities(certificates []certificateutil.CertificateInfoModel, isAskForPassword bool) (models.Certificates, error) {
160+
// exportIdentities exports the given certificates merged in a single .p12 file
161+
func exportIdentities(certificates []certificateutil.CertificateInfoModel, isAskForPassword bool) (models.Certificates, error) {
152162
if len(certificates) == 0 {
153163
return models.Certificates{}, nil
154164
}
@@ -215,8 +225,8 @@ func writeIdentities(identites []byte, absExportOutputDirPath string) error {
215225
return ioutil.WriteFile(filepath.Join(absExportOutputDirPath, "Identities.p12"), identites, 0600)
216226
}
217227

218-
// collectAndExportProvisioningProfiles returns provisioning profies
219-
func collectAndExportProvisioningProfiles(profiles []profileutil.ProvisioningProfileInfoModel) ([]models.ProvisioningProfile, error) {
228+
// exportProvisioningProfiles returns provisioning profies
229+
func exportProvisioningProfiles(profiles []profileutil.ProvisioningProfileInfoModel) ([]models.ProvisioningProfile, error) {
220230
if len(profiles) == 0 {
221231
return nil, nil
222232
}
@@ -242,9 +252,6 @@ func collectAndExportProvisioningProfiles(profiles []profileutil.ProvisioningPro
242252
if err != nil {
243253
return nil, fmt.Errorf("failed to parse exported profile, error: %s", err)
244254
}
245-
if bytes.Compare(profile.Content(), exportedProfile.Content()) != 0 {
246-
return nil, fmt.Errorf("Profile found in the archive does not match found profile")
247-
}
248255

249256
contents, err := ioutil.ReadFile(pth)
250257
if err != nil {

0 commit comments

Comments
 (0)