cri_kit/build/
mod.rs

1mod transformer;
2
3use std::{env, fs::File, io::Write, path::Path};
4
5/// This is used as such:
6/// in your build.rs;
7/// instead of:
8///     ```rust
9///     tonic_build::configure()
10///     // ...
11///    ```
12/// do this:
13///     ```rust
14///     rust_cri_kit::build::cri_init();
15///     ```
16#[inline]
17pub fn cri_init(project_name: &str) -> Result<(), Box<dyn std::error::Error>> {
18    #[cfg(feature = "v1")]
19    let proto_path = format!("{}/v1.proto", env::var("OUT_DIR").unwrap());
20    #[cfg(feature = "v1alpha2")]
21    let proto_path = format!("{}/v1alpha2.proto", env::var("OUT_DIR").unwrap());
22
23    let mut file = File::create(&proto_path)?;
24    #[cfg(feature = "v1")]
25    let content = include_str!("../../proto/v1.proto").to_string();
26    #[cfg(feature = "v1alpha2")]
27    let content = include_str!("../../proto/v1alpha2.proto").to_string();
28
29    file.write_all(content.as_bytes())?;
30    tonic_build::configure()
31        .build_client(false)
32        .build_server(true)
33        .compile(
34            &[&proto_path],
35            &[Path::new(proto_path.as_str()).parent().unwrap()],
36        )?;
37
38    // gen src/cri.rs
39    let mut file = File::create("src/cri.rs")?;
40
41    #[cfg(feature = "v1")]
42    let fname = "/runtime.v1.rs";
43    #[cfg(feature = "v1alpha2")]
44    let fname = "/runtime.v1alpha2.rs";
45
46    let gen_rs_path = format!("{}{}", env::var("OUT_DIR").unwrap(), fname);
47    let content = transformer::transform(gen_rs_path, project_name.to_string())?;
48
49    let stub = format!(
50        "\
51// AUTO GENERATED BY rust-cri-kit, DO NOT CHANGE YOURSELF
52
53#![allow(clippy::all)]
54{}
55",
56        content
57    );
58
59    let stub = stub.as_bytes();
60
61    file.write_all(stub)?;
62
63    Ok(())
64}