Skip to content

Commit ea069fb

Browse files
authored
Merge pull request #168 from h-michael/gitter-link
Make it possible to generate formatted code
2 parents 7ba3553 + c56a805 commit ea069fb

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

graphql_client_cli/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ structopt = "0.2"
1818
serde = "1.0"
1919
serde_derive = "1.0"
2020
serde_json = "1.0"
21+
22+
rustfmt-nightly = { version = "0.99" , optional = true }
23+
24+
[features]
25+
default = []
26+
rustfmt = ["rustfmt-nightly"]

graphql_client_cli/src/generate.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub fn generate_code(
1010
selected_operation: String,
1111
additional_derives: Option<String>,
1212
deprecation_strategy: Option<String>,
13+
no_formatting: bool,
1314
output: PathBuf,
1415
) -> Result<(), failure::Error> {
1516
let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str());
@@ -25,8 +26,43 @@ pub fn generate_code(
2526
additional_derives,
2627
deprecation_strategy,
2728
};
29+
2830
let gen = generate_module_token_stream(query_path, schema_path, Some(options))?;
29-
let mut file = File::create(output)?;
30-
write!(file, "{}", gen.to_string());
31+
32+
let mut file = File::create(output.clone())?;
33+
34+
let codes = gen.to_string();
35+
36+
if cfg!(feature = "rustfmt") && !no_formatting {
37+
let codes = format(&codes);
38+
write!(file, "{}", codes);
39+
} else {
40+
write!(file, "{}", codes);
41+
}
42+
3143
Ok(())
3244
}
45+
46+
#[allow(unused_variables)]
47+
fn format(codes: &str) -> String {
48+
#[cfg(feature = "rustfmt")]
49+
{
50+
use rustfmt::{Config, Input, Session};
51+
use std::default::Default;
52+
53+
let mut config = Config::default();
54+
55+
config.set().emit_mode(rustfmt_nightly::EmitMode::Stdout);
56+
config.set().verbose(rustfmt_nightly::Verbosity::Quiet);
57+
58+
let mut out = Vec::with_capacity(codes.len() * 2);
59+
60+
Session::new(config, Some(&mut out))
61+
.format(Input::Text(codes.to_string()))
62+
.unwrap_or_else(|err| panic!("rustfmt error: {}", err));
63+
64+
return String::from_utf8(out).unwrap();
65+
}
66+
#[cfg(not(feature = "rustfmt"))]
67+
unreachable!()
68+
}

graphql_client_cli/src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extern crate failure;
22
extern crate reqwest;
3+
34
#[macro_use]
45
extern crate structopt;
56
#[macro_use]
@@ -10,6 +11,9 @@ extern crate serde_derive;
1011
extern crate serde;
1112
extern crate serde_json;
1213

14+
#[cfg(feature = "rustfmt")]
15+
extern crate rustfmt_nightly as rustfmt;
16+
1317
mod generate;
1418
mod introspect_schema;
1519
use std::path::PathBuf;
@@ -46,8 +50,13 @@ enum Cli {
4650
additional_derives: Option<String>,
4751
/// You can choose deprecation strategy from allow, deny, or warn.
4852
/// Default value is warn.
49-
#[structopt(short = "d", long = "deprecation-strategy",)]
53+
#[structopt(short = "d", long = "deprecation-strategy")]
5054
deprecation_strategy: Option<String>,
55+
/// If you don't want to execute rustfmt to generated code, set this option.
56+
/// Default value is false.
57+
/// Formating feature is disabled as default installation.
58+
#[structopt(long = "no-formatting")]
59+
no_formatting: bool,
5160
#[structopt(parse(from_os_str))]
5261
output: PathBuf,
5362
},
@@ -67,13 +76,15 @@ fn main() -> Result<(), failure::Error> {
6776
selected_operation,
6877
additional_derives,
6978
deprecation_strategy,
79+
no_formatting,
7080
output,
7181
} => generate::generate_code(
7282
query_path,
7383
schema_path,
7484
selected_operation,
7585
additional_derives,
7686
deprecation_strategy,
87+
no_formatting,
7788
output,
7889
),
7990
}

0 commit comments

Comments
 (0)