forked from wailsapp/wails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
317 lines (275 loc) · 8.52 KB
/
system.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
package cmd
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"time"
)
// SystemHelper - Defines everything related to the system
type SystemHelper struct {
log *Logger
fs *FSHelper
configFilename string
homeDir string
wailsSystemDir string
wailsSystemConfig string
}
// NewSystemHelper - Creates a new System Helper
func NewSystemHelper() *SystemHelper {
result := &SystemHelper{
fs: NewFSHelper(),
log: NewLogger(),
configFilename: "wails.json",
}
result.setSystemDirs()
return result
}
// Internal
// setSystemDirs calculates the system directories it is interested in
func (s *SystemHelper) setSystemDirs() {
var err error
s.homeDir, err = os.UserHomeDir()
if err != nil {
log.Fatal("Cannot find home directory! Please file a bug report!")
}
// TODO: A better config system
s.wailsSystemDir = filepath.Join(s.homeDir, ".wails")
s.wailsSystemConfig = filepath.Join(s.wailsSystemDir, s.configFilename)
}
// ConfigFileExists - Returns true if it does!
func (s *SystemHelper) ConfigFileExists() bool {
return s.fs.FileExists(s.wailsSystemConfig)
}
// SystemDirExists - Returns true if it does!
func (s *SystemHelper) systemDirExists() bool {
return s.fs.DirExists(s.wailsSystemDir)
}
// LoadConfig attempts to load the Wails system config
func (s *SystemHelper) LoadConfig() (*SystemConfig, error) {
return NewSystemConfig(s.wailsSystemConfig)
}
// ConfigFileIsValid checks if the config file is valid
func (s *SystemHelper) ConfigFileIsValid() bool {
_, err := NewSystemConfig(s.wailsSystemConfig)
return err == nil
}
// GetAuthor returns a formatted string of the user's name and email
func (s *SystemHelper) GetAuthor() (string, error) {
var config *SystemConfig
config, err := s.LoadConfig()
if err != nil {
return "", err
}
return fmt.Sprintf("%s <%s>", config.Name, config.Email), nil
}
// BackupConfig attempts to backup the system config file
func (s *SystemHelper) BackupConfig() (string, error) {
now := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
backupFilename := s.wailsSystemConfig + "." + now
err := s.fs.CopyFile(s.wailsSystemConfig, backupFilename)
if err != nil {
return "", err
}
return backupFilename, nil
}
func (s *SystemHelper) setup() error {
systemConfig := make(map[string]string)
// Try to load current values - ignore errors
config, _ := s.LoadConfig()
if config.Name != "" {
systemConfig["name"] = PromptRequired("What is your name", config.Name)
} else if n, err := getGitConfigValue("user.name"); err == nil && n != "" {
systemConfig["name"] = PromptRequired("What is your name", n)
} else {
systemConfig["name"] = PromptRequired("What is your name")
}
if config.Email != "" {
systemConfig["email"] = PromptRequired("What is your email address", config.Email)
} else if e, err := getGitConfigValue("user.email"); err == nil && e != "" {
systemConfig["email"] = PromptRequired("What is your email address", e)
} else {
systemConfig["email"] = PromptRequired("What is your email address")
}
// Create the directory
err := s.fs.MkDirs(s.wailsSystemDir)
if err != nil {
return err
}
// Save
configData, err := json.Marshal(&systemConfig)
if err != nil {
return err
}
err = os.WriteFile(s.wailsSystemConfig, configData, 0755)
if err != nil {
return err
}
fmt.Println()
s.log.White("Wails config saved to: " + s.wailsSystemConfig)
s.log.White("Feel free to customise these settings.")
fmt.Println()
return nil
}
const introText = `
Wails is a lightweight framework for creating web-like desktop apps in Go.
I'll need to ask you a few questions so I can fill in your project templates and then I will try and see if you have the correct dependencies installed. If you don't have the right tools installed, I'll try and suggest how to install them.
`
// CheckInitialised checks if the system has been set up
// and if not, runs setup
func (s *SystemHelper) CheckInitialised() error {
if !s.systemDirExists() {
s.log.Yellow("System not initialised. Running setup.")
return s.setup()
}
return nil
}
// Initialise attempts to set up the Wails system.
// An error is returns if there is a problem
func (s *SystemHelper) Initialise() error {
// System dir doesn't exist
if !s.systemDirExists() {
s.log.Green("Welcome to Wails!")
s.log.Green(introText)
return s.setup()
}
// Config doesn't exist
if !s.ConfigFileExists() {
s.log.Green("Looks like the system config is missing.")
s.log.Green("To get you back on track, I'll need to ask you a few things...")
return s.setup()
}
// Config exists but isn't valid.
if !s.ConfigFileIsValid() {
s.log.Green("Looks like the system config got corrupted.")
backupFile, err := s.BackupConfig()
if err != nil {
s.log.Green("I tried to backup your config file but got this error: %s", err.Error())
} else {
s.log.Green("Just in case you needed it, I backed up your config file here: %s", backupFile)
}
s.log.Green("To get you back on track, I'll need to ask you a few things...")
return s.setup()
}
return s.setup()
}
// SystemConfig - Defines system wide configuration data
type SystemConfig struct {
Name string `json:"name"`
Email string `json:"email"`
}
// NewSystemConfig - Creates a new SystemConfig helper object
func NewSystemConfig(filename string) (*SystemConfig, error) {
result := &SystemConfig{}
err := result.load(filename)
return result, err
}
// Save - Saves the system config to the given filename
func (sc *SystemConfig) Save(filename string) error {
// Convert config to JSON string
theJSON, err := json.MarshalIndent(sc, "", " ")
if err != nil {
return err
}
// Write it out to the config file
return os.WriteFile(filename, theJSON, 0644)
}
func (sc *SystemConfig) load(filename string) error {
configData, err := os.ReadFile(filename)
if err != nil {
return err
}
// Load and unmarshall!
err = json.Unmarshal(configData, &sc)
if err != nil {
return err
}
return nil
}
// CheckDependenciesSilent checks for dependencies but
// only outputs if there's an error
func CheckDependenciesSilent(logger *Logger) (bool, error) {
logger.SetErrorOnly(true)
result, err := CheckDependencies(logger)
logger.SetErrorOnly(false)
return result, err
}
// CheckDependencies will look for Wails dependencies on the system
// Errors are reported in error and the bool return value is whether
// the dependencies are all installed.
func CheckDependencies(logger *Logger) (bool, error) {
switch runtime.GOOS {
case "darwin":
logger.Yellow("Detected Platform: OSX")
case "windows":
logger.Yellow("Detected Platform: Windows")
case "linux":
logger.Yellow("Detected Platform: Linux")
default:
return false, fmt.Errorf("Platform %s is currently not supported", runtime.GOOS)
}
logger.Yellow("Checking for prerequisites...")
// Check we have a cgo capable environment
requiredPrograms, err := GetRequiredPrograms()
if err != nil {
return false, nil
}
errors := false
programHelper := NewProgramHelper()
for _, program := range *requiredPrograms {
bin := programHelper.FindProgram(program.Name)
if bin == nil {
errors = true
logger.Error("Program '%s' not found. %s", program.Name, program.Help)
} else {
logger.Green("Program '%s' found: %s", program.Name, bin.Path)
}
}
// Linux has library deps
if runtime.GOOS == "linux" {
// Check library prerequisites
requiredLibraries, err := GetRequiredLibraries()
if err != nil {
return false, err
}
var libraryChecker CheckPkgInstalled
distroInfo := GetLinuxDistroInfo()
switch distroInfo.Distribution {
case Ubuntu, Debian, Zorin, Parrot, Linuxmint, Elementary, Kali, Neon, Deepin, Raspbian, PopOS, Uos:
libraryChecker = DpkgInstalled
case Arch, ArcoLinux, ArchLabs, Ctlos, Manjaro, ManjaroARM, EndeavourOS, ArtixLinux:
libraryChecker = PacmanInstalled
case CentOS, Fedora, Tumbleweed, Leap, RHEL:
libraryChecker = RpmInstalled
case Gentoo:
libraryChecker = EqueryInstalled
case VoidLinux:
libraryChecker = XbpsInstalled
case Solus:
libraryChecker = EOpkgInstalled
case Crux:
libraryChecker = PrtGetInstalled
case NixOS:
libraryChecker = NixEnvInstalled
default:
return false, RequestSupportForDistribution(distroInfo)
}
for _, library := range *requiredLibraries {
installed, err := libraryChecker(library.Name)
if err != nil {
return false, err
}
if !installed {
errors = true
logger.Error("Library '%s' not found. %s", library.Name, library.Help)
} else {
logger.Green("Library '%s' installed.", library.Name)
}
}
}
logger.White("")
return !errors, err
}