/
pack.go
395 lines (356 loc) · 9.22 KB
/
pack.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
389
390
391
392
393
394
395
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cmd
import (
_ "embed"
"os"
"path/filepath"
"strings"
"text/template"
"cogentcore.org/core/base/exec"
"cogentcore.org/core/base/iox/imagex"
"cogentcore.org/core/base/strcase"
"cogentcore.org/core/cmd/core/config"
"cogentcore.org/core/cmd/core/rendericon"
"github.com/jackmordaunt/icns/v2"
)
// Pack builds and packages the app for the target platform.
// For android, ios, and web, it is equivalent to build.
func Pack(c *config.Config) error { //types:add
err := Build(c)
if err != nil {
return err
}
for _, platform := range c.Build.Target {
switch platform.OS {
case "android", "ios", "web": // build already packages
continue
case "linux":
err := PackLinux(c)
if err != nil {
return err
}
case "darwin":
err := PackDarwin(c)
if err != nil {
return err
}
case "windows":
err := PackWindows(c)
if err != nil {
return err
}
}
}
return nil
}
// PackLinux packages the app for Linux by generating a .deb file.
func PackLinux(c *config.Config) error {
// based on https://ubuntuforums.org/showthread.php?t=910717
anm := strcase.ToKebab(c.Name)
vnm := strings.TrimPrefix(c.Version, "v")
avnm := anm + "_" + vnm
bpath := filepath.Join("bin", "linux")
apath := filepath.Join(bpath, avnm)
ulbpath := filepath.Join(apath, "usr", "local", "bin")
usipath := filepath.Join(apath, "usr", "share", "icons", "hicolor")
usapath := filepath.Join(apath, "usr", "share", "applications")
dpath := filepath.Join(apath, "DEBIAN")
err := os.MkdirAll(ulbpath, 0777)
if err != nil {
return err
}
err = exec.Run("cp", "-p", c.Name, filepath.Join(ulbpath, anm))
if err != nil {
return err
}
// see https://martin.hoppenheit.info/blog/2016/where-to-put-application-icons-on-linux/
// TODO(kai): consider rendering more icon sizes and/or an XPM icon
ic, err := rendericon.Render(48)
if err != nil {
return err
}
i48path := filepath.Join(usipath, "48x48", "apps")
err = os.MkdirAll(i48path, 0777)
if err != nil {
return err
}
err = imagex.Save(ic, filepath.Join(i48path, anm+".png"))
if err != nil {
return err
}
iscpath := filepath.Join(usipath, "scalable", "apps")
err = os.MkdirAll(iscpath, 0777)
if err != nil {
return err
}
err = exec.Run("cp", "icon.svg", filepath.Join(iscpath, anm+".svg"))
if err != nil {
return err
}
// we need a description
if c.Desc == "" {
c.Desc = c.Name
}
err = os.MkdirAll(usapath, 0777)
if err != nil {
return err
}
fapp, err := os.Create(filepath.Join(usapath, anm+".desktop"))
if err != nil {
return err
}
defer fapp.Close()
dfd := &DesktopFileData{
Name: c.Name,
Desc: c.Desc,
Exec: anm,
}
err = DesktopFileTmpl.Execute(fapp, dfd)
if err != nil {
return err
}
err = os.MkdirAll(dpath, 0777)
if err != nil {
return err
}
fctrl, err := os.Create(filepath.Join(dpath, "control"))
if err != nil {
return err
}
defer fctrl.Close()
dcd := &DebianControlData{
Name: anm,
Version: vnm,
Desc: c.Desc,
}
err = DebianControlTmpl.Execute(fctrl, dcd)
if err != nil {
return err
}
return exec.Run("dpkg-deb", "--build", apath)
}
// DesktopFileData is the data passed to [DesktopFileTmpl]
type DesktopFileData struct {
Name string
Desc string
Exec string
}
// TODO(kai): project website
// DesktopFileTmpl is the template for the Linux .desktop file
var DesktopFileTmpl = template.Must(template.New("DesktopFileTmpl").Parse(
`[Desktop Entry]
Type=Application
Version=1.0
Name={{.Name}}
Comment={{.Desc}}
Exec={{.Exec}}
Icon={{.Exec}}
Terminal=false
`))
// DebianControlData is the data passed to [DebianControlTmpl]
type DebianControlData struct {
Name string
Version string
Desc string
}
// TODO(kai): architecture, maintainer, dependencies
// DebianControlTmpl is the template for the Linux DEBIAN/control file
var DebianControlTmpl = template.Must(template.New("DebianControlTmpl").Parse(
`Package: {{.Name}}
Version: {{.Version}}
Section: base
Priority: optional
Architecture: all
Maintainer: Your Name <you@email.com>
Description: {{.Desc}}
`))
// PackDarwin packages the app for macOS by generating a .app and .dmg file.
func PackDarwin(c *config.Config) error {
// based on https://github.com/machinebox/appify
anm := c.Name + ".app"
bpath := filepath.Join("bin", "darwin")
apath := filepath.Join(bpath, anm)
cpath := filepath.Join(apath, "Contents")
mpath := filepath.Join(cpath, "MacOS")
rpath := filepath.Join(cpath, "Resources")
err := os.MkdirAll(mpath, 0777)
if err != nil {
return err
}
err = os.MkdirAll(rpath, 0777)
if err != nil {
return err
}
err = exec.Run("cp", "-p", c.Name, filepath.Join(mpath, anm))
if err != nil {
return err
}
err = exec.Run("chmod", "+x", mpath)
if err != nil {
return err
}
inm := filepath.Join(rpath, "icon.icns")
fdsi, err := os.Create(inm)
if err != nil {
return err
}
defer fdsi.Close()
// 1024x1024 is the largest icon size on macOS
sic, err := rendericon.Render(1024)
if err != nil {
return err
}
err = icns.Encode(fdsi, sic)
if err != nil {
return err
}
fplist, err := os.Create(filepath.Join(cpath, "Info.plist"))
if err != nil {
return err
}
defer fplist.Close()
ipd := &InfoPlistData{
Name: c.Name,
Executable: filepath.Join("MacOS", anm),
Identifier: c.ID,
Version: c.Version,
InfoString: c.Desc,
ShortVersionString: c.Version,
IconFile: filepath.Join("Contents", "Resources", "icon.icns"),
}
err = InfoPlistTmpl.Execute(fplist, ipd)
if err != nil {
return err
}
if !c.Pack.DMG {
return nil
}
// install dmgbuild if we don't already have it
if _, err := exec.LookPath("dmgbuild"); err != nil {
err = exec.Verbose().SetBuffer(false).Run("pip", "install", "dmgbuild")
if err != nil {
return err
}
}
dmgsnm := filepath.Join(bpath, ".tempDmgBuildSettings.py")
fdmg, err := os.Create(dmgsnm)
if err != nil {
return err
}
defer fdmg.Close()
dmgbd := &DmgBuildData{
AppPath: apath,
AppName: anm,
IconPath: inm,
}
err = DmgBuildTmpl.Execute(fdmg, dmgbd)
if err != nil {
return err
}
err = exec.Run("dmgbuild",
"-s", dmgsnm,
c.Name, filepath.Join(bpath, c.Name+".dmg"))
if err != nil {
return err
}
return os.Remove(dmgsnm)
}
// InfoPlistData is the data passed to [InfoPlistTmpl]
type InfoPlistData struct {
Name string
Executable string
Identifier string
Version string
InfoString string
ShortVersionString string
IconFile string
}
// InfoPlistTmpl is the template for the macOS .app Info.plist
var InfoPlistTmpl = template.Must(template.New("InfoPlistTmpl").Parse(
`<?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>
<key>CFBundleIconFile</key>
<string>{{ .IconFile }}</string>
</dict>
</plist>
`))
// DmgBuildData is the data passed to [DmgBuildTmpl]
type DmgBuildData struct {
AppPath string
AppName string
IconPath string
}
// DmgBuildTmpl is the template for the dmgbuild python settings file
var DmgBuildTmpl = template.Must(template.New("DmgBuildTmpl").Parse(
`files = ['{{.AppPath}}']
symlinks = {"Applications": "/Applications"}
icon = '{{.IconPath}}'
icon_locations = {'{{.AppName}}': (140, 120), "Applications": (500, 120)}
background = "builtin-arrow"
`))
// PackWindows packages the app for Windows by generating a .msi file.
func PackWindows(c *config.Config) error {
opath := filepath.Join("bin", "windows")
ipath := filepath.Join(opath, "tempWindowsInstaller")
gpath := filepath.Join(ipath, "installer.go")
epath := filepath.Join(opath, c.Name+" Installer.exe")
err := os.MkdirAll(ipath, 0777)
if err != nil {
return err
}
fman, err := os.Create(gpath)
if err != nil {
return err
}
wmd := &WindowsInstallerData{
Name: c.Name,
Desc: c.Desc,
}
err = WindowsInstallerTmpl.Execute(fman, wmd)
fman.Close()
if err != nil {
return err
}
err = exec.Run("cp", c.Name+".exe", filepath.Join(ipath, "app.exe"))
if err != nil {
return err
}
err = exec.Run("cp", "icon.svg", filepath.Join(ipath, "icon.svg"))
if err != nil {
return err
}
err = exec.Run("go", "build", "-o", epath, gpath)
if err != nil {
return err
}
return os.RemoveAll(ipath)
}
// WindowsInstallerData is the data passed to [WindowsInstallerTmpl]
type WindowsInstallerData struct {
Name string
Desc string
}
//go:embed windowsinstaller.go.tmpl
var windowsInstallerTmplString string
// WindowsInstallerTmpl is the template for the Windows installer Go file
var WindowsInstallerTmpl = template.Must(template.New("WindowsInstallerTmpl").Parse(windowsInstallerTmplString))