refactor (backend-rs): read version from package.json at compile time
This commit is contained in:
parent
7dd3b8ec5a
commit
ada5ff7e75
6 changed files with 25 additions and 59 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1745,6 +1745,8 @@ dependencies = [
|
||||||
"napi",
|
"napi",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"syn 2.0.64",
|
"syn 2.0.64",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
extern crate napi_build;
|
extern crate napi_build;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// watch the version in the project root package.json
|
||||||
|
println!("cargo:rerun-if-changed=../../package.json");
|
||||||
|
|
||||||
|
// napi
|
||||||
napi_build::setup();
|
napi_build::setup();
|
||||||
}
|
}
|
||||||
|
|
12
packages/backend-rs/index.d.ts
vendored
12
packages/backend-rs/index.d.ts
vendored
|
@ -191,18 +191,6 @@ export interface Config {
|
||||||
authUrl: string
|
authUrl: string
|
||||||
driveUrl: string
|
driveUrl: string
|
||||||
userAgent: string
|
userAgent: string
|
||||||
clientEntry: Manifest
|
|
||||||
}
|
|
||||||
export interface Manifest {
|
|
||||||
file: string
|
|
||||||
name: string
|
|
||||||
src: string
|
|
||||||
isEntry: boolean
|
|
||||||
isDynamicEntry: boolean
|
|
||||||
imports: Array<string>
|
|
||||||
dynamicImports: Array<string>
|
|
||||||
css: Array<string>
|
|
||||||
assets: Array<string>
|
|
||||||
}
|
}
|
||||||
export function loadConfig(): Config
|
export function loadConfig(): Config
|
||||||
export interface Acct {
|
export interface Acct {
|
||||||
|
|
|
@ -231,34 +231,6 @@ pub struct Config {
|
||||||
pub auth_url: String,
|
pub auth_url: String,
|
||||||
pub drive_url: String,
|
pub drive_url: String,
|
||||||
pub user_agent: String,
|
pub user_agent: String,
|
||||||
pub client_entry: Manifest,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Deserialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
struct Meta {
|
|
||||||
pub version: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Deserialize)]
|
|
||||||
struct ManifestJson {
|
|
||||||
#[serde(rename = "src/init.ts")]
|
|
||||||
pub init_ts: Manifest,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Deserialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
#[crate::export(object, use_nullable = false)]
|
|
||||||
pub struct Manifest {
|
|
||||||
pub file: String,
|
|
||||||
pub name: String,
|
|
||||||
pub src: String,
|
|
||||||
pub is_entry: bool,
|
|
||||||
pub is_dynamic_entry: bool,
|
|
||||||
pub imports: Vec<String>,
|
|
||||||
pub dynamic_imports: Vec<String>,
|
|
||||||
pub css: Vec<String>,
|
|
||||||
pub assets: Vec<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config_file() -> ServerConfig {
|
fn read_config_file() -> ServerConfig {
|
||||||
|
@ -280,28 +252,12 @@ fn read_config_file() -> ServerConfig {
|
||||||
data
|
data
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_meta() -> Meta {
|
const VERSION: &str = macro_rs::read_version_from_package_json!();
|
||||||
let cwd = env::current_dir().unwrap();
|
|
||||||
let meta_json = fs::File::open(cwd.join("../../built/meta.json"))
|
|
||||||
.expect("Failed to open 'built/meta.json'");
|
|
||||||
serde_json::from_reader(meta_json).expect("Failed to parse built/meta.json")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_manifest() -> Manifest {
|
|
||||||
let cwd = env::current_dir().unwrap();
|
|
||||||
let manifest_json = fs::File::open(cwd.join("../../built/_client_dist_/manifest.json"))
|
|
||||||
.expect("Failed to open 'built/_client_dist_/manifest.json'");
|
|
||||||
let manifest: ManifestJson = serde_json::from_reader(manifest_json)
|
|
||||||
.expect("Failed to parse built/_client_dist_/manifest.json");
|
|
||||||
|
|
||||||
manifest.init_ts
|
|
||||||
}
|
|
||||||
|
|
||||||
#[crate::export]
|
#[crate::export]
|
||||||
pub fn load_config() -> Config {
|
pub fn load_config() -> Config {
|
||||||
let server_config = read_config_file();
|
let server_config = read_config_file();
|
||||||
let version = read_meta().version;
|
let version = VERSION.to_owned();
|
||||||
let manifest = read_manifest();
|
|
||||||
let url = url::Url::parse(&server_config.url).expect("Config url is invalid");
|
let url = url::Url::parse(&server_config.url).expect("Config url is invalid");
|
||||||
let hostname = url
|
let hostname = url
|
||||||
.host_str()
|
.host_str()
|
||||||
|
@ -379,7 +335,6 @@ pub fn load_config() -> Config {
|
||||||
redis_key_prefix,
|
redis_key_prefix,
|
||||||
scheme,
|
scheme,
|
||||||
ws_scheme,
|
ws_scheme,
|
||||||
client_entry: manifest,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,8 @@ proc-macro = true
|
||||||
convert_case = { workspace = true }
|
convert_case = { workspace = true }
|
||||||
proc-macro2 = { workspace = true }
|
proc-macro2 = { workspace = true }
|
||||||
quote = { workspace = true }
|
quote = { workspace = true }
|
||||||
|
serde = { workspace = true, features = ["derive"] }
|
||||||
|
serde_json = { workspace = true }
|
||||||
syn = { workspace = true, features = ["full", "extra-traits"] }
|
syn = { workspace = true, features = ["full", "extra-traits"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -2,6 +2,21 @@ use convert_case::{Case, Casing};
|
||||||
use proc_macro2::{TokenStream, TokenTree};
|
use proc_macro2::{TokenStream, TokenTree};
|
||||||
use quote::{quote, ToTokens};
|
use quote::{quote, ToTokens};
|
||||||
|
|
||||||
|
/// Read the version field in the project root package.json at compile time
|
||||||
|
#[proc_macro]
|
||||||
|
pub fn read_version_from_package_json(_item: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct PackageJson {
|
||||||
|
version: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
let file = std::fs::File::open("package.json").expect("Failed to open package.json");
|
||||||
|
let json: PackageJson = serde_json::from_reader(file).unwrap();
|
||||||
|
let version = &json.version;
|
||||||
|
|
||||||
|
quote! { #version }.into()
|
||||||
|
}
|
||||||
|
|
||||||
/// Export this function, struct, enum, const, etc. to TypeScript.
|
/// Export this function, struct, enum, const, etc. to TypeScript.
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn export(
|
pub fn export(
|
||||||
|
|
Loading…
Reference in a new issue