Skip to content

Commit d6c0fc1

Browse files
authored
feat(build): use prettyplease to format output (#890) (#904)
1 parent 468e422 commit d6c0fc1

File tree

11 files changed

+8
-93
lines changed

11 files changed

+8
-93
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ the generated code.
4545

4646
```bash
4747
$ rustup update
48-
$ rustup component add rustfmt
4948
$ cargo build
5049
```
5150

examples/helloworld-tutorial.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ feature.
2626

2727
```bash
2828
$ rustup update
29-
$ rustup component add rustfmt
3029
```
3130

3231
## Defining the HelloWorld service

examples/routeguide-tutorial.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ Change your current directory to Tonic's repository root:
4747
$ cd tonic
4848
```
4949

50-
Tonic uses `rustfmt` to tidy up the code it generates, so we'll make sure it's installed.
51-
52-
```shell
53-
$ rustup component add rustfmt
54-
```
55-
5650
Run the server
5751
```shell
5852
$ cargo run --bin routeguide-server

tonic-build/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ repository = "https://github.com/hyperium/tonic"
1515
version = "0.6.2"
1616

1717
[dependencies]
18+
prettyplease = {version = "0.1"}
1819
proc-macro2 = "1.0"
1920
prost-build = {version = "0.9", optional = true}
2021
quote = "1.0"
2122
syn = "1.0"
2223

2324
[features]
2425
compression = []
25-
default = ["transport", "rustfmt", "prost"]
26+
default = ["transport", "prost"]
2627
prost = ["prost-build"]
27-
rustfmt = []
2828
transport = []
2929

3030
[package.metadata.docs.rs]

tonic-build/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Compiles proto files via prost and generates service stubs and proto definitione
44

55
## Features
66

7-
- rustfmt: This feature enables the use of rustfmt to format the output code this makes the code readable and the error messages nice. This requires that rustfmt is installed. This is enabled by default.
8-
97
Required dependencies
108

119
```toml
@@ -103,4 +101,4 @@ pub mod google {
103101
}
104102
}
105103
```
106-
See [the example here](https://github.com/hyperium/tonic/tree/master/examples/src/gcp)
104+
See [the example here](https://github.com/hyperium/tonic/tree/master/examples/src/gcp)

tonic-build/src/lib.rs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
//! `tonic-build` compiles `proto` files via `prost` and generates service stubs
22
//! and proto definitiones for use with `tonic`.
33
//!
4-
//! # Features
5-
//!
6-
//! - `rustfmt`: This feature enables the use of `rustfmt` to format the output code
7-
//! this makes the code readable and the error messages nice. This requires that `rustfmt`
8-
//! is installed. This is enabled by default.
9-
//!
104
//! # Required dependencies
115
//!
126
//! ```toml
@@ -85,13 +79,6 @@ mod prost;
8579
#[cfg_attr(docsrs, doc(cfg(feature = "prost")))]
8680
pub use prost::{compile_protos, configure, Builder};
8781

88-
#[cfg(feature = "rustfmt")]
89-
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
90-
use std::io::{self, Write};
91-
#[cfg(feature = "rustfmt")]
92-
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
93-
use std::process::{exit, Command};
94-
9582
/// Service code generation for client
9683
pub mod client;
9784
/// Service code generation for Server
@@ -217,42 +204,6 @@ fn generate_attributes<'a>(
217204
.collect::<Vec<_>>()
218205
}
219206

220-
/// Format files under the out_dir with rustfmt
221-
#[cfg(feature = "rustfmt")]
222-
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
223-
pub fn fmt(out_dir: &str) {
224-
let dir = std::fs::read_dir(out_dir).unwrap();
225-
226-
for entry in dir {
227-
let file = entry.unwrap().file_name().into_string().unwrap();
228-
if !file.ends_with(".rs") {
229-
continue;
230-
}
231-
let result =
232-
Command::new(std::env::var("RUSTFMT").unwrap_or_else(|_| "rustfmt".to_owned()))
233-
.arg("--emit")
234-
.arg("files")
235-
.arg("--edition")
236-
.arg("2018")
237-
.arg(format!("{}/{}", out_dir, file))
238-
.output();
239-
240-
match result {
241-
Err(e) => {
242-
eprintln!("error running rustfmt: {:?}", e);
243-
exit(1)
244-
}
245-
Ok(output) => {
246-
if !output.status.success() {
247-
io::stdout().write_all(&output.stdout).unwrap();
248-
io::stderr().write_all(&output.stderr).unwrap();
249-
exit(output.status.code().unwrap_or(1))
250-
}
251-
}
252-
}
253-
}
254-
}
255-
256207
// Generate a singular line of a doc comment
257208
fn generate_doc_comment<S: AsRef<str>>(comment: S) -> TokenStream {
258209
let mut doc_stream = TokenStream::new();

tonic-build/src/prost.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ pub fn configure() -> Builder {
2222
client_attributes: Attributes::default(),
2323
proto_path: "super".to_string(),
2424
compile_well_known_types: false,
25-
#[cfg(feature = "rustfmt")]
26-
format: true,
2725
emit_package: true,
2826
protoc_args: Vec::new(),
2927
include_file: None,
@@ -184,7 +182,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
184182
#clients
185183
};
186184

187-
let code = format!("{}", client_service);
185+
let ast: syn::File = syn::parse2(client_service).expect("not a valid tokenstream");
186+
let code = prettyplease::unparse(&ast);
188187
buf.push_str(&code);
189188

190189
self.clients = TokenStream::default();
@@ -197,7 +196,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
197196
#servers
198197
};
199198

200-
let code = format!("{}", server_service);
199+
let ast: syn::File = syn::parse2(server_service).expect("not a valid tokenstream");
200+
let code = prettyplease::unparse(&ast);
201201
buf.push_str(&code);
202202

203203
self.servers = TokenStream::default();
@@ -223,8 +223,6 @@ pub struct Builder {
223223
pub(crate) include_file: Option<PathBuf>,
224224

225225
out_dir: Option<PathBuf>,
226-
#[cfg(feature = "rustfmt")]
227-
format: bool,
228226
}
229227

230228
impl Builder {
@@ -247,13 +245,6 @@ impl Builder {
247245
self
248246
}
249247

250-
/// Enable the output to be formated by rustfmt.
251-
#[cfg(feature = "rustfmt")]
252-
pub fn format(mut self, run: bool) -> Self {
253-
self.format = run;
254-
self
255-
}
256-
257248
/// Set the output directory to generate code to.
258249
///
259250
/// Defaults to the `OUT_DIR` environment variable.
@@ -397,9 +388,6 @@ impl Builder {
397388
PathBuf::from(std::env::var("OUT_DIR").unwrap())
398389
};
399390

400-
#[cfg(feature = "rustfmt")]
401-
let format = self.format;
402-
403391
config.out_dir(out_dir.clone());
404392
if let Some(path) = self.file_descriptor_set_path.as_ref() {
405393
config.file_descriptor_set_path(path);
@@ -428,13 +416,6 @@ impl Builder {
428416

429417
config.compile_protos(protos, includes)?;
430418

431-
#[cfg(feature = "rustfmt")]
432-
{
433-
if format {
434-
super::fmt(out_dir.to_str().expect("Expected utf8 out_dir"));
435-
}
436-
}
437-
438419
Ok(())
439420
}
440421
}

tonic-health/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ repository = "https://github.com/hyperium/tonic"
1515
version = "0.5.0"
1616

1717
[features]
18-
default = ["transport", "rustfmt"]
19-
rustfmt = ["tonic-build/rustfmt"]
18+
default = ["transport"]
2019
transport = ["tonic/transport", "tonic-build/transport"]
2120

2221
[dependencies]

tonic-health/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
88
.file_descriptor_set_path(grpc_health_v1_descriptor_set_path)
99
.build_server(true)
1010
.build_client(true)
11-
.format(false)
1211
.compile(&["proto/health.proto"], &["proto/"])?;
1312

1413
Ok(())

tonic-reflection/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ readme = "README.md"
1616
repository = "https://github.com/hyperium/tonic"
1717
version = "0.3.0"
1818

19-
[features]
20-
default = ["rustfmt"]
21-
rustfmt = ["tonic-build/rustfmt"]
22-
2319
[dependencies]
2420
bytes = "1.0"
2521
prost = "0.9"

0 commit comments

Comments
 (0)