icepick/crates/icepick/build.rs

62 lines
2.0 KiB
Rust

use icepick_workflow::Workflow;
use std::{collections::HashMap, path::{PathBuf, Path}};
fn env_var(var: &'static str) -> String {
println!("cargo::rerun-if-env-changed={var}");
std::env::var(var).expect(var)
}
fn track_path(path: &Path) {
println!("cargo::rerun-if-changed={}", path.to_str().unwrap());
}
fn out_dir() -> PathBuf {
// example: [("OUT_DIR", "/tmp/cargo-target/debug/build/wat-c321129a1ce97284/out")]
// this gives us: /tmp/cargo-target/debug
let out_dir = env_var("OUT_DIR");
let mut out_dir_path = PathBuf::from(out_dir);
out_dir_path.pop();
out_dir_path.pop();
out_dir_path.pop();
out_dir_path
}
fn main() {
let out_dir = out_dir();
let crate_dir = env_var("CARGO_MANIFEST_DIR");
let workflows_dir = PathBuf::from(crate_dir).join("workflows");
track_path(&workflows_dir);
let mut workflows_by_module: HashMap<String, Vec<Workflow>> = Default::default();
for module_dir in std::fs::read_dir(&workflows_dir).unwrap() {
let module_dir = module_dir.unwrap();
let path = module_dir.path();
if !path.is_dir() {
panic!("found unexpected file {}", path.to_string_lossy());
}
let mut workflows = vec![];
for workflow_file in std::fs::read_dir(&path).unwrap() {
let workflow_file = workflow_file.unwrap();
let path = workflow_file.path();
if !path.is_file() {
panic!("found unexpected non-file {}", path.to_string_lossy());
}
let file_content = std::fs::read(path).unwrap();
let workflow: Workflow = serde_yaml::from_slice(&file_content).unwrap();
workflows.push(workflow);
}
workflows_by_module.insert(
module_dir.file_name().to_str().unwrap().to_owned(),
workflows,
);
}
let out_path = out_dir.join("workflows.yaml");
let out_file = std::fs::File::create(&out_path).unwrap();
serde_yaml::to_writer(out_file, &workflows_by_module).unwrap();
}