Skip to content

Commit cc1d9a1

Browse files
committed
feat(cli): embed release version via build.rs instead of CI string replacement
Closes #976
1 parent e3607ec commit cc1d9a1

4 files changed

Lines changed: 30 additions & 10 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ jobs:
5353
contents: read
5454
env:
5555
VERSION: ${{ needs.prepare.outputs.version }}
56+
VITE_PLUS_VERSION: ${{ needs.prepare.outputs.version }}
5657
strategy:
5758
fail-fast: false
5859
matrix:
@@ -87,17 +88,10 @@ jobs:
8788
shell: bash
8889
run: |
8990
pnpm exec tool replace-file-content packages/cli/binding/Cargo.toml 'version = "0.0.0"' 'version = "${{ env.VERSION }}"'
90-
pnpm exec tool replace-file-content crates/vite_global_cli/Cargo.toml 'version = "0.0.0"' 'version = "${{ env.VERSION }}"'
91-
cat crates/vite_global_cli/Cargo.toml
9291
9392
- name: Verify version replacement
9493
shell: bash
9594
run: |
96-
if grep -q 'version = "0.0.0"' crates/vite_global_cli/Cargo.toml; then
97-
echo "ERROR: Version replacement failed for crates/vite_global_cli/Cargo.toml"
98-
head -5 crates/vite_global_cli/Cargo.toml
99-
exit 1
100-
fi
10195
if grep -q 'version = "0.0.0"' packages/cli/binding/Cargo.toml; then
10296
echo "ERROR: Version replacement failed for packages/cli/binding/Cargo.toml"
10397
head -5 packages/cli/binding/Cargo.toml

crates/vite_global_cli/build.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
fn main() {
2+
println!("cargo:rerun-if-env-changed=VITE_PLUS_VERSION");
3+
4+
let version = std::env::var("VITE_PLUS_VERSION")
5+
.ok()
6+
.filter(|v| !v.is_empty())
7+
.or_else(version_from_git)
8+
.unwrap_or_else(|| std::env::var("CARGO_PKG_VERSION").unwrap());
9+
10+
println!("cargo:rustc-env=VITE_PLUS_VERSION={version}");
11+
}
12+
13+
fn version_from_git() -> Option<String> {
14+
let output = std::process::Command::new("git")
15+
.args(["describe", "--tags", "--match", "v*", "--abbrev=0"])
16+
.output()
17+
.ok()?;
18+
19+
if !output.status.success() {
20+
return None;
21+
}
22+
23+
let tag = String::from_utf8(output.stdout).ok()?;
24+
let tag = tag.trim();
25+
tag.strip_prefix('v').map(|s| s.to_owned())
26+
}

crates/vite_global_cli/src/commands/upgrade/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub async fn execute(options: UpgradeOptions) -> Result<ExitStatus, Error> {
6464
registry::resolve_version(version_or_tag, &platform_suffix, options.registry.as_deref())
6565
.await?;
6666

67-
let current_version = env!("CARGO_PKG_VERSION");
67+
let current_version = env!("VITE_PLUS_VERSION");
6868

6969
if !options.silent {
7070
output::info(&format!(
@@ -226,7 +226,7 @@ async fn execute_rollback(
226226
}
227227

228228
if !silent {
229-
let current_version = env!("CARGO_PKG_VERSION");
229+
let current_version = env!("VITE_PLUS_VERSION");
230230
output::info("rolling back to previous version...");
231231
output::info(&format!("switching from {} {} {}", current_version, output::ARROW, previous));
232232
}

crates/vite_global_cli/src/commands/version.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub async fn execute(cwd: AbsolutePathBuf) -> Result<ExitStatus, Error> {
148148
println!("{}", vite_shared::header::vite_plus_header());
149149
println!();
150150

151-
println!("vp v{}", env!("CARGO_PKG_VERSION"));
151+
println!("vp v{}", env!("VITE_PLUS_VERSION"));
152152
println!();
153153

154154
// Local vite-plus and tools

0 commit comments

Comments
 (0)