forked from fyne-io/fyne
/
release.go
372 lines (328 loc) · 12 KB
/
release.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
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
370
371
372
package commands
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"fyne.io/fyne"
"fyne.io/fyne/cmd/fyne/internal/mobile"
"fyne.io/fyne/cmd/fyne/internal/templates"
"fyne.io/fyne/cmd/fyne/internal/util"
)
var (
macAppStoreCategories = []string{
"business", "developer-tools", "education", "entertainment", "finance", "games", "action-games",
"adventure-games", "arcade-games", "board-games", "card-games", "casino-games", "dice-games",
"educational-games", "family-games", "kids-games", "music-games", "puzzle-games", "racing-games",
"role-playing-games", "simulation-games", "sports-games", "strategy-games", "trivia-games", "word-games",
"graphics-design", "healthcare-fitness", "lifestyle", "medical", "music", "news", "photography",
"productivity", "reference", "social-networking", "sports", "travel", "utilities", "video", "weather",
}
)
// Declare conformity to Command interface
var _ Command = (*releaser)(nil)
type releaser struct {
packager
keyStore string
keyStorePass string
keyPass string
developer string
password string
}
// NewReleaser returns a command that can adapt app packages for distribution
func NewReleaser() Command {
return &releaser{}
}
func (r *releaser) AddFlags() {
flag.StringVar(&r.os, "os", "", "The operating system to target (android, android/arm, android/arm64, android/amd64, android/386, darwin, freebsd, ios, linux, netbsd, openbsd, windows)")
flag.StringVar(&r.name, "name", "", "The name of the application, default is the executable file name")
flag.StringVar(&r.icon, "icon", "Icon.png", "The name of the application icon file")
flag.StringVar(&r.appID, "appID", "", "For ios or darwin targets an appID is required, for ios this must \nmatch a valid provisioning profile")
flag.StringVar(&r.appVersion, "appVersion", "", "Version number in the form x, x.y or x.y.z semantic version")
flag.IntVar(&r.appBuild, "appBuild", 0, "Build number, should be greater than 0 and incremented for each build")
flag.StringVar(&r.keyStore, "keyStore", "", "Android: location of .keystore file containing signing information")
flag.StringVar(&r.keyStorePass, "keyStorePass", "", "Android: password for the .keystore file, default take the password from stdin")
flag.StringVar(&r.keyPass, "keyPass", "", "Android: password for the signer's private key, which is needed if the private key is password-protected. Default take the password from stdin")
flag.StringVar(&r.certificate, "certificate", "", "iOS/macOS/Windows: name of the certificate to sign the build")
flag.StringVar(&r.profile, "profile", "", "iOS/macOS: name of the provisioning profile for this release build")
flag.StringVar(&r.developer, "developer", "", "Windows: the developer identity for your Microsoft store account")
flag.StringVar(&r.password, "password", "", "Windows: password for the certificate used to sign the build")
flag.StringVar(&r.tags, "tags", "", "A comma-separated list of build tags")
flag.StringVar(&r.category, "category", "", "iOS: category of the app for store listing")
}
func (r *releaser) PrintHelp(indent string) {
fmt.Println(indent, "The release command prepares an application for public distribution.")
}
func (r *releaser) Run(params []string) {
if err := r.validate(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
return
}
r.packager.release = true
if err := r.beforePackage(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
return
}
r.packager.Run(params)
if err := r.afterPackage(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
}
}
func (r *releaser) afterPackage() error {
if util.IsAndroid(r.os) {
target := mobile.AppOutputName(r.os, r.packager.name)
apk := filepath.Join(r.dir, target)
if err := r.zipAlign(apk); err != nil {
return err
}
return r.signAndroid(apk)
}
if r.os == "darwin" {
return r.packageMacOSRelease()
}
if r.os == "ios" {
return r.packageIOSRelease()
}
if r.os == "windows" {
outName := r.name + ".appx"
if pos := strings.LastIndex(r.name, ".exe"); pos > 0 {
outName = r.name[:pos] + ".appx"
}
outPath := filepath.Join(r.dir, outName)
os.Remove(outPath) // MakeAppx will hang if the file exists... ignore result
if err := r.packageWindowsRelease(outPath); err != nil {
return err
}
return r.signWindows(outPath)
}
return nil
}
func (r *releaser) beforePackage() error {
if util.IsAndroid(r.os) {
if err := util.RequireAndroidSDK(); err != nil {
return err
}
}
return nil
}
func (r *releaser) nameFromCertInfo(info string) string {
// format should be "CN=Company, O=Company, L=City, S=State, C=Country"
parts := strings.Split(info, ",")
cn := parts[0]
pos := strings.Index(strings.ToUpper(cn), "CN=")
if pos == -1 {
return cn // not what we were expecting, but should be OK
}
return cn[pos+3:]
}
func (r *releaser) packageIOSRelease() error {
team, err := mobile.DetectIOSTeamID(r.certificate)
if err != nil {
return errors.New("failed to determine team ID")
}
payload := filepath.Join(r.dir, "Payload")
os.Mkdir(payload, 0750)
defer os.RemoveAll(payload)
appName := mobile.AppOutputName(r.os, r.name)
payloadAppDir := filepath.Join(payload, appName)
os.Rename(filepath.Join(r.dir, appName), payloadAppDir)
entitlementPath := filepath.Join(r.dir, "entitlements.plist")
entitlements, _ := os.Create(entitlementPath)
entitlementData := struct{ TeamID, AppID string }{
TeamID: team,
AppID: r.appID,
}
err = templates.EntitlementsDarwinMobile.Execute(entitlements, entitlementData)
entitlements.Close()
if err != nil {
return errors.New("failed to write entitlements plist template")
}
cmd := exec.Command("codesign", "-f", "-vv", "-s", r.certificate, "--entitlements",
"entitlements.plist", "Payload/"+appName+"/")
if err := cmd.Run(); err != nil {
fyne.LogError("Codesign failed", err)
return errors.New("unable to codesign application bundle")
}
return exec.Command("zip", "-r", appName[:len(appName)-4]+".ipa", "Payload/").Run()
}
func (r *releaser) packageMacOSRelease() error {
appCert := r.certificate // try to derive two certificates from one name (they will be consistent)
if strings.Index(appCert, "Installer") != -1 {
appCert = strings.Replace(appCert, "Installer", "Application", 1)
}
installCert := strings.Replace(appCert, "Application", "Installer", 1)
unsignedPath := r.name + "-unsigned.pkg"
defer os.RemoveAll(r.name + ".app") // this was the output of package and it can get in the way of future builds
entitlementPath := filepath.Join(r.dir, "entitlements.plist")
entitlements, _ := os.Create(entitlementPath)
err := templates.EntitlementsDarwin.Execute(entitlements, nil)
entitlements.Close()
if err != nil {
return errors.New("failed to write entitlements plist template")
}
defer os.Remove(entitlementPath)
cmd := exec.Command("codesign", "-vfs", appCert, "--entitlement", "entitlements.plist", r.name+".app")
err = cmd.Run()
if err != nil {
fyne.LogError("Codesign failed", err)
return errors.New("unable to codesign application bundle")
}
cmd = exec.Command("productbuild", "--component", r.name+".app", "/Applications/",
"--product", r.name+".app/Contents/Info.plist", unsignedPath)
err = cmd.Run()
if err != nil {
fyne.LogError("Product build failed", err)
return errors.New("unable to build macOS app package")
}
defer os.Remove(unsignedPath)
cmd = exec.Command("productsign", "--sign", installCert, unsignedPath, r.name+".pkg")
return cmd.Run()
}
func (r *releaser) packageWindowsRelease(outFile string) error {
payload := filepath.Join(r.dir, "Payload")
os.Mkdir(payload, 0750)
defer os.RemoveAll(payload)
manifestPath := filepath.Join(payload, "appxmanifest.xml")
manifest, err := os.Create(manifestPath)
if err != nil {
return err
}
manifestData := struct{ AppID, Developer, DeveloperName, Name, Version string }{
AppID: r.appID,
// TODO read this info
Developer: r.developer,
DeveloperName: r.nameFromCertInfo(r.developer),
Name: r.name,
Version: r.combinedVersion(),
}
err = templates.AppxManifestWindows.Execute(manifest, manifestData)
manifest.Close()
if err != nil {
return errors.New("failed to write application manifest template")
}
util.CopyFile(r.icon, filepath.Join(payload, "Icon.png"))
util.CopyFile(r.name, filepath.Join(payload, r.name))
binDir, err := findWindowsSDKBin()
if err != nil {
return errors.New("cannot find makeappx.exe, make sure you have installed the Windows SDK")
}
cmd := exec.Command(filepath.Join(binDir, "makeappx.exe"), "pack", "/d", payload, "/p", outFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
}
func (r *releaser) signAndroid(path string) error {
signer := filepath.Join(util.AndroidBuildToolsPath(), "/apksigner")
args := []string{"sign", "--ks", r.keyStore}
if r.keyStorePass != "" {
args = append(args, "--ks-pass", "pass:"+r.keyStorePass)
}
if r.keyPass != "" {
args = append(args, "--key-pass", "pass:"+r.keyPass)
}
args = append(args, path)
cmd := exec.Command(signer, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
}
func (r *releaser) signWindows(appx string) error {
binDir, err := findWindowsSDKBin()
if err != nil {
return errors.New("cannot find signtool.exe, make sure you have installed the Windows SDK")
}
cmd := exec.Command(filepath.Join(binDir, "signtool.exe"),
"sign", "/a", "/v", "/fd", "SHA256", "/f", r.certificate, "/p", r.password, appx)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
}
func (r *releaser) validate() error {
if r.os == "" {
r.os = targetOS()
}
if util.IsMobile(r.os) || r.os == "windows" {
if r.appVersion == "" { // Here it is required, if provided then package validate will check format
return errors.New("missing required -appVersion parameter")
}
if r.appBuild <= 0 {
return errors.New("missing required -appBuild parameter")
}
}
if r.os == "windows" {
if r.developer == "" {
return errors.New("missing required -developer parameter for windows release,\n" +
"use data from Partner Portal, format \"CN=Company, O=Company, L=City, S=State, C=Country\"")
}
if r.certificate == "" {
return errors.New("missing required -certificate parameter for windows release")
}
if r.password == "" {
return errors.New("missing required -password parameter for windows release")
}
}
if util.IsAndroid(r.os) {
if r.keyStore == "" {
return errors.New("missing required -keyStore parameter for android release")
}
} else if r.os == "darwin" {
if r.certificate == "" {
r.certificate = "3rd Party Mac Developer Application"
}
if r.category == "" {
return errors.New("missing required -category parameter for macOS release")
} else if !isValidMacOSCategory(r.category) {
return errors.New("category does not match one of the supported list: " +
strings.Join(macAppStoreCategories, ", "))
}
} else if r.os == "ios" {
if r.certificate == "" {
r.certificate = "Apple Distribution"
}
if r.profile == "" {
return errors.New("missing required -profile parameter for iOS release")
}
}
return nil
}
func (r *releaser) zipAlign(path string) error {
unaligned := filepath.Join(filepath.Dir(path), "unaligned.apk")
err := os.Rename(path, unaligned)
if err != nil {
return nil
}
cmd := filepath.Join(util.AndroidBuildToolsPath(), "zipalign")
err = exec.Command(cmd, "4", unaligned, path).Run()
if err != nil {
_ = os.Rename(path, unaligned) // ignore error, return previous
return err
}
return os.Remove(unaligned)
}
func findWindowsSDKBin() (string, error) {
inPath, err := exec.LookPath("makeappx.exe")
if err == nil {
return inPath, nil
}
matches, err := filepath.Glob("C:\\Program Files (x86)\\Windows Kits\\*\\bin\\*\\*\\makeappx.exe")
if err != nil || len(matches) == 0 {
return "", errors.New("failed to look up standard locations for makeappx.exe")
}
return filepath.Dir(matches[0]), nil
}
func isValidMacOSCategory(in string) bool {
found := false
for _, cat := range macAppStoreCategories {
if cat == strings.ToLower(in) {
found = true
break
}
}
return found
}