Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for the specification of the rustfmt path #571

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion tonic-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,22 @@ pub trait Method {
#[cfg(feature = "rustfmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
pub fn fmt(out_dir: &str) {
// Since fmt is public, preserve the API of fmt by dispatching to a helper function that is
// called within the crate.
format_with("rustfmt", &out_dir);
}

#[cfg(feature = "rustfmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
fn format_with(rustfmt_path: impl AsRef<std::ffi::OsStr>, 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("rustfmt")
let result = Command::new(rustfmt_path.as_ref())
.arg("--emit")
.arg("files")
.arg("--edition")
Expand Down
21 changes: 20 additions & 1 deletion tonic-build/src/prost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub fn configure() -> Builder {
compile_well_known_types: false,
#[cfg(feature = "rustfmt")]
format: true,
#[cfg(feature = "rustfmt")]
rustfmt_path: None,
emit_package: true,
}
}
Expand Down Expand Up @@ -210,6 +212,8 @@ pub struct Builder {
out_dir: Option<PathBuf>,
#[cfg(feature = "rustfmt")]
format: bool,
#[cfg(feature = "rustfmt")]
rustfmt_path: Option<PathBuf>,
}

impl Builder {
Expand Down Expand Up @@ -239,6 +243,13 @@ impl Builder {
self
}

/// The path that should be used to find rustfmt, if format is enabled.
#[cfg(feature = "rustfmt")]
pub fn format_with(mut self, path: impl AsRef<Path>) -> Self {
self.rustfmt_path = Some(path.as_ref().to_path_buf());
self
}

/// Set the output directory to generate code to.
///
/// Defaults to the `OUT_DIR` environment variable.
Expand Down Expand Up @@ -331,6 +342,11 @@ impl Builder {

#[cfg(feature = "rustfmt")]
let format = self.format;
#[cfg(feature = "rustfmt")]
let rustfmt_path = self
.rustfmt_path
.clone()
.unwrap_or_else(|| "rustfmt".into());

config.out_dir(out_dir.clone());
if let Some(path) = self.file_descriptor_set_path.as_ref() {
Expand All @@ -355,7 +371,10 @@ impl Builder {
#[cfg(feature = "rustfmt")]
{
if format {
super::fmt(out_dir.to_str().expect("Expected utf8 out_dir"));
super::format_with(
rustfmt_path,
out_dir.to_str().expect("Expected utf8 out_dir"),
);
}
}

Expand Down