-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem_dependencies.rs
575 lines (556 loc) · 22.4 KB
/
system_dependencies.rs
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use std::env;
use log::{debug, trace, warn};
use crate::command_executor;
/// Determines the package manager installed on the system.
///
/// This function attempts to identify the package manager by executing each
/// listed package manager's version command and checking if the command
/// execution is successful.
///
/// This should be only executed on Linux systems, as package managers on other operating systems
/// are not supported.
///
/// # Returns
///
/// * `Some(&'static str)` - If a package manager is found, returns the name of the package manager.
/// * `None` - If no package manager is found, returns None.
fn determine_package_manager() -> Option<&'static str> {
let package_managers = vec!["apt", "dpkg", "dnf", "pacman", "zypper"];
for manager in package_managers {
let output = command_executor::execute_command(manager, &["--version"]);
match output {
Ok(output) => {
if output.status.success() {
return Some(manager);
}
}
Err(_) => continue,
}
}
None
}
/// Returns a hardcoded vector of required tools based on the operating system.
///
/// # Returns
///
/// * `Vec<&'static str>` - A vector of required tools for the current operating system.
pub fn get_prequisites() -> Vec<&'static str> {
match std::env::consts::OS {
"linux" => vec![
"git",
"cmake",
"ninja",
"wget",
"flex",
"bison",
"gperf",
"ccache",
"libffi-dev",
"libssl-dev",
"dfu-util",
"libusb-1.0-0",
],
"windows" => vec!["git", "cmake", "ninja"], // temporary added cmake back before solving why it does not install from tools.json
"macos" => vec!["dfu-util", "cmake", "ninja"],
_ => vec![],
}
}
/// Checks the system for the required tools and returns a list of unsatisfied tools.
///
/// This function determines the operating system and package manager, then checks if each required tool is installed.
/// If a tool is not found, it is added to the `unsatisfied` vector and returned.
/// The prerequsites are met when empty vector is returned.
///
/// # Returns
///
/// * `Ok(Vec<&'static str>)` - If the function completes successfully, returns a vector of unsatisfied tools.
/// * `Err(String)` - If an error occurs, returns an error message.
pub fn check_prerequisites() -> Result<Vec<&'static str>, String> {
let list_of_required_tools = get_prequisites();
debug!("Checking for prerequisites...");
debug!("will be checking for : {:?}", list_of_required_tools);
let mut unsatisfied = vec![];
match std::env::consts::OS {
"linux" => {
let package_manager = determine_package_manager();
debug!("Detected package manager: {:?}", package_manager);
match package_manager {
Some("apt") => {
for tool in list_of_required_tools {
let output = command_executor::execute_command(
"sh",
&["-c", &format!("apt list --installed | grep {}", tool)],
);
match output {
Ok(o) => {
if o.status.success() {
debug!("{} is already installed: {:?}", tool, o);
} else {
debug!("check for {} failed: {:?}", tool, o);
unsatisfied.push(tool);
}
}
Err(_e) => {
unsatisfied.push(tool);
}
}
}
}
Some("dpkg") => {
for tool in list_of_required_tools {
let output = command_executor::execute_command(
"sh",
&["-c", &format!("dpkg -l | grep {}", tool)],
);
match output {
Ok(o) => {
if o.status.success() {
debug!("{} is already installed: {:?}", tool, o);
} else {
debug!("check for {} failed: {:?}", tool, o);
unsatisfied.push(tool);
}
}
Err(_e) => {
unsatisfied.push(tool);
}
}
}
}
Some("dnf") => {
for tool in list_of_required_tools {
let output = command_executor::execute_command(
"sh",
&["-c", &format!("dnf list installed | grep {}", tool)],
);
match output {
Ok(o) => {
if o.status.success() {
debug!("{} is already installed: {:?}", tool, o);
} else {
unsatisfied.push(tool);
}
}
Err(_e) => {
unsatisfied.push(tool);
}
}
}
}
Some("pacman") => {
for tool in list_of_required_tools {
let output = command_executor::execute_command(
"sh",
&["-c", &format!("pacman -Qs | grep {}", tool)],
);
match output {
Ok(o) => {
if o.status.success() {
debug!("{} is already installed: {:?}", tool, o);
} else {
unsatisfied.push(tool);
}
}
Err(_e) => {
unsatisfied.push(tool);
}
}
}
}
Some("zypper") => {
for tool in list_of_required_tools {
let output = command_executor::execute_command(
"sh",
&["-c", &format!("zypper se --installed-only {}", tool)],
);
match output {
Ok(o) => {
if o.status.success() {
debug!("{} is already installed: {:?}", tool, o);
} else {
unsatisfied.push(tool);
}
}
Err(_e) => {
unsatisfied.push(tool);
}
}
}
}
None => {
return Err(format!(
"Unsupported package manager - {}",
package_manager.unwrap()
));
}
_ => {
return Err(format!(
"Unsupported package manager - {}",
package_manager.unwrap()
));
}
}
}
"macos" => {
for tool in list_of_required_tools {
let output = command_executor::execute_command(
"zsh",
&["-c", &format!("brew list | grep {}", tool)],
);
match output {
Ok(o) => {
if o.status.success() {
debug!("{} is already installed: {:?}", tool, o);
} else {
debug!("check for {} failed: {:?}", tool, o);
unsatisfied.push(tool);
}
}
Err(_e) => {
unsatisfied.push(tool);
}
}
}
}
"windows" => {
for tool in list_of_required_tools {
let output = command_executor::execute_command(
"powershell",
&["-Command", &format!("{} --version", tool)],
);
match output {
Ok(o) => {
if o.status.success() {
debug!("{} is already installed: {:?}", tool, o);
} else {
debug!("check for {} failed: {:?}", tool, o);
unsatisfied.push(tool);
}
}
Err(_e) => {
unsatisfied.push(tool);
}
}
}
}
_ => {
return Err(format!("Unsupported OS - {}", std::env::consts::OS));
}
}
Ok(unsatisfied)
}
/// Returns the path to the Scoop shims directory.
/// This function is only relevant for Windows systems.
///
/// # Returns
///
/// * `Some(String)` - If the function is executed on a Windows system and the Scoop shims directory is found,
/// the function returns the path to the Scoop shims directory.
/// * `None` - If the function is executed on a non-Windows system or if the Scoop shims directory cannot be found,
/// the function returns None.
pub fn get_scoop_path() -> Option<String> {
if std::env::consts::OS == "windows" {
let home_dir = match dirs::home_dir() {
Some(d) => d,
None => {
debug!("Could not get home directory");
return None;
}
};
let scoop_shims_path = home_dir.join("scoop").join("shims");
Some(scoop_shims_path.to_string_lossy().to_string())
} else {
None
}
}
/// Installs the Scoop package manager on Windows.
///
/// This function is only relevant for Windows systems. It sets the execution policy to RemoteSigned,
/// downloads the Scoop installer script from the official website, and executes it.
///
/// # Returns
///
/// * `Ok(())` - If the Scoop package manager is successfully installed.
/// * `Err(String)` - If an error occurs during the installation process.
fn install_scoop_package_manager() -> Result<(), String> {
match std::env::consts::OS {
"windows" => {
let path_with_scoop = match get_scoop_path() {
Some(s) => s,
None => {
debug!("Could not get scoop path");
return Err(String::from("Could not get scoop path"));
}
};
add_to_path(&path_with_scoop).unwrap();
let scoop_install_cmd = include_str!("./../powershell_scripts/install_scoop.ps1");
let output = crate::run_powershell_script(scoop_install_cmd);
match output {
Ok(o) => {
trace!("output: {}", o);
debug!("Successfully installed Scoop package manager. Adding to PATH");
add_to_path(&path_with_scoop).unwrap();
Ok(())
}
Err(e) => Err(e.to_string()),
}
}
_ => {
// this function should not be called on non-windows platforms
debug!("Scoop package manager is only supported on Windows. Skipping installation.");
Err(format!("Unsupported OS - {}", std::env::consts::OS))
}
}
}
/// Ensures that the Scoop package manager is installed on Windows.
///
/// This function checks if the Scoop package manager is installed on the system.
/// If it is not installed, the function installs it by setting the execution policy to RemoteSigned,
/// downloading the Scoop installer script from the official website, and executing it.
///
/// # Returns
///
/// * `Ok(())` - If the Scoop package manager is successfully installed.
/// * `Err(String)` - If an error occurs during the installation process.
pub fn ensure_scoop_package_manager() -> Result<(), String> {
match std::env::consts::OS {
"windows" => {
let path_with_scoop = match get_scoop_path() {
Some(s) => s,
None => {
debug!("Could not get scoop path");
return Err(String::from("Could not get scoop path"));
}
};
// #[cfg(windows)]
// crate::win_tools::add_to_win_path(&path_with_scoop).unwrap();
// add_to_windows_path(&path_with_scoop).unwrap();
add_to_path(&path_with_scoop).unwrap();
let output = command_executor::execute_command(
"powershell",
&["-Command", "scoop", "--version"],
);
match output {
Ok(o) => {
if o.status.success() {
debug!("Scoop package manager is already installed");
Ok(())
} else {
debug!("Installing scoop package manager");
install_scoop_package_manager()
}
}
Err(_) => install_scoop_package_manager(),
}
}
_ => {
// this function should not be called on non-windows platforms
debug!("Scoop package manager is only supported on Windows. Skipping installation.");
Err(format!("Unsupported OS - {}", std::env::consts::OS))
}
}
}
/// Installs the required packages based on the operating system.
/// This function actually panics if the required packages install fail.
/// This is to ensure that user actually sees the error and realize which package failed to install.
///
/// # Parameters
///
/// * `packages_list` - A vector of strings representing the names of the packages to be installed.
/// this can be obtained by calling the check_prerequisites() function.
///
/// # Returns
///
/// * `Ok(())` - If the packages are successfully installed.
/// * `Err(String)` - If an error occurs during the installation process.
pub fn install_prerequisites(packages_list: Vec<String>) -> Result<(), String> {
match std::env::consts::OS {
"linux" => {
let package_manager = determine_package_manager();
match package_manager {
Some("apt") => {
for package in packages_list {
let output = command_executor::execute_command(
"sudo",
&["apt", "install", "-y", &package],
);
match output {
Ok(_) => {
debug!("Successfully installed {}", package);
}
Err(e) => panic!("Failed to install {}: {}", package, e),
}
}
}
Some("dnf") => {
for package in packages_list {
let output = command_executor::execute_command(
"sudo",
&["dnf", "install", "-y", &package],
);
match output {
Ok(_) => {
debug!("Successfully installed {}", package);
}
Err(e) => panic!("Failed to install {}: {}", package, e),
}
}
}
Some("pacman") => {
for package in packages_list {
let output = command_executor::execute_command(
"sudo",
&["pacman", "-S", "--noconfirm", &package],
);
match output {
Ok(_) => {
debug!("Successfully installed {}", package);
}
Err(e) => panic!("Failed to install {}: {}", package, e),
}
}
}
Some("zypper") => {
for package in packages_list {
let output = command_executor::execute_command(
"sudo",
&["zypper", "install", "-y", &package],
);
match output {
Ok(_) => {
debug!("Successfully installed {}", package);
}
Err(e) => panic!("Failed to install {}: {}", package, e),
}
}
}
_ => {
return Err(format!(
"Unsupported package manager - {}",
package_manager.unwrap()
));
}
}
}
"macos" => {
for package in packages_list {
let output = command_executor::execute_command("brew", &["install", &package]);
match output {
Ok(_) => {
debug!("Successfully installed {}", package);
}
Err(e) => panic!("Failed to install {}: {}", package, e),
}
}
}
"windows" => {
ensure_scoop_package_manager()?;
for package in packages_list {
let path_with_scoop = match get_scoop_path() {
Some(s) => s,
None => {
debug!("Could not get scoop path");
return Err(String::from("Could not get scoop path"));
}
};
debug!("Installing {} with scoop: {}", package, path_with_scoop);
let mut main_command = "powershell";
let test_for_pwsh = command_executor::execute_command("pwsh", &["--version"]);
match test_for_pwsh {
// this needs to be used in powershell 7
Ok(_) => {
debug!("Found powershell core");
main_command = "pwsh";
}
Err(_) => {
debug!("Powershell core not found, using powershell");
}
}
let output = command_executor::execute_command_with_env(
main_command,
&vec![
"-ExecutionPolicy",
"Bypass",
"-Command",
"scoop",
"install",
&package,
],
vec![("PATH", &add_to_path(&path_with_scoop).unwrap())],
);
match output {
Ok(o) => {
if o.status.success() {
trace!("{}", String::from_utf8(o.stdout).unwrap());
debug!("Successfully installed {:?}", package);
} else {
let output = String::from_utf8(o.stdout).unwrap();
let error_message = String::from_utf8(o.stderr).unwrap();
debug!("Failed to install {}: {}", package, error_message);
debug!("Output: {}", output);
}
}
Err(e) => panic!("Failed to install {}: {}", package, e),
}
}
}
_ => {
return Err(format!("Unsupported OS - {}", std::env::consts::OS));
}
}
Ok(())
}
/// Adds a new directory to the system's PATH environment variable.
///
/// This function appends the new directory to the current PATH if it's not already present.
/// On Windows systems, it also updates the user's PATH environment variable persistently.
///
/// # Parameters
///
/// * `new_path` - A string slice representing the new directory path to be added to the PATH.
///
/// # Returns
///
/// * `Ok(String)` - Returns the updated PATH string if the operation is successful.
/// * `Err(std::io::Error)` - Returns an IO error if the PATH update fails on Windows systems.
fn add_to_path(new_path: &str) -> Result<String, std::io::Error> {
let binding = env::var_os("PATH").unwrap_or_default();
let paths = binding.to_str().unwrap();
let new_path_string = match std::env::consts::OS {
"windows" => format!("{};{}", new_path, paths),
_ => format!("{}:{}", new_path, paths),
};
if !paths.contains(new_path) {
// Update current process PATH
env::set_var("PATH", &new_path_string);
}
if std::env::consts::OS == "windows" {
// PowerShell 7+ compatible command
let ps_command = format!(
"$oldPath = [Environment]::GetEnvironmentVariable('PATH', 'User'); \
if (-not $oldPath.Contains('{}')) {{ \
$newPath = '{}' + ';' + $oldPath; \
[Environment]::SetEnvironmentVariable('PATH', $newPath, 'User'); \
}}",
new_path.replace("'", "''"),
new_path.replace("'", "''")
);
let res = command_executor::execute_command(
"powershell",
&["-NoProfile", "-NonInteractive", "-Command", &ps_command],
);
match res {
Ok(_) => {
debug!("Added {} to PATH", new_path);
}
Err(e) => {
warn!("Failed to add {} to PATH: {}", new_path, e);
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to update PATH: {}", e),
));
}
}
}
Ok(new_path_string)
}