@@ -15,13 +15,22 @@ pub(crate) struct CliCodegenParams {
1515 pub deprecation_strategy : Option < String > ,
1616 pub no_formatting : bool ,
1717 pub module_visibility : Option < String > ,
18+ pub output_directory : Option < PathBuf > ,
1819}
1920
2021pub ( crate ) fn generate_code ( params : CliCodegenParams ) -> Result < ( ) , failure:: Error > {
21- let deprecation_strategy = params
22- . deprecation_strategy
23- . as_ref ( )
24- . and_then ( |s| s. parse ( ) . ok ( ) ) ;
22+ let CliCodegenParams {
23+ additional_derives,
24+ deprecation_strategy,
25+ no_formatting,
26+ output_directory,
27+ module_visibility : _module_visibility,
28+ query_path,
29+ schema_path,
30+ selected_operation,
31+ } = params;
32+
33+ let deprecation_strategy = deprecation_strategy. as_ref ( ) . and_then ( |s| s. parse ( ) . ok ( ) ) ;
2534
2635 let mut options = GraphQLClientCodegenOptions :: new ( CodegenMode :: Cli ) ;
2736
@@ -32,35 +41,38 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Err
3241 . into ( ) ,
3342 ) ;
3443
35- if let Some ( selected_operation) = params . selected_operation {
44+ if let Some ( selected_operation) = selected_operation {
3645 options. set_operation_name ( selected_operation) ;
3746 }
3847
39- if let Some ( additional_derives) = params . additional_derives {
48+ if let Some ( additional_derives) = additional_derives {
4049 options. set_additional_derives ( additional_derives) ;
4150 }
4251
4352 if let Some ( deprecation_strategy) = deprecation_strategy {
4453 options. set_deprecation_strategy ( deprecation_strategy) ;
4554 }
4655
47- let gen =
48- generate_module_token_stream ( params. query_path . clone ( ) , & params. schema_path , options) ?;
56+ let gen = generate_module_token_stream ( query_path. clone ( ) , & schema_path, options) ?;
4957
5058 let generated_code = gen. to_string ( ) ;
51- let generated_code = if cfg ! ( feature = "rustfmt" ) && !params . no_formatting {
59+ let generated_code = if cfg ! ( feature = "rustfmt" ) && !no_formatting {
5260 format ( & generated_code)
5361 } else {
5462 generated_code
5563 } ;
5664
57- let mut dest_path = params. query_path . clone ( ) ;
58- if dest_path. set_extension ( "rs" ) {
59- let mut file = File :: create ( dest_path) ?;
60- write ! ( file, "{}" , generated_code) ?;
61- } else {
62- log:: error!( "Could not set the file extension on {:?}" , dest_path) ;
63- }
65+ let query_file_name: :: std:: ffi:: OsString = query_path
66+ . file_name ( )
67+ . map ( |s| s. to_owned ( ) )
68+ . ok_or_else ( || format_err ! ( "Failed to find a file name in the provided query path." ) ) ?;
69+
70+ let dest_file_path: PathBuf = output_directory
71+ . map ( |output_dir| output_dir. join ( query_file_name) . with_extension ( "rs" ) )
72+ . unwrap_or_else ( move || query_path. with_extension ( "rs" ) ) ;
73+
74+ let mut file = File :: create ( dest_file_path) ?;
75+ write ! ( file, "{}" , generated_code) ?;
6476
6577 Ok ( ( ) )
6678}
0 commit comments