forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 1
/
package.go
306 lines (274 loc) · 8.64 KB
/
package.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
package commands
import (
"flag"
"fmt"
// import image encodings
_ "image/jpeg"
_ "image/png"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"fyne.io/fyne/v2/cmd/fyne/internal/util"
)
const (
defaultAppBuild = 1
defaultAppVersion = "1.0.0"
)
// Package returns the cli command for packaging fyne applications
func Package() *cli.Command {
p := &Packager{}
return &cli.Command{
Name: "package",
Usage: "Packages an application for distribution.",
Description: "You may specify the -executable to package, otherwise -sourceDir will be built.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "target",
Aliases: []string{"os"},
Usage: "The mobile platform to target (android, android/arm, android/arm64, android/amd64, android/386, ios).",
Destination: &p.os,
},
&cli.StringFlag{
Name: "executable",
Aliases: []string{"exe"},
Usage: "The path to the executable, default is the current dir main binary",
Destination: &p.exe,
},
&cli.StringFlag{
Name: "name",
Usage: "The name of the application, default is the executable file name",
Destination: &p.name,
},
&cli.StringFlag{
Name: "tags",
Usage: "A comma-separated list of build tags.",
Destination: &p.tags,
},
&cli.StringFlag{
Name: "appVersion",
Usage: "Version number in the form x, x.y or x.y.z semantic version",
Destination: &p.appVersion,
},
&cli.IntFlag{
Name: "appBuild",
Usage: "Build number, should be greater than 0 and incremented for each build",
Destination: &p.appBuild,
},
&cli.StringFlag{
Name: "sourceDir",
Aliases: []string{"src"},
Usage: "The directory to package, if executable is not set.",
Destination: &p.srcDir,
},
&cli.StringFlag{
Name: "icon",
Usage: "The name of the application icon file.",
Value: "Icon.png",
Destination: &p.icon,
},
&cli.StringFlag{
Name: "appID",
Aliases: []string{"id"},
Usage: "For Android, darwin, iOS and Windows targets an appID in the form of a reversed domain name is required, for ios this must match a valid provisioning profile",
Destination: &p.appID,
},
&cli.BoolFlag{
Name: "release",
Usage: "Enable installation in release mode (disable debug etc).",
Destination: &p.release,
},
},
Action: func(_ *cli.Context) error {
return p.Package()
},
}
}
// Packager wraps executables into full GUI app packages.
type Packager struct {
name, srcDir, dir, exe, icon string
os, appID, appVersion string
appBuild int
install, release bool
certificate, profile string // optional flags for releasing
tags, category string
}
// AddFlags adds the flags for interacting with the package command.
//
// Deprecated: Access to the individual cli commands are being removed.
func (p *Packager) AddFlags() {
flag.StringVar(&p.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(&p.exe, "executable", "", "Specify an existing binary instead of building before package")
flag.StringVar(&p.srcDir, "sourceDir", "", "The directory to package, if executable is not set")
flag.StringVar(&p.name, "name", "", "The name of the application, default is the executable file name")
flag.StringVar(&p.icon, "icon", "Icon.png", "The name of the application icon file")
flag.StringVar(&p.appID, "appID", "", "For ios or darwin targets an appID is required, for ios this must \nmatch a valid provisioning profile")
flag.StringVar(&p.appVersion, "appVersion", "", "Version number in the form x, x.y or x.y.z semantic version")
flag.IntVar(&p.appBuild, "appBuild", 0, "Build number, should be greater than 0 and incremented for each build")
flag.BoolVar(&p.release, "release", false, "Should this package be prepared for release? (disable debug etc)")
flag.StringVar(&p.tags, "tags", "", "A comma-separated list of build tags")
}
// PrintHelp prints the help for the package command.
//
// Deprecated: Access to the individual cli commands are being removed.
func (*Packager) PrintHelp(indent string) {
fmt.Println(indent, "The package command prepares an application for installation and testing.")
fmt.Println(indent, "You may specify the -executable to package, otherwise -sourceDir will be built.")
fmt.Println(indent, "Command usage: fyne package [parameters]")
}
// Run runs the package command.
//
// Deprecated: A better version will be exposed in the future.
func (p *Packager) Run(_ []string) {
err := p.validate()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
err = p.doPackage()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
}
// Package starts the packaging process
func (p *Packager) Package() error {
err := p.validate()
if err != nil {
return err
}
err = p.doPackage()
if err != nil {
return err
}
return nil
}
func (p *Packager) buildPackage() error {
tags := strings.Split(p.tags, ",")
b := &builder{
os: p.os,
srcdir: p.srcDir,
release: p.release,
tags: tags,
}
return b.build()
}
func (p *Packager) combinedVersion() string {
return fmt.Sprintf("%s.%d", p.appVersion, p.appBuild)
}
func (p *Packager) doPackage() error {
// sensible defaults - validation deemed them optional
if p.appVersion == "" {
p.appVersion = defaultAppVersion
}
if p.appBuild <= 0 {
p.appBuild = defaultAppBuild
}
if !util.Exists(p.exe) && !util.IsMobile(p.os) {
err := p.buildPackage()
if err != nil {
return errors.Wrap(err, "Error building application")
}
if !util.Exists(p.exe) {
return fmt.Errorf("unable to build directory to expected executable, %s", p.exe)
}
if p.os != "windows" {
defer p.removeBuild()
}
}
switch p.os {
case "darwin":
return p.packageDarwin()
case "linux", "openbsd", "freebsd", "netbsd":
return p.packageUNIX()
case "windows":
return p.packageWindows()
case "android/arm", "android/arm64", "android/amd64", "android/386", "android":
return p.packageAndroid(p.os)
case "ios":
return p.packageIOS()
default:
return fmt.Errorf("unsupported target operating system \"%s\"", p.os)
}
}
func (p *Packager) removeBuild() {
err := os.Remove(p.exe)
if err != nil {
log.Println("Unable to remove temporary build file", p.exe)
}
}
func (p *Packager) validate() error {
if p.os == "" {
p.os = targetOS()
}
baseDir, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "Unable to get the current directory, needed to find main executable")
}
if p.dir == "" {
p.dir = baseDir
}
if p.srcDir == "" {
p.srcDir = baseDir
} else if p.os == "ios" || p.os == "android" {
return errors.New("Parameter -sourceDir is currently not supported for mobile builds.\n" +
"Change directory to the main package and try again.")
}
exeName := calculateExeName(p.srcDir, p.os)
if p.exe == "" {
p.exe = filepath.Join(p.srcDir, exeName)
if util.Exists(p.exe) { // the exe was not specified, assume stale
p.removeBuild()
}
} else if p.os == "ios" || p.os == "android" {
_, _ = fmt.Fprint(os.Stderr, "Parameter -executable is ignored for mobile builds.\n")
}
if p.name == "" {
p.name = exeName
}
if p.icon == "" || p.icon == "Icon.png" {
p.icon = filepath.Join(p.srcDir, "Icon.png")
}
if !util.Exists(p.icon) {
return errors.New("Missing application icon at \"" + p.icon + "\"")
}
p.appID, err = validateAppID(p.appID, p.os, p.name, p.release)
if err != nil {
return err
}
if p.appVersion != "" && !isValidVersion(p.appVersion) {
return errors.New("invalid -appVersion parameter, integer and '.' characters only up to x.y.z")
}
return nil
}
func isValidVersion(ver string) bool {
nums := strings.Split(ver, ".")
if len(nums) == 0 || len(nums) > 3 {
return false
}
for _, num := range nums {
if _, err := strconv.Atoi(num); err != nil {
return false
}
}
return true
}
func validateAppID(appID, os, name string, release bool) (string, error) {
// old darwin compatibility
if os == "darwin" {
if appID == "" {
return "com.example." + name, nil
}
} else if os == "ios" || util.IsAndroid(os) || (os == "windows" && release) {
// all mobile, and for windows when releasing, needs a unique id - usually reverse DNS style
if appID == "" {
return "", errors.New("Missing appID parameter for package")
} else if !strings.Contains(appID, ".") {
return "", errors.New("appID must be globally unique and contain at least 1 '.'")
}
}
return appID, nil
}