-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.go
190 lines (181 loc) · 5.16 KB
/
main.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
package main
import (
"flag"
"fmt"
"image"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"github.com/JackMordaunt/icns"
"github.com/pkg/errors"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(2)
}
}
func run() error {
var (
name = flag.String("name", "My Go Application", "app name")
author = flag.String("author", "Appify by Machine Box", "author")
version = flag.String("version", "1.0", "app version")
identifier = flag.String("id", "", "bundle identifier")
icon = flag.String("icon", "", "icon image file (.icns|.png|.jpg|.jpeg)")
)
flag.Parse()
args := flag.Args()
if len(args) < 1 {
return errors.New("missing executable argument")
}
bin := args[0]
appname := *name + ".app"
contentsPath := filepath.Join(appname, "Contents")
appPath := filepath.Join(contentsPath, "MacOS")
resouresPath := filepath.Join(contentsPath, "Resources")
binPath := filepath.Join(appPath, appname)
if err := os.MkdirAll(appPath, 0777); err != nil {
return errors.Wrap(err, "os.MkdirAll appPath")
}
fdst, err := os.Create(binPath)
if err != nil {
return errors.Wrap(err, "create bin")
}
defer fdst.Close()
fsrc, err := os.Open(bin)
if err != nil {
if os.IsNotExist(err) {
return errors.New(bin + " not found")
}
return errors.Wrap(err, "os.Open")
}
defer fsrc.Close()
if _, err := io.Copy(fdst, fsrc); err != nil {
return errors.Wrap(err, "copy bin")
}
if err := exec.Command("chmod", "+x", appPath).Run(); err != nil {
return errors.Wrap(err, "chmod: "+appPath)
}
if err := exec.Command("chmod", "+x", binPath).Run(); err != nil {
return errors.Wrap(err, "chmod: "+binPath)
}
id := *identifier
if id == "" {
id = *author + "." + *name
}
info := infoListData{
Name: *name,
Executable: filepath.Join("MacOS", appname),
Identifier: id,
Version: *version,
InfoString: *name + " by " + *author,
ShortVersionString: *version,
}
if *icon != "" {
iconPath, err := prepareIcons(*icon, resouresPath)
if err != nil {
return errors.Wrap(err, "icon")
}
info.IconFile = filepath.Base(iconPath)
}
tpl, err := template.New("template").Parse(infoPlistTemplate)
if err != nil {
return errors.Wrap(err, "infoPlistTemplate")
}
fplist, err := os.Create(filepath.Join(contentsPath, "Info.plist"))
if err != nil {
return errors.Wrap(err, "create Info.plist")
}
defer fplist.Close()
if err := tpl.Execute(fplist, info); err != nil {
return errors.Wrap(err, "execute Info.plist template")
}
if err := ioutil.WriteFile(filepath.Join(contentsPath, "README"), []byte(readme), 0666); err != nil {
return errors.Wrap(err, "ioutil.WriteFile")
}
return nil
}
func prepareIcons(iconPath, resourcesPath string) (string, error) {
ext := filepath.Ext(strings.ToLower(iconPath))
fsrc, err := os.Open(iconPath)
if err != nil {
if os.IsNotExist(err) {
return "", errors.New("icon file not found")
}
return "", errors.Wrap(err, "open icon file")
}
defer fsrc.Close()
if err := os.MkdirAll(resourcesPath, 0777); err != nil {
return "", errors.Wrap(err, "os.MkdirAll resourcesPath")
}
destFile := filepath.Join(resourcesPath, "icon.icns")
fdst, err := os.Create(destFile)
if err != nil {
return "", errors.Wrap(err, "create icon.icns file")
}
defer fdst.Close()
switch ext {
case ".icns": // just copy the .icns file
_, err := io.Copy(fdst, fsrc)
if err != nil {
return destFile, errors.Wrap(err, "copying "+iconPath)
}
case ".png", ".jpg", ".jpeg", ".gif": // process any images
srcImg, _, err := image.Decode(fsrc)
if err != nil {
return destFile, errors.Wrap(err, "decode image")
}
if err := icns.Encode(fdst, srcImg); err != nil {
return destFile, errors.Wrap(err, "generate icns file")
}
default:
return destFile, errors.New(ext + " icons not supported")
}
return destFile, nil
}
type infoListData struct {
Name string
Executable string
Identifier string
Version string
InfoString string
ShortVersionString string
IconFile string
}
const infoPlistTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>{{ .Name }}</string>
<key>CFBundleExecutable</key>
<string>{{ .Executable }}</string>
<key>CFBundleIdentifier</key>
<string>{{ .Identifier }}</string>
<key>CFBundleVersion</key>
<string>{{ .Version }}</string>
<key>CFBundleGetInfoString</key>
<string>{{ .InfoString }}</string>
<key>CFBundleShortVersionString</key>
<string>{{ .ShortVersionString }}</string>
{{ if .IconFile -}}
<key>CFBundleIconFile</key>
<string>{{ .IconFile }}</string>
{{- end }}
</dict>
</plist>
`
// readme goes into a README file inside the package for
// future reference.
const readme = `Made with Appify by Machine Box
https://github.com/machinebox/appify
Inspired by https://gist.github.com/anmoljagetia/d37da67b9d408b35ac753ce51e420132
`