Skip to content

Commit

Permalink
feat: Add ability to customize bindings output path.
Browse files Browse the repository at this point in the history
  • Loading branch information
gcoguiec committed Sep 3, 2024
1 parent bcb2087 commit df9abbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,14 +829,17 @@ async fn generate_package_bindings(
None => return Ok(HashMap::new()),
};

// TODO: make the output path configurable
let output_dir = resolution
.metadata
.manifest_path
.parent()
.unwrap()
.join("src");
let bindings_path = output_dir.join("bindings.rs");
let bindings_path_meta = &resolution.metadata.section.bindings.path;
let bindings_path = if bindings_path_meta.is_absolute() {
bindings_path_meta
} else {
&resolution
.metadata
.manifest_path
.parent()
.unwrap()
.join(bindings_path_meta)
};

config.terminal().status(
"Generating",
Expand All @@ -845,20 +848,22 @@ async fn generate_package_bindings(
name = resolution.metadata.name,
path = bindings_path
.strip_prefix(cwd)
.unwrap_or(&bindings_path)
.unwrap_or(bindings_path)
.display()
),
)?;

let bindings = generator.generate()?;
fs::create_dir_all(&output_dir).with_context(|| {
format!(
"failed to create output directory `{path}`",
path = output_dir.display()
)
})?;
if let Some(output_dir) = &bindings_path.parent() {
fs::create_dir_all(output_dir).with_context(|| {
format!(
"failed to create output directory `{path}`",
path = output_dir.display()
)
})?;
}

fs::write(&bindings_path, bindings).with_context(|| {
fs::write(bindings_path, bindings).with_context(|| {
format!(
"failed to write bindings file `{path}`",
path = bindings_path.display()
Expand Down
3 changes: 3 additions & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ impl FromStr for Ownership {
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct Bindings {
/// The path where bindings will be generated (default to `src/bindings.rs`).
pub path: PathBuf,
/// Whether or not to run `rustfmt` on the bindings; defaults to true.
pub format: bool,
/// The ownership model for generated types.
Expand Down Expand Up @@ -106,6 +108,7 @@ pub struct Bindings {
impl Default for Bindings {
fn default() -> Self {
Self {
path: PathBuf::from("src/bindings.rs"),
format: true,
ownership: Default::default(),
derives: Default::default(),
Expand Down

0 comments on commit df9abbd

Please sign in to comment.