This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
/
dist.go
221 lines (208 loc) · 7.53 KB
/
dist.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
// Copyright ©2018-2020 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package cmd
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"runtime"
"strconv"
"time"
"github.com/richardwilkes/toolbox"
"github.com/richardwilkes/toolbox/atexit"
"github.com/richardwilkes/toolbox/cmdline"
"github.com/richardwilkes/toolbox/xio/fs"
)
type dist struct {
root string
bundleName string
bundleID string
exeName string
icon string
version string
shortVersion string
copyrightYears string
copyrightOwner string
}
// NewDist returns the dist command.
func NewDist() cmdline.Cmd {
d := &dist{
root: "dist",
bundleName: "Example",
bundleID: "com.example",
exeName: "example",
version: "1.0.0",
shortVersion: "1.0",
copyrightYears: strconv.Itoa(time.Now().Year()),
copyrightOwner: "Unknown",
}
switch runtime.GOOS {
case toolbox.MacOS:
d.root = path.Join(d.root, "macos")
d.icon = "AppIcon.icns"
default:
d.root = path.Join(d.root, runtime.GOOS)
}
return d
}
func (d *dist) Name() string {
return "dist"
}
func (d *dist) Usage() string {
return "Creates a distribution tree, adding the necessary CEF libraries."
}
func (d *dist) Run(cl *cmdline.CmdLine, args []string) error {
cl.NewStringOption(&d.root).SetSingle('d').SetName("dir").SetUsage("Set the root distribution directory")
cl.NewStringOption(&d.bundleName).SetSingle('b').SetName("bundle").SetUsage("Set the bundle name (macOS-only)")
cl.NewStringOption(&d.bundleID).SetSingle('B').SetName("id").SetUsage("Set the bundle ID (macOS-only)")
cl.NewStringOption(&d.exeName).SetSingle('e').SetName("executable").SetUsage("Set the executable name (macOS-only)")
cl.NewStringOption(&d.icon).SetSingle('i').SetName("icon").SetUsage("Set the icon (macOS-only)")
cl.NewStringOption(&d.version).SetSingle('r').SetName("release").SetUsage("Set the release version (macOS-only)")
cl.NewStringOption(&d.shortVersion).SetSingle('s').SetName("short-release").SetUsage("Set the short release version (macOS-only)")
cl.NewStringOption(&d.copyrightYears).SetSingle('y').SetName("year").SetUsage("Set the copyright year(s) (macOS-only)")
cl.NewStringOption(&d.copyrightOwner).SetSingle('o').SetName("owner").SetUsage("Set the copyright owner (macOS-only)")
cl.Parse(args)
checkPlatform()
if err := os.RemoveAll(d.root); err != nil {
fmt.Printf("Unable to remove old dist at %s\n", d.root)
fmt.Println(err)
atexit.Exit(1)
}
createDir(d.root, 0755) //nolint:gocritic
switch runtime.GOOS {
case toolbox.MacOS:
d.distMacOS()
case toolbox.LinuxOS, toolbox.WindowsOS:
d.distNotMacOS()
default:
return fmt.Errorf("unhandled OS: %s", runtime.GOOS)
}
return nil
}
func (d *dist) distMacOS() {
appBundleContentsDir := path.Join(d.root, d.bundleName+".app", "Contents")
createDir(path.Join(appBundleContentsDir, "MacOS"), 0755) //nolint:gocritic
appBundleResourcesDir := path.Join(appBundleContentsDir, "Resources")
createDir(appBundleResourcesDir, 0755) //nolint:gocritic
appFrameworksDir := path.Join(appBundleContentsDir, "Frameworks")
helperAppBundleContentsDir := path.Join(appFrameworksDir, d.exeName+" Helper.app", "Contents")
helperAppBundleMacOSDir := path.Join(helperAppBundleContentsDir, "MacOS")
createDir(helperAppBundleMacOSDir, 0755) //nolint:gocritic
createDir(path.Join(helperAppBundleContentsDir, "Frameworks"), 0755) //nolint:gocritic
releaseDir := path.Join(installPrefix, "Release")
cc := exec.Command("cc", "-I", installPrefix, path.Join(installPrefix, "helper", "helper.c"), "-F", releaseDir, "-framework", "Chromium Embedded Framework", "-o", path.Join(helperAppBundleMacOSDir, d.exeName+" Helper")) //nolint:gosec
if result, err := cc.CombinedOutput(); err != nil {
fmt.Println("Failed to compile the helper.")
fmt.Println(err)
fmt.Println(string(result))
atexit.Exit(1)
}
if err := fs.Copy(path.Join(releaseDir, "Chromium Embedded Framework.framework"), path.Join(appFrameworksDir, "Chromium Embedded Framework.framework")); err != nil {
fmt.Println(err)
atexit.Exit(1)
}
if err := os.Symlink("../../../Chromium Embedded Framework.framework", path.Join(helperAppBundleContentsDir, "Frameworks", "Chromium Embedded Framework.framework")); err != nil {
fmt.Println(err)
atexit.Exit(1)
}
if err := fs.Copy(d.icon, path.Join(appBundleResourcesDir, "AppIcon.icns")); err != nil {
fmt.Println(err)
atexit.Exit(1)
}
plist := path.Join(appBundleContentsDir, "Info.plist")
f, err := os.Create(plist)
checkFileError(err, "create", plist)
_, err = fmt.Fprintf(f, `<?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>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleDisplayName</key>
<string>%s</string>
<key>CFBundleName</key>
<string>%s</string>
<key>CFBundleExecutable</key>
<string>%s</string>
<key>CFBundleIconFile</key>
<string>AppIcon.icns</string>
<key>CFBundleIdentifier</key>
<string>%s</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleVersion</key>
<string>%s</string>
<key>CFBundleShortVersionString</key>
<string>%s</string>
<key>NSHumanReadableCopyright</key>
<string>© %s by %s. All rights reserved.</string>
<key>NSHighResolutionCapable</key>
<true/>
<key>NSSupportsAutomaticGraphicsSwitching</key>
<true/>
</dict>
</plist>
`, d.bundleName, d.bundleName, d.exeName, d.bundleID, d.version, d.shortVersion, d.copyrightYears, d.copyrightOwner)
checkFileError(err, "write", plist)
checkFileError(f.Close(), "write", plist)
plist = path.Join(helperAppBundleContentsDir, "Info.plist")
f, err = os.Create(plist)
checkFileError(err, "create", plist)
_, err = fmt.Fprintf(f, `<?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>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleDisplayName</key>
<string>%s Helper</string>
<key>CFBundleName</key>
<string>%s Helper</string>
<key>CFBundleExecutable</key>
<string>%s Helper</string>
<key>CFBundleIdentifier</key>
<string>%s.helper</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleVersion</key>
<string>%s</string>
<key>CFBundleShortVersionString</key>
<string>%s</string>
<key>NSHumanReadableCopyright</key>
<string>© %s by %s. All rights reserved.</string>
<key>NSHighResolutionCapable</key>
<true/>
<key>NSSupportsAutomaticGraphicsSwitching</key>
<true/>
</dict>
</plist>
`, d.bundleName, d.bundleName, d.exeName, d.bundleID, d.version, d.shortVersion, d.copyrightYears, d.copyrightOwner)
checkFileError(err, "write", plist)
checkFileError(f.Close(), "write", plist)
}
func (d *dist) distNotMacOS() {
copyDirContents(path.Join(installPrefix, "Release"), d.root)
copyDirContents(path.Join(installPrefix, "Resources"), d.root)
}
func copyDirContents(srcdir, dstdir string) {
list, err := ioutil.ReadDir(srcdir)
if err != nil {
fmt.Println(err)
atexit.Exit(1)
}
for _, one := range list {
name := one.Name()
if err = fs.Copy(path.Join(srcdir, name), path.Join(dstdir, name)); err != nil {
fmt.Println(err)
atexit.Exit(1)
}
}
}