|
1 |
| -use std::io::Result; |
| 1 | +use std::cell::RefCell; |
| 2 | +use std::rc::Rc; |
| 3 | + |
| 4 | +#[derive(Default)] |
| 5 | +struct GeneratorState { |
| 6 | + service_names: Vec<String>, |
| 7 | + package_names: Vec<String>, |
| 8 | + finalized: usize, |
| 9 | +} |
| 10 | + |
| 11 | +struct KubeGenerator { |
| 12 | + data: String, |
| 13 | + state: Rc<RefCell<GeneratorState>>, |
| 14 | +} |
| 15 | +impl KubeGenerator { |
| 16 | + fn new(state: Rc<RefCell<GeneratorState>>) -> Self { |
| 17 | + let data = std::fs::read_to_string("./openapi/api-resources.json").unwrap(); |
| 18 | + Self { data, state } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl prost_build::ServiceGenerator for KubeGenerator { |
| 23 | + fn generate(&mut self, service: prost_build::Service, _buf: &mut String) { |
| 24 | + let mut state = self.state.borrow_mut(); |
| 25 | + state.service_names.push(service.name); |
| 26 | + } |
| 27 | + |
| 28 | + fn finalize(&mut self, _buf: &mut String) { |
| 29 | + let mut state = self.state.borrow_mut(); |
| 30 | + state.finalized += 1; |
| 31 | + } |
| 32 | + |
| 33 | + fn finalize_package(&mut self, package: &str, buf: &mut String) { |
| 34 | + let mut state = self.state.borrow_mut(); |
| 35 | + state.package_names.push(package.to_string()); |
| 36 | + // TODO: generate generics for pkg here using self.data |
| 37 | + let pkg_generics = format!("// blahtest"); |
| 38 | + buf.push_str(&pkg_generics); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +fn main() -> std::io::Result<()> { |
| 43 | + let protos: Vec<&str> = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/protos.list")) |
| 44 | + .lines() |
| 45 | + .collect(); |
| 46 | + |
| 47 | + let state = Rc::new(RefCell::new(GeneratorState::default())); |
| 48 | + prost_build::Config::new() |
| 49 | + .service_generator(Box::new(KubeGenerator::new(Rc::clone(&state)))) |
| 50 | + .compile_protos(protos.as_slice(), &["protos/"])?; |
| 51 | + |
| 52 | + // sanity |
| 53 | + let state = state.borrow(); |
| 54 | + //assert_eq!(state.service_names.len(), protos.len()); zero atm.. |
| 55 | + assert_eq!(state.finalized, protos.len()); |
2 | 56 |
|
3 |
| -fn main() -> Result<()> { |
4 |
| - prost_build::Config::new().compile_protos( |
5 |
| - &[ |
6 |
| - "protos/api/admissionregistration/v1beta1/generated.proto", |
7 |
| - "protos/api/admissionregistration/v1/generated.proto", |
8 |
| - "protos/api/admission/v1beta1/generated.proto", |
9 |
| - "protos/api/admission/v1/generated.proto", |
10 |
| - "protos/api/apiserverinternal/v1alpha1/generated.proto", |
11 |
| - "protos/api/apps/v1beta1/generated.proto", |
12 |
| - "protos/api/apps/v1beta2/generated.proto", |
13 |
| - "protos/api/apps/v1/generated.proto", |
14 |
| - "protos/api/authentication/v1beta1/generated.proto", |
15 |
| - "protos/api/authentication/v1/generated.proto", |
16 |
| - "protos/api/authorization/v1beta1/generated.proto", |
17 |
| - "protos/api/authorization/v1/generated.proto", |
18 |
| - "protos/api/autoscaling/v1/generated.proto", |
19 |
| - "protos/api/autoscaling/v2beta1/generated.proto", |
20 |
| - "protos/api/autoscaling/v2beta2/generated.proto", |
21 |
| - "protos/api/batch/v1beta1/generated.proto", |
22 |
| - "protos/api/batch/v1/generated.proto", |
23 |
| - "protos/api/certificates/v1beta1/generated.proto", |
24 |
| - "protos/api/certificates/v1/generated.proto", |
25 |
| - "protos/api/coordination/v1beta1/generated.proto", |
26 |
| - "protos/api/coordination/v1/generated.proto", |
27 |
| - "protos/api/core/v1/generated.proto", |
28 |
| - "protos/api/discovery/v1beta1/generated.proto", |
29 |
| - "protos/api/discovery/v1/generated.proto", |
30 |
| - "protos/api/events/v1beta1/generated.proto", |
31 |
| - "protos/api/events/v1/generated.proto", |
32 |
| - "protos/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1/generated.proto", |
33 |
| - "protos/apiextensions-apiserver/pkg/apis/apiextensions/v1/generated.proto", |
34 |
| - "protos/api/extensions/v1beta1/generated.proto", |
35 |
| - "protos/api/flowcontrol/v1alpha1/generated.proto", |
36 |
| - "protos/api/flowcontrol/v1beta1/generated.proto", |
37 |
| - "protos/api/imagepolicy/v1alpha1/generated.proto", |
38 |
| - "protos/apimachinery/pkg/api/resource/generated.proto", |
39 |
| - "protos/apimachinery/pkg/apis/meta/v1beta1/generated.proto", |
40 |
| - "protos/apimachinery/pkg/apis/meta/v1/generated.proto", |
41 |
| - "protos/apimachinery/pkg/apis/testapigroup/v1/generated.proto", |
42 |
| - "protos/apimachinery/pkg/runtime/generated.proto", |
43 |
| - "protos/apimachinery/pkg/runtime/schema/generated.proto", |
44 |
| - "protos/apimachinery/pkg/util/intstr/generated.proto", |
45 |
| - "protos/api/networking/v1beta1/generated.proto", |
46 |
| - "protos/api/networking/v1/generated.proto", |
47 |
| - "protos/api/node/v1alpha1/generated.proto", |
48 |
| - "protos/api/node/v1beta1/generated.proto", |
49 |
| - "protos/api/node/v1/generated.proto", |
50 |
| - "protos/api/policy/v1beta1/generated.proto", |
51 |
| - "protos/api/policy/v1/generated.proto", |
52 |
| - "protos/api/rbac/v1alpha1/generated.proto", |
53 |
| - "protos/api/rbac/v1beta1/generated.proto", |
54 |
| - "protos/api/rbac/v1/generated.proto", |
55 |
| - "protos/api/scheduling/v1alpha1/generated.proto", |
56 |
| - "protos/api/scheduling/v1beta1/generated.proto", |
57 |
| - "protos/api/scheduling/v1/generated.proto", |
58 |
| - "protos/api/storage/v1alpha1/generated.proto", |
59 |
| - "protos/api/storage/v1beta1/generated.proto", |
60 |
| - "protos/api/storage/v1/generated.proto", |
61 |
| - "protos/kube-aggregator/pkg/apis/apiregistration/v1beta1/generated.proto", |
62 |
| - "protos/kube-aggregator/pkg/apis/apiregistration/v1/generated.proto", |
63 |
| - "protos/metrics/pkg/apis/custom_metrics/v1beta1/generated.proto", |
64 |
| - "protos/metrics/pkg/apis/custom_metrics/v1beta2/generated.proto", |
65 |
| - "protos/metrics/pkg/apis/external_metrics/v1beta1/generated.proto", |
66 |
| - "protos/metrics/pkg/apis/metrics/v1alpha1/generated.proto", |
67 |
| - "protos/metrics/pkg/apis/metrics/v1beta1/generated.proto", |
68 |
| - ], |
69 |
| - &["protos/"], |
70 |
| - )?; |
71 | 57 | // Generate code in `src/` by reading files in `OUT_DIR`?
|
72 | 58 | // Need to create `mod.rs` file for each level based on the filename, and write generated code in correct file.
|
73 | 59 | Ok(())
|
|
0 commit comments