23
23
//! .run()
24
24
//! .expect("Gen async code failed.");
25
25
//! }
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"));
26
30
27
31
pub use protobuf_codegen:: {
28
32
Customize as ProtobufCustomize , CustomizeCallback as ProtobufCustomizeCallback ,
@@ -45,8 +49,8 @@ mod str_lit;
45
49
/// Invoke pure rust codegen.
46
50
#[ derive( Debug , Default ) ]
47
51
pub 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 > ,
50
54
/// -I args
51
55
includes : Vec < PathBuf > ,
52
56
/// List of .proto files to compile
@@ -65,9 +69,9 @@ impl Codegen {
65
69
Self :: default ( )
66
70
}
67
71
68
- /// Set the output directory for codegen.
72
+ /// Set the output directory for codegen. Support None out_dir
69
73
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 ( ) ) ;
71
75
self
72
76
}
73
77
@@ -132,11 +136,19 @@ impl Codegen {
132
136
let includes: Vec < & Path > = self . includes . iter ( ) . map ( |p| p. as_path ( ) ) . collect ( ) ;
133
137
let inputs: Vec < & Path > = self . inputs . iter ( ) . map ( |p| p. as_path ( ) ) . collect ( ) ;
134
138
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
+ } ) ;
135
147
136
148
if self . rust_protobuf {
137
149
self . rust_protobuf_codegen
138
150
. pure ( )
139
- . out_dir ( & self . out_dir )
151
+ . out_dir ( & dst_path )
140
152
. inputs ( & self . inputs )
141
153
. includes ( & self . includes )
142
154
. run ( )
@@ -146,7 +158,7 @@ impl Codegen {
146
158
ttrpc_compiler:: codegen:: gen_and_write (
147
159
& p. file_descriptors ,
148
160
& p. relative_paths ,
149
- & self . out_dir ,
161
+ & dst_path ,
150
162
& self . customize ,
151
163
)
152
164
}
0 commit comments