This repository was archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathios_symbol_uploader.go
230 lines (200 loc) · 5.43 KB
/
ios_symbol_uploader.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
package parsecmd
import (
"bytes"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/DHowett/go-plist"
"github.com/ParsePlatform/parse-cli/parsecli"
"github.com/facebookgo/stackerr"
"github.com/inconshreveable/go-update"
"github.com/mitchellh/go-homedir"
)
const (
osxSymbolConverterDownloadURL = "https://www.parse.com/downloads/cloud_code/parse_symbol_converter"
osxSymbolConverterVersion = "1.0.2"
)
type iosSymbolUploader struct {
Path string
SkipOsCheck bool
}
func (i *iosSymbolUploader) validate() error {
if i.SkipOsCheck {
return nil
}
if runtime.GOOS != "darwin" {
return stackerr.New("Upload of iOS symbol files is only available on OS X.")
}
return nil
}
func getDwarfPath(path string) (string, error) {
// E.g. Yarr.xcarchive/dSYMs/yarr.dSYM/Contents/Resources/DWARF/Yarr
// Find first dSYM bundle if it is an `xcarchive` bundle
if filepath.Ext(path) == ".xcarchive" {
path = filepath.Join(path, "dSYMs")
filenames, err := readDirNames(path)
if err != nil {
return "", err
}
for _, filename := range filenames {
if filepath.Ext(filename) == ".dSYM" {
path = filename
break
}
}
}
// Find first DWARF file inside if it is a `dSYM` bundle
if filepath.Ext(path) == ".dSYM" {
dwarfFilesDir := filepath.Join(path, "Contents", "Resources", "DWARF")
filenames, err := readDirNames(dwarfFilesDir)
if err != nil {
return "", stackerr.Wrap(err)
}
if len(filenames) > 0 {
return filenames[0], nil
}
}
// If no extension - just return the file, as it probably is DWARF
if filepath.Ext(path) == "" {
return path, nil
}
return "", nil
}
func (i *iosSymbolUploader) acceptsPath() bool {
dwarfPath, err := getDwarfPath(i.Path)
if err != nil {
return false
}
return dwarfPath != ""
}
func (i *iosSymbolUploader) getBuildVersionFromXcarchive() (string, error) {
if filepath.Ext(i.Path) != ".xcarchive" {
return "1", nil
}
plistPath := filepath.Join(i.Path, "Info.plist")
plistFile, err := os.Open(plistPath)
if err != nil {
return "", stackerr.Wrap(err)
}
type version struct {
CFBundleVersion string `plist:"CFBundleVersion"`
}
var properties struct {
ApplicationProperties version `plist:"ApplicationProperties"`
}
if err := plist.NewDecoder(plistFile).Decode(&properties); err != nil {
return "", stackerr.Wrap(err)
}
return properties.ApplicationProperties.CFBundleVersion, nil
}
func (i *iosSymbolUploader) uploadSymbols(e *parsecli.Env) error {
appBuildVersion, err := i.getBuildVersionFromXcarchive()
if err != nil {
return err
}
dwarfFilePath, err := getDwarfPath(i.Path)
if err != nil {
return err
}
dwarfChecksum, err := base64MD5OfFile(dwarfFilePath)
if err != nil {
return err
}
symbolFiles, err := i.convertSymbols(e)
if err != nil {
return err
}
return uploadSymbolFiles(symbolFiles, map[string]string{
"X-Parse-App-Build-Version": appBuildVersion,
"X-Parse-Apple-DWARF-MD5": dwarfChecksum,
}, true, e)
}
func (i *iosSymbolUploader) prepareSymbolsFolder(folderPath string, e *parsecli.Env) error {
if err := os.RemoveAll(folderPath); err != nil {
return stackerr.Wrap(err)
}
if err := os.MkdirAll(folderPath, 0755); err != nil {
return stackerr.Wrap(err)
}
return nil
}
type updateTool func(string, string) error
func (i *iosSymbolUploader) symbolConversionTool(baseDir string,
updater updateTool,
e *parsecli.Env) (string, error) {
converter := os.Getenv("PARSE_SYMBOL_CONVERTER")
if converter != "" {
return converter, nil
}
toUpdate := true
toolFolder := filepath.Join(baseDir, ".parse")
toolPath := filepath.Join(toolFolder, "parse_symbol_converter")
if err := os.MkdirAll(toolFolder, 0755); err != nil {
return "", stackerr.Wrap(err)
}
_, err := os.Lstat(toolPath)
if err == nil {
version, err := exec.Command(toolPath, "--version").Output()
if err == nil && string(bytes.TrimSpace(version)) == osxSymbolConverterVersion {
toUpdate = false
}
}
if os.IsNotExist(err) {
file, err := os.Create(toolPath)
if err != nil {
return "", stackerr.Wrap(err)
}
if err := file.Close(); err != nil {
return "", stackerr.Wrap(err)
}
}
if updater == nil {
if toUpdate {
fmt.Fprintln(e.Out, "Fetching required resources...")
var resp *http.Response
resp, err = http.Get(osxSymbolConverterDownloadURL)
if err == nil {
defer resp.Body.Close()
err = update.Apply(resp.Body, update.Options{TargetPath: toolPath})
}
}
} else {
err = updater(toolPath, osxSymbolConverterDownloadURL)
}
if err != nil {
return "", stackerr.Wrap(err)
}
if err := os.Chmod(toolPath, 0755); err != nil {
return "", stackerr.Wrap(err)
}
if toUpdate {
fmt.Fprintln(e.Out, "Additional resources installed.")
}
return toolPath, nil
}
func (i *iosSymbolUploader) convertSymbols(e *parsecli.Env) ([]string, error) {
homedir, err := homedir.Dir()
if err != nil {
return nil, stackerr.Wrap(err)
}
folderPath := filepath.Join(homedir, ".parse", "CrashReportingSymbols")
if err := i.prepareSymbolsFolder(folderPath, e); err != nil {
return nil, stackerr.Wrap(err)
}
conversionTool, err := i.symbolConversionTool(homedir, nil, e)
if err != nil {
return nil, stackerr.Wrap(err)
}
cmd := exec.Command(conversionTool, i.Path, folderPath)
if out, err := cmd.CombinedOutput(); err != nil {
return nil, stackerr.Newf("Symbol conversion failed with:\n%s", string(out))
}
filenames, err := readDirNames(folderPath)
if err != nil {
return nil, stackerr.Wrap(err)
}
return filenames, nil
}