Skip to content

Commit

Permalink
use OUT_DIR env variable if defined as prefix path for location of te…
Browse files Browse the repository at this point in the history
…mporary bindings.json file. fixes #72 (#82)
  • Loading branch information
vectronic authored Jul 17, 2022
1 parent 3d9f15c commit 877ad34
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
11 changes: 7 additions & 4 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { ensureDir } from "https://deno.land/[email protected]/fs/ensure_dir.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";
import { codegen } from "./codegen.ts";

const flags = parse(Deno.args, { "--": true });
Expand All @@ -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");
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions deno_bindgen_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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)
Expand All @@ -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::<ItemFn>(input.clone()) {
Expand Down

0 comments on commit 877ad34

Please sign in to comment.