This commit is contained in:
parent
2f4d9d4447
commit
5398b16238
5 changed files with 33 additions and 5 deletions
|
@ -513,6 +513,7 @@ export class BuildCommand extends Command {
|
|||
...additionalEnv,
|
||||
TYPE_DEF_TMP_PATH: intermediateTypeFile,
|
||||
WASI_REGISTER_TMP_PATH: intermediateWasiRegisterFile,
|
||||
CARGO_CFG_NAPI_RS_CLI_VERSION: version,
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
|
@ -15,7 +15,7 @@ independent = true
|
|||
[features]
|
||||
noop = []
|
||||
strict = []
|
||||
type-def = ["regex", "once_cell"]
|
||||
type-def = ["regex", "once_cell", "semver"]
|
||||
|
||||
[dependencies]
|
||||
convert_case = "0.6"
|
||||
|
@ -30,3 +30,7 @@ version = "1"
|
|||
[dependencies.once_cell]
|
||||
optional = true
|
||||
version = "1"
|
||||
|
||||
[dependencies.semver]
|
||||
optional = true
|
||||
version = "1"
|
||||
|
|
|
@ -14,6 +14,8 @@ pub use ast::*;
|
|||
pub use codegen::*;
|
||||
pub use error::{BindgenResult, Diagnostic};
|
||||
#[cfg(feature = "type-def")]
|
||||
pub use semver;
|
||||
#[cfg(feature = "type-def")]
|
||||
pub use typegen::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
@ -3,11 +3,19 @@ mod r#enum;
|
|||
mod r#fn;
|
||||
pub(crate) mod r#struct;
|
||||
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
use std::{cell::RefCell, collections::HashMap, env};
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use syn::Type;
|
||||
|
||||
pub static NAPI_RS_CLI_VERSION: Lazy<semver::Version> = Lazy::new(|| {
|
||||
let version = env::var("CARGO_CFG_NAPI_RS_CLI_VERSION").unwrap_or_else(|_| "0.0.0".to_string());
|
||||
semver::Version::parse(&version).unwrap_or_else(|_| semver::Version::new(0, 0, 0))
|
||||
});
|
||||
|
||||
pub static NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX: Lazy<semver::Version> =
|
||||
Lazy::new(|| semver::Version::new(2, 15, 1));
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct TypeDef {
|
||||
pub kind: String,
|
||||
|
@ -103,9 +111,17 @@ impl ToString for TypeDef {
|
|||
} else {
|
||||
"".to_owned()
|
||||
};
|
||||
// TODO: remove this in v3
|
||||
// This is a workaround for lower version of @napi-rs/cli
|
||||
// See https://github.com/napi-rs/napi-rs/pull/1531
|
||||
let prefix = if *NAPI_RS_CLI_VERSION >= *NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX {
|
||||
format!("{}:", pkg_name)
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
format!(
|
||||
r#"{}:{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#,
|
||||
pkg_name,
|
||||
r#"{}{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#,
|
||||
prefix,
|
||||
self.kind,
|
||||
self.name,
|
||||
escape_json(&self.js_doc),
|
||||
|
|
|
@ -68,7 +68,12 @@ pub fn napi(attr: RawStream, input: RawStream) -> RawStream {
|
|||
// logic on first macro expansion
|
||||
#[cfg(feature = "type-def")]
|
||||
if let Ok(ref type_def_file) = env::var("TYPE_DEF_TMP_PATH") {
|
||||
if let Err(_e) = remove_existed_type_def(type_def_file) {
|
||||
use napi_derive_backend::{NAPI_RS_CLI_VERSION, NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX};
|
||||
if let Err(_e) = if *NAPI_RS_CLI_VERSION >= *NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX {
|
||||
remove_existed_type_def(type_def_file)
|
||||
} else {
|
||||
fs::remove_file(type_def_file)
|
||||
} {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
println!("Failed to manipulate type def file: {:?}", _e);
|
||||
|
|
Loading…
Reference in a new issue