1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::fs;
use std::path::PathBuf;
use tonic_prost_build::configure;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_dir = PathBuf::from(std::env::var("OUT_DIR")?);
// Create proto directory structure in OUT_DIR
let proto_dir = out_dir.join("pkg/lib/pb/model/protos");
fs::create_dir_all(&proto_dir)?;
// Pre-process: Fix naming conflicts in models.proto
// The proto has `oneof content` + `message Content` in the same scope,
// which causes prost to generate both `enum Content` and `struct Content`.
// Rename the oneofs to avoid the conflict.
let models_proto = fs::read_to_string("./pb/pkg/lib/pb/model/protos/models.proto")?;
let models_proto = models_proto
// In Block message: rename `oneof content` to `oneof content_value`
.replace("oneof content {", "oneof content_value {")
// In Metadata message: rename `oneof payload` to `oneof payload_value`
.replace(
"oneof payload {\n Payload.IdentityPayload",
"oneof payload_value {\n Payload.IdentityPayload",
);
fs::write(proto_dir.join("models.proto"), models_proto)?;
// Copy localstore.proto (it imports models.proto)
fs::copy(
"./pb/pkg/lib/pb/model/protos/localstore.proto",
proto_dir.join("localstore.proto"),
)?;
// Stage 1: Compile both model protos
configure()
.build_client(false)
.build_server(false)
.compile_protos(
&[
proto_dir.join("models.proto").to_str().unwrap(),
proto_dir.join("localstore.proto").to_str().unwrap(),
],
&[&out_dir.to_string_lossy()],
)?;
// Stage 2: Compile the service protos, using extern_path to reference
// the model types we just generated
configure()
.build_client(true)
.build_server(false)
.extern_path(".anytype.model", "crate::model")
.compile_protos(
&[
"./pb/protos/service/service.proto",
"./pb/protos/commands.proto",
"./pb/protos/events.proto",
"./pb/protos/snapshot.proto",
"./pb/protos/changes.proto",
],
&[".", "./pb"],
)?;
Ok(())
}