1 use std::process::Command;
2
3 /// Invokes `build_helper::util::detail_exit` with `cfg!(test)`
4 #[macro_export]
5 macro_rules! detail_exit_macro {
6 ($code:expr) => {
7 build_helper::util::detail_exit($code, cfg!(test));
8 };
9 }
10
11 /// If code is not 0 (successful exit status), exit status is 101 (rust's default error code.)
12 /// If `is_test` true and code is an error code, it will cause a panic.
detail_exit(code: i32, is_test: bool) -> !13 pub fn detail_exit(code: i32, is_test: bool) -> ! {
14 // if in test and code is an error code, panic with status code provided
15 if is_test {
16 panic!("status code: {}", code);
17 } else {
18 // otherwise,exit with provided status code
19 std::process::exit(code);
20 }
21 }
22
fail(s: &str) -> !23 pub fn fail(s: &str) -> ! {
24 eprintln!("\n\n{}\n\n", s);
25 detail_exit(1, cfg!(test));
26 }
27
try_run(cmd: &mut Command, print_cmd_on_fail: bool) -> Result<(), ()>28 pub fn try_run(cmd: &mut Command, print_cmd_on_fail: bool) -> Result<(), ()> {
29 let status = match cmd.status() {
30 Ok(status) => status,
31 Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", cmd, e)),
32 };
33 if !status.success() {
34 if print_cmd_on_fail {
35 println!(
36 "\n\ncommand did not execute successfully: {:?}\n\
37 expected success, got: {}\n\n",
38 cmd, status
39 );
40 }
41 Err(())
42 } else {
43 Ok(())
44 }
45 }
46