forked from asticode/go-astilectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovisioner.go
322 lines (283 loc) · 11.2 KB
/
provisioner.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
package astilectron
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"github.com/asticode/go-astilog"
"github.com/asticode/go-astitools/os"
"github.com/asticode/go-astitools/regexp"
"github.com/pkg/errors"
)
// Var
var (
defaultHTTPClient = &http.Client{}
regexpDarwinInfoPList = regexp.MustCompile("<string>Electron")
)
// Provisioner represents an object capable of provisioning Astilectron
type Provisioner interface {
Provision(ctx context.Context, appName, os, arch string, p Paths) error
}
// mover is a function that moves a package
type mover func(ctx context.Context, p Paths) error
// DefaultProvisioner represents the default provisioner
var DefaultProvisioner = &defaultProvisioner{
moverAstilectron: func(ctx context.Context, p Paths) (err error) {
if err = Download(ctx, defaultHTTPClient, p.AstilectronDownloadSrc(), p.AstilectronDownloadDst()); err != nil {
return errors.Wrapf(err, "downloading %s into %s failed", p.AstilectronDownloadSrc(), p.AstilectronDownloadDst())
}
return
},
moverElectron: func(ctx context.Context, p Paths) (err error) {
if err = Download(ctx, defaultHTTPClient, p.ElectronDownloadSrc(), p.ElectronDownloadDst()); err != nil {
return errors.Wrapf(err, "downloading %s into %s failed", p.ElectronDownloadSrc(), p.ElectronDownloadDst())
}
return
},
}
// defaultProvisioner represents the default provisioner
type defaultProvisioner struct {
moverAstilectron mover
moverElectron mover
}
// provisionStatusElectronKey returns the electron's provision status key
func provisionStatusElectronKey(os, arch string) string {
return fmt.Sprintf("%s-%s", os, arch)
}
// Provision implements the provisioner interface
// TODO Package app using electron instead of downloading Electron + Astilectron separately
func (p *defaultProvisioner) Provision(ctx context.Context, appName, os, arch string, paths Paths) (err error) {
// Retrieve provision status
var s ProvisionStatus
if s, err = p.ProvisionStatus(paths); err != nil {
err = errors.Wrap(err, "retrieving provisioning status failed")
return
}
defer p.updateProvisionStatus(paths, &s)
// Provision astilectron
if err = p.provisionAstilectron(ctx, paths, s); err != nil {
err = errors.Wrap(err, "provisioning astilectron failed")
return
}
s.Astilectron = &ProvisionStatusPackage{Version: VersionAstilectron}
// Provision electron
if err = p.provisionElectron(ctx, paths, s, appName, os, arch); err != nil {
err = errors.Wrap(err, "provisioning electron failed")
return
}
s.Electron[provisionStatusElectronKey(os, arch)] = &ProvisionStatusPackage{Version: VersionElectron}
return
}
// ProvisionStatus represents the provision status
type ProvisionStatus struct {
Astilectron *ProvisionStatusPackage `json:"astilectron,omitempty"`
Electron map[string]*ProvisionStatusPackage `json:"electron,omitempty"`
}
// ProvisionStatusPackage represents the provision status of a package
type ProvisionStatusPackage struct {
Version string `json:"version"`
}
// ProvisionStatus returns the provision status
func (p *defaultProvisioner) ProvisionStatus(paths Paths) (s ProvisionStatus, err error) {
// Open the file
var f *os.File
s.Electron = make(map[string]*ProvisionStatusPackage)
if f, err = os.Open(paths.ProvisionStatus()); err != nil {
if !os.IsNotExist(err) {
err = errors.Wrapf(err, "opening file %s failed", paths.ProvisionStatus())
} else {
err = nil
}
return
}
defer f.Close()
// Unmarshal
if errLocal := json.NewDecoder(f).Decode(&s); errLocal != nil {
// For backward compatibility purposes, if there's an unmarshal error we delete the status file and make the
// assumption that provisioning has to be done all over again
astilog.Error(errors.Wrapf(errLocal, "json decoding from %s failed", paths.ProvisionStatus()))
astilog.Debugf("Removing %s", f.Name())
if errLocal = os.RemoveAll(f.Name()); errLocal != nil {
astilog.Error(errors.Wrapf(errLocal, "removing %s failed", f.Name()))
}
return
}
return
}
// ProvisionStatus updates the provision status
func (p *defaultProvisioner) updateProvisionStatus(paths Paths, s *ProvisionStatus) (err error) {
// Create the file
var f *os.File
if f, err = os.Create(paths.ProvisionStatus()); err != nil {
err = errors.Wrapf(err, "creating file %s failed", paths.ProvisionStatus())
return
}
defer f.Close()
// Marshal
if err = json.NewEncoder(f).Encode(s); err != nil {
err = errors.Wrapf(err, "json encoding into %s failed", paths.ProvisionStatus())
return
}
return
}
// provisionAstilectron provisions astilectron
func (p *defaultProvisioner) provisionAstilectron(ctx context.Context, paths Paths, s ProvisionStatus) error {
return p.provisionPackage(ctx, paths, s.Astilectron, p.moverAstilectron, "Astilectron", VersionAstilectron, paths.AstilectronUnzipSrc(), paths.AstilectronDirectory(), nil)
}
// provisionElectron provisions electron
func (p *defaultProvisioner) provisionElectron(ctx context.Context, paths Paths, s ProvisionStatus, appName, os, arch string) error {
return p.provisionPackage(ctx, paths, s.Electron[provisionStatusElectronKey(os, arch)], p.moverElectron, "Electron", VersionElectron, paths.ElectronUnzipSrc(), paths.ElectronDirectory(), func() (err error) {
switch os {
case "darwin":
if err = p.provisionElectronFinishDarwin(appName, paths); err != nil {
return errors.Wrap(err, "finishing provisioning electron for darwin systems failed")
}
default:
astilog.Debug("System doesn't require finshing provisioning electron, moving on...")
}
return
})
}
// provisionPackage provisions a package
func (p *defaultProvisioner) provisionPackage(ctx context.Context, paths Paths, s *ProvisionStatusPackage, m mover, name, version, pathUnzipSrc, pathDirectory string, finish func() error) (err error) {
// Package has already been provisioned
if s != nil && s.Version == version {
astilog.Debugf("%s has already been provisioned to version %s, moving on...", name, version)
return
}
astilog.Debugf("Provisioning %s...", name)
// Remove previous install
astilog.Debugf("Removing directory %s", pathDirectory)
if err = os.RemoveAll(pathDirectory); err != nil && !os.IsNotExist(err) {
return errors.Wrapf(err, "removing %s failed", pathDirectory)
}
// Move
if err = m(ctx, paths); err != nil {
return errors.Wrapf(err, "moving %s failed", name)
}
// Create directory
astilog.Debugf("Creating directory %s", pathDirectory)
if err = os.MkdirAll(pathDirectory, 0755); err != nil {
return errors.Wrapf(err, "mkdirall %s failed", pathDirectory)
}
// Unzip
if err = Unzip(ctx, pathUnzipSrc, pathDirectory); err != nil {
return errors.Wrapf(err, "unzipping %s into %s failed", pathUnzipSrc, pathDirectory)
}
// Finish
if finish != nil {
if err = finish(); err != nil {
return errors.Wrap(err, "finishing failed")
}
}
return
}
// provisionElectronFinishDarwin finishes provisioning electron for Darwin systems
// https://github.com/electron/electron/blob/v1.8.1/docs/tutorial/application-distribution.md#macos
func (p *defaultProvisioner) provisionElectronFinishDarwin(appName string, paths Paths) (err error) {
// Log
astilog.Debug("Finishing provisioning electron for darwin system")
// Custom app icon
if paths.AppIconDarwinSrc() != "" {
if err = p.provisionElectronFinishDarwinCopy(paths); err != nil {
return errors.Wrap(err, "copying for darwin system finish failed")
}
}
// Custom app name
if appName != "" {
// Replace
if err = p.provisionElectronFinishDarwinReplace(appName, paths); err != nil {
return errors.Wrap(err, "replacing for darwin system finish failed")
}
// Rename
if err = p.provisionElectronFinishDarwinRename(appName, paths); err != nil {
return errors.Wrap(err, "renaming for darwin system finish failed")
}
}
return
}
// provisionElectronFinishDarwinCopy copies the proper darwin files
func (p *defaultProvisioner) provisionElectronFinishDarwinCopy(paths Paths) (err error) {
// Icon
var src, dst = paths.AppIconDarwinSrc(), filepath.Join(paths.ElectronDirectory(), "Electron.app", "Contents", "Resources", "electron.icns")
if src != "" {
astilog.Debugf("Copying %s to %s", src, dst)
if err = astios.Copy(context.Background(), src, dst); err != nil {
return errors.Wrapf(err, "copying %s to %s failed", src, dst)
}
}
return
}
// provisionElectronFinishDarwinReplace makes the proper replacements in the proper darwin files
func (p *defaultProvisioner) provisionElectronFinishDarwinReplace(appName string, paths Paths) (err error) {
for _, p := range []string{
filepath.Join(paths.electronDirectory, "Electron.app", "Contents", "Info.plist"),
filepath.Join(paths.electronDirectory, "Electron.app", "Contents", "Frameworks", "Electron Helper.app", "Contents", "Info.plist"),
} {
// Log
astilog.Debugf("Replacing in %s", p)
// Read file
var b []byte
if b, err = ioutil.ReadFile(p); err != nil {
return errors.Wrapf(err, "reading %s failed", p)
}
// Open and truncate file
var f *os.File
if f, err = os.Create(p); err != nil {
return errors.Wrapf(err, "creating %s failed", p)
}
defer f.Close()
// Replace
astiregexp.ReplaceAll(regexpDarwinInfoPList, &b, []byte("<string>"+appName))
// Write
if _, err = f.Write(b); err != nil {
return errors.Wrapf(err, "writing to %s failed", p)
}
}
return
}
// rename represents a rename
type rename struct {
src, dst string
}
// provisionElectronFinishDarwinRename renames the proper darwin folders
func (p *defaultProvisioner) provisionElectronFinishDarwinRename(appName string, paths Paths) (err error) {
var appDirectory = filepath.Join(paths.electronDirectory, appName+".app")
var frameworksDirectory = filepath.Join(appDirectory, "Contents", "Frameworks")
var helper = filepath.Join(frameworksDirectory, appName+" Helper.app")
for _, r := range []rename{
{src: filepath.Join(paths.electronDirectory, "Electron.app"), dst: appDirectory},
{src: filepath.Join(appDirectory, "Contents", "MacOS", "Electron"), dst: paths.AppExecutable()},
{src: filepath.Join(frameworksDirectory, "Electron Helper.app"), dst: filepath.Join(helper)},
{src: filepath.Join(helper, "Contents", "MacOS", "Electron Helper"), dst: filepath.Join(helper, "Contents", "MacOS", appName+" Helper")},
} {
astilog.Debugf("Renaming %s into %s", r.src, r.dst)
if err = os.Rename(r.src, r.dst); err != nil {
return errors.Wrapf(err, "renaming %s into %s failed", r.src, r.dst)
}
}
return
}
// Disembedder is a functions that allows to disembed data from a path
type Disembedder func(src string) ([]byte, error)
// NewDisembedderProvisioner creates a provisioner that can provision based on embedded data
func NewDisembedderProvisioner(d Disembedder, pathAstilectron, pathElectron string) Provisioner {
return &defaultProvisioner{
moverAstilectron: func(ctx context.Context, p Paths) (err error) {
if err = Disembed(ctx, d, pathAstilectron, p.AstilectronDownloadDst()); err != nil {
return errors.Wrapf(err, "disembedding %s into %s failed", pathAstilectron, p.AstilectronDownloadDst())
}
return
},
moverElectron: func(ctx context.Context, p Paths) (err error) {
if err = Disembed(ctx, d, pathElectron, p.ElectronDownloadDst()); err != nil {
return errors.Wrapf(err, "disembedding %s into %s failed", pathElectron, p.ElectronDownloadDst())
}
return
},
}
}