-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
369 lines (295 loc) · 11.7 KB
/
main.go
File metadata and controls
369 lines (295 loc) · 11.7 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-xcode/exportoptions"
"github.com/bitrise-io/go-xcode/utility"
"github.com/bitrise-io/go-xcode/xcarchive"
"github.com/bitrise-io/go-xcode/xcodebuild"
"github.com/bitrise-steplib/steps-export-xcarchive-mac/utils"
)
const (
bitriseAppPathEnvKey = "BITRISE_APP_PATH"
bitrisePKGPathEnvKey = "BITRISE_PKG_PATH"
bitriseIDEDistributionLogsPthEnvKey = "BITRISE_IDEDISTRIBUTION_LOGS_PATH"
)
// ConfigsModel ...
type ConfigsModel struct {
ArchivePath string
ExportMethod string
UploadBitcode string
CompileBitcode string
TeamID string
CustomExportOptionsPlistContent string
UseLegacyExport string
LegacyExportProvisioningProfileName string
LegacyExportOutputFormat string
VerboseLog string
DeployDir string
}
func createConfigsModelFromEnvs() ConfigsModel {
return ConfigsModel{
ArchivePath: os.Getenv("archive_path"),
ExportMethod: os.Getenv("export_method"),
UploadBitcode: os.Getenv("upload_bitcode"),
CompileBitcode: os.Getenv("compile_bitcode"),
TeamID: os.Getenv("team_id"),
CustomExportOptionsPlistContent: os.Getenv("custom_export_options_plist_content"),
UseLegacyExport: os.Getenv("use_legacy_export"),
LegacyExportProvisioningProfileName: os.Getenv("legacy_export_provisioning_profile_name"),
LegacyExportOutputFormat: os.Getenv("legacy_export_output_format"),
DeployDir: os.Getenv("BITRISE_DEPLOY_DIR"),
VerboseLog: os.Getenv("verbose_log"),
}
}
func (configs ConfigsModel) print() {
log.Infof("Configs:")
log.Printf("- ArchivePath: %s", configs.ArchivePath)
log.Printf("- ExportMethod: %s", configs.ExportMethod)
log.Printf("- UploadBitcode: %s", configs.UploadBitcode)
log.Printf("- CompileBitcode: %s", configs.CompileBitcode)
log.Printf("- TeamID: %s", configs.TeamID)
log.Printf("- VerboseLog: %s", configs.VerboseLog)
log.Infof("Experimental Configs:")
log.Printf("- UseLegacyExport: %s", configs.UseLegacyExport)
log.Printf("- LegacyExportProvisioningProfileName: %s", configs.LegacyExportProvisioningProfileName)
log.Printf("- LegacyExportOutputFormat: %s", configs.LegacyExportOutputFormat)
log.Printf("- CustomExportOptionsPlistContent:")
if configs.CustomExportOptionsPlistContent != "" {
fmt.Println(configs.CustomExportOptionsPlistContent)
}
log.Infof("Other Configs:")
log.Printf("- DeployDir: %s", configs.DeployDir)
}
func (configs ConfigsModel) validate() error {
if configs.ArchivePath == "" {
return errors.New("no ArchivePath specified")
}
if exist, err := pathutil.IsPathExists(configs.ArchivePath); err != nil {
return fmt.Errorf("failed to check if ArchivePath exist at: %s, error: %s", configs.ArchivePath, err)
} else if !exist {
return fmt.Errorf("ArchivePath not exist at: %s", configs.ArchivePath)
}
if configs.ExportMethod == "" {
return errors.New("no ExportMethod specified")
}
if configs.UploadBitcode == "" {
return errors.New("no UploadBitcode specified")
}
if configs.CompileBitcode == "" {
return errors.New("no CompileBitcode specified")
}
if configs.UseLegacyExport == "" {
return errors.New("no UseLegacyExport specified")
}
if configs.LegacyExportOutputFormat == "" {
return errors.New("no LegacyExportOutputFormat specified")
}
return nil
}
func fail(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}
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 main() {
configs := createConfigsModelFromEnvs()
fmt.Println()
configs.print()
if err := configs.validate(); err != nil {
fail("Issue with input: %s", err)
}
log.SetEnableDebugLog(configs.VerboseLog == "yes")
archiveExt := filepath.Ext(configs.ArchivePath)
archiveName := filepath.Base(configs.ArchivePath)
archiveName = strings.TrimSuffix(archiveName, archiveExt)
appPath := filepath.Join(configs.DeployDir, archiveName+".app")
pkgPath := filepath.Join(configs.DeployDir, archiveName+".pkg")
exportOptionsPath := filepath.Join(configs.DeployDir, "export_options.plist")
ideDistributionLogsZipPath := filepath.Join(configs.DeployDir, "xcodebuild.xcdistributionlogs.zip")
xcodebuildVersion, err := utility.GetXcodeVersion()
if err != nil {
fail("Failed to determine xcode version, error: %s", err)
}
log.Printf("- xcodebuildVersion: %s (%s)", xcodebuildVersion.Version, xcodebuildVersion.BuildVersion)
customExportOptionsPlistContent := strings.TrimSpace(configs.CustomExportOptionsPlistContent)
if customExportOptionsPlistContent != configs.CustomExportOptionsPlistContent {
fmt.Println()
log.Warnf("CustomExportOptionsPlistContent is stripped to remove spaces and new lines:")
log.Printf(customExportOptionsPlistContent)
}
envsToUnset := []string{"GEM_HOME", "GEM_PATH", "RUBYLIB", "RUBYOPT", "BUNDLE_BIN_PATH", "_ORIGINAL_GEM_PATH", "BUNDLE_GEMFILE"}
for _, key := range envsToUnset {
if err := os.Unsetenv(key); err != nil {
fail("Failed to unset (%s), error: %s", key, err)
}
}
archive, err := xcarchive.NewMacosArchive(configs.ArchivePath)
if err != nil {
fail("Failed to parse archive, error: %s", err)
}
// do a simple export if method set to none
{
if configs.ExportMethod == "none" {
log.Infof("Exporting app without re-sign...")
if err := utils.ExportAppFromArchive(configs.ArchivePath, configs.DeployDir, bitriseAppPathEnvKey); err != nil {
fail("Failed to archive app, error: %s", err)
}
log.Donef("The app path is now available in the Environment Variable: %s", bitriseAppPathEnvKey)
return
}
}
exportMethod, err := exportoptions.ParseMethod(configs.ExportMethod)
if err != nil {
fail("Failed to parse export options, error: %s", err)
}
// legacy export
{
if configs.UseLegacyExport == "yes" {
log.Infof("Using legacy export method...")
if xcodebuildVersion.MajorVersion >= 9 {
fail("Legacy export method (using '-exportFormat ipa' flag) is not supported from Xcode version 9")
}
provisioningProfileName := ""
if configs.LegacyExportProvisioningProfileName != "" {
log.Printf("Using provisioning profile: %s", configs.LegacyExportProvisioningProfileName)
provisioningProfileName = configs.LegacyExportProvisioningProfileName
} else {
log.Printf("Using embedded provisioning profile")
if archive.Application.ProvisioningProfile == nil {
fail("No embedded.provisionprofile found nor Provisioning Profile name to use by export specified")
}
provisioningProfileName = archive.Application.ProvisioningProfile.Name
log.Printf("embedded profile name: %s", provisioningProfileName)
}
exportingApp := true
if configs.LegacyExportOutputFormat == "pkg" {
exportingApp = false
}
legacyExportCmd := xcodebuild.NewLegacyExportCommand()
legacyExportCmd.SetExportFormat(configs.LegacyExportOutputFormat)
if exportingApp {
legacyExportCmd.SetExportPath(appPath)
} else {
legacyExportCmd.SetExportPath(pkgPath)
}
legacyExportCmd.SetArchivePath(configs.ArchivePath)
legacyExportCmd.SetExportProvisioningProfileName(provisioningProfileName)
log.Donef("$ %s", legacyExportCmd.PrintableCmd())
fmt.Println()
if err := legacyExportCmd.Run(); err != nil {
fail("Export failed, error: %s", err)
}
if exportingApp {
if err := utils.ExportOutputFile(appPath, appPath, bitriseAppPathEnvKey); err != nil {
fail("Failed to export %s, error: %s", bitriseAppPathEnvKey, err)
}
log.Donef("The app path is now available in the Environment Variable: %s (value: %s)", bitriseAppPathEnvKey, appPath)
} else {
if err := utils.ExportOutputFile(pkgPath, pkgPath, bitrisePKGPathEnvKey); err != nil {
fail("Failed to export %s, error: %s", bitrisePKGPathEnvKey, err)
}
log.Donef("The pkg path is now available in the Environment Variable: %s (value: %s)", bitrisePKGPathEnvKey, appPath)
}
return
}
}
log.Infof("Exporting with export options...")
exportOptionsPlistContent := ""
if customExportOptionsPlistContent != "" {
log.Printf("Custom export options content provided:")
fmt.Println(customExportOptionsPlistContent)
exportOptionsPlistContent = customExportOptionsPlistContent
}
if exportOptionsPlistContent == "" {
log.Printf("Generating export options")
if xcodebuildVersion.MajorVersion >= 9 {
log.Printf("xcode major version > 9, generating provisioningProfiles node")
exportOpts, err := generateMacExportOptionsPlist(archive, exportMethod, configs.TeamID)
if err != nil {
fail("Export options could not be generated: %v", err)
}
log.Printf("generated export options content:")
fmt.Println()
fmt.Println(exportOpts.String())
exportOptionsPlistContent, err = exportOpts.String()
if err != nil {
fail("Failed to get exportOptions, error: %s", err)
}
}
}
if err := fileutil.WriteStringToFile(exportOptionsPath, exportOptionsPlistContent); err != nil {
fail("Failed to write export options to file, error: %s", err)
}
fmt.Println()
tmpDir, err := pathutil.NormalizedOSTempDirPath("__export__")
if err != nil {
fail("Failed to create tmp dir, error: %s", err)
}
exportCmd := xcodebuild.NewExportCommand()
exportCmd.SetArchivePath(configs.ArchivePath)
exportCmd.SetExportDir(tmpDir)
exportCmd.SetExportOptionsPlist(exportOptionsPath)
log.Donef("$ %s", exportCmd.PrintableCmd())
fmt.Println()
if xcodebuildOut, err := exportCmd.RunAndReturnOutput(); err != nil {
// xcdistributionlogs
if logsDirPth, err := findIDEDistrubutionLogsPath(xcodebuildOut); err != nil {
log.Warnf("Failed to find xcdistributionlogs, error: %s", err)
} else if err := utils.ExportOutputDirAsZip(logsDirPth, ideDistributionLogsZipPath, bitriseIDEDistributionLogsPthEnvKey); err != nil {
log.Warnf("Failed to export %s, error: %s", bitriseIDEDistributionLogsPthEnvKey, err)
} else {
log.Warnf(`If you can't find the reason of the error in the log, please check the xcdistributionlogs
The logs directory is stored in $BITRISE_DEPLOY_DIR, and its full path
is available in the $BITRISE_IDEDISTRIBUTION_LOGS_PATH environment variable`)
}
fail("Export failed, error: %s", err)
}
pattern := filepath.Join(tmpDir, "*.app")
apps, err := filepath.Glob(pattern)
if err != nil {
fail("Failed to find app, with pattern: %s, error: %s", pattern, err)
}
if len(apps) > 0 {
if err := utils.ExportOutputDirAsZip(apps[0], appPath, bitriseAppPathEnvKey); err != nil {
fail("Failed to export %s, error: %s", bitriseAppPathEnvKey, err)
}
log.Donef("The app path is now available in the Environment Variable: %s (value: %s)", bitriseAppPathEnvKey, appPath)
return
}
pattern = filepath.Join(tmpDir, "*.pkg")
pkgs, err := filepath.Glob(pattern)
if err != nil {
fail("Failed to find pkg, with pattern: %s, error: %s", pattern, err)
}
if len(pkgs) > 0 {
if err := utils.ExportOutputFile(pkgs[0], pkgPath, bitrisePKGPathEnvKey); err != nil {
fail("Failed to export %s, error: %s", bitrisePKGPathEnvKey, err)
}
log.Donef("The pkg path is now available in the Environment Variable: %s (value: %s)", bitrisePKGPathEnvKey, appPath)
} else {
fail("No app nor pkg output generated")
}
}