Skip to content

Commit

Permalink
Fix rebuild for codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Oct 11, 2023
1 parent 0901547 commit 7cf0fc3
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions spdlog/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,28 @@ fn generate_code_test_utils() -> Result<(), Box<dyn Error>> {
fs::create_dir(&out_dir)?;
}

let input = fs::read_to_string("src/test_utils/common.rs")?;
let input = read_code("src/test_utils/common.rs")?;

write_generated_code(
fs::write(
out_dir.join("common_for_doc_test.rs"),
format!("mod test_utils {{\n{}\n}}", input)
.lines()
.map(|line| format!("# {}\n", line))
.collect::<String>(),
)?;
write_generated_code(
fs::write(
out_dir.join("common_for_integration_test.rs"),
format!("#[allow(dead_code)]\nmod test_utils {{\n{}\n}}", input),
)?;
write_generated_code(
fs::write(
out_dir.join("common_for_unit_test.rs"),
input.replace("spdlog::", "crate::"),
)?;

Ok(())
}

fn write_generated_code(
path: impl AsRef<Path>,
contents: impl AsRef<[u8]>,
) -> Result<(), Box<dyn Error>> {
fn read_code(path: impl AsRef<Path>) -> Result<String, Box<dyn Error>> {
println!("cargo:rerun-if-changed={}", path.as_ref().display());
fs::write(path, contents)?;
Ok(())
Ok(fs::read_to_string(path)?)
}

0 comments on commit 7cf0fc3

Please sign in to comment.