From c441a820e928288c4156c806f37e387566681b85 Mon Sep 17 00:00:00 2001 From: vectronic Date: Tue, 12 Jul 2022 15:28:35 +0100 Subject: [PATCH] use OUT_DIR env variable if defined as prefix path for location of temporary bindings.json file. fixes #72 --- cli.ts | 11 +++++++---- deno_bindgen_macro/src/lib.rs | 13 +++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cli.ts b/cli.ts index 53d715d..06e6323 100644 --- a/cli.ts +++ b/cli.ts @@ -2,6 +2,7 @@ import { ensureDir } from "https://deno.land/std@0.132.0/fs/ensure_dir.ts"; import { parse } from "https://deno.land/std@0.132.0/flags/mod.ts"; +import { join } from "https://deno.land/std@0.132.0/path/mod.ts"; import { codegen } from "./codegen.ts"; const flags = parse(Deno.args, { "--": true }); @@ -11,6 +12,8 @@ const fetchPrefix = typeof flags.release == "string" ? flags.release : "../target/" + (release ? "release" : "debug"); +const metafile = join(Deno.env.get("OUT_DIR") || "", "bindings.json"); + async function build() { const cmd = ["cargo", "build"]; if (release) cmd.push("--release"); @@ -23,7 +26,7 @@ let source = null; async function generate() { let conf; try { - conf = JSON.parse(await Deno.readTextFile("bindings.json")); + conf = JSON.parse(await Deno.readTextFile(metafile)); } catch (_) { // Nothing to update. return; @@ -44,16 +47,16 @@ async function generate() { }, ); - await Deno.remove("bindings.json"); + await Deno.remove(metafile); } try { - await Deno.remove("bindings.json"); + await Deno.remove(metafile); } catch (e) { // no op } -const status = await build().catch((_) => Deno.removeSync("bindings.json")); +const status = await build().catch((_) => Deno.removeSync(metafile)); if (status?.success || typeof flags.release == "string") { await generate(); if (source) { diff --git a/deno_bindgen_macro/src/lib.rs b/deno_bindgen_macro/src/lib.rs index 7f84618..8032966 100644 --- a/deno_bindgen_macro/src/lib.rs +++ b/deno_bindgen_macro/src/lib.rs @@ -7,6 +7,7 @@ use std::env; use std::fs::OpenOptions; use std::io::Read; use std::io::Write; +use std::path::Path; use syn::parse_macro_input; use syn::parse_quote; use syn::ItemFn; @@ -22,8 +23,6 @@ use crate::derive_struct::process_struct; use crate::meta::Glue; use crate::meta::Type; -const METAFILE: &str = "bindings.json"; - #[cfg(target_endian = "little")] const ENDIANNESS: bool = true; @@ -32,7 +31,13 @@ const ENDIANNESS: bool = false; #[proc_macro_attribute] pub fn deno_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream { - let mut metadata: Glue = match OpenOptions::new().read(true).open(METAFILE) { + + let metafile_path: String = match env::var("OUT_DIR") { + Ok(out_dir) => Path::new(&out_dir).join("bindings.json").into_os_string().into_string().unwrap(), + Err(_e) => String::from("bindings.json") + }; + + let mut metadata: Glue = match OpenOptions::new().read(true).open(metafile_path.as_str()) { Ok(mut fd) => { let mut meta = String::new(); fd.read_to_string(&mut meta) @@ -50,7 +55,7 @@ pub fn deno_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream { let mut metafile = OpenOptions::new() .write(true) .create(true) - .open(METAFILE) + .open(metafile_path.as_str()) .expect("Error opening meta file"); match syn::parse::(input.clone()) {