2323//! .run()
2424//! .expect("Gen async code failed.");
2525//! }
26+ //! ```
27+ //! If there's no out_dir and use 'gen_mod' feature
28+ //! You can use the following method to include the target file
29+ //! include!(concat!(env!("OUT_DIR"), "/mod.rs"));
2630
2731pub use protobuf_codegen:: {
2832 Customize as ProtobufCustomize , CustomizeCallback as ProtobufCustomizeCallback ,
@@ -45,8 +49,8 @@ mod str_lit;
4549/// Invoke pure rust codegen.
4650#[ derive( Debug , Default ) ]
4751pub struct Codegen {
48- /// --lang_out= param
49- out_dir : PathBuf ,
52+ /// --lang_out= param ,if out_dir is none ,will use env 'OUT_DIR' path
53+ out_dir : Option < PathBuf > ,
5054 /// -I args
5155 includes : Vec < PathBuf > ,
5256 /// List of .proto files to compile
@@ -65,9 +69,9 @@ impl Codegen {
6569 Self :: default ( )
6670 }
6771
68- /// Set the output directory for codegen.
72+ /// Set the output directory for codegen. Support None out_dir
6973 pub fn out_dir ( & mut self , out_dir : impl AsRef < Path > ) -> & mut Self {
70- self . out_dir = out_dir. as_ref ( ) . to_owned ( ) ;
74+ self . out_dir = Some ( out_dir. as_ref ( ) . to_owned ( ) ) ;
7175 self
7276 }
7377
@@ -132,11 +136,19 @@ impl Codegen {
132136 let includes: Vec < & Path > = self . includes . iter ( ) . map ( |p| p. as_path ( ) ) . collect ( ) ;
133137 let inputs: Vec < & Path > = self . inputs . iter ( ) . map ( |p| p. as_path ( ) ) . collect ( ) ;
134138 let p = parse_and_typecheck ( & includes, & inputs) ?;
139+ // If out_dir is none ,dst_path will be setting in path_dir
140+ let dst_path = self . out_dir . clone ( ) . unwrap_or_else ( || {
141+ // Add default path from env OUT_DIR, if no OUT_DIR env ,that's will be current path
142+ std:: env:: var ( "OUT_DIR" ) . map_or_else (
143+ |_| std:: env:: current_dir ( ) . unwrap_or_default ( ) ,
144+ PathBuf :: from,
145+ )
146+ } ) ;
135147
136148 if self . rust_protobuf {
137149 self . rust_protobuf_codegen
138150 . pure ( )
139- . out_dir ( & self . out_dir )
151+ . out_dir ( & dst_path )
140152 . inputs ( & self . inputs )
141153 . includes ( & self . includes )
142154 . run ( )
@@ -146,7 +158,7 @@ impl Codegen {
146158 ttrpc_compiler:: codegen:: gen_and_write (
147159 & p. file_descriptors ,
148160 & p. relative_paths ,
149- & self . out_dir ,
161+ & dst_path ,
150162 & self . customize ,
151163 )
152164 }
0 commit comments