/
package-darwin.go
77 lines (67 loc) · 1.94 KB
/
package-darwin.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
package commands
import (
"image"
"os"
"path/filepath"
"strings"
"fyne.io/fyne/v2/cmd/fyne/internal/templates"
"fyne.io/fyne/v2/cmd/fyne/internal/util"
"github.com/jackmordaunt/icns"
"github.com/pkg/errors"
)
type darwinData struct {
Name, ExeName string
AppID string
Version string
Build int
Category string
}
func (p *Packager) packageDarwin() (err error) {
appDir := util.EnsureSubDir(p.dir, p.name+".app")
exeName := filepath.Base(p.exe)
contentsDir := util.EnsureSubDir(appDir, "Contents")
info := filepath.Join(contentsDir, "Info.plist")
infoFile, err := os.Create(info)
if err != nil {
return errors.Wrap(err, "Failed to write plist template")
}
defer func() {
if r := infoFile.Close(); r != nil && err == nil {
err = r
}
}()
tplData := darwinData{Name: p.name, ExeName: exeName, AppID: p.appID, Version: p.appVersion, Build: p.appBuild,
Category: strings.ToLower(p.category)}
if err := templates.InfoPlistDarwin.Execute(infoFile, tplData); err != nil {
return errors.Wrap(err, "Failed to write plist template")
}
macOSDir := util.EnsureSubDir(contentsDir, "MacOS")
binName := filepath.Join(macOSDir, exeName)
if err := util.CopyExeFile(p.exe, binName); err != nil {
return errors.Wrap(err, "Failed to copy exe file")
}
resDir := util.EnsureSubDir(contentsDir, "Resources")
icnsPath := filepath.Join(resDir, "icon.icns")
img, err := os.Open(p.icon)
if err != nil {
return errors.Wrapf(err, "Failed to open source image \"%s\"", p.icon)
}
defer img.Close()
srcImg, _, err := image.Decode(img)
if err != nil {
return errors.Wrapf(err, "Failed to decode source image")
}
dest, err := os.Create(icnsPath)
if err != nil {
return errors.Wrap(err, "Failed to open destination file")
}
defer func() {
if r := dest.Close(); r != nil && err == nil {
err = r
}
}()
if err := icns.Encode(dest, srcImg); err != nil {
return errors.Wrap(err, "Failed to encode icns")
}
return nil
}