1- use std:: path:: { Path , PathBuf } ;
2-
31fn main ( ) {
42 let output = std:: process:: Command :: new ( "git" )
53 . args ( [ "rev-parse" , "HEAD" ] )
@@ -8,55 +6,4 @@ fn main() {
86 let rev = String :: from_utf8 ( output. stdout ) . expect ( "Failed to parse git output" ) ;
97 println ! ( "cargo:rustc-env=GIT_COMMIT_SHA={rev}" ) ;
108 println ! ( "cargo:rustc-rerun-if-changed=.git/HEAD" ) ;
11-
12- let root = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) . join ( "protos" ) ;
13- let descriptor_path = root. join ( "proto_descriptor.bin" ) ;
14- println ! ( "cargo:rerun-if-changed={}" , descriptor_path. display( ) ) ;
15- let descriptor_mtime = std:: fs:: metadata ( & descriptor_path)
16- . map ( |m| m. modified ( ) . unwrap ( ) )
17- . unwrap_or ( std:: time:: SystemTime :: UNIX_EPOCH ) ;
18- let mut run_protoc = false ;
19- let proto_files = vec ! [ root. join( "report.proto" ) ] ;
20- for proto_file in & proto_files {
21- println ! ( "cargo:rerun-if-changed={}" , proto_file. display( ) ) ;
22- let mtime = match std:: fs:: metadata ( proto_file) {
23- Ok ( m) => m. modified ( ) . unwrap ( ) ,
24- Err ( e) => panic ! ( "Failed to stat proto file {}: {:?}" , proto_file. display( ) , e) ,
25- } ;
26- if mtime > descriptor_mtime {
27- run_protoc = true ;
28- }
29- }
30-
31- fn prost_config ( descriptor_path : & Path , run_protoc : bool ) -> prost_build:: Config {
32- let mut config = prost_build:: Config :: new ( ) ;
33- config. file_descriptor_set_path ( descriptor_path) ;
34- // If our cached descriptor is up-to-date, we don't need to run protoc.
35- // This is helpful so that users don't need to have protoc installed
36- // unless they're updating the protos.
37- if !run_protoc {
38- config. skip_protoc_run ( ) ;
39- }
40- config
41- }
42- if let Err ( e) =
43- prost_config ( & descriptor_path, run_protoc) . compile_protos ( & proto_files, & [ root. as_path ( ) ] )
44- {
45- if e. kind ( ) == std:: io:: ErrorKind :: NotFound && e. to_string ( ) . contains ( "protoc" ) {
46- eprintln ! ( "protoc not found, skipping protobuf compilation" ) ;
47- prost_config ( & descriptor_path, false )
48- . compile_protos ( & proto_files, & [ root. as_path ( ) ] )
49- . expect ( "Failed to compile protos" ) ;
50- } else {
51- panic ! ( "Failed to compile protos: {e:?}" ) ;
52- }
53- }
54-
55- let descriptor_set = std:: fs:: read ( descriptor_path) . expect ( "Failed to read descriptor set" ) ;
56- pbjson_build:: Builder :: new ( )
57- . register_descriptors ( & descriptor_set)
58- . expect ( "Failed to register descriptors" )
59- . preserve_proto_field_names ( )
60- . build ( & [ ".objdiff" ] )
61- . expect ( "Failed to build pbjson" ) ;
629}
0 commit comments