Skip to content

Commit 02c8389

Browse files
committed
refactor(installer): minor cleanup from code review
- Extract VP_BINARY_NAME constant to avoid duplicated cfg!(windows) binary name literals - Remove redundant exists() check in replace_windows_exe — just attempt the rename unconditionally (rename failure is already ignored) - Trim verbose WHAT-comments to concise single-line doc comments
1 parent a9696b8 commit 02c8389

2 files changed

Lines changed: 15 additions & 25 deletions

File tree

crates/vite_installer/src/main.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use vite_install::request::HttpClient;
2222
use vite_path::AbsolutePathBuf;
2323
use vite_setup::{install, integrity, platform, registry};
2424

25+
const VP_BINARY_NAME: &str = if cfg!(windows) { "vp.exe" } else { "vp" };
26+
2527
/// Restrict DLL search to system32 only to prevent DLL hijacking
2628
/// when the installer is run from a Downloads folder.
2729
#[cfg(windows)]
@@ -141,8 +143,7 @@ async fn do_install(
141143
}
142144
install::extract_platform_package(&platform_data, &version_dir).await?;
143145

144-
let binary_name = if cfg!(windows) { "vp.exe" } else { "vp" };
145-
let binary_path = version_dir.join("bin").join(binary_name);
146+
let binary_path = version_dir.join("bin").join(VP_BINARY_NAME);
146147
if !tokio::fs::try_exists(&binary_path).await.unwrap_or(false) {
147148
return Err("Binary not found after extraction. The download may be corrupted.".into());
148149
}
@@ -193,31 +194,26 @@ async fn do_install(
193194
Ok(())
194195
}
195196

196-
/// On Windows, rename a running exe to `.old` then copy the new one in place.
197+
/// Windows locks running `.exe` files — rename the old one out of the way before copying.
197198
#[cfg(windows)]
198199
async fn replace_windows_exe(
199200
src: &vite_path::AbsolutePathBuf,
200201
dst: &vite_path::AbsolutePathBuf,
201202
bin_dir: &vite_path::AbsolutePathBuf,
202203
) -> Result<(), Box<dyn std::error::Error>> {
203-
if dst.as_path().exists() {
204-
let old_name = format!(
205-
"vp.exe.{}.old",
206-
std::time::SystemTime::now()
207-
.duration_since(std::time::UNIX_EPOCH)
208-
.unwrap_or_default()
209-
.as_secs()
210-
);
211-
let _ = tokio::fs::rename(dst, &bin_dir.join(&old_name)).await;
212-
}
204+
let old_name = format!(
205+
"vp.exe.{}.old",
206+
std::time::SystemTime::now()
207+
.duration_since(std::time::UNIX_EPOCH)
208+
.unwrap_or_default()
209+
.as_secs()
210+
);
211+
let _ = tokio::fs::rename(dst, &bin_dir.join(&old_name)).await;
213212
tokio::fs::copy(src, dst).await?;
214213
Ok(())
215214
}
216215

217-
/// Set up the bin/ directory with the initial `vp` shim.
218-
///
219-
/// On Windows, copies `vp-shim.exe` from `current/bin/` to `bin/vp.exe`.
220-
/// On Unix, creates a symlink from `bin/vp` to `../current/bin/vp`.
216+
/// Set up the `bin/vp` entry point (trampoline copy on Windows, symlink on Unix).
221217
async fn setup_bin_shims(
222218
install_dir: &vite_path::AbsolutePath,
223219
) -> Result<(), Box<dyn std::error::Error>> {
@@ -284,10 +280,8 @@ async fn download_with_progress(
284280
Ok(data)
285281
}
286282

287-
/// Create shell env files by spawning `vp env setup --env-only`.
288283
async fn create_env_files(install_dir: &vite_path::AbsolutePath) {
289-
let vp_binary =
290-
install_dir.join("current").join("bin").join(if cfg!(windows) { "vp.exe" } else { "vp" });
284+
let vp_binary = install_dir.join("current").join("bin").join(VP_BINARY_NAME);
291285

292286
if !tokio::fs::try_exists(&vp_binary).await.unwrap_or(false) {
293287
return;

crates/vite_installer/src/windows_path.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ fn broadcast_settings_change() {
4343
}
4444
}
4545

46-
/// Add a directory to the User PATH if not already present.
47-
///
48-
/// Reads `HKCU\Environment\Path`, checks if `bin_dir` is already there
49-
/// (case-insensitive, with/without trailing backslash), and prepends if not.
50-
/// Broadcasts `WM_SETTINGCHANGE` so new terminal sessions see the change.
46+
/// Add a directory to the User PATH (`HKCU\Environment\Path`) if not already present.
5147
pub fn add_to_user_path(bin_dir: &str) -> io::Result<()> {
5248
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
5349
let env = hkcu.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)?;

0 commit comments

Comments
 (0)