From ada5ff7e7565a64e37f8a586eb958b4b0b930864 Mon Sep 17 00:00:00 2001 From: sup39 Date: Sat, 18 May 2024 15:47:21 +0800 Subject: [PATCH] refactor (backend-rs): read version from package.json at compile time --- Cargo.lock | 2 + packages/backend-rs/build.rs | 4 ++ packages/backend-rs/index.d.ts | 12 ------ packages/backend-rs/src/config/server.rs | 49 +----------------------- packages/macro-rs/Cargo.toml | 2 + packages/macro-rs/src/lib.rs | 15 ++++++++ 6 files changed, 25 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9491235eb8..618cb616d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1745,6 +1745,8 @@ dependencies = [ "napi", "proc-macro2", "quote", + "serde", + "serde_json", "syn 2.0.64", "thiserror", ] diff --git a/packages/backend-rs/build.rs b/packages/backend-rs/build.rs index 9fc2367889..9e5e97713c 100644 --- a/packages/backend-rs/build.rs +++ b/packages/backend-rs/build.rs @@ -1,5 +1,9 @@ extern crate napi_build; fn main() { + // watch the version in the project root package.json + println!("cargo:rerun-if-changed=../../package.json"); + + // napi napi_build::setup(); } diff --git a/packages/backend-rs/index.d.ts b/packages/backend-rs/index.d.ts index 7352414f20..5fadb517e4 100644 --- a/packages/backend-rs/index.d.ts +++ b/packages/backend-rs/index.d.ts @@ -191,18 +191,6 @@ export interface Config { authUrl: string driveUrl: string userAgent: string - clientEntry: Manifest -} -export interface Manifest { - file: string - name: string - src: string - isEntry: boolean - isDynamicEntry: boolean - imports: Array - dynamicImports: Array - css: Array - assets: Array } export function loadConfig(): Config export interface Acct { diff --git a/packages/backend-rs/src/config/server.rs b/packages/backend-rs/src/config/server.rs index 52ff2e7707..5c77a46363 100644 --- a/packages/backend-rs/src/config/server.rs +++ b/packages/backend-rs/src/config/server.rs @@ -231,34 +231,6 @@ pub struct Config { pub auth_url: String, pub drive_url: 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, - pub dynamic_imports: Vec, - pub css: Vec, - pub assets: Vec, } fn read_config_file() -> ServerConfig { @@ -280,28 +252,12 @@ fn read_config_file() -> ServerConfig { data } -fn read_meta() -> Meta { - 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 -} +const VERSION: &str = macro_rs::read_version_from_package_json!(); #[crate::export] pub fn load_config() -> Config { let server_config = read_config_file(); - let version = read_meta().version; - let manifest = read_manifest(); + let version = VERSION.to_owned(); let url = url::Url::parse(&server_config.url).expect("Config url is invalid"); let hostname = url .host_str() @@ -379,7 +335,6 @@ pub fn load_config() -> Config { redis_key_prefix, scheme, ws_scheme, - client_entry: manifest, } } diff --git a/packages/macro-rs/Cargo.toml b/packages/macro-rs/Cargo.toml index 25fa8e74bf..7ba5964aaf 100644 --- a/packages/macro-rs/Cargo.toml +++ b/packages/macro-rs/Cargo.toml @@ -11,6 +11,8 @@ proc-macro = true convert_case = { workspace = true } proc-macro2 = { workspace = true } quote = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } syn = { workspace = true, features = ["full", "extra-traits"] } [dev-dependencies] diff --git a/packages/macro-rs/src/lib.rs b/packages/macro-rs/src/lib.rs index 3481d0cd87..b70bfdbe2e 100644 --- a/packages/macro-rs/src/lib.rs +++ b/packages/macro-rs/src/lib.rs @@ -2,6 +2,21 @@ use convert_case::{Case, Casing}; use proc_macro2::{TokenStream, TokenTree}; 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. #[proc_macro_attribute] pub fn export(