Skip to content

Commit

Permalink
Fix error handling for command dotnet csharpier
Browse files Browse the repository at this point in the history
In Rust, command exit status has to be handled manually. Not handling
exit status would cause the generator to generate invalid code when
`dotnet` is installed, but `csharpier` is not installed.

Switch from using `dotnet csharpier` to using `dotnet-csharpier`. The
former produces really verbose output by `dotnet` when `csharpier` is
not installed or dotnet tools directory is not in PATH, while the latter
returns a neat `command not found` error if CSharpier is not yet
installed.

Signed-off-by: Kristupas Antanavicius <kristupas.antanavicius@nordsec.com>
  • Loading branch information
arg0d committed Nov 29, 2023
1 parent 44e8082 commit 0bc97d3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 11 additions & 2 deletions bindgen/src/gen_cs/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::io::Write;
use std::process::{Command, Stdio};

pub fn format(bindings: String) -> Result<String, anyhow::Error> {
let mut csharpier = Command::new("dotnet")
.arg("csharpier")
let mut csharpier = Command::new("dotnet-csharpier")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
Expand All @@ -15,6 +14,16 @@ pub fn format(bindings: String) -> Result<String, anyhow::Error> {
.write_all(bindings.as_bytes())?;

let output = csharpier.wait_with_output()?;
if !output.status.success() {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"command returned non-zero exit status: {:?}",
output.status.code()
),
))?;
}

let formatted = String::from_utf8(output.stdout)?;

Ok(formatted)
Expand Down
8 changes: 6 additions & 2 deletions bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ pub fn main() -> Result<()> {
.out_dir
.expect("--out-dir is required when using --library");
uniffi_bindgen::library_mode::generate_external_bindings(
BindingGenerator { try_format_code: !cli.no_format },
BindingGenerator {
try_format_code: !cli.no_format,
},
&cli.source,
cli.crate_name,
cli.config.as_deref(),
Expand All @@ -139,7 +141,9 @@ pub fn main() -> Result<()> {
.map(|_| ())
} else {
uniffi_bindgen::generate_external_bindings(
BindingGenerator { try_format_code: !cli.no_format },
BindingGenerator {
try_format_code: !cli.no_format,
},
&cli.source,
cli.config.as_deref(),
cli.out_dir.as_deref(),
Expand Down

0 comments on commit 0bc97d3

Please sign in to comment.