Skip to content

Commit

Permalink
Format bindings using CSharpier and prepend auto generated header
Browse files Browse the repository at this point in the history
  • Loading branch information
meenzen authored Nov 28, 2023
1 parent 5a4512f commit 44e8082
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
bin/
gen/
obj/
.idea/
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions bindgen/src/gen_cs/formatting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;

csharpier
.stdin
.take()
.ok_or(anyhow::anyhow!("Failed to open stdin"))?
.write_all(bindings.as_bytes())?;

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

Ok(formatted)
}

pub fn add_header(bindings: String) -> String {
let version = env!("CARGO_PKG_VERSION");
let header = format!(
r##"// <auto-generated>
// This file was generated by uniffi-bindgen-cs v{version}
// See https://github.com/NordSecurity/uniffi-bindgen-cs for more information.
// </auto-generated>
#nullable enable
"##
);

header + &bindings
}
1 change: 1 addition & 0 deletions bindgen/src/gen_cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod custom;
mod enum_;
mod error;
mod external;
pub mod formatting;
mod miscellany;
mod object;
mod primitives;
Expand Down
32 changes: 26 additions & 6 deletions bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

pub mod gen_cs;

use anyhow::{bail, Result};
use camino::{Utf8Path, Utf8PathBuf};
use clap::Parser;
Expand Down Expand Up @@ -40,6 +41,10 @@ struct Cli {

/// Path to the UDL file, or cdylib if `library-mode` is specified
source: Utf8PathBuf,

/// Do not try to format the generated bindings.
#[clap(long, short)]
no_format: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -73,7 +78,9 @@ impl BindingsConfig for ConfigRoot {
}
}

struct BindingGenerator {}
struct BindingGenerator {
try_format_code: bool,
}

impl uniffi_bindgen::BindingGenerator for BindingGenerator {
type Config = ConfigRoot;
Expand All @@ -86,10 +93,23 @@ impl uniffi_bindgen::BindingGenerator for BindingGenerator {
) -> anyhow::Result<()> {
let bindings_file = out_dir.join(format!("{}.cs", ci.namespace()));
let mut f = File::create(&bindings_file)?;
write!(f, "{}", generate_bindings(&config.bindings.csharp, &ci)?)?;

// TODO: find a way to easily format standalone C# files
// https://github.com/dotnet/format
let mut bindings = generate_bindings(&config.bindings.csharp, &ci)?;

if self.try_format_code {
match gen_cs::formatting::format(bindings.clone()) {
Ok(formatted) => bindings = formatted,
Err(e) => {
println!(
"Warning: Unable to auto-format {} using CSharpier (hint: 'dotnet tool install -g csharpier'): {e:?}",
bindings_file.file_name().unwrap(),
);
}
}
}

bindings = gen_cs::formatting::add_header(bindings);
write!(f, "{}", bindings)?;

Ok(())
}
Expand All @@ -110,7 +130,7 @@ pub fn main() -> Result<()> {
.out_dir
.expect("--out-dir is required when using --library");
uniffi_bindgen::library_mode::generate_external_bindings(
BindingGenerator {},
BindingGenerator { try_format_code: !cli.no_format },
&cli.source,
cli.crate_name,
cli.config.as_deref(),
Expand All @@ -119,7 +139,7 @@ pub fn main() -> Result<()> {
.map(|_| ())
} else {
uniffi_bindgen::generate_external_bindings(
BindingGenerator {},
BindingGenerator { try_format_code: !cli.no_format },
&cli.source,
cli.config.as_deref(),
cli.out_dir.as_deref(),
Expand Down

0 comments on commit 44e8082

Please sign in to comment.