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{}\n\n", s);
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: {:?}\nerror: {}", cmd, e)),
37 };
38 if !status.success() {
39 if print_cmd_on_fail {
40 println!(
41 "\n\ncommand did not execute successfully: {:?}\n\
42 expected success, got: {}\n\n",
43 cmd, status
44 );
45 }
46 Err(())
47 } else {
48 Ok(())
49 }
50}
51
52pub fn parse_gitmodules(target_dir: &Path) -> Vec<String> {
54 let gitmodules = target_dir.join(".gitmodules");
55 assert!(gitmodules.exists(), "'{}' file is missing.", gitmodules.display());
56
57 let file = File::open(gitmodules).unwrap();
58
59 let mut submodules_paths = vec![];
60 for line in BufReader::new(file).lines().map_while(Result::ok) {
61 let line = line.trim();
62 if line.starts_with("path") {
63 let actual_path = line.split(' ').last().expect("Couldn't get value of path");
64 submodules_paths.push(actual_path.to_owned());
65 }
66 }
67
68 submodules_paths
69}