-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
225 lines (189 loc) · 6.92 KB
/
main.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
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"time"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-steputils/tools"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/sliceutil"
"github.com/bitrise-io/go-utils/stringutil"
"github.com/bitrise-io/go-xcode/xcodebuild"
cache "github.com/bitrise-io/go-xcode/xcodecache"
"github.com/bitrise-io/go-xcode/xcpretty"
"github.com/bitrise-steplib/steps-xcode-archive/utils"
"github.com/kballard/go-shellquote"
)
const bitriseXcodeRawResultTextEnvKey = "BITRISE_XCODE_RAW_RESULT_TEXT_PATH"
// Config ...
type Config struct {
Workdir string `env:"workdir"`
ProjectPath string `env:"project_path,required"`
Scheme string `env:"scheme,required"`
IsCleanBuild bool `env:"is_clean_build,opt[yes,no]"`
ForceProvisioningProfile string `env:"force_provisioning_profile"`
ForceCodeSignIdentity string `env:"force_code_sign_identity"`
DisableCodesign bool `env:"disable_codesign,opt[yes,no]"`
DisableIndexWhileBuilding bool `env:"disable_index_while_building,opt[yes,no]"`
CacheLevel string `env:"cache_level,opt[none,swift_packages]"`
XcodebuildOptions string `env:"xcodebuild_options"`
OutputTool string `env:"output_tool,opt[xcpretty,xcodebuild]"`
OutputDir string `env:"output_dir,dir"`
VerboseLog bool `env:"verbose_log,opt[yes,no]"`
DeployDir string `env:"BITRISE_DEPLOY_DIR"`
}
func main() {
var conf Config
if err := stepconf.Parse(&conf); err != nil {
fail("Failed to parse configs, error: %s", err)
}
stepconf.Print(conf)
log.SetEnableDebugLog(conf.VerboseLog)
fmt.Println()
log.Infof("Step determined configs:")
absProjectPath, err := filepath.Abs(conf.ProjectPath)
if err != nil {
fail("Failed to expand project path (%s), error: %s", conf.ProjectPath, err)
}
// Detect xcpretty version
outputTool := conf.OutputTool
if outputTool == "xcpretty" {
fmt.Println()
log.Infof("Checking if output tool (xcpretty) is installed")
installed, err := xcpretty.IsInstalled()
if err != nil {
log.Warnf("Failed to check if xcpretty is installed, error: %s", err)
log.Printf("Switching to xcodebuild for output tool")
outputTool = "xcodebuild"
} else if !installed {
log.Warnf(`xcpretty is not installed`)
fmt.Println()
log.Printf("Installing xcpretty")
if cmds, err := xcpretty.Install(); err != nil {
log.Warnf("Failed to create xcpretty install command: %s", err)
log.Warnf("Switching to xcodebuild for output tool")
outputTool = "xcodebuild"
} else {
for _, cmd := range cmds {
if out, err := cmd.RunAndReturnTrimmedCombinedOutput(); err != nil {
if errorutil.IsExitStatusError(err) {
log.Warnf("%s failed: %s", out)
} else {
log.Warnf("%s failed: %s", err)
}
log.Warnf("Switching to xcodebuild for output tool")
outputTool = "xcodebuild"
}
}
}
}
}
if outputTool == "xcpretty" {
xcprettyVersion, err := xcpretty.Version()
if err != nil {
log.Warnf("Failed to determin xcpretty version, error: %s", err)
log.Printf("Switching to xcodebuild for output tool")
outputTool = "xcodebuild"
}
log.Printf("- xcprettyVersion: %s", xcprettyVersion.String())
}
// Output files
rawXcodebuildOutputLogPath := filepath.Join(conf.OutputDir, "raw-xcodebuild-output.log")
tempDir, err := os.MkdirTemp("", "XCOutput")
if err != nil {
fail("Could not create result bundle path directory: %s", err)
}
xcresultPath := path.Join(tempDir, "Analyze.xcresult")
//
// Cleanup
filesToCleanup := []string{
rawXcodebuildOutputLogPath,
}
for _, pth := range filesToCleanup {
if exist, err := pathutil.IsPathExists(pth); err != nil {
fail("Failed to check if path (%s) exist, error: %s", pth, err)
} else if exist {
if err := os.RemoveAll(pth); err != nil {
fail("Failed to remove path (%s), error: %s", pth, err)
}
}
}
//
// Analyze project with Xcode Command Line tools
fmt.Println()
log.Infof("Analyzing the project")
analyzeCmd := xcodebuild.NewCommandBuilder(absProjectPath, "analyze")
analyzeCmd.SetScheme(conf.Scheme)
if conf.DisableCodesign {
analyzeCmd.SetDisableCodesign(true)
}
var customOptions []string
if conf.XcodebuildOptions != "" {
if customOptions, err = shellquote.Split(conf.XcodebuildOptions); err != nil {
fail("failed to shell split XcodebuildOptions (%s), error: %s", conf.XcodebuildOptions, err)
}
}
if conf.DisableIndexWhileBuilding {
customOptions = append(customOptions, "COMPILER_INDEX_STORE_ENABLE=NO")
}
analyzeCmd.SetCustomOptions(customOptions)
if !sliceutil.IsStringInSlice("-resultBundlePath", customOptions) {
analyzeCmd.SetResultBundlePath(xcresultPath)
}
swiftPackagesPath, err := cache.SwiftPackagesPath(absProjectPath)
if err != nil {
fail("Failed to get Swift Packages path, error: %s", err)
}
rawXcodebuildOut, xcErr := runCommandWithRetry(analyzeCmd, outputTool == "xcpretty", swiftPackagesPath)
if xcErr != nil {
if outputTool == "xcpretty" {
log.Errorf("\nLast lines of the Xcode's build log:")
fmt.Println(stringutil.LastNLines(rawXcodebuildOut, 10))
if err := utils.ExportOutputFileContent(rawXcodebuildOut, rawXcodebuildOutputLogPath, bitriseXcodeRawResultTextEnvKey); err != nil {
log.Warnf("Failed to export %s, error: %s", bitriseXcodeRawResultTextEnvKey, err)
} else {
log.Warnf(`You can find the last couple of lines of Xcode's build log above, but the full log is also available in the raw-xcodebuild-output.log
The log file is stored in $BITRISE_DEPLOY_DIR, and its full path is available in the $BITRISE_XCODE_RAW_RESULT_TEXT_PATH environment variable
(value: %s)`, rawXcodebuildOutputLogPath)
}
}
}
fmt.Println()
if xcresultPath != "" {
// export xcresult bundle
if err := tools.ExportEnvironmentWithEnvman("BITRISE_XCRESULT_PATH", xcresultPath); err != nil {
log.Warnf("Failed to export: BITRISE_XCRESULT_PATH, error: %s", err)
} else {
log.Printf("Exported BITRISE_XCRESULT_PATH: %s", xcresultPath)
}
}
if xcErr != nil {
fail("Analyze failed: %s", xcErr)
}
// Cache swift PM
if conf.CacheLevel == "swift_packages" {
if err := cache.CollectSwiftPackages(absProjectPath); err != nil {
log.Warnf("Failed to mark swift packages for caching, error: %s", err)
}
}
}
func fail(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}
func currentTimestamp() string {
timeStampFormat := "15:04:05"
currentTime := time.Now()
return currentTime.Format(timeStampFormat)
}
// ColoringFunc ...
type ColoringFunc func(...interface{}) string
func logWithTimestamp(coloringFunc ColoringFunc, format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
messageWithTimeStamp := fmt.Sprintf("[%s] %s", currentTimestamp(), coloringFunc(message))
fmt.Println(messageWithTimeStamp)
}