Skip to content

Commit 2655bca

Browse files
committed
Implement --output-directory option in the CLI
1 parent c137fbe commit 2655bca

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

graphql_client_cli/src/generate.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2021
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());
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
}

graphql_client_cli/src/main.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extern crate log;
33
use env_logger::fmt::{Color, Style, StyledValue};
44
use log::Level;
55

6+
#[macro_use]
67
extern crate failure;
78
extern crate reqwest;
89
extern crate structopt;
@@ -43,10 +44,8 @@ enum Cli {
4344
Generate {
4445
// should be a glob
4546
/// Path to graphql query file.
46-
#[structopt(parse(from_os_str))]
4747
query_path: PathBuf,
4848
/// Path to graphql schema file.
49-
#[structopt(parse(from_os_str))]
5049
schema_path: PathBuf,
5150
/// Name of target query. If you don't set this parameter, cli generate all queries in query file.
5251
#[structopt(short = "o", long = "selected-operation")]
@@ -66,8 +65,14 @@ enum Cli {
6665
no_formatting: bool,
6766
/// You can choose module and target struct visibility from pub and private.
6867
/// Default value is pub.
69-
#[structopt(short = "m", long = "module_visibility")]
68+
#[structopt(short = "m", long = "module-visibility")]
7069
module_visibility: Option<String>,
70+
/// The directory in which the code will be generated.
71+
///
72+
/// If this option is omitted, the code will be generated next to the .graphql
73+
/// file, with the same name and the .rs extension.
74+
#[structopt(short = "o", long = "output-directory")]
75+
output_directory: Option<PathBuf>,
7176
},
7277
}
7378

@@ -82,21 +87,23 @@ fn main() -> Result<(), failure::Error> {
8287
authorization,
8388
} => introspect_schema::introspect_schema(&schema_location, output, authorization),
8489
Cli::Generate {
85-
query_path,
86-
schema_path,
87-
selected_operation,
8890
additional_derives,
8991
deprecation_strategy,
90-
no_formatting,
9192
module_visibility,
92-
} => generate::generate_code(generate::CliCodegenParams {
93+
no_formatting,
94+
output_directory,
9395
query_path,
9496
schema_path,
9597
selected_operation,
98+
} => generate::generate_code(generate::CliCodegenParams {
9699
additional_derives,
97100
deprecation_strategy,
98-
no_formatting,
99101
module_visibility,
102+
no_formatting,
103+
output_directory,
104+
query_path,
105+
schema_path,
106+
selected_operation,
100107
}),
101108
}
102109
}

graphql_client_codegen/src/generated_module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<'a> GeneratedModule<'a> {
4545
let impls = self.build_impls()?;
4646

4747
let struct_declaration = match self.options.mode {
48-
CodegenMode::Cli => quote!(#module_visibility struct #operation_name_ident),
48+
CodegenMode::Cli => quote!(#module_visibility struct #operation_name_ident;),
4949
// The struct is already present in derive mode.
5050
CodegenMode::Derive => quote!(),
5151
};

0 commit comments

Comments
 (0)