use std::path::PathBuf;
use std::process::Command;
use std::{fmt, str::FromStr};
pub struct HandlerProcess{
handled_path: Option<PathBuf>, worker_program_path: Option<PathBuf>, pid: Option<u32> }
impl HandlerProcess {
pub fn new(handled_str_path: &str, program_str_path: &str) -> Self {
let mut command = Command::new(program_str_path);
if let Ok(child) = command.spawn() {
let handled_path = PathBuf::from_str(handled_str_path).unwrap();
let program_path = PathBuf::from_str(program_str_path).unwrap();
Self{pid: Some(child.id()), handled_path: Some(handled_path), worker_program_path: Some(program_path)}
} else {
Self{pid: None, handled_path: None, worker_program_path: None}
}
}
pub fn new_handle_current_folder(program_str_path: &str) -> Self {
Self::new(".", program_str_path)
}
}
impl fmt::Display for HandlerProcess{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "PID: {:?}. Handle path: {:?}. Worker path: {:?}", self.pid, self.handled_path, self.worker_program_path)
}
}
pub fn build_worker(package_folder_str_path: &str){
assert!(try_clean_worker_build(package_folder_str_path));
let status = Command::new("cargo").arg("build")
.current_dir(package_folder_str_path).status().expect("Worker build process failed to execute");
assert!(status.success());
}
fn try_clean_worker_build(package_folder_str_path: &str) -> bool{
let status = Command::new("cargo").arg("clean")
.current_dir(package_folder_str_path).status().expect("Worker cleanup process failed to execute");
return status.success();
}