From cc1d9a195f7ad3df0c52f02db2a2bc528d202b9f Mon Sep 17 00:00:00 2001 From: Firdavs Date: Tue, 17 Mar 2026 10:13:52 +0300 Subject: [PATCH] feat(cli): embed release version via build.rs instead of CI string replacement Closes #976 --- .github/workflows/release.yml | 8 +----- crates/vite_global_cli/build.rs | 26 +++++++++++++++++++ .../src/commands/upgrade/mod.rs | 4 +-- .../vite_global_cli/src/commands/version.rs | 2 +- 4 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 crates/vite_global_cli/build.rs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 13544a624a..da0e0d0611 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -53,6 +53,7 @@ jobs: contents: read env: VERSION: ${{ needs.prepare.outputs.version }} + VITE_PLUS_VERSION: ${{ needs.prepare.outputs.version }} strategy: fail-fast: false matrix: @@ -87,17 +88,10 @@ jobs: shell: bash run: | pnpm exec tool replace-file-content packages/cli/binding/Cargo.toml 'version = "0.0.0"' 'version = "${{ env.VERSION }}"' - pnpm exec tool replace-file-content crates/vite_global_cli/Cargo.toml 'version = "0.0.0"' 'version = "${{ env.VERSION }}"' - cat crates/vite_global_cli/Cargo.toml - name: Verify version replacement shell: bash run: | - if grep -q 'version = "0.0.0"' crates/vite_global_cli/Cargo.toml; then - echo "ERROR: Version replacement failed for crates/vite_global_cli/Cargo.toml" - head -5 crates/vite_global_cli/Cargo.toml - exit 1 - fi if grep -q 'version = "0.0.0"' packages/cli/binding/Cargo.toml; then echo "ERROR: Version replacement failed for packages/cli/binding/Cargo.toml" head -5 packages/cli/binding/Cargo.toml diff --git a/crates/vite_global_cli/build.rs b/crates/vite_global_cli/build.rs new file mode 100644 index 0000000000..16b087beb4 --- /dev/null +++ b/crates/vite_global_cli/build.rs @@ -0,0 +1,26 @@ +fn main() { + println!("cargo:rerun-if-env-changed=VITE_PLUS_VERSION"); + + let version = std::env::var("VITE_PLUS_VERSION") + .ok() + .filter(|v| !v.is_empty()) + .or_else(version_from_git) + .unwrap_or_else(|| std::env::var("CARGO_PKG_VERSION").unwrap()); + + println!("cargo:rustc-env=VITE_PLUS_VERSION={version}"); +} + +fn version_from_git() -> Option { + let output = std::process::Command::new("git") + .args(["describe", "--tags", "--match", "v*", "--abbrev=0"]) + .output() + .ok()?; + + if !output.status.success() { + return None; + } + + let tag = String::from_utf8(output.stdout).ok()?; + let tag = tag.trim(); + tag.strip_prefix('v').map(|s| s.to_owned()) +} diff --git a/crates/vite_global_cli/src/commands/upgrade/mod.rs b/crates/vite_global_cli/src/commands/upgrade/mod.rs index 09b0247776..804bacd111 100644 --- a/crates/vite_global_cli/src/commands/upgrade/mod.rs +++ b/crates/vite_global_cli/src/commands/upgrade/mod.rs @@ -64,7 +64,7 @@ pub async fn execute(options: UpgradeOptions) -> Result { registry::resolve_version(version_or_tag, &platform_suffix, options.registry.as_deref()) .await?; - let current_version = env!("CARGO_PKG_VERSION"); + let current_version = env!("VITE_PLUS_VERSION"); if !options.silent { output::info(&format!( @@ -226,7 +226,7 @@ async fn execute_rollback( } if !silent { - let current_version = env!("CARGO_PKG_VERSION"); + let current_version = env!("VITE_PLUS_VERSION"); output::info("rolling back to previous version..."); output::info(&format!("switching from {} {} {}", current_version, output::ARROW, previous)); } diff --git a/crates/vite_global_cli/src/commands/version.rs b/crates/vite_global_cli/src/commands/version.rs index 91c06a0bd1..3382443af3 100644 --- a/crates/vite_global_cli/src/commands/version.rs +++ b/crates/vite_global_cli/src/commands/version.rs @@ -148,7 +148,7 @@ pub async fn execute(cwd: AbsolutePathBuf) -> Result { println!("{}", vite_shared::header::vite_plus_header()); println!(); - println!("vp v{}", env!("CARGO_PKG_VERSION")); + println!("vp v{}", env!("VITE_PLUS_VERSION")); println!(); // Local vite-plus and tools