wakelib/
lib.rs

1use colored::*;
2use std::path::PathBuf;
3#[allow(unused_imports)]
4use std::{fs, path, process};
5
6pub fn run_unified(command: String, _debug: String) {
7    let os = get_os();
8    if os == "WINDOWS" {
9        println!("{}", "Running on Windows".bright_green());
10        windows::run_pwsh(command, &_debug);
11    } else if os == "LINUX" {
12        println!("{}", "Running on GNU/Linux".bright_green());
13        linux::run_bash(command, _debug);
14    } else {
15        println!("{}", "Unknown OS, aborting!".bright_red());
16        process::exit(1);
17    }
18}
19
20pub fn get_wakefiles(content: String) -> Vec<PathBuf> {
21    let mut wakefiles: Vec<PathBuf> = Vec::new();
22    let lines: Vec<&str> = content.split("\n").collect();
23    for line in lines {
24        if line.contains(".Wakefile") {
25            //let fin = ".wake".to_string() + sep + line;
26            let fin = path::Path::new(".wake").join(line.trim());
27            wakefiles.push(fin);
28        } else {
29            println!("{}", "Warning: not a Wakefile!".yellow());
30        }
31    }
32    return wakefiles;
33}
34
35pub fn get_os() -> String {
36    let os = std::env::consts::OS;
37    if os == "windows" {
38        return "WINDOWS".to_string();
39    } else if os == "linux" {
40        return "LINUX".to_string();
41    } else {
42        return "other".to_string();
43    }
44}
45pub mod windows {
46    use colored::*;
47    pub fn run_pwsh(command: String, _debug: &String) {
48        let lines: Vec<&str> = command.split("\n").collect();
49        for line in lines {
50            if line.contains(" && ") || line.contains(" | ") || line.contains(" >> ") {
51                println!(
52                    "{}",
53                    "Some powershell syntax is not supported skipping the line!".bright_red()
54                )
55            } else if line == "" {
56            } else {
57                println!("{} {}", "Executing".bright_yellow(), line.bright_blue());
58                let out = std::process::Command::new("powershell")
59                    .arg("-Command")
60                    .arg(line)
61                    .status()
62                    .expect("The program failed");
63                if out.success() {
64                    println!(
65                        "{} {}",
66                        "The program finished with".bright_green(),
67                        out.to_string().bright_green()
68                    )
69                } else {
70                    println!(
71                        "{}, {}",
72                        "The program failed with exit code ".bright_red(),
73                        out
74                    )
75                }
76            }
77        }
78    }
79}
80pub mod linux {
81    use crate::*;
82
83    pub fn run_bash(command: String, _debug: String) {
84        let lines: Vec<&str> = command.split("\n").collect();
85        for line in lines {
86            if line.contains(" && ") || line.contains(" | ") || line.contains(" >> ") {
87                println!(
88                    "{}",
89                    "Some bash syntax is not supported skipping the line!".bright_red()
90                )
91            } else if line == "" {
92            } else {
93                println!("{} {}", "Executing".bright_yellow(), line.bright_blue());
94                let out = std::process::Command::new("bash")
95                    .arg("-c")
96                    .arg(line)
97                    .status()
98                    .expect("The program failed");
99                if out.success() {
100                    println!(
101                        "{} {}",
102                        "The program finished with".bright_green(),
103                        out.to_string().bright_green()
104                    )
105                } else {
106                    println!(
107                        "{}, {}",
108                        "The program failed with exit code ".bright_red(),
109                        out
110                    )
111                }
112            }
113        }
114    }
115}