mirror of
https://github.com/hanzoai/dev.git
synced 2026-10-25 03:23:33 +00:00
feat(cli/preview): save downloads under ~/.code/bin and suffix with PR id\n\nDefault out dir is now ~/.code/bin when -o is not provided.\nRename extracted binary to <name>-<pr> to avoid dropping artifacts into repo.
This commit is contained in:
@@ -428,8 +428,22 @@ async fn preview_main(args: PreviewArgs) -> anyhow::Result<()> {
|
||||
None
|
||||
}
|
||||
|
||||
let out_dir = args.out_dir.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")));
|
||||
fs::create_dir_all(&out_dir).ok();
|
||||
// Determine output directory
|
||||
// Default: ~/.code/bin
|
||||
let out_dir = if let Some(dir) = args.out_dir {
|
||||
dir
|
||||
} else {
|
||||
let home = if cfg!(windows) {
|
||||
env::var_os("USERPROFILE")
|
||||
} else {
|
||||
env::var_os("HOME")
|
||||
};
|
||||
let base = home
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| PathBuf::from("."));
|
||||
base.join(".code").join("bin")
|
||||
};
|
||||
let _ = fs::create_dir_all(&out_dir);
|
||||
|
||||
#[cfg(target_family = "unix")]
|
||||
fn make_exec(p: &Path) { use std::os::unix::fs::PermissionsExt; let _ = fs::set_permissions(p, fs::Permissions::from_mode(0o755)); }
|
||||
@@ -446,9 +460,13 @@ async fn preview_main(args: PreviewArgs) -> anyhow::Result<()> {
|
||||
ar.unpack(&out_dir)?;
|
||||
// Find extracted binary
|
||||
let bin = first_match(&out_dir, "code-").unwrap_or(out_dir.join("code"));
|
||||
make_exec(&bin);
|
||||
println!("Downloaded preview to {}", bin.display());
|
||||
if !args.extra.is_empty() { let _ = std::process::Command::new(&bin).args(&args.extra).status(); } else { let _ = std::process::Command::new(&bin).status(); }
|
||||
let dest_name = format!("{}-{}", bin.file_name().and_then(|s| s.to_str()).unwrap_or("code"), pr_number);
|
||||
let dest = out_dir.join(dest_name);
|
||||
// Rename/move to include PR number suffix
|
||||
let _ = fs::rename(&bin, &dest).or_else(|_| { fs::copy(&bin, &dest).map(|_| () ) });
|
||||
make_exec(&dest);
|
||||
println!("Downloaded preview to {}", dest.display());
|
||||
if !args.extra.is_empty() { let _ = std::process::Command::new(&dest).args(&args.extra).status(); } else { let _ = std::process::Command::new(&dest).status(); }
|
||||
return Ok(());
|
||||
}
|
||||
} else {
|
||||
@@ -458,8 +476,17 @@ async fn preview_main(args: PreviewArgs) -> anyhow::Result<()> {
|
||||
let mut z = ZipArchive::new(f)?;
|
||||
z.extract(&out_dir)?;
|
||||
let exe = first_match(&out_dir, "code-").unwrap_or(out_dir.join("code.exe"));
|
||||
println!("Downloaded preview to {}", exe.display());
|
||||
if !args.extra.is_empty() { let _ = std::process::Command::new(&exe).args(&args.extra).spawn(); } else { let _ = std::process::Command::new(&exe).spawn(); }
|
||||
// Append PR number before extension if present
|
||||
let dest = match exe.extension().and_then(|e| e.to_str()) {
|
||||
Some(ext) => {
|
||||
let stem = exe.file_stem().and_then(|s| s.to_str()).unwrap_or("code");
|
||||
out_dir.join(format!("{}-{}.{}", stem, pr_number, ext))
|
||||
}
|
||||
None => out_dir.join(format!("{}-{}", exe.file_name().and_then(|s| s.to_str()).unwrap_or("code"), pr_number)),
|
||||
};
|
||||
let _ = fs::rename(&exe, &dest).or_else(|_| { fs::copy(&exe, &dest).map(|_| () ) });
|
||||
println!("Downloaded preview to {}", dest.display());
|
||||
if !args.extra.is_empty() { let _ = std::process::Command::new(&dest).args(&args.extra).spawn(); } else { let _ = std::process::Command::new(&dest).spawn(); }
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
@@ -468,7 +495,16 @@ async fn preview_main(args: PreviewArgs) -> anyhow::Result<()> {
|
||||
if path.file_name().and_then(|s| s.to_str()).map(|n| n.ends_with(".zst")).unwrap_or(false) {
|
||||
// Try to decompress .zst to 'code'
|
||||
if which::which("zstd").is_ok() {
|
||||
let dest = out_dir.join("code");
|
||||
// Derive base name from archive (e.g., code-aarch64-apple-darwin.zst -> code-aarch64-apple-darwin-<pr>.{exe?})
|
||||
let stem = path
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("code");
|
||||
let dest = if cfg!(windows) {
|
||||
out_dir.join(format!("{}-{}.exe", stem, pr_number))
|
||||
} else {
|
||||
out_dir.join(format!("{}-{}", stem, pr_number))
|
||||
};
|
||||
let status = std::process::Command::new("zstd").arg("-d").arg(&path).arg("-o").arg(&dest).status()?;
|
||||
if status.success() {
|
||||
make_exec(&dest);
|
||||
|
||||
Reference in New Issue
Block a user