1use std::fs::File;
2use std::io::{BufRead, BufReader};
3use std::path::Path;
4use std::process::Command;
5
6#[macro_export]
10macro_rules! exit {
11 ($code:expr) => {
12 $crate::util::detail_exit($code, cfg!(test));
13 };
14}
15
16pub fn detail_exit(code: i32, is_test: bool) -> ! {
19 if is_test {
21 panic!("status code: {code}");
22 } else {
23 std::process::exit(code);
25 }
26}
27
28pub fn fail(s: &str) -> ! {
29 eprintln!("\n\n{s}\n\n");
30 detail_exit(1, cfg!(test));
31}
32
33pub fn try_run(cmd: &mut Command, print_cmd_on_fail: bool) -> Result<(), ()> {
34 let status = match cmd.status() {
35 Ok(status) => status,
36 Err(e) => fail(&format!("failed to execute command: {cmd:?}\nerror: {e}")),
37 };
38 if !status.success() {
39 if print_cmd_on_fail {
40 println!(
41 "\n\ncommand did not execute successfully: {cmd:?}\n\
42 expected success, got: {status}\n\n"
43 );
44 }
45 Err(())
46 } else {
47 Ok(())
48 }
49}
50
51pub fn parse_gitmodules(target_dir: &Path) -> Vec<String> {
53 let gitmodules = target_dir.join(".gitmodules");
54 assert!(gitmodules.exists(), "'{}' file is missing.", gitmodules.display());
55
56 let file = File::open(gitmodules).unwrap();
57
58 let mut submodules_paths = vec![];
59 for line in BufReader::new(file).lines().map_while(Result::ok) {
60 let line = line.trim();
61 if line.starts_with("path") {
62 let actual_path = line.split(' ').next_back().expect("Couldn't get value of path");
63 submodules_paths.push(actual_path.to_owned());
64 }
65 }
66
67 submodules_paths
68}