#![feature(unix_send_signal)] use core::time::Duration; use std::{ io::Read, os::unix::process::ChildExt, path::Path, process::{ChildStdout, Command, ExitStatus, Stdio}, thread::sleep, time::Instant, }; fn main() { // add_from_dir("../"); return; let server_path = std::env::current_dir().unwrap_or_else(|_| { std::env::var("CARGO_MANIFEST_DIR") .expect("CARGO_MANIFEST_DIR") .into() }); unsafe { std::env::set_var( "CARGO_TARGET_DIR", server_path .parent() .unwrap() .join("target-server") .to_str() .unwrap(), ) }; let web_path = server_path.parent().expect("no parent").join("calendar"); add_from_dir(&web_path); build_calendar(&web_path); } fn build_calendar(web_path: &Path) { let mut cmd = Command::new("trunk"); let target_dir = web_path.parent().unwrap().join("target-calendar"); cmd.current_dir(web_path) // .env_clear() // .env("PATH", std::env::var("PATH").unwrap_or_default()) .arg("build") .arg("--release") .arg("--color") .arg("never") .arg("--verbose") .env("CARGO_TARGET_DIR", target_dir.to_str().unwrap()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .stdin(Stdio::null()); let mut child = cmd.spawn().unwrap(); let start = Instant::now(); let status = loop { if let Some(status) = child.try_wait().unwrap() { break Some(status); } if (Instant::now() - start) > Duration::from_secs(30) { eprintln!("build_calendar timed out"); if let Err(err) = child.send_signal(9) { panic!("killing build_calendar build: {err}"); } // panic!("a"); break None; } sleep(Duration::from_millis(100)); }; let status_code = status.and_then(|status| status.code()).unwrap_or(!0u8 as _); if status_code != 0 { eprintln!("trunk build status code: {status_code}"); // // if let Some(mut stdout) = child.stdout.take() { // // // let mut buf = vec![]; // // let mut buf = [0u8; 1]; // // let mut cnt = 0usize; // // while let Ok(len) = stdout.read(&mut buf) { // // if cnt == 2269 { // // panic!("A"); // // } // // if len == 0 { // // break; // // } else { // // eprint!("{}", buf[0] as char); // // cnt += 1; // // } // // } // // // panic!("a"); // // // stdout.read_to_end(&mut buf).unwrap(); // // // eprintln!("trunk build stdout: {}", String::from_utf8(buf).unwrap()); // // } // if let Some(mut stderr) = child.stderr.take() { // // let mut buf = vec![]; // // stderr.read_to_end(&mut buf).unwrap(); // let mut buf = [0u8; 1]; // let mut cnt = 0usize; // while let Ok(len) = stderr.read(&mut buf) { // if len == 0 { // break; // } else { // eprint!("{}", buf[0] as char); // cnt += 1; // } // if cnt == 50 { // panic!("A"); // } // } // panic!("a"); // // eprintln!("trunk build stderr: {}", String::from_utf8(buf).unwrap()); // } eprintln!("calendar build failed"); } } #[allow(unused)] fn add_from_dir(dir: &Path) { for item in std::fs::read_dir(dir) .unwrap_or_else(|err| panic!("could not read calendar source directory ({dir:?}): {err}")) { let item = item.unwrap_or_else(|err| panic!("item in {dir:?} source directory: {err}")); if let Some(file_name) = item.file_name().to_str() && file_name.ends_with(".rs") { let path = dir.join(file_name); println!("cargo:rerun-if-changed={}", path.to_str().unwrap()); } else if item.file_type().map(|t| t.is_dir()).unwrap_or_default() { add_from_dir(&item.path()); } } }