1#[cfg(feature = "codegen")]
2use std::io::Write;
3
4use crate::{
5 defined_constants::{
6 DEPENDENCY_INCLUDE_DIR, GENERATED_DESCRIPTORS_FILE, GENERATED_SOURCES_DIR,
7 },
8 error::ChurResult,
9 manifest::Manifest,
10 Config,
11};
12
13pub fn execute(cfg: Config) -> ChurResult<()> {
14 let mut manifest = Manifest::load().unwrap_or_default();
15
16 let mut additional_include_dirs = vec![];
18
19 for dep in cfg.dependencies {
20 if let Some(hash) = manifest.get_cached_dep_from_url(&dep.url) {
21 additional_include_dirs.push(format!("{hash}/{}", dep.subdir.unwrap_or_default()));
22 } else {
23 let hash = dep.fetch()?;
24 manifest.register_cached_manifest(dep.url, &hash);
25 additional_include_dirs.push(format!("{hash}/{}", dep.subdir.unwrap_or_default()));
26 }
27 }
28
29 manifest.save()?;
30
31 let mut include_dirs = vec![cfg.root_dir];
32
33 let mut builder = tonic_build::configure().out_dir(GENERATED_SOURCES_DIR.as_path());
34
35 for dir in additional_include_dirs {
36 include_dirs.push(DEPENDENCY_INCLUDE_DIR.join(dir));
37 }
38
39 if cfg.file_descriptors {
40 builder = builder.file_descriptor_set_path(GENERATED_DESCRIPTORS_FILE.as_path())
41 }
42
43 builder.compile(&cfg.protos, &include_dirs)?;
44
45 #[cfg(feature = "codegen")]
46 if let Some(codegen_output) = cfg.codegen {
47 let absolute_output = crate::defined_constants::ROOT_MANIFEST_DIR.join(codegen_output);
48 let mut output_file = std::fs::OpenOptions::new()
49 .truncate(true)
50 .write(true)
51 .open(absolute_output)?;
52
53 let parsed = syn::parse2(crate::include_tree::include_tree()).unwrap();
54
55 let output_string = prettyplease::unparse(&parsed);
56
57 output_file.write_all(output_string.as_bytes())?;
58 }
59
60 Ok(())
61}