diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-07-11 13:45:44 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-07-11 13:46:52 +0800 |
| commit | d60f7d0963165455811ad60e54112e591bb428c4 (patch) | |
| tree | 2f8cc7c00350c3a1fc1cec37337b95de8bfb494a /.run/src/lib.rs | |
| parent | d58c10f0c14be5bc394aff4cde968b17eb2ae5d9 (diff) | |
feat(release): add workspace release script and refactor run_cmd helpers
Diffstat (limited to '.run/src/lib.rs')
| -rw-r--r-- | .run/src/lib.rs | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/.run/src/lib.rs b/.run/src/lib.rs index 1a956b1..d52c7fd 100644 --- a/.run/src/lib.rs +++ b/.run/src/lib.rs @@ -125,7 +125,7 @@ pub fn wprintln_cargo_style(str: impl Into<String>) { ); } -/// Run a shell command and return its exit status. +/// Run a shell command in the current directory and return its exit status. /// /// # Panics /// @@ -135,6 +135,20 @@ pub fn wprintln_cargo_style(str: impl Into<String>) { /// /// Returns `Err` with the exit code if the command finishes with a non-zero exit code. pub fn run_cmd(cmd: impl Into<String>) -> Result<(), i32> { + let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")); + run_cmd_with_dir(cmd.into(), &cwd) +} + +/// Run a shell command in the specified directory and return its exit status. +/// +/// # Panics +/// +/// Panics if the shell command cannot be spawned (e.g. the shell binary is not found). +/// +/// # Errors +/// +/// Returns `Err` with the exit code if the command finishes with a non-zero exit code. +pub fn run_cmd_with_dir(cmd: impl Into<String>, dir: &std::path::Path) -> Result<(), i32> { let shell = if cfg!(target_os = "windows") { "powershell" } else { @@ -143,7 +157,7 @@ pub fn run_cmd(cmd: impl Into<String>) -> Result<(), i32> { let status = std::process::Command::new(shell) .arg("-c") .arg(cmd.into()) - .current_dir(std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."))) + .current_dir(dir) .status() .expect("failed to execute command"); @@ -160,6 +174,18 @@ pub fn run_cmd(cmd: impl Into<String>) -> Result<(), i32> { /// On success returns `Ok(combined_output)`. On failure returns `Err((exit_code, stderr))`. /// Stderr falls back to stdout if stderr is empty. pub fn run_cmd_capture(cmd: impl Into<String>) -> Result<String, (i32, String)> { + let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")); + run_cmd_capture_with_dir(cmd.into(), &cwd) +} + +/// Run a shell command in the specified directory and capture its combined stdout+stderr output. +/// +/// On success returns `Ok(combined_output)`. On failure returns `Err((exit_code, stderr))`. +/// Stderr falls back to stdout if stderr is empty. +pub fn run_cmd_capture_with_dir( + cmd: impl Into<String>, + dir: &std::path::Path, +) -> Result<String, (i32, String)> { let shell = if cfg!(target_os = "windows") { "powershell" } else { @@ -168,7 +194,7 @@ pub fn run_cmd_capture(cmd: impl Into<String>) -> Result<String, (i32, String)> let output = std::process::Command::new(shell) .arg("-c") .arg(cmd.into()) - .current_dir(std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."))) + .current_dir(dir) .output() .expect("failed to execute command"); |
