Skip to content

Commit

Permalink
Merge pull request #26 from EmbarkStudios/bwe/format-top-mod
Browse files Browse the repository at this point in the history
Include top module file when formatting
  • Loading branch information
bwestlin authored Oct 24, 2024
2 parents 355e471 + e70fc87 commit b5a233a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
4 changes: 3 additions & 1 deletion proto-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- next-header -->
## [Unreleased] - ReleaseDate
### Fixed
- [PR#26](https://github.com/EmbarkStudios/proto-gen/pull/26) Include top module file when formatting
## [0.2.7] - 2024-08-05
### Added
- [PR#24](https://github.com/EmbarkStudios/proto-gen/pull/24) Added `--btree-map` option to output BTreeMaps instead of HashMaps.
- [PR#25](https://github.com/EmbarkStudios/proto-gen/pull/25) Added `--btree-map` option to output BTreeMaps instead of HashMaps.

## [0.2.6] - 2024-04-25
### Added
Expand Down
33 changes: 31 additions & 2 deletions proto-gen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ pub fn run_generation(
config: prost_build::Config,
gen_opts: &GenOptions,
) -> Result<(), String> {
let top_mod_content = generate_to_tmp(proto_ws, opts, config, gen_opts).map_err(|e| {
let mut top_mod_content = generate_to_tmp(proto_ws, opts, config, gen_opts).map_err(|e| {
format!("Failed to generate protos into temp dir for proto workspace {proto_ws:#?} \n{e}")
})?;
let old = &proto_ws.output_dir;
let new = &proto_ws.tmp_dir;
if gen_opts.format {
recurse_fmt(new)?;
top_mod_content = fmt(&top_mod_content)?;
}
let diff = run_diff(old, new, &top_mod_content)?;
if diff > 0 {
Expand Down Expand Up @@ -518,7 +519,7 @@ fn recurse_fmt(base: impl AsRef<Path>) -> Result<(), String> {
for file in
fs::read_dir(path).map_err(|e| format!("failed to read_dir for path {path:?} \n{e}"))?
{
let entry = file.map_err(|e| format!("Failed to read entry in paht {path:?} \n{e}"))?;
let entry = file.map_err(|e| format!("Failed to read entry in path {path:?} \n{e}"))?;
let metadata = entry
.metadata()
.map_err(|e| format!("Failed to read metadata for entry {entry:?} \n{e}"))?;
Expand All @@ -544,6 +545,34 @@ fn recurse_fmt(base: impl AsRef<Path>) -> Result<(), String> {
Ok(())
}

fn fmt(code: &str) -> Result<String, String> {
use std::io::Write;
use std::process::Stdio;

let mut child = std::process::Command::new("rustfmt")
.arg("--edition")
.arg("2021")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.map_err(|e| format!("Failed to format, failed to launch rustfmt\n{e}"))?;

let child_stdin = child.stdin.as_mut().unwrap();
child_stdin
.write_all(code.as_bytes())
.map_err(|e| format!("Failed to format, failed to write data to rustfmt \n{e}"))?;
// drop(child_stdin);

let formatted_code = String::from_utf8(
child
.wait_with_output()
.map_err(|e| format!("Failed to format, rustfmt failed to run \n{e}"))?
.stdout,
)
.map_err(|e| format!("Failed to read formtted generated code \n{e}"))?;
Ok(formatted_code)
}

/// Rustdoc assumes all comments with 4 or more spaces or three backticks are things it absolutely
/// should try to compile and run, which seems like an insane assumption, we try our best
/// to strip those symbols here.
Expand Down

0 comments on commit b5a233a

Please sign in to comment.