1fad3a1d3Sopenharmony_ciuse crate::workspace_path;
2fad3a1d3Sopenharmony_ciuse anyhow::Result;
3fad3a1d3Sopenharmony_ciuse proc_macro2::TokenStream;
4fad3a1d3Sopenharmony_ciuse std::fs;
5fad3a1d3Sopenharmony_ciuse std::io::Write;
6fad3a1d3Sopenharmony_ciuse std::path::Path;
7fad3a1d3Sopenharmony_ci
8fad3a1d3Sopenharmony_cipub fn write(relative_to_workspace_root: impl AsRef<Path>, content: TokenStream) -> Result<()> {
9fad3a1d3Sopenharmony_ci    let mut formatted = Vec::new();
10fad3a1d3Sopenharmony_ci    writeln!(
11fad3a1d3Sopenharmony_ci        formatted,
12fad3a1d3Sopenharmony_ci        "// This file is @generated by syn-internal-codegen."
13fad3a1d3Sopenharmony_ci    )?;
14fad3a1d3Sopenharmony_ci    writeln!(formatted, "// It is not intended for manual editing.")?;
15fad3a1d3Sopenharmony_ci    writeln!(formatted)?;
16fad3a1d3Sopenharmony_ci
17fad3a1d3Sopenharmony_ci    let syntax_tree: syn::File = syn::parse2(content).unwrap();
18fad3a1d3Sopenharmony_ci    let pretty = prettyplease::unparse(&syntax_tree);
19fad3a1d3Sopenharmony_ci    write!(formatted, "{}", pretty)?;
20fad3a1d3Sopenharmony_ci
21fad3a1d3Sopenharmony_ci    let path = workspace_path::get(relative_to_workspace_root);
22fad3a1d3Sopenharmony_ci    if path.is_file() && fs::read(&path)? == formatted {
23fad3a1d3Sopenharmony_ci        return Ok(());
24fad3a1d3Sopenharmony_ci    }
25fad3a1d3Sopenharmony_ci
26fad3a1d3Sopenharmony_ci    fs::write(path, formatted)?;
27fad3a1d3Sopenharmony_ci    Ok(())
28fad3a1d3Sopenharmony_ci}
29