Skip to content

Commit 7a20ea3

Browse files
committed
Replace rustfmt_nightly feature in CLI with direct calls to rustfmt
It is opt-out, but it's a reasonable assumption that it will be there
1 parent 60c5d73 commit 7a20ea3

File tree

10 files changed

+38
-48
lines changed

10 files changed

+38
-48
lines changed

examples/github/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ serde = "^1.0"
1111
reqwest = { version = "^0.11", features = ["json", "blocking"] }
1212
prettytable-rs = "^0.7"
1313
structopt = "^0.3"
14-
dotenv = "^0.13"
1514
envy = "^0.3"
1615
log = "^0.4"
1716
env_logger = "^0.5"

examples/github/examples/github.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ fn parse_repo_name(repo_name: &str) -> Result<(&str, &str), anyhow::Error> {
3737
}
3838

3939
fn main() -> Result<(), anyhow::Error> {
40-
dotenv::dotenv().ok();
4140
env_logger::init();
4241

4342
let config: Env = envy::from_env().context("while reading from environment")?;

examples/hasura/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ edition = "2018"
77
[dev-dependencies]
88
anyhow = "1.0"
99
graphql_client = { path = "../../graphql_client", features = ["reqwest"] }
10-
serde = "1.0"
11-
serde_derive = "1.0"
10+
serde = { version = "1.0", features = ["derive"] }
1211
serde_json = "1.0"
1312
reqwest = { version = "^0.11", features = ["json", "blocking"] }
1413
prettytable-rs = "0.7.0"
15-
dotenv = "0.13.0"
1614
log = "0.4.3"
1715
env_logger = "0.5.10"

examples/hasura/examples/hasura.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ struct UpsertIssue;
1616

1717
fn main() -> Result<(), anyhow::Error> {
1818
use upsert_issue::{IssuesUpdateColumn::*, *};
19-
dotenv::dotenv().ok();
2019
env_logger::init();
2120

2221
let q = UpsertIssue::build_query(Variables {

graphql_client_cli/Cargo.toml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@ path = "src/main.rs"
1313

1414
[dependencies]
1515
anyhow = "1.0"
16-
reqwest = { version = "^0.10", features = ["json", "blocking"] }
17-
graphql_client = { version = "0.9.0", path = "../graphql_client", features = [] }
16+
reqwest = { version = "^0.11", features = ["json", "blocking"] }
17+
graphql_client = { version = "0.9.0", path = "../graphql_client", default-features = false, features = ["graphql_query_derive"] }
1818
graphql_client_codegen = { path = "../graphql_client_codegen/", version = "0.9.0" }
1919
structopt = "0.3"
2020
serde = { version = "^1.0", features = ["derive"] }
2121
serde_json = "^1.0"
22-
syn = "^1.0"
2322
log = "^0.4"
2423
env_logger = "^0.6"
25-
26-
rustfmt-nightly = { version = "1.4.5", optional = true }
24+
syn = "1.0"
2725

2826
[features]
2927
default = []
30-
rustfmt = ["rustfmt-nightly"]

graphql_client_cli/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ USAGE:
4040
FLAGS:
4141
-h, --help Prints help information
4242
--no-formatting If you don't want to execute rustfmt to generated code, set this option. Default value is
43-
false. Formating feature is disabled as default installation.
43+
false.
4444
-V, --version Prints version information
4545
4646
OPTIONS:
@@ -66,5 +66,5 @@ ARGS:
6666
If you want to use formatting feature, you should install like this.
6767

6868
```bash
69-
cargo install graphql_client_cli --features rustfmt --force
69+
cargo install graphql_client_cli
7070
```

graphql_client_cli/src/generate.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use graphql_client_codegen::{
55
use std::fs::File;
66
use std::io::Write as _;
77
use std::path::PathBuf;
8+
use std::process::Stdio;
89
use syn::Token;
910

1011
pub(crate) struct CliCodegenParams {
@@ -75,7 +76,7 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<()> {
7576
let gen = generate_module_token_stream(query_path.clone(), &schema_path, options).unwrap();
7677

7778
let generated_code = gen.to_string();
78-
let generated_code = if cfg!(feature = "rustfmt") && !no_formatting {
79+
let generated_code = if !no_formatting {
7980
format(&generated_code)
8081
} else {
8182
generated_code
@@ -90,31 +91,33 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> Result<()> {
9091
.map(|output_dir| output_dir.join(query_file_name).with_extension("rs"))
9192
.unwrap_or_else(move || query_path.with_extension("rs"));
9293

94+
log::info!("Writing generated query to {:?}", dest_file_path);
95+
9396
let mut file = File::create(dest_file_path)?;
9497
write!(file, "{}", generated_code)?;
9598

9699
Ok(())
97100
}
98101

99-
#[allow(unused_variables)]
100-
fn format(codes: &str) -> String {
101-
#[cfg(feature = "rustfmt")]
102-
{
103-
use rustfmt::{Config, Input, Session};
104-
105-
let mut config = Config::default();
102+
fn format(code: &str) -> String {
103+
let binary = "rustfmt";
106104

107-
config.set().emit_mode(rustfmt_nightly::EmitMode::Stdout);
108-
config.set().verbose(rustfmt_nightly::Verbosity::Quiet);
105+
let mut child = std::process::Command::new(binary)
106+
.stdin(Stdio::piped())
107+
.stdout(Stdio::piped())
108+
.spawn()
109+
.unwrap();
110+
let child_stdin = child.stdin.as_mut().unwrap();
111+
write!(child_stdin, "{}", code).unwrap();
109112

110-
let mut out = Vec::with_capacity(codes.len() * 2);
113+
let output = child.wait_with_output().unwrap();
111114

112-
Session::new(config, Some(&mut out))
113-
.format(Input::Text(codes.to_string()))
114-
.unwrap_or_else(|err| panic!("rustfmt error: {}", err));
115-
116-
return String::from_utf8(out).unwrap();
115+
if !output.status.success() {
116+
panic!(
117+
"rustfmt error\n\n{}",
118+
String::from_utf8_lossy(&output.stderr)
119+
);
117120
}
118-
#[cfg(not(feature = "rustfmt"))]
119-
unreachable!("called format() without the rustfmt feature")
121+
122+
String::from_utf8(output.stdout).unwrap()
120123
}

graphql_client_cli/src/main.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use env_logger::fmt::{Color, Style, StyledValue};
22
use log::Level;
33

4-
#[cfg(feature = "rustfmt")]
5-
extern crate rustfmt_nightly as rustfmt;
6-
74
mod generate;
85
mod introspect_schema;
96
use std::path::PathBuf;
@@ -57,7 +54,6 @@ enum Cli {
5754
deprecation_strategy: Option<String>,
5855
/// If you don't want to execute rustfmt to generated code, set this option.
5956
/// Default value is false.
60-
/// Formating feature is disabled as default installation.
6157
#[structopt(long = "no-formatting")]
6258
no_formatting: bool,
6359
/// You can choose module and target struct visibility from pub and private.

graphql_query_derive/src/attributes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn extract_attr_list(ast: &syn::DeriveInput, attr: &str) -> Result<Vec<Strin
7878
pub fn extract_deprecation_strategy(
7979
ast: &syn::DeriveInput,
8080
) -> Result<DeprecationStrategy, syn::Error> {
81-
extract_attr(&ast, "deprecated")?
81+
extract_attr(ast, "deprecated")?
8282
.to_lowercase()
8383
.as_str()
8484
.parse()
@@ -87,7 +87,7 @@ pub fn extract_deprecation_strategy(
8787

8888
/// Get the deprecation from a struct attribute in the derive case.
8989
pub fn extract_normalization(ast: &syn::DeriveInput) -> Result<Normalization, syn::Error> {
90-
extract_attr(&ast, "normalization")?
90+
extract_attr(ast, "normalization")?
9191
.to_lowercase()
9292
.as_str()
9393
.parse()

graphql_query_derive/src/lib.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ fn graphql_query_derive_inner(
2828
let ast = syn::parse2(input)?;
2929
let (query_path, schema_path) = build_query_and_schema_path(&ast)?;
3030
let options = build_graphql_client_derive_options(&ast, query_path.clone())?;
31-
Ok(
32-
generate_module_token_stream(query_path, &schema_path, options)
33-
.map(Into::into)
34-
.map_err(|err| {
35-
syn::Error::new_spanned(
36-
ast,
37-
format!("Failed to generate GraphQLQuery impl: {}", err),
38-
)
39-
}),
40-
)
31+
32+
generate_module_token_stream(query_path, &schema_path, options)
33+
.map(Into::into)
34+
.map_err(|err| {
35+
syn::Error::new_spanned(
36+
ast,
37+
format!("Failed to generate GraphQLQuery impl: {}", err),
38+
)
39+
})
4140
}
4241

4342
fn build_query_and_schema_path(input: &syn::DeriveInput) -> Result<(PathBuf, PathBuf), syn::Error> {

0 commit comments

Comments
 (0)