fix(cli,napi-derive): backward compatible with older cli with #1531 (#1536)

This commit is contained in:
LongYinan 2023-03-22 17:35:55 +08:00 committed by GitHub
parent 2f4d9d4447
commit 5398b16238
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 5 deletions

View file

@ -513,6 +513,7 @@ export class BuildCommand extends Command {
...additionalEnv, ...additionalEnv,
TYPE_DEF_TMP_PATH: intermediateTypeFile, TYPE_DEF_TMP_PATH: intermediateTypeFile,
WASI_REGISTER_TMP_PATH: intermediateWasiRegisterFile, WASI_REGISTER_TMP_PATH: intermediateWasiRegisterFile,
CARGO_CFG_NAPI_RS_CLI_VERSION: version,
} }
try { try {

View file

@ -15,7 +15,7 @@ independent = true
[features] [features]
noop = [] noop = []
strict = [] strict = []
type-def = ["regex", "once_cell"] type-def = ["regex", "once_cell", "semver"]
[dependencies] [dependencies]
convert_case = "0.6" convert_case = "0.6"
@ -30,3 +30,7 @@ version = "1"
[dependencies.once_cell] [dependencies.once_cell]
optional = true optional = true
version = "1" version = "1"
[dependencies.semver]
optional = true
version = "1"

View file

@ -14,6 +14,8 @@ pub use ast::*;
pub use codegen::*; pub use codegen::*;
pub use error::{BindgenResult, Diagnostic}; pub use error::{BindgenResult, Diagnostic};
#[cfg(feature = "type-def")] #[cfg(feature = "type-def")]
pub use semver;
#[cfg(feature = "type-def")]
pub use typegen::*; pub use typegen::*;
#[derive(Debug)] #[derive(Debug)]

View file

@ -3,11 +3,19 @@ mod r#enum;
mod r#fn; mod r#fn;
pub(crate) mod r#struct; pub(crate) mod r#struct;
use std::{cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap, env};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use syn::Type; 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)] #[derive(Default, Debug)]
pub struct TypeDef { pub struct TypeDef {
pub kind: String, pub kind: String,
@ -103,9 +111,17 @@ impl ToString for TypeDef {
} else { } else {
"".to_owned() "".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!( format!(
r#"{}:{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#, r#"{}{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#,
pkg_name, prefix,
self.kind, self.kind,
self.name, self.name,
escape_json(&self.js_doc), escape_json(&self.js_doc),

View file

@ -68,7 +68,12 @@ pub fn napi(attr: RawStream, input: RawStream) -> RawStream {
// logic on first macro expansion // logic on first macro expansion
#[cfg(feature = "type-def")] #[cfg(feature = "type-def")]
if let Ok(ref type_def_file) = env::var("TYPE_DEF_TMP_PATH") { 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)] #[cfg(debug_assertions)]
{ {
println!("Failed to manipulate type def file: {:?}", _e); println!("Failed to manipulate type def file: {:?}", _e);