forked from wailsapp/wails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packager.go
301 lines (255 loc) · 6.34 KB
/
packager.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
package build
import (
"bytes"
"fmt"
"image"
"os"
"path/filepath"
"runtime"
"github.com/leaanthony/winicon"
"github.com/tc-hib/winres"
"github.com/tc-hib/winres/version"
"github.com/frankfang/wails/v2/pkg/buildassets"
"github.com/jackmordaunt/icns"
"github.com/pkg/errors"
"github.com/frankfang/wails/v2/internal/fs"
)
// PackageProject packages the application
func packageProject(options *Options, platform string) error {
var err error
switch platform {
case "darwin":
err = packageApplicationForDarwin(options)
case "windows":
err = packageApplicationForWindows(options)
case "linux":
err = packageApplicationForLinux(options)
default:
err = fmt.Errorf("packing not supported for %s yet", platform)
}
if err != nil {
return err
}
return nil
}
// cleanBuildDirectory will remove an existing build directory and recreate it
func cleanBuildDirectory(options *Options) error {
buildDirectory := options.BuildDirectory
// Clear out old builds
if fs.DirExists(buildDirectory) {
err := os.RemoveAll(buildDirectory)
if err != nil {
return err
}
}
// Create clean directory
err := os.MkdirAll(buildDirectory, 0700)
if err != nil {
return err
}
return nil
}
// Gets the platform dependent package assets directory
func getPackageAssetsDirectory() string {
return fs.RelativePath("internal/packager", runtime.GOOS)
}
func packageApplicationForDarwin(options *Options) error {
var err error
// Create directory structure
bundlename := options.BundleName
if bundlename == "" {
bundlename = options.ProjectData.Name + ".app"
}
contentsDirectory := filepath.Join(options.BuildDirectory, bundlename, "/Contents")
exeDir := filepath.Join(contentsDirectory, "/MacOS")
err = fs.MkDirs(exeDir, 0755)
if err != nil {
return err
}
resourceDir := filepath.Join(contentsDirectory, "/Resources")
err = fs.MkDirs(resourceDir, 0755)
if err != nil {
return err
}
// Copy binary
packedBinaryPath := filepath.Join(exeDir, options.ProjectData.Name)
err = fs.MoveFile(options.CompiledBinary, packedBinaryPath)
if err != nil {
return errors.Wrap(err, "Cannot move file: "+options.ProjectData.OutputFilename)
}
// Generate Info.plist
err = processPList(options, contentsDirectory)
if err != nil {
return err
}
// Generate Icons
err = processApplicationIcon(options, resourceDir)
if err != nil {
return err
}
options.CompiledBinary = packedBinaryPath
return nil
}
func processPList(options *Options, contentsDirectory string) error {
// Read the resolved BuildAssets file and copy it to the destination
content, err := buildassets.ReadFileWithProjectData(options.ProjectData, "darwin/Info.plist")
if err != nil {
return err
}
targetFile := filepath.Join(contentsDirectory, "Info.plist")
return os.WriteFile(targetFile, content, 0644)
}
func processApplicationIcon(options *Options, resourceDir string) (err error) {
appIcon, err := buildassets.ReadFile(options.ProjectData, "appicon.png")
if err != nil {
return err
}
srcImg, _, err := image.Decode(bytes.NewBuffer(appIcon))
if err != nil {
return err
}
tgtBundle := filepath.Join(resourceDir, "iconfile.icns")
dest, err := os.Create(tgtBundle)
if err != nil {
return err
}
defer func() {
err = dest.Close()
if err == nil {
return
}
}()
return icns.Encode(dest, srcImg)
}
func packageApplicationForWindows(options *Options) error {
// Generate icon
var err error
err = generateIcoFile(options)
if err != nil {
return err
}
// Create syso file
err = compileResources(options)
if err != nil {
return err
}
return nil
}
func packageApplicationForLinux(options *Options) error {
// Generate icon
//var err error
//err = generateIcoFile(options)
//if err != nil {
// return err
//}
//
//// Ensure Manifest is present
//err = generateManifest(options)
//if err != nil {
// return err
//}
//
//// Create syso file
//err = compileResources(options)
//if err != nil {
// return err
//}
return nil
}
func generateManifest(options *Options) error {
return nil
}
func generateIcoFile(options *Options) error {
content, err := buildassets.ReadFile(options.ProjectData, "appicon.png")
if err != nil {
return err
}
// Check ico file exists already
icoFile := buildassets.GetLocalPath(options.ProjectData, "windows/icon.ico")
if !fs.FileExists(icoFile) {
if dir := filepath.Dir(icoFile); !fs.DirExists(dir) {
if err := fs.MkDirs(dir, 0755); err != nil {
return err
}
}
output, err := os.OpenFile(icoFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer output.Close()
err = winicon.GenerateIcon(bytes.NewBuffer(content), output, []int{256, 128, 64, 48, 32, 16})
if err != nil {
return err
}
}
return nil
}
func compileResources(options *Options) error {
currentDir, err := os.Getwd()
if err != nil {
return err
}
defer func() {
os.Chdir(currentDir)
}()
windowsDir := filepath.Join(options.ProjectData.Path, "build", "windows")
err = os.Chdir(windowsDir)
if err != nil {
return err
}
rs := winres.ResourceSet{}
icon := filepath.Join(windowsDir, "icon.ico")
iconFile, err := os.Open(icon)
if err != nil {
return err
}
defer iconFile.Close()
ico, err := winres.LoadICO(iconFile)
if err != nil {
return fmt.Errorf("couldn't load icon from icon.ico: %w", err)
}
err = rs.SetIcon(winres.RT_ICON, ico)
if err != nil {
return err
}
manifestData, err := buildassets.ReadFileWithProjectData(options.ProjectData, "windows/wails.exe.manifest")
if err != nil {
return err
}
xmlData, err := winres.AppManifestFromXML(manifestData)
if err != nil {
return err
}
rs.SetManifest(xmlData)
versionInfo, err := buildassets.ReadFileWithProjectData(options.ProjectData, "windows/info.json")
if err != nil {
return err
}
if len(versionInfo) != 0 {
var v version.Info
if err := v.UnmarshalJSON(versionInfo); err != nil {
return err
}
rs.SetVersionInfo(v)
}
targetFile := filepath.Join(options.ProjectData.Path, options.ProjectData.Name+"-res.syso")
fout, err := os.Create(targetFile)
if err != nil {
return err
}
defer fout.Close()
archs := map[string]winres.Arch{
"amd64": winres.ArchAMD64,
"arm64": winres.ArchARM64,
"386": winres.ArchI386,
}
targetArch, supported := archs[options.Arch]
if !supported {
return fmt.Errorf("arch '%s' not supported", options.Arch)
}
err = rs.WriteObject(fout, targetArch)
if err != nil {
return err
}
return nil
}