forked from massalabs/massa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
96 lines (82 loc) · 3.53 KB
/
build.rs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2023 MASSA LABS <[email protected]>
fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "build-tonic")]
tonic::build()?;
Ok(())
}
#[cfg(feature = "build-tonic")]
mod tonic {
use glob::glob;
use std::{path::PathBuf, process::Command};
/// This function is responsible for building the Massa protobuf API and generating documentation
pub fn build() -> Result<(), Box<dyn std::error::Error>> {
// Find all protobuf files in the 'proto/massa/' directory
let protos = find_protos("proto/massa/")?;
// Configure and compile the protobuf API
tonic_build::configure()
.build_server(true)
.build_transport(true)
.build_client(true)
.type_attribute(
".google.api.HttpRule",
"#[cfg(not(doctest))]\n\
#[allow(dead_code)]\n\
pub struct HttpRuleComment{}\n\
/// HACK: see docs in [`HttpRuleComment`] ignored in doctest pass",
)
.file_descriptor_set_path("src/api.bin")
.include_file("_includes.rs")
.out_dir("src/")
.compile(
&protos,
&["proto/massa/api/v1/", "proto/third-party"], // specify the root location to search proto dependencies
)
.map_err(|e| format!("protobuf compilation error: {:?}", e))?;
// Generate documentation for the protobuf API
generate_doc(&protos).map_err(|e| format!("protobuf documentation error: {:?}", e))?;
// Return Ok if the build and documentation generation were successful
Ok(())
}
/// Find all .proto files in the specified directory and its subdirectories
fn find_protos(dir_path: &str) -> Result<Vec<PathBuf>, Box<dyn std::error::Error>> {
let glob_pattern = format!("{dir_path}/**/*.proto", dir_path = dir_path);
let paths = glob(&glob_pattern)?.flatten().collect();
Ok(paths)
}
/// Generate markdown and HTML documentation for the given protocol buffer files
fn generate_doc(protos: &Vec<PathBuf>) -> Result<(), Box<dyn std::error::Error>> {
// Generate markdown documentation using protoc.
let protoc_md_cmd_output = Command::new("protoc")
.args(protos)
.arg("--proto_path=./proto/massa/api/v1")
.arg("--proto_path=./proto/third-party")
.arg("--doc_out=./doc")
.arg("--doc_opt=markdown,api.md")
.output()?;
// If protoc failed to generate markdown documentation, return an error.
if !protoc_md_cmd_output.status.success() {
return Err(format!(
"protoc generate MARKDOWN documentation failed: {}",
String::from_utf8_lossy(&protoc_md_cmd_output.stderr)
)
.into());
}
// Generate HTML documentation using protoc.
let protoc_html_cmd_output = Command::new("protoc")
.args(protos)
.arg("--proto_path=./proto/massa/api/v1")
.arg("--proto_path=./proto/third-party")
.arg("--doc_out=./doc")
.arg("--doc_opt=html,index.html")
.output()?;
// If protoc failed to generate HTML documentation, return an error.
if !protoc_html_cmd_output.status.success() {
return Err(format!(
"protoc generate HTML documentation failed: {}",
String::from_utf8_lossy(&protoc_md_cmd_output.stderr)
)
.into());
}
Ok(())
}
}