use std::process::{Command, Output};
use crate::error::{self, throw};
use crate::makefile::get_skinner_file;
pub fn execute_makefile(file: &str, target: &str, action: &str) -> String {
let output: Output = Command::new("make")
.args(["-f", file, action, &format!("TARGET={}", target)])
.output()
.expect("Failed to execute make command");
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
format!("{}\n{}", stdout, stderr)
}
pub fn r#do(target: &str, skinner: &str) -> (bool, Option<String>) {
let skinner_file = get_skinner_file(skinner);
if std::fs::metadata(&skinner_file).is_err() {
throw!(error::SKINNER_FILE_NOT_FOUND, exit);
}
let output = execute_makefile(&skinner_file, target, "do");
let mut replaced_with = None;
if output.contains("REPLACED_WITH: ") {
replaced_with = Some(
output
.split("REPLACED_WITH: ")
.nth(1)
.unwrap()
.trim()
.to_string(),
);
}
(output.contains("do: success"), replaced_with)
}
pub fn undo(target: &str, skinner: &str) -> bool {
let skinner_file = get_skinner_file(skinner);
let output = execute_makefile(&skinner_file, target, "undo");
output.contains("undo: success")
}