Skip to content

Commit

Permalink
refactor: detect msys2
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed May 22, 2024
1 parent 0d3bae6 commit 6398c0d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ mod utils;
mod vendor;

pub use error::UptError;
pub use utils::detect_os;
pub use utils::{detect_os, run_command};
pub use vendor::{detect_vendor, init_vendor, Vendor};
1 change: 1 addition & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ macro_rules! os_vendors {
$(
$os => vec![$($tool),+].into_iter().filter_map(|tool| which_cmd(tool).map(|bin_name| (tool, bin_name))).collect(),
)+
"windows/msys2" => vec![("pacman","pacman")],
_ => ["apt", "dnf", "pacman"].into_iter().map(|tool| (tool, tool)).collect(),
};
match $crate::utils::find_tool(&pairs) {
Expand Down
35 changes: 9 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use std::env;
use std::path::Path;
use std::process::{self, Command};
use upt::{detect_os, detect_vendor, init_vendor, UptError, Vendor};
use upt::{detect_os, detect_vendor, init_vendor, run_command, UptError, Vendor};

fn main() {
match run() {
Ok(v) => v,
Ok(c) => {
std::process::exit(c);
}
Err(e) => {
eprintln!("{}", e);
process::exit(1);
std::process::exit(1);
}
}
}

fn run() -> Result<(), Box<dyn std::error::Error>> {
fn run() -> Result<i32, Box<dyn std::error::Error>> {
let env_args = env::args().collect::<Vec<String>>();
let bin = Path::new(&env_args[0])
.file_stem()
Expand All @@ -28,17 +29,17 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
Ok(v) => v,
Err(UptError::DisplayHelp(t)) => {
println!("{t}");
return Ok(());
return Ok(0);
}
Err(e) => return Err(e.into()),
};
if let Ok(v) = std::env::var("UPT_DRY_RUN") {
if v == "true" || v == "1" {
println!("{}", cmd);
return Ok(());
return Ok(0);
}
}
run_cmd(cmd.as_str(), &os)
run_command(cmd.as_str(), &os)
}

fn create_cmd(vendor: &Vendor, args: &[String], os: &str) -> Result<String, UptError> {
Expand All @@ -50,21 +51,3 @@ fn create_cmd(vendor: &Vendor, args: &[String], os: &str) -> Result<String, UptE
let cmd = tool.eval(&task)?;
Ok(cmd)
}

#[cfg(not(target_os = "windows"))]
fn run_cmd(cmd: &str, _os: &str) -> Result<(), Box<dyn std::error::Error>> {
let mut child = Command::new("sh").arg("-c").arg(cmd).spawn()?;
child.wait()?;
Ok(())
}

#[cfg(target_os = "windows")]
fn run_cmd(cmd: &str, os: &str) -> Result<(), Box<dyn std::error::Error>> {
let mut child = if os == "windows/msys2" {
Command::new("sh").arg("-c").arg(cmd).spawn()?
} else {
Command::new("cmd").args(["/C", cmd]).spawn()?
};
child.wait()?;
Ok(())
}
32 changes: 28 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::process::{Command, Output};

use which::which;

pub fn find_tool(pairs: &[(&str, &str)]) -> Option<String> {
Expand Down Expand Up @@ -38,11 +40,15 @@ pub fn find_tool(pairs: &[(&str, &str)]) -> Option<String> {

#[cfg(target_os = "windows")]
pub fn detect_os() -> Option<String> {
if let Ok(true) = std::env::var("OSTYPE").map(|v| v == "msys") {
Some("windows/msys2".to_string())
} else {
Some("windows".to_string())
if std::env::var("MSYSTEM").is_ok() {
let os = "windows/msys2";
if let Ok(output) = run_command_with_output("which pacman", os) {
if output.status.success() {
return Some(os.to_string());
}
}
}
Some("windows".to_string())
}

#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -72,3 +78,21 @@ pub fn detect_os() -> Option<String> {
let id = id[3..].trim_matches('"');
Some(id.to_string())
}

pub fn run_command(cmd: &str, os: &str) -> Result<i32, Box<dyn std::error::Error>> {
let exit_status = if os == "windows" {
Command::new("cmd").args(["/C", cmd]).status()?
} else {
Command::new("sh").arg("-c").arg(cmd).status()?
};
Ok(exit_status.code().unwrap_or_default())
}

pub fn run_command_with_output(cmd: &str, os: &str) -> Result<Output, Box<dyn std::error::Error>> {
let output = if os == "windows" {
Command::new("cmd").args(["/C", cmd]).output()?
} else {
Command::new("sh").arg("-c").arg(cmd).output()?
};
Ok(output)
}
1 change: 0 additions & 1 deletion src/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ os_vendors!(
"garuda" => "pacman";
"antergos" => "pacman";
"kaos" => "pacman";
"windows/msys2" => "pacman";
// apk
"alpine" => "apk";
"postmarket" => "apk";
Expand Down

0 comments on commit 6398c0d

Please sign in to comment.