add TS::export_path

This commit is contained in:
Gustavo
2024-03-06 17:53:27 -03:00
parent 91485a3f55
commit 5ecae522de
4 changed files with 29 additions and 49 deletions
+17 -10
View File
@@ -286,9 +286,23 @@ pub trait TS {
/// ```
type WithoutGenerics: TS + ?Sized;
/// The path given to `#[ts(export_to = "...")]`
const EXPORT_TO: Option<&'static str> = None;
const DOCS: Option<&'static str> = None;
/// Returns the path to which the type will be exported, taking
/// `TS_RS_EXPORT_DIR` into account
fn export_path() -> Option<PathBuf> {
let base = std::env::var("TS_RS_EXPORT_DIR")
.ok()
.as_deref()
.map(Path::new)
.unwrap_or_else(|| Path::new("."))
.to_owned();
Some(base.join(Self::EXPORT_TO?))
}
/// Identifier of this type, excluding generic parameters.
fn ident() -> String {
// by default, fall back to `TS::name()`.
@@ -421,17 +435,10 @@ impl Dependency {
/// If `T` is not exportable (meaning `T::EXPORT_TO` is `None`), this function will return
/// `None`
pub fn from_ty<T: TS + 'static + ?Sized>() -> Option<Self> {
let base = std::env::var("TS_RS_EXPORT_DIR")
.ok()
let exported_to = T::export_path()
.as_deref()
.map(Path::new)
.unwrap_or_else(|| Path::new("."))
.to_owned();
let exported_to = base
.join(T::EXPORT_TO.map(ToOwned::to_owned)?)
.to_string_lossy()
.to_string();
.and_then(Path::to_str)
.map(ToOwned::to_owned)?;
Some(Dependency {
type_id: TypeId::of::<T>(),
ts_name: T::ident(),
+8 -22
View File
@@ -1,6 +1,6 @@
#![allow(dead_code)]
use std::{concat, fs, path::PathBuf};
use std::{concat, fs};
use ts_rs::TS;
@@ -96,13 +96,6 @@ struct G {
/* ============================================================================================== */
fn generate_path(path: &str) -> PathBuf {
std::env::current_dir().unwrap()
.join(std::env::var("TS_RS_EXPORT_DIR").unwrap_or_default())
.join("tests-out/docs/")
.join(path)
}
#[test]
fn export_a() {
A::export().unwrap();
@@ -144,14 +137,7 @@ fn export_a() {
)
};
let Ok(actual_content) = fs::read_to_string(generate_path("A.ts")) else {
panic!(
"failed to read path: {}\ncurrent dir: {}\nTS_RS_EXPORT_DIR: {}",
generate_path("A.ts").to_string_lossy(),
std::env::current_dir().unwrap().to_string_lossy(),
std::env::var("TS_RS_EXPORT_DIR").unwrap_or_default()
)
};
let actual_content = fs::read_to_string(A::export_path().unwrap()).unwrap();
assert_eq!(actual_content, expected_content);
}
@@ -197,7 +183,7 @@ fn export_b() {
)
};
let actual_content = fs::read_to_string(generate_path("B.ts")).unwrap();
let actual_content = fs::read_to_string(B::export_path().unwrap()).unwrap();
assert_eq!(actual_content, expected_content);
}
@@ -230,7 +216,7 @@ fn export_c() {
)
};
let actual_content = fs::read_to_string(generate_path("C.ts")).unwrap();
let actual_content = fs::read_to_string(C::export_path().unwrap()).unwrap();
assert_eq!(actual_content, expected_content);
}
@@ -262,7 +248,7 @@ fn export_d() {
"export type D = null;"
)
};
let actual_content = fs::read_to_string(generate_path("D.ts")).unwrap();
let actual_content = fs::read_to_string(D::export_path().unwrap()).unwrap();
assert_eq!(actual_content, expected_content);
}
@@ -295,7 +281,7 @@ fn export_e() {
)
};
let actual_content = fs::read_to_string(generate_path("E.ts")).unwrap();
let actual_content = fs::read_to_string(E::export_path().unwrap()).unwrap();
assert_eq!(actual_content, expected_content);
}
@@ -343,7 +329,7 @@ fn export_f() {
)
};
let actual_content = fs::read_to_string(generate_path("F.ts")).unwrap();
let actual_content = fs::read_to_string(F::export_path().unwrap()).unwrap();
assert_eq!(actual_content, expected_content);
}
@@ -391,7 +377,7 @@ fn export_g() {
)
};
let actual_content = fs::read_to_string(generate_path("G.ts")).unwrap();
let actual_content = fs::read_to_string(G::export_path().unwrap()).unwrap();
assert_eq!(actual_content, expected_content);
}
+2 -12
View File
@@ -36,12 +36,7 @@ fn export_manually() {
)
};
let actual_content = fs::read_to_string(
match std::env::var("TS_RS_EXPORT_DIR") {
Ok(path) => format!("{}/tests-out/export_manually/UserFile.ts", path.trim_end_matches('/')),
Err(_) => "tests-out/export_manually/UserFile.ts".to_owned(),
}
).unwrap();
let actual_content = fs::read_to_string(User::export_path().unwrap()).unwrap();
assert_eq!(actual_content, expected_content);
}
@@ -62,12 +57,7 @@ fn export_manually_dir() {
)
};
let actual_content = fs::read_to_string(
match std::env::var("TS_RS_EXPORT_DIR") {
Ok(path) => format!("{}/tests-out/export_manually/dir/UserDir.ts", path.trim_end_matches('/')),
Err(_) => "tests-out/export_manually/dir/UserDir.ts".to_owned(),
}
).unwrap();
let actual_content = fs::read_to_string(UserDir::export_path().unwrap()).unwrap();
assert_eq!(actual_content, expected_content);
}
+2 -5
View File
@@ -17,9 +17,6 @@ struct Bar {
fn path_bug() {
export_bindings_foo();
let base = std::env::current_dir()
.unwrap()
.join(std::env::var("TS_RS_EXPORT_DIR").unwrap_or("".to_owned()));
assert!(base.join("../ts-rs/tests-out/path_bug/Foo.ts").is_file());
assert!(base.join("tests-out/path_bug/aaa/Bar.ts").is_file());
assert!(Foo::export_path().unwrap().is_file());
assert!(Bar::export_path().unwrap().is_file());
}