From 929cd4798d8841db9666ff4a1093358fe8d2cb64 Mon Sep 17 00:00:00 2001 From: Matthias Vogelgesang Date: Wed, 2 Feb 2022 20:09:25 +0100 Subject: [PATCH] feat(build): use prettyplease to format output (#890) Make it a hard dependency and drop the optional rustfmt feature. Fixes: #890 --- README.md | 1 - examples/helloworld-tutorial.md | 1 - examples/routeguide-tutorial.md | 6 ---- tonic-build/Cargo.toml | 4 +-- tonic-build/README.md | 4 +-- tonic-build/src/lib.rs | 49 --------------------------------- tonic-build/src/prost.rs | 27 +++--------------- tonic-health/Cargo.toml | 3 +- tonic-health/build.rs | 1 - tonic-reflection/Cargo.toml | 4 --- tonic-reflection/build.rs | 1 - 11 files changed, 8 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 6bc9cd678..26f136798 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,6 @@ the generated code. ```bash $ rustup update -$ rustup component add rustfmt $ cargo build ``` diff --git a/examples/helloworld-tutorial.md b/examples/helloworld-tutorial.md index bc3c15b68..8442dad2d 100644 --- a/examples/helloworld-tutorial.md +++ b/examples/helloworld-tutorial.md @@ -26,7 +26,6 @@ feature. ```bash $ rustup update -$ rustup component add rustfmt ``` ## Defining the HelloWorld service diff --git a/examples/routeguide-tutorial.md b/examples/routeguide-tutorial.md index 138fecdff..a6f14db11 100644 --- a/examples/routeguide-tutorial.md +++ b/examples/routeguide-tutorial.md @@ -47,12 +47,6 @@ Change your current directory to Tonic's repository root: $ cd tonic ``` -Tonic uses `rustfmt` to tidy up the code it generates, so we'll make sure it's installed. - -```shell -$ rustup component add rustfmt -``` - Run the server ```shell $ cargo run --bin routeguide-server diff --git a/tonic-build/Cargo.toml b/tonic-build/Cargo.toml index dbb833a42..86602ff37 100644 --- a/tonic-build/Cargo.toml +++ b/tonic-build/Cargo.toml @@ -15,6 +15,7 @@ repository = "https://github.com/hyperium/tonic" version = "0.6.2" [dependencies] +prettyplease = {version = "0.1"} proc-macro2 = "1.0" prost-build = {version = "0.9", optional = true} quote = "1.0" @@ -22,9 +23,8 @@ syn = "1.0" [features] compression = [] -default = ["transport", "rustfmt", "prost"] +default = ["transport", "prost"] prost = ["prost-build"] -rustfmt = [] transport = [] [package.metadata.docs.rs] diff --git a/tonic-build/README.md b/tonic-build/README.md index 1a96e69b5..a513d1840 100644 --- a/tonic-build/README.md +++ b/tonic-build/README.md @@ -4,8 +4,6 @@ Compiles proto files via prost and generates service stubs and proto definitione ## Features -- 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. - Required dependencies ```toml @@ -103,4 +101,4 @@ pub mod google { } } ``` -See [the example here](https://github.com/hyperium/tonic/tree/master/examples/src/gcp) \ No newline at end of file +See [the example here](https://github.com/hyperium/tonic/tree/master/examples/src/gcp) diff --git a/tonic-build/src/lib.rs b/tonic-build/src/lib.rs index a0bc9cbc8..385051419 100644 --- a/tonic-build/src/lib.rs +++ b/tonic-build/src/lib.rs @@ -1,12 +1,6 @@ //! `tonic-build` compiles `proto` files via `prost` and generates service stubs //! and proto definitiones for use with `tonic`. //! -//! # Features -//! -//! - `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. -//! //! # Required dependencies //! //! ```toml @@ -85,13 +79,6 @@ mod prost; #[cfg_attr(docsrs, doc(cfg(feature = "prost")))] pub use prost::{compile_protos, configure, Builder}; -#[cfg(feature = "rustfmt")] -#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))] -use std::io::{self, Write}; -#[cfg(feature = "rustfmt")] -#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))] -use std::process::{exit, Command}; - /// Service code generation for client pub mod client; /// Service code generation for Server @@ -217,42 +204,6 @@ fn generate_attributes<'a>( .collect::>() } -/// Format files under the out_dir with rustfmt -#[cfg(feature = "rustfmt")] -#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))] -pub fn fmt(out_dir: &str) { - let dir = std::fs::read_dir(out_dir).unwrap(); - - for entry in dir { - let file = entry.unwrap().file_name().into_string().unwrap(); - if !file.ends_with(".rs") { - continue; - } - let result = - Command::new(std::env::var("RUSTFMT").unwrap_or_else(|_| "rustfmt".to_owned())) - .arg("--emit") - .arg("files") - .arg("--edition") - .arg("2018") - .arg(format!("{}/{}", out_dir, file)) - .output(); - - match result { - Err(e) => { - eprintln!("error running rustfmt: {:?}", e); - exit(1) - } - Ok(output) => { - if !output.status.success() { - io::stdout().write_all(&output.stdout).unwrap(); - io::stderr().write_all(&output.stderr).unwrap(); - exit(output.status.code().unwrap_or(1)) - } - } - } - } -} - // Generate a singular line of a doc comment fn generate_doc_comment>(comment: S) -> TokenStream { let mut doc_stream = TokenStream::new(); diff --git a/tonic-build/src/prost.rs b/tonic-build/src/prost.rs index 3b0f5148d..d5a1b2789 100644 --- a/tonic-build/src/prost.rs +++ b/tonic-build/src/prost.rs @@ -22,8 +22,6 @@ pub fn configure() -> Builder { client_attributes: Attributes::default(), proto_path: "super".to_string(), compile_well_known_types: false, - #[cfg(feature = "rustfmt")] - format: true, emit_package: true, protoc_args: Vec::new(), include_file: None, @@ -184,7 +182,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator { #clients }; - let code = format!("{}", client_service); + let ast: syn::File = syn::parse2(client_service).expect("not a valid tokenstream"); + let code = prettyplease::unparse(&ast); buf.push_str(&code); self.clients = TokenStream::default(); @@ -197,7 +196,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator { #servers }; - let code = format!("{}", server_service); + let ast: syn::File = syn::parse2(server_service).expect("not a valid tokenstream"); + let code = prettyplease::unparse(&ast); buf.push_str(&code); self.servers = TokenStream::default(); @@ -223,8 +223,6 @@ pub struct Builder { pub(crate) include_file: Option, out_dir: Option, - #[cfg(feature = "rustfmt")] - format: bool, } impl Builder { @@ -247,13 +245,6 @@ impl Builder { self } - /// Enable the output to be formated by rustfmt. - #[cfg(feature = "rustfmt")] - pub fn format(mut self, run: bool) -> Self { - self.format = run; - self - } - /// Set the output directory to generate code to. /// /// Defaults to the `OUT_DIR` environment variable. @@ -397,9 +388,6 @@ impl Builder { PathBuf::from(std::env::var("OUT_DIR").unwrap()) }; - #[cfg(feature = "rustfmt")] - let format = self.format; - config.out_dir(out_dir.clone()); if let Some(path) = self.file_descriptor_set_path.as_ref() { config.file_descriptor_set_path(path); @@ -428,13 +416,6 @@ impl Builder { config.compile_protos(protos, includes)?; - #[cfg(feature = "rustfmt")] - { - if format { - super::fmt(out_dir.to_str().expect("Expected utf8 out_dir")); - } - } - Ok(()) } } diff --git a/tonic-health/Cargo.toml b/tonic-health/Cargo.toml index ace438e70..3993f1bc2 100644 --- a/tonic-health/Cargo.toml +++ b/tonic-health/Cargo.toml @@ -15,8 +15,7 @@ repository = "https://github.com/hyperium/tonic" version = "0.5.0" [features] -default = ["transport", "rustfmt"] -rustfmt = ["tonic-build/rustfmt"] +default = ["transport"] transport = ["tonic/transport", "tonic-build/transport"] [dependencies] diff --git a/tonic-health/build.rs b/tonic-health/build.rs index fcbf5e0a4..5b04330ef 100644 --- a/tonic-health/build.rs +++ b/tonic-health/build.rs @@ -8,7 +8,6 @@ fn main() -> Result<(), Box> { .file_descriptor_set_path(grpc_health_v1_descriptor_set_path) .build_server(true) .build_client(true) - .format(false) .compile(&["proto/health.proto"], &["proto/"])?; Ok(()) diff --git a/tonic-reflection/Cargo.toml b/tonic-reflection/Cargo.toml index 885381852..719b4ce8d 100644 --- a/tonic-reflection/Cargo.toml +++ b/tonic-reflection/Cargo.toml @@ -16,10 +16,6 @@ readme = "README.md" repository = "https://github.com/hyperium/tonic" version = "0.3.0" -[features] -default = ["rustfmt"] -rustfmt = ["tonic-build/rustfmt"] - [dependencies] bytes = "1.0" prost = "0.9" diff --git a/tonic-reflection/build.rs b/tonic-reflection/build.rs index 0ad2cef81..3bd2dc137 100644 --- a/tonic-reflection/build.rs +++ b/tonic-reflection/build.rs @@ -13,7 +13,6 @@ fn main() -> Result<(), Box> { ) .build_server(true) .build_client(true) // Client is only used for tests - .format(true) .compile(&["proto/reflection.proto"], &["proto/"])?; Ok(())