Skip to content

Commit

Permalink
refactor: detect msys2 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored May 22, 2024
1 parent 0d3bae6 commit d0db666
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 36 deletions.
46 changes: 42 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ on:
branches:
- main

defaults:
run:
shell: bash

jobs:
all:
name: All
Expand Down Expand Up @@ -49,4 +45,46 @@ jobs:
run: cargo run -- --help

- name: Test upt list
run: cargo run -- list

msys2:
name: MSYS2

runs-on: windows-latest

env:
RUSTFLAGS: --deny warnings

steps:
- uses: actions/checkout@v4

- name: Install Rust Toolchain Components
uses: dtolnay/rust-toolchain@stable

- uses: Swatinem/rust-cache@v2

- name: Test
run: cargo test --all

- name: Clippy
run: cargo clippy --all --all-targets -- -D warnings

- name: Format
run: cargo fmt --all --check

- uses: msys2/setup-msys2@v2
with:
path-type: inherit

- name: Test upt --help on Windows Bash
if: runner.os == 'Windows'
run: cargo run -- --help
shell: bash

- name: Test upt --help on MSYS2
shell: msys2 {0}
run: cargo run -- --help

- name: Test upt list
shell: msys2 {0}
run: cargo run -- list
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 std::{env, process};
use upt::{detect_os, detect_vendor, init_vendor, run_command, UptError, Vendor};

fn main() {
match run() {
Ok(v) => v,
Ok(c) => {
process::exit(c);
}
Err(e) => {
eprintln!("{}", e);
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(())
}
23 changes: 19 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::process::Command;

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) = Command::new("sh").arg("-c").arg("which pacman").output() {
if output.status.success() {
return Some(os.to_string());
}
}
}
Some("windows".to_string())
}

#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -72,3 +78,12 @@ 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())
}
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 d0db666

Please sign in to comment.