-
Notifications
You must be signed in to change notification settings - Fork 4
/
setapp.go
101 lines (88 loc) · 2.74 KB
/
setapp.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
package svg
import (
"fmt"
"path/filepath"
"strings"
"sync"
"github.com/pkg/errors"
"go.uber.org/zap"
"projectforge.dev/projectforge/app/lib/filesystem"
"projectforge.dev/projectforge/app/project"
"projectforge.dev/projectforge/app/util"
)
const (
pngMsg = "convert -density 1000 -resize %dx%d -define png:exclude-chunks=date,time logo.svg %s"
noBG = "convert -density 1000 -background none -resize %dx%d -define png:exclude-chunks=date,time logo.svg %s"
)
func proc(cmd string, path string) error {
exit, out, err := util.RunProcessSimple(cmd, path)
if err != nil {
return errors.Wrap(err, "unable to convert [logo.png]")
}
if exit != 0 {
return errors.Wrapf(errors.Errorf("bad output: %s", out), "unable to convert [logo.png], exit code [%d]", exit)
}
return nil
}
func SetAppIcon(prj *project.Project, fs filesystem.FileLoader, x *SVG, logger *zap.SugaredLogger) error {
orig := x.Markup
for strings.Contains(orig, "<!--") {
startIdx := strings.Index(orig, "<!--")
endIdx := strings.Index(orig, "-->")
if endIdx == -1 {
break
}
orig = strings.TrimPrefix(orig[:startIdx]+orig[endIdx+3:], "\n")
}
var wg sync.WaitGroup
wg.Add(4)
var errs []error
queue := func(f func(prj *project.Project, orig string, fs filesystem.FileLoader, logger *zap.SugaredLogger) error) {
err := f(prj, orig, fs, logger)
if err != nil {
errs = append(errs, err)
}
wg.Done()
}
queue(webAssets)
queue(androidAssets)
queue(iOSAssets)
queue(macOSAssets)
wg.Wait()
if len(errs) > 0 {
return errs[0]
}
// app icon
appIconContent := strings.ReplaceAll(orig, "svg-"+x.Key, "svg-app")
appIconContent = "<!-- $PF_IGNORE$ -->\n" + appIconContent
err := fs.WriteFile("client/src/svg/app.svg", []byte(appIconContent), filesystem.DefaultMode, true)
if err != nil {
return errors.Wrap(err, "unable to write initial [logo.svg]")
}
_, err = Build(fs)
if err != nil {
return errors.Wrap(err, "unable to build SVG library")
}
return nil
}
func webAssets(prj *project.Project, orig string, fs filesystem.FileLoader, logger *zap.SugaredLogger) error {
webResize := func(size int, fn string, p string) {
err := proc(fmt.Sprintf(noBG, size, size, fn), p)
if err != nil {
logger.Warnf("error processing icon [%s]: %+v", fn, err)
}
}
// web assets
webPath := filepath.Join(fs.Root(), "assets")
err := fs.WriteFile("assets/logo.svg", []byte(orig), filesystem.DefaultMode, true)
if err != nil {
return errors.Wrap(err, "unable to write initial [logo.svg]")
}
webResize(256, "logo.png", webPath)
webResize(64, "favicon.png", webPath)
err = proc("convert -density 1000 -background none logo.svg -define icon:auto-resize=128,64,32 favicon.ico", webPath)
if err != nil {
return errors.Wrap(err, "unable to convert [favicon.ico]")
}
return nil
}