-
Notifications
You must be signed in to change notification settings - Fork 6
/
macapp.go
388 lines (343 loc) · 10.7 KB
/
macapp.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
var (
assetsDir string
binaryName string
iconFile string
appName string
outputDir string
bundleIdentifier string
templateDMG string
)
func init() {
flag.StringVar(&assetsDir, "assets", "", "The folder path that contains all the application assets")
flag.StringVar(&binaryName, "bin", "", "The name of the binary file, relative to the assets folder")
flag.StringVar(&iconFile, "icon", "", "The file of the icon to use for the application")
flag.StringVar(&appName, "name", "", "The user-facing name of the application")
flag.StringVar(&outputDir, "o", ".", "The folder into which to output the artefacts")
flag.StringVar(&bundleIdentifier, "identifier", "com.example.unknown", "The bundle identifier (make it your own)")
flag.StringVar(&templateDMG, "dmg", "", "If set, will package the app in a DMG based on this template")
}
func main() {
flag.Parse()
if assetsDir == "" || iconFile == "" || binaryName == "" || appName == "" {
log.Println("[ERROR] Assets directory, binary name, icon file, and application name are required.")
flag.PrintDefaults()
return
}
// make and fill out the .app bundle
appName = strings.TrimSuffix(appName, ".app")
appFilename := appName + ".app"
appBundleName := filepath.Join(outputDir, appFilename)
err := makeAppBundle(appBundleName)
if err != nil {
log.Fatalf("[ERROR] Making .app folder: %v", err)
}
// make the .dmg image from a template
if templateDMG != "" {
err := makeDMGFromTemplate(templateDMG, appBundleName)
if err != nil {
log.Fatalf("[ERROR] Making DMG from template: %v", err)
}
}
}
func makeAppBundle(appFilename string) error {
// make the basic directory structure
for _, dirName := range []string{
filepath.Join(appFilename, "Contents", "MacOS"),
filepath.Join(appFilename, "Contents", "Resources"),
} {
err := os.MkdirAll(dirName, 0755)
if err != nil {
return fmt.Errorf("making app folder structure: %v", err)
}
}
// write the Info.plist file into the bundle
infoPlist := strings.Replace(infoPlistTpl, "{{.AppName}}", binaryName, -1)
infoPlist = strings.Replace(infoPlist, "{{.BundleIdentifier}}", bundleIdentifier, -1)
infoPlistPath := filepath.Join(appFilename, "Contents", "Info.plist")
err := ioutil.WriteFile(infoPlistPath, []byte(infoPlist), 0644)
if err != nil {
return fmt.Errorf("writing plist file: %v", err)
}
// set the icons
err = makeAppIcons(appFilename)
if err != nil {
return fmt.Errorf("making icons: %v", err)
}
// copy the binary into the bundle
binarySrc := filepath.Join(assetsDir, binaryName)
binaryDest := filepath.Join(appFilename, "Contents", "MacOS", binaryName)
err = copyFile(binarySrc, binaryDest, nil)
if err != nil {
return fmt.Errorf("copying the binary into the bundle: %v", err)
}
// get the list of assets to copy
assetsDirFile, err := os.Open(assetsDir)
if err != nil {
return fmt.Errorf("opening assets directory: %v", err)
}
dirEntries, err := assetsDirFile.Readdirnames(100000)
if err != nil {
return fmt.Errorf("reading list of assets directory contents: %v", err)
}
// copy the assets into the bundle
for _, entry := range dirEntries {
if entry == binaryName {
continue // we already copied the binary, and it went into a different folder
}
src := filepath.Join(assetsDir, entry)
dest := filepath.Join(appFilename, "Contents", "Resources")
err = deepCopy(src, dest)
if err != nil {
return fmt.Errorf("copying assets '%s': %v", entry, err)
}
}
return nil
}
func makeAppIcons(appFolder string) error {
// start by copying the icon into the bundle
iconFilename := filepath.Base(iconFile)
resFolder := filepath.Join(appFolder, "Contents", "Resources")
copyTo := filepath.Join(resFolder, iconFilename)
err := copyFile(iconFile, copyTo, nil)
if err != nil {
return err
}
useIcon := iconFile // usable icon files are of type .png, .jpg, .gif, or .tiff - and we handle .svg
tmpFolder := filepath.Join(resFolder, "tmp")
err = os.MkdirAll(tmpFolder, 0755)
if err != nil {
return err
}
defer os.RemoveAll(tmpFolder)
// lazy way to convert SVG files to PNG, by using QuickLook
// -z displays generation performance info (instead of showing thumbnail)
// -t Computes the thumbnail
// -s sets the size of the thumbnail
// -o sets the output directory (NOT the actual output file)
if filepath.Ext(iconFile) == ".svg" {
cmd := exec.Command("qlmanage", "-z", "-t", "-s", "1024", "-o", tmpFolder, iconFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("running qlmanage: %v", err)
}
useIcon = filepath.Join(tmpFolder, iconFile+".png")
}
// make the various icon sizes
// see https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Optimizing/Optimizing.html
iconset := filepath.Join(tmpFolder, "icon.iconset")
err = os.Mkdir(iconset, 0755)
if err != nil {
return err
}
sizes := []int{16, 32, 64, 128, 256, 512, 1024}
for i, size := range sizes {
nameSize := size
var suffix string
if i > 0 {
nameSize = sizes[i-1]
suffix = "@2x"
}
iconName := fmt.Sprintf("icon_%dx%d%s.png", nameSize, nameSize, suffix)
outIconFile := filepath.Join(iconset, iconName)
sizeStr := fmt.Sprintf("%d", size)
cmd := exec.Command("sips", "-z", sizeStr, sizeStr, useIcon, "--out", outIconFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("running sips: %v", err)
}
// make standard-DPI version if we didn't already
if i > 0 && i < len(sizes)-1 {
stdName := fmt.Sprintf("icon_%dx%d.png", size, size)
err := copyFile(outIconFile, filepath.Join(iconset, stdName), nil)
if err != nil {
return fmt.Errorf("copying icon file: %v", err)
}
}
}
// create the final .icns file
icnsFile := filepath.Join(resFolder, "icon.icns")
cmd := exec.Command("iconutil", "-c", "icns", "-o", icnsFile, iconset)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("running iconutil: %v", err)
}
return nil
}
func makeDMGFromTemplate(templateDMG, appBundleName string) error {
tmpDir := "./tmp"
err := os.Mkdir(tmpDir, 0755)
if err != nil {
return fmt.Errorf("making temporary directory: %v", err)
}
defer os.RemoveAll(tmpDir)
// copy the template image, since we'll be modifying it
tmpDMG := "./tmp.dmg"
err = copyFile(templateDMG, tmpDMG, nil)
fmt.Println(templateDMG, tmpDMG)
if err != nil {
return fmt.Errorf("making copy of template DMG: %v", err)
}
defer os.Remove(tmpDMG)
// attach the template dmg
cmd := exec.Command("hdiutil", "attach", tmpDMG, "-noautoopen", "-mountpoint", tmpDir)
attachBuf := new(bytes.Buffer)
cmd.Stdout = attachBuf
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("running hdiutil attach: %v", err)
}
// move bundle file into it
err = deepCopy(appBundleName, tmpDir)
if err != nil {
return fmt.Errorf("copying app into dmg: %v", err)
}
// get attached image's device; it should be the
// first device that is outputted
hdiutilOutFields := strings.Fields(attachBuf.String())
if len(hdiutilOutFields) == 0 {
return fmt.Errorf("no device output by hdiutil attach")
}
dmgDevice := hdiutilOutFields[0]
// detach image
cmd = exec.Command("hdiutil", "detach", dmgDevice)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("running hdiutil detach: %v", err)
}
// convert to compressed image
outputDMG := filepath.Join(outputDir, appName+".dmg")
cmd = exec.Command("hdiutil", "convert", tmpDMG, "-format", "UDZO", "-imagekey", "zlib-level=9", "-o", outputDMG)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("running hdiutil convert: %v", err)
}
return nil
}
func copyFile(from, to string, fromInfo os.FileInfo) error {
log.Printf("[INFO] Copying %s to %s", from, to)
if fromInfo == nil {
var err error
fromInfo, err = os.Stat(from)
if err != nil {
return err
}
}
// open source file
fsrc, err := os.Open(from)
if err != nil {
return err
}
// create destination file, with identical permissions
fdest, err := os.OpenFile(to, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fromInfo.Mode()&os.ModePerm)
if err != nil {
fsrc.Close()
if _, err2 := os.Stat(to); err2 == nil {
return fmt.Errorf("opening destination (which already exists): %v", err)
}
return err
}
// copy the file and ensure it gets flushed to disk
if _, err = io.Copy(fdest, fsrc); err != nil {
fsrc.Close()
fdest.Close()
return err
}
if err = fdest.Sync(); err != nil {
fsrc.Close()
fdest.Close()
return err
}
// close both files
if err = fsrc.Close(); err != nil {
fdest.Close()
return err
}
if err = fdest.Close(); err != nil {
return err
}
return nil
}
// deepCopy makes a deep copy of from into to.
func deepCopy(from, to string) error {
if from == "" || to == "" {
return fmt.Errorf("no source or no destination; both required")
}
// traverse the source directory and copy each file
return filepath.Walk(from, func(path string, info os.FileInfo, err error) error {
// error accessing current file
if err != nil {
return err
}
// skip files/folders without a name
if info.Name() == "" {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
// if directory, create destination directory (if not
// already created by our pre-walk)
if info.IsDir() {
subdir := strings.TrimPrefix(path, filepath.Dir(from))
destDir := filepath.Join(to, subdir)
if _, err := os.Stat(destDir); os.IsNotExist(err) {
err := os.Mkdir(destDir, info.Mode()&os.ModePerm)
if err != nil {
return err
}
}
return nil
}
destPath := filepath.Join(to, strings.TrimPrefix(path, filepath.Dir(from)))
err = copyFile(path, destPath, info)
if err != nil {
return fmt.Errorf("copying file %s: %v", path, err)
}
return nil
})
}
// See https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW19
// for information about the Info.plist and bundling an application.
const infoPlistTpl = `<?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>CFBundleExecutable</key>
<string>{{.AppName}}</string>
<key>CFBundleIconFile</key>
<string>icon.icns</string>
<key>CFBundleIdentifier</key>
<string>{{.BundleIdentifier}}</string>
<key>NSHighResolutionCapable</key>
<true/>
<key>LSUIElement</key>
<true/>
</dict>
</plist>
`