From 1453507dd5ede7a1a696b1fd23aaba891868e05c Mon Sep 17 00:00:00 2001 From: Levi Zim Date: Fri, 17 Jan 2025 20:36:17 +0800 Subject: [PATCH] build: replace independent script with build.rs Replace independent build script with build.rs by building frontend in build.rs. Disable build timestamp and git info emission in debug mode to avoid triggering build.rs too often. Fix #100 --- Cargo.lock | 7 -- Cargo.toml | 2 +- cli/build.rs | 118 +++++++++++++++++++++++++--- cli/src/version.rs | 6 +- tools/build-from-source/Cargo.toml | 12 --- tools/build-from-source/dist.toml | 3 - tools/build-from-source/src/main.rs | 49 ------------ 7 files changed, 111 insertions(+), 86 deletions(-) delete mode 100644 tools/build-from-source/Cargo.toml delete mode 100644 tools/build-from-source/dist.toml delete mode 100644 tools/build-from-source/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 69b77c6..31d4e5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3254,13 +3254,6 @@ dependencies = [ "warp", ] -[[package]] -name = "shiroa-build" -version = "0.2.0" -dependencies = [ - "anyhow", -] - [[package]] name = "shlex" version = "1.3.0" diff --git a/Cargo.toml b/Cargo.toml index 901f150..efe98d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ repository = "https://github.com/Myriad-Dreamin/shiroa" [workspace] resolver = "2" -members = ["cli", "tools/build-from-source"] +members = ["cli"] [profile.release] codegen-units = 1 # Reduce number of codegen units to increase optimizations diff --git a/cli/build.rs b/cli/build.rs index 0f67be6..4ab2ba2 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -1,9 +1,95 @@ use anyhow::Result; -use vergen::{BuildBuilder, CargoBuilder, Emitter, RustcBuilder}; -use vergen_gitcl::GitclBuilder; +use vergen::{ + AddCustomEntries, BuildBuilder, CargoBuilder, CargoRerunIfChanged, CargoWarning, DefaultConfig, + Emitter, RustcBuilder, +}; + +use std::{ + collections::BTreeMap, + path::Path, + process::{Command, Stdio}, +}; + +#[derive(Default)] +struct FrontendEmitter; + +impl AddCustomEntries<&str, &str> for FrontendEmitter { + fn add_calculated_entries( + &self, + _idempotent: bool, + _cargo_rustc_env_map: &mut BTreeMap<&str, &str>, + cargo_rerun_if_changed: &mut CargoRerunIfChanged, + _cargo_warning: &mut CargoWarning, + ) -> Result<()> { + cargo_rerun_if_changed.extend( + ["src", "package.json", "tsconfig.json", "vite.config.mjs"] + .into_iter() + .map(|s| "../frontend/".to_string() + s), + ); + cargo_rerun_if_changed.push("../package.json".to_string()); + Ok(()) + } + + fn add_default_entries( + &self, + _config: &DefaultConfig, + _cargo_rustc_env_map: &mut BTreeMap<&str, &str>, + _cargo_rerun_if_changed: &mut CargoRerunIfChanged, + _cargo_warning: &mut CargoWarning, + ) -> Result<()> { + Ok(()) + } +} + +fn run(mut cmd: Command) -> anyhow::Result<()> { + Ok(cmd + .stderr(Stdio::inherit()) + .stdin(Stdio::inherit()) + .status() + .map(|_| ())?) +} + +const fn yarn_cmd() -> &'static str { + if cfg!(windows) { + "yarn.cmd" + } else { + "yarn" + } +} + +fn build_frontend() -> anyhow::Result<()> { + let m = Path::new(env!("CARGO_MANIFEST_DIR")); + let project = m.parent().unwrap(); + println!("Running yarn install..."); + let mut cmd = Command::new(yarn_cmd()); + cmd.args(["install"]); + cmd.current_dir(project.join("frontend")); + run(cmd)?; + + println!("Running yarn build..."); + let mut cmd = Command::new(yarn_cmd()); + cmd.args(["build"]); + cmd.current_dir(project.join("frontend")); + run(cmd)?; + + // copy to assets\artifacts\book.mjs + let src = project.join("frontend/dist/book.mjs"); + let dst = project.join("assets/artifacts/book.mjs"); + std::fs::copy(src, dst)?; + + // copy typst ts renderer wasm module + let src = + project.join("node_modules/@myriaddreamin/typst-ts-renderer/pkg/typst_ts_renderer_bg.wasm"); + let dst = project.join("assets/artifacts/typst_ts_renderer_bg.wasm"); + std::fs::copy(src, dst)?; + + Ok(()) +} fn main() -> Result<()> { - let build = BuildBuilder::default().build_timestamp(true).build()?; + let build = BuildBuilder::default() + .build_timestamp(!cfg!(debug_assertions)) + .build()?; let cargo = CargoBuilder::all_cargo()?; let rustc = RustcBuilder::default() .commit_hash(true) @@ -12,17 +98,27 @@ fn main() -> Result<()> { .channel(true) .llvm_version(true) .build()?; - let gitcl = GitclBuilder::default() - .sha(false) - .describe(true, true, None) - .build()?; - // Emit the instructions - Emitter::default() + let emitter = &mut Emitter::default(); + emitter .add_instructions(&build)? .add_instructions(&cargo)? .add_instructions(&rustc)? - .add_instructions(&gitcl)? - .emit()?; + .add_custom_instructions(&FrontendEmitter)?; + + #[cfg(not(debug_assertions))] + { + let gitcl = vergen_gitcl::GitclBuilder::default() + .sha(false) + .describe(true, true, None) + .build()?; + emitter.add_instructions(&gitcl)?; + } + + // Emit the instructions + emitter.emit()?; + + // Build frontend + build_frontend()?; Ok(()) } diff --git a/cli/src/version.rs b/cli/src/version.rs index dc63ca3..9ff7f16 100644 --- a/cli/src/version.rs +++ b/cli/src/version.rs @@ -49,11 +49,11 @@ impl VersionInfo { version: VERSION, features: env!("VERGEN_CARGO_FEATURES").split(',').collect::>(), - program_semver: env!("VERGEN_GIT_DESCRIBE"), - program_commit_hash: env!("VERGEN_GIT_SHA"), + program_semver: option_env!("VERGEN_GIT_DESCRIBE").unwrap_or("N/A"), + program_commit_hash: option_env!("VERGEN_GIT_SHA").unwrap_or("N/A"), program_target_triple: env!("VERGEN_CARGO_TARGET_TRIPLE"), program_opt_level: env!("VERGEN_CARGO_OPT_LEVEL"), - program_build_timestamp: env!("VERGEN_BUILD_TIMESTAMP"), + program_build_timestamp: option_env!("VERGEN_BUILD_TIMESTAMP").unwrap_or("N/A"), rustc_semver: env!("VERGEN_RUSTC_SEMVER"), rustc_commit_hash: env!("VERGEN_RUSTC_COMMIT_HASH"), diff --git a/tools/build-from-source/Cargo.toml b/tools/build-from-source/Cargo.toml deleted file mode 100644 index 84017dd..0000000 --- a/tools/build-from-source/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "shiroa-build" -description = "Build shiroa from source." -authors.workspace = true -version.workspace = true -license.workspace = true -edition.workspace = true -homepage.workspace = true -repository.workspace = true - -[dependencies] -anyhow.workspace = true diff --git a/tools/build-from-source/dist.toml b/tools/build-from-source/dist.toml deleted file mode 100644 index 228c130..0000000 --- a/tools/build-from-source/dist.toml +++ /dev/null @@ -1,3 +0,0 @@ - -[dist] -dist = false diff --git a/tools/build-from-source/src/main.rs b/tools/build-from-source/src/main.rs deleted file mode 100644 index 3bccfaa..0000000 --- a/tools/build-from-source/src/main.rs +++ /dev/null @@ -1,49 +0,0 @@ -use std::{ - path::Path, - process::{Command, Stdio}, -}; - -fn run(mut cmd: Command) -> anyhow::Result<()> { - Ok(cmd - .stderr(Stdio::inherit()) - .stdin(Stdio::inherit()) - .status() - .map(|_| ())?) -} - -const fn yarn_cmd() -> &'static str { - if cfg!(windows) { - "yarn.cmd" - } else { - "yarn" - } -} - -fn main() -> anyhow::Result<()> { - let m = Path::new(env!("CARGO_MANIFEST_DIR")); - let project = m.parent().unwrap().parent().unwrap(); - - println!("Running yarn install..."); - let mut cmd = Command::new(yarn_cmd()); - cmd.args(["install"]); - cmd.current_dir(project.join("frontend")); - run(cmd)?; - - println!("Running yarn build..."); - let mut cmd = Command::new(yarn_cmd()); - cmd.args(["build"]); - cmd.current_dir(project.join("frontend")); - run(cmd)?; - - // copy to assets\artifacts\book.mjs - let src = project.join("frontend/dist/book.mjs"); - let dst = project.join("assets/artifacts/book.mjs"); - std::fs::copy(src, dst)?; - - println!("Running cargo build..."); - let mut cmd = Command::new("cargo"); - cmd.args(["build", "--release"]); - run(cmd)?; - - Ok(()) -}