forked from parse-community/parse-cli
/
android_symbol_uploader.go
161 lines (142 loc) · 4.12 KB
/
android_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
package parsecmd
import (
"bytes"
"encoding/xml"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"github.com/back4app/parse-cli/parsecli"
"github.com/facebookgo/stackerr"
)
var androidCodeversion = regexp.MustCompile(`versionCode='(\d+)'`)
type androidSymbolUploader struct {
Path string
Apk string
Manifest string
AAPT string
}
func findAAPT(root string) []string {
var aapts []string
// we ignore the error returned by filepath.Walk because we are only making
// a best eeffort search for aapt, if there is an error for any reason,
// we just ignore it & forgo the search
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info != nil && filepath.Base(path) == "aapt" {
aapts = append(aapts, path)
}
return nil
})
return aapts
}
func (a *androidSymbolUploader) validate() error {
if (a.Apk == "") == (a.Manifest == "") {
return stackerr.New("You need to supply one of either the apk or manifest file.")
}
return nil
}
func (a *androidSymbolUploader) acceptsPath() bool {
return filepath.Base(a.Path) == "mapping.txt"
}
func (a *androidSymbolUploader) getAAPT(androidHome string) (string, error) {
options := []string{a.AAPT}
// either in $ANDROID_HOME/platform-tools/aapt
options = append(options, filepath.Join(androidHome, "platform_tools", "aapt"))
// or $ANDROID_HOME/build-tools/<VERSION>/aapt
buildTools := filepath.Join(androidHome, "build_tools")
options = append(options, findAAPT(buildTools)...)
for _, o := range options {
if out, err := exec.Command(o).Output(); err == nil {
if bytes.HasPrefix(out, []byte("Android Asset Packaging Tool")) {
return o, nil
}
}
}
return "", stackerr.New("Cannot find aapt, you might need to set ANDROID_HOME.")
}
func (a *androidSymbolUploader) getBuildVersionFromManifest() (int, error) {
if a.Manifest == "" {
return 0, stackerr.New("Please provide manifest file path.")
}
var version struct {
XMLName xml.Name `xml:"manifest"`
VersionCode int `xml:"http://schemas.android.com/apk/res/android versionCode,attr"`
}
manifest, err := os.Open(a.Manifest)
if err != nil {
return 0, stackerr.Wrap(err)
}
defer manifest.Close()
if err := xml.NewDecoder(manifest).Decode(&version); err != nil {
return 0, stackerr.Wrap(err)
}
if version.VersionCode == 0 {
return 0, stackerr.New("Manifest does not contain build version.")
}
return version.VersionCode, nil
}
func (a *androidSymbolUploader) getBuildVersionFromAPK() (int, error) {
if a.Apk == "" {
return 0, stackerr.New("Please provide apk file path.")
}
if a.AAPT == "" {
androidHome := os.Getenv("ANDROID_HOME")
if androidHome == "" {
androidHome = os.Getenv("ANDROID_SDK")
}
if androidHome == "" {
return 0, stackerr.New("Cannot find aapt, you might need to set ANDROID_HOME.")
}
aapt, err := a.getAAPT(androidHome)
if err != nil {
return 0, err
}
a.AAPT = aapt
}
bytes, err := exec.Command(a.AAPT, "dump", "badging", a.Apk).Output()
if err != nil {
return 0, stackerr.Wrap(err)
}
return parseVersionFromBytes(bytes)
}
func parseVersionFromBytes(bytes []byte) (int, error) {
v := androidCodeversion.FindAllSubmatch(bytes, 1)
if len(v) == 0 {
return 0, stackerr.New("Cannot determine build version.")
}
version, err := strconv.Atoi(string(v[0][1]))
if err != nil {
return 0, stackerr.Wrap(err)
}
return int(version), nil
}
func (a *androidSymbolUploader) getBuildVersion(e *parsecli.Env) (int, error) {
var handleError = func(versionCode int, err error) (int, error) {
if err != nil {
return 0, err
}
if versionCode == 1 {
fmt.Fprintln(e.Out, "Warning: build number is '1'.")
}
return versionCode, nil
}
if a.Manifest != "" {
return handleError(a.getBuildVersionFromManifest())
}
return handleError(a.getBuildVersionFromAPK())
}
func (a *androidSymbolUploader) uploadSymbols(e *parsecli.Env) error {
appBuildVersion, err := a.getBuildVersion(e)
if err != nil {
return err
}
commonHeaders := map[string]string{
"X-Parse-App-Build-Version": fmt.Sprintf("%d", appBuildVersion),
}
return uploadSymbolFiles([]string{a.Path}, commonHeaders, false, e)
}