-
Notifications
You must be signed in to change notification settings - Fork 1
/
dart.go
253 lines (190 loc) · 6.65 KB
/
dart.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
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
package upload
import (
"debug/elf"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/bugsnag/bugsnag-cli/pkg/log"
"github.com/bugsnag/bugsnag-cli/pkg/server"
"github.com/bugsnag/bugsnag-cli/pkg/utils"
)
type DartSymbol struct {
Path utils.UploadPaths `arg:"" name:"path" help:"(required) Path to directory or file to upload" type:"path"`
IosAppPath string `help:"(optional) the path to the built iOS app."`
VersionName string `help:"The version of the application." xor:"app-version,version-name"`
AppVersion string `help:"(deprecated) The version of the application." xor:"app-version,version-name"`
VersionCode string `help:"The version code for the application (Android only)." xor:"app-version-code,version-code"`
AppVersionCode string `help:"(deprecated) The version code for the application (Android only)." xor:"app-version-code,version-code"`
BundleVersion string `help:"The bundle version for the application (iOS only)." xor:"app-bundle-version,bundle-version"`
AppBundleVersion string `help:"(deprecated) The bundle version for the application (iOS only)." xor:"app-bundle-version,bundle-version"`
}
func Dart(paths []string, version string, versionCode string, bundleVersion string, iosAppPath string, endpoint string, timeout int, retries int, overwrite bool, apiKey string, failOnUploadError bool) error {
log.Info("Building file list from path")
fileList, err := utils.BuildFileList(paths)
numberOfFiles := len(fileList)
if err != nil {
log.Error("error building file list", 1)
}
log.Info("File list built")
for _, file := range fileList {
// Check if we're dealing with an android or iOS symbol file
androidPlatform, _ := regexp.MatchString("android-([^;]*).symbols", file)
iosPlatform, _ := regexp.MatchString("ios-([^;]*).symbols", file)
// Start processing the android symbol file
if androidPlatform {
log.Info("Processing android symbol file: " + file)
buildId, err := GetBuildIdFromElfFile(file)
if err != nil {
return err
}
// Build Upload options
uploadOptions := utils.BuildDartUploadOptions(apiKey, buildId, "android", overwrite, version, versionCode)
fileFieldData := make(map[string]string)
fileFieldData["symbolFile"] = file
requestStatus := server.ProcessRequest(endpoint+"/dart-symbol", uploadOptions, fileFieldData, timeout)
if requestStatus != nil {
if numberOfFiles > 1 && failOnUploadError {
return requestStatus
} else {
log.Warn(requestStatus.Error())
}
} else {
log.Success(file)
}
continue
}
// Process iOS file
if iosPlatform {
log.Info("Processing iOS symbol file: " + file)
if iosAppPath == "" {
iosAppPath, err = GetIosAppPath(file)
if err != nil {
return err
}
}
arch, err := GetArchFromElfFile(file)
if err != nil {
return err
}
buildId, err := DwarfDumpUuid(file, iosAppPath, arch)
if err != nil {
return err
}
// Build Upload options
uploadOptions := utils.BuildDartUploadOptions(apiKey, buildId, "ios", overwrite, version, bundleVersion)
fileFieldData := make(map[string]string)
fileFieldData["symbolFile"] = file
requestStatus := server.ProcessRequest(endpoint+"/dart-symbol", uploadOptions, fileFieldData, timeout)
if requestStatus != nil {
if numberOfFiles > 1 && failOnUploadError {
return requestStatus
} else {
log.Warn(requestStatus.Error())
}
} else {
log.Success(file)
}
continue
}
log.Info("Skipping " + file)
}
return nil
}
// ReadElfFile - Gets all data from the symbol file
func ReadElfFile(symbolFile string) (*elf.File, error) {
file, err := os.OpenFile(symbolFile, os.O_RDONLY, 0)
if err != nil {
return nil, fmt.Errorf("unable to open file: %w", err)
}
_elf, err := elf.NewFile(file)
if err != nil {
return nil, fmt.Errorf("error reading symbol file: %w", err)
}
return _elf, nil
}
// GetBuildIdFromElfFile - Gets the build ID from the symbol file
func GetBuildIdFromElfFile(symbolFile string) (string, error) {
elfData, err := ReadElfFile(symbolFile)
if err != nil {
return "", err
}
if sect := elfData.Section(".note.gnu.build-id"); sect != nil {
data, err := sect.Data()
if err != nil {
return "", fmt.Errorf("error reading symbol file")
}
buildId := fmt.Sprintf("%x", data[16:])
return buildId, nil
}
return "", fmt.Errorf("unable to find buildId")
}
// GetArchFromElfFile - Gets the Arch from the symbol file to help with getting the UUID from the built iOS app
func GetArchFromElfFile(symbolFile string) (string, error) {
elfData, err := ReadElfFile(symbolFile)
if err != nil {
return "", err
}
var arch string
switch elfData.Machine {
case elf.EM_AARCH64:
arch = "arm64"
case elf.EM_386:
arch = "x86"
case elf.EM_X86_64:
arch = "x86_64"
case elf.EM_ARM:
arch = "armv7"
}
if arch == "" {
return "", fmt.Errorf("unable to find arch type")
}
return arch, nil
}
// GetIosAppPath - Gets the path to the built iOS app relative to the symbol files
func GetIosAppPath(symbolFile string) (string, error) {
sampleRegexp := regexp.MustCompile(`/[^/]*/[^/]*$`)
basePath := filepath.Join(sampleRegexp.ReplaceAllString(symbolFile, "") + "/build/ios/iphoneos")
files, err := ioutil.ReadDir(basePath)
if err != nil {
return "", err
}
for _, file := range files {
if strings.Contains(file.Name(), ".app") && file.IsDir() {
iosAppPath := filepath.Join(basePath + "/" + file.Name() + "/Frameworks/App.framework/App")
_, err := os.Stat(iosAppPath)
if errors.Is(err, os.ErrNotExist) {
return "", err
}
return iosAppPath, nil
}
}
return "", fmt.Errorf("unable to find iOS app path, try adding --ios-app-path")
}
// DwarfDumpUuid - Gets the UUID/Build ID from the Dwarf debug info of a file for a given Arch
func DwarfDumpUuid(symbolFile string, dwarfFile string, arch string) (string, error) {
dwarfDumpLocation, err := exec.LookPath("dwarfdump")
if err != nil {
dwarfDumpLocation, err = exec.LookPath("llvm-dwarfdump")
if err != nil {
return "", fmt.Errorf("unable to find dwarfdump on system: %w", err)
}
}
uuidArray := make(map[string]string)
cmd := exec.Command(dwarfDumpLocation, "--uuid", dwarfFile, "--arch", arch)
output, _ := cmd.CombinedOutput()
outputArray := strings.Fields(string(output))
uuidArray[outputArray[2]] = outputArray[1]
for key, value := range uuidArray {
uuidArch := strings.Replace(key, "(", "", -1)
uuidArch = strings.Replace(uuidArch, ")", "", -1)
if strings.Contains(symbolFile, uuidArch) {
return value, nil
}
}
return "", fmt.Errorf("unable to find matching UUID")
}