@@ -15,13 +15,22 @@ pub(crate) struct CliCodegenParams {
15
15
pub deprecation_strategy : Option < String > ,
16
16
pub no_formatting : bool ,
17
17
pub module_visibility : Option < String > ,
18
+ pub output_directory : Option < PathBuf > ,
18
19
}
19
20
20
21
pub ( 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 ( ) ) ;
25
34
26
35
let mut options = GraphQLClientCodegenOptions :: new ( CodegenMode :: Cli ) ;
27
36
@@ -32,35 +41,38 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Err
32
41
. into ( ) ,
33
42
) ;
34
43
35
- if let Some ( selected_operation) = params . selected_operation {
44
+ if let Some ( selected_operation) = selected_operation {
36
45
options. set_operation_name ( selected_operation) ;
37
46
}
38
47
39
- if let Some ( additional_derives) = params . additional_derives {
48
+ if let Some ( additional_derives) = additional_derives {
40
49
options. set_additional_derives ( additional_derives) ;
41
50
}
42
51
43
52
if let Some ( deprecation_strategy) = deprecation_strategy {
44
53
options. set_deprecation_strategy ( deprecation_strategy) ;
45
54
}
46
55
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) ?;
49
57
50
58
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 {
52
60
format ( & generated_code)
53
61
} else {
54
62
generated_code
55
63
} ;
56
64
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) ?;
64
76
65
77
Ok ( ( ) )
66
78
}
0 commit comments