Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use OUT_DIR env variable if defined as prefix path #82

Merged
merged 1 commit into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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