This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
/
paths.go
244 lines (212 loc) · 7.29 KB
/
paths.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
package astilectron
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// Paths represents the set of paths needed by Astilectron
type Paths struct {
appExecutable string
appIconDarwinSrc string
appIconDefaultSrc string
astilectronApplication string
astilectronDirectory string
astilectronDownloadSrc string
astilectronDownloadDst string
astilectronUnzipSrc string
baseDirectory string
dataDirectory string
electronDirectory string
electronDownloadSrc string
electronDownloadDst string
electronUnzipSrc string
provisionStatus string
vendorDirectory string
}
// newPaths creates new paths
func newPaths(os, arch string, o Options) (p *Paths, err error) {
// Init base directory path
p = &Paths{}
if err = p.initBaseDirectory(o.BaseDirectoryPath); err != nil {
err = fmt.Errorf("initializing base directory failed: %w", err)
return
}
// Init data directory path
if err = p.initDataDirectory(o.DataDirectoryPath, o.AppName); err != nil {
err = fmt.Errorf("initializing data directory failed: %w", err)
return
}
// Init other paths
//!\\ Order matters
p.appIconDarwinSrc = o.AppIconDarwinPath
if len(p.appIconDarwinSrc) > 0 && !filepath.IsAbs(p.appIconDarwinSrc) {
p.appIconDarwinSrc = filepath.Join(p.dataDirectory, p.appIconDarwinSrc)
}
p.appIconDefaultSrc = o.AppIconDefaultPath
if len(p.appIconDefaultSrc) > 0 && !filepath.IsAbs(p.appIconDefaultSrc) {
p.appIconDefaultSrc = filepath.Join(p.dataDirectory, p.appIconDefaultSrc)
}
p.vendorDirectory = filepath.Join(p.dataDirectory, "vendor")
p.provisionStatus = filepath.Join(p.vendorDirectory, "status.json")
p.astilectronDirectory = filepath.Join(p.vendorDirectory, "astilectron")
p.astilectronApplication = filepath.Join(p.astilectronDirectory, "main.js")
p.astilectronDownloadSrc = AstilectronDownloadSrc(o.VersionAstilectron)
p.astilectronDownloadDst = filepath.Join(p.vendorDirectory, fmt.Sprintf("astilectron-v%s.zip", o.VersionAstilectron))
p.astilectronUnzipSrc = filepath.Join(p.astilectronDownloadDst, fmt.Sprintf("astilectron-%s", o.VersionAstilectron))
if o.CustomElectronPath == "" {
p.electronDirectory = filepath.Join(p.vendorDirectory, fmt.Sprintf("electron-%s-%s", os, arch))
p.electronDownloadSrc = ElectronDownloadSrc(os, arch, o.VersionElectron)
p.electronDownloadDst = filepath.Join(p.vendorDirectory, fmt.Sprintf("electron-%s-%s-v%s.zip", os, arch, o.VersionElectron))
p.electronUnzipSrc = p.electronDownloadDst
p.initAppExecutable(os, o.AppName)
} else {
p.appExecutable = o.CustomElectronPath
}
return
}
// initBaseDirectory initializes the base directory path
func (p *Paths) initBaseDirectory(baseDirectoryPath string) (err error) {
// No path specified in the options
p.baseDirectory = baseDirectoryPath
if len(p.baseDirectory) == 0 {
// Retrieve executable path
var ep string
if ep, err = os.Executable(); err != nil {
err = fmt.Errorf("retrieving executable path failed: %w", err)
return
}
p.baseDirectory = filepath.Dir(ep)
}
// We need the absolute path
if p.baseDirectory, err = filepath.Abs(p.baseDirectory); err != nil {
err = fmt.Errorf("computing absolute path failed: %w", err)
return
}
return
}
func (p *Paths) initDataDirectory(dataDirectoryPath, appName string) (err error) {
// Path is specified in the options
if len(dataDirectoryPath) > 0 {
// We need the absolute path
if p.dataDirectory, err = filepath.Abs(dataDirectoryPath); err != nil {
err = fmt.Errorf("computing absolute path of %s failed: %w", dataDirectoryPath, err)
return
}
return
}
// If the APPDATA env exists, we use it
if v := os.Getenv("APPDATA"); len(v) > 0 {
p.dataDirectory = filepath.Join(v, appName)
return
}
// Default to base directory path
p.dataDirectory = p.baseDirectory
return
}
// AstilectronDownloadSrc returns the download URL of the (currently platform-independent) astilectron zip file
func AstilectronDownloadSrc(versionAstilectron string) string {
return fmt.Sprintf("https://github.com/asticode/astilectron/archive/v%s.zip", versionAstilectron)
}
// ElectronDownloadSrc returns the download URL of the platform-dependant electron zipfile
func ElectronDownloadSrc(os, arch, versionElectron string) string {
// Get OS name
var o string
switch strings.ToLower(os) {
case "darwin":
o = "darwin"
case "linux":
o = "linux"
case "windows":
o = "win32"
}
// Get arch name
var a = "ia32"
if strings.ToLower(arch) == "amd64" {
a = "x64"
} else if strings.ToLower(arch) == "arm" && o == "linux" {
a = "armv7l"
} else if strings.ToLower(arch) == "arm64" {
a = "arm64"
}
// Return url
return fmt.Sprintf("https://github.com/electron/electron/releases/download/v%s/electron-v%s-%s-%s.zip", versionElectron, versionElectron, o, a)
}
// initAppExecutable initializes the app executable path
func (p *Paths) initAppExecutable(os, appName string) {
switch os {
case "darwin":
if appName == "" {
appName = "Electron"
}
p.appExecutable = filepath.Join(p.electronDirectory, appName+".app", "Contents", "MacOS", appName)
case "linux":
p.appExecutable = filepath.Join(p.electronDirectory, "electron")
case "windows":
p.appExecutable = filepath.Join(p.electronDirectory, "electron.exe")
}
}
// AppExecutable returns the app executable path
func (p Paths) AppExecutable() string {
return p.appExecutable
}
// AppIconDarwinSrc returns the darwin app icon path
func (p Paths) AppIconDarwinSrc() string {
return p.appIconDarwinSrc
}
// AppIconDefaultSrc returns the default app icon path
func (p Paths) AppIconDefaultSrc() string {
return p.appIconDefaultSrc
}
// BaseDirectory returns the base directory path
func (p Paths) BaseDirectory() string {
return p.baseDirectory
}
// AstilectronApplication returns the astilectron application path
func (p Paths) AstilectronApplication() string {
return p.astilectronApplication
}
// AstilectronDirectory returns the astilectron directory path
func (p Paths) AstilectronDirectory() string {
return p.astilectronDirectory
}
// AstilectronDownloadDst returns the astilectron download destination path
func (p Paths) AstilectronDownloadDst() string {
return p.astilectronDownloadDst
}
// AstilectronDownloadSrc returns the astilectron download source path
func (p Paths) AstilectronDownloadSrc() string {
return p.astilectronDownloadSrc
}
// AstilectronUnzipSrc returns the astilectron unzip source path
func (p Paths) AstilectronUnzipSrc() string {
return p.astilectronUnzipSrc
}
// DataDirectory returns the data directory path
func (p Paths) DataDirectory() string {
return p.dataDirectory
}
// ElectronDirectory returns the electron directory path
func (p Paths) ElectronDirectory() string {
return p.electronDirectory
}
// ElectronDownloadDst returns the electron download destination path
func (p Paths) ElectronDownloadDst() string {
return p.electronDownloadDst
}
// ElectronDownloadSrc returns the electron download source path
func (p Paths) ElectronDownloadSrc() string {
return p.electronDownloadSrc
}
// ElectronUnzipSrc returns the electron unzip source path
func (p Paths) ElectronUnzipSrc() string {
return p.electronUnzipSrc
}
// ProvisionStatus returns the provision status path
func (p Paths) ProvisionStatus() string {
return p.provisionStatus
}
// VendorDirectory returns the vendor directory path
func (p Paths) VendorDirectory() string {
return p.vendorDirectory
}