Skip to content

Commit 1350ff7

Browse files
committed
drop servicegenerater and use fds to inject into files
Signed-off-by: clux <sszynrae@gmail.com>
1 parent 80289eb commit 1350ff7

67 files changed

Lines changed: 103 additions & 73 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
22
Cargo.lock
3+
protos.fds

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ edition = "2018"
77
[dependencies]
88
bytes = "1.0.1"
99
prost = "0.8.0"
10-
prost-types = "0.8"
1110

1211
[build-dependencies]
1312
prost-build = "0.8.0"
13+
prost-types = "0.8.0"
14+
prost = "0.8.0"

README.md

Lines changed: 3 additions & 24 deletions

build.rs

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,33 @@
1-
use std::cell::RefCell;
2-
use std::rc::Rc;
3-
4-
#[derive(Default)]
5-
struct GeneratorState {
6-
service_names: Vec<String>,
7-
finalized: usize,
8-
generated: 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-
state.generated += 1;
27-
// TODO: THIS doesn't work? never called by prost_build, bug?
28-
let generics = format!("// TODO: generate\n");
29-
buf.push_str(&generics);
30-
}
31-
32-
fn finalize(&mut self, buf: &mut String) {
33-
let mut state = self.state.borrow_mut();
34-
state.finalized += 1;
35-
// NB: THIS works, but we need a name here before it's useful
36-
//let generics = format!("// TODO: finalize\n");
37-
//buf.push_str(&generics);
38-
}
39-
}
1+
use prost_types::{FileDescriptorProto, FileDescriptorSet};
2+
use prost::Message;
403

414
fn main() -> std::io::Result<()> {
42-
let protos: Vec<&str> = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/protos.list"))
5+
let protos = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/protos.list"))
436
.lines()
44-
.collect();
7+
.collect::<Vec<&str>>();
458

46-
let state = Rc::new(RefCell::new(GeneratorState::default()));
479
prost_build::Config::new()
48-
.service_generator(Box::new(KubeGenerator::new(Rc::clone(&state))))
10+
// should probably switch to this
11+
//.btree_map(&["."])
4912
.out_dir("./out")
5013
.compile_protos(protos.as_slice(), &["protos/"])?;
5114

52-
// sanity
53-
let state = state.borrow();
54-
assert_eq!(state.finalized, protos.len());
55-
assert_eq!(state.generated, protos.len()); // TODO: why does generate not trigger
15+
let apis = std::fs::read_to_string("./openapi/api-resources.json")?;
16+
17+
let buf = std::fs::read("./protos.fds").unwrap();
18+
let fds = FileDescriptorSet::decode(&*buf).unwrap(); // pulls in proto::Message
19+
20+
// NB: FDS fields: https://github.com/tokio-rs/prost/blob/32bc87cd0b7301f6af1a338e9afd7717d0f42ca9/prost-types/src/protobuf.rs#L1-L7
21+
// FDS usage: https://github.com/tokio-rs/prost/blob/32bc87cd0b7301f6af1a338e9afd7717d0f42ca9/prost-build/src/lib.rs#L765-L825
22+
for f in fds.file {
23+
use std::io::Write;
24+
if let Some(pkg) = f.package {
25+
let pkgpath = std::path::Path::new("./out").join(format!("{}.rs", pkg));
26+
let generics = format!("// TODO genericsfor {}\n", pkg);
27+
let mut file = std::fs::OpenOptions::new().write(true).append(true).open(&pkgpath)?;
28+
file.write(generics.as_bytes())?;
29+
}
30+
}
5631

5732
// Generate code in `src/` by reading files in `OUT_DIR`?
5833
// Need to create `mod.rs` file for each level based on the filename, and write generated code in correct file.

justfile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,20 @@ swagger-transform:
6161
# Download and generate all swagger dependent files
6262
swagger: swagger-dl swagger-patch swagger-transform
6363

64+
# Build a FileDescriptorSet for custom code generation
65+
build-fds:
66+
#!/usr/bin/env bash
67+
set -exuo pipefail
68+
shopt -s globstar
69+
protoc \
70+
--include_imports \
71+
--include_source_info \
72+
--descriptor_set_out=protos.fds \
73+
--proto_path=./protos \
74+
./protos/**/*.proto
75+
6476
# Generate the library code from completed swagger and protos
65-
build:
77+
build: build-fds
6678
#!/usr/bin/env bash
6779
set -exuo pipefail
6880
rm -rf out/ && mkdir out

out/api.admission.v1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,4 @@ pub struct AdmissionReview {
137137
#[prost(message, optional, tag="2")]
138138
pub response: ::core::option::Option<AdmissionResponse>,
139139
}
140+
// TODO genericsfor api.admission.v1

out/api.admission.v1beta1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,4 @@ pub struct AdmissionReview {
137137
#[prost(message, optional, tag="2")]
138138
pub response: ::core::option::Option<AdmissionResponse>,
139139
}
140+
// TODO genericsfor api.admission.v1beta1

out/api.admissionregistration.v1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,4 @@ pub struct WebhookClientConfig {
457457
#[prost(bytes="vec", optional, tag="2")]
458458
pub ca_bundle: ::core::option::Option<::prost::alloc::vec::Vec<u8>>,
459459
}
460+
// TODO genericsfor api.admissionregistration.v1

out/api.admissionregistration.v1beta1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,4 @@ pub struct WebhookClientConfig {
465465
#[prost(bytes="vec", optional, tag="2")]
466466
pub ca_bundle: ::core::option::Option<::prost::alloc::vec::Vec<u8>>,
467467
}
468+
// TODO genericsfor api.admissionregistration.v1beta1

out/api.apiserverinternal.v1alpha1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,4 @@ pub struct StorageVersionStatus {
9797
#[prost(message, repeated, tag="3")]
9898
pub conditions: ::prost::alloc::vec::Vec<StorageVersionCondition>,
9999
}
100+
// TODO genericsfor api.apiserverinternal.v1alpha1

0 commit comments

Comments
 (0)