xref: /tonic/codegen/src/main.rs (revision aa57ffeb)
1 use std::path::{Path, PathBuf};
2 
3 fn main() {
4     // tonic-health
5     codegen(
6         &PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
7             .parent()
8             .unwrap()
9             .join("tonic-health"),
10         &["proto/health.proto"],
11         &["proto"],
12         &PathBuf::from("src/generated"),
13         &PathBuf::from("src/generated/grpc_health_v1.bin"),
14         true,
15         true,
16     );
17 
18     // tonic-reflection
19     codegen(
20         &PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
21             .parent()
22             .unwrap()
23             .join("tonic-reflection"),
24         &["proto/reflection_v1.proto"],
25         &["proto"],
26         &PathBuf::from("src/generated"),
27         &PathBuf::from("src/generated/reflection_v1.bin"),
28         true,
29         true,
30     );
31     codegen(
32         &PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
33             .parent()
34             .unwrap()
35             .join("tonic-reflection"),
36         &["proto/reflection_v1alpha.proto"],
37         &["proto"],
38         &PathBuf::from("src/generated"),
39         &PathBuf::from("src/generated/reflection_v1alpha1.bin"),
40         true,
41         true,
42     );
43 
44     // tonic-types
45     codegen(
46         &PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
47             .parent()
48             .unwrap()
49             .join("tonic-types"),
50         &["proto/status.proto", "proto/error_details.proto"],
51         &["proto"],
52         &PathBuf::from("src/generated"),
53         &PathBuf::from("src/generated/types.bin"),
54         false,
55         false,
56     );
57 }
58 
59 fn codegen(
60     root_dir: &Path,
61     iface_files: &[&str],
62     include_dirs: &[&str],
63     out_dir: &Path,
64     file_descriptor_set_path: &Path,
65     build_client: bool,
66     build_server: bool,
67 ) {
68     let tempdir = tempfile::Builder::new()
69         .prefix("tonic-codegen-")
70         .tempdir()
71         .unwrap();
72 
73     let iface_files: Vec<PathBuf> = iface_files
74         .iter()
75         .map(|&path| root_dir.join(path))
76         .collect();
77 
78     let include_dirs: Vec<PathBuf> = include_dirs
79         .iter()
80         .map(|&path| root_dir.join(path))
81         .collect();
82     let out_dir = root_dir.join(out_dir);
83     let file_descriptor_set_path = root_dir.join(file_descriptor_set_path);
84 
85     tonic_build::configure()
86         .build_client(build_client)
87         .build_server(build_server)
88         .out_dir(&tempdir)
89         .file_descriptor_set_path(file_descriptor_set_path)
90         .compile(&iface_files, &include_dirs)
91         .unwrap();
92 
93     for path in std::fs::read_dir(tempdir.path()).unwrap() {
94         let path = path.unwrap().path();
95         let to = out_dir.join(
96             path.file_name()
97                 .unwrap()
98                 .to_str()
99                 .unwrap()
100                 .strip_suffix(".rs")
101                 .unwrap()
102                 .replace('.', "_")
103                 + ".rs",
104         );
105         std::fs::copy(&path, &to).unwrap();
106     }
107 }
108