-
Notifications
You must be signed in to change notification settings - Fork 0
Add package manager detection feature to show binary source information #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,6 @@ | ||
| name: Package and Publish | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - master | ||
| - release/* | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,13 +9,23 @@ pub struct Binary<'a> { | |
| pub name: Cow<'a, str>, | ||
| // TODO: use a custom version type | ||
| pub version: Option<SemVersion>, | ||
| pub package_manager: Option<String>, | ||
| } | ||
|
|
||
| impl<'a> Binary<'a> { | ||
| pub fn new(name: Cow<'a, str>) -> Self { | ||
| Self { | ||
| name, | ||
| version: None, | ||
| package_manager: None, | ||
| } | ||
| } | ||
|
|
||
| pub fn new_with_package_manager(name: Cow<'a, str>, package_manager: Option<String>) -> Self { | ||
| Self { | ||
| name, | ||
| version: None, | ||
| package_manager, | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -25,21 +35,26 @@ impl Default for Binary<'_> { | |
| Self { | ||
| name: Cow::borrowed(""), | ||
| version: Some(unknown_version()), | ||
| package_manager: None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Display for Binary<'_> { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| if self.version.is_none() { | ||
| write!(f, "{} ?", self.name) | ||
| if let Some(ref pm) = self.package_manager { | ||
| write!(f, "{} ? ({})", self.name, pm) | ||
| } else { | ||
| write!(f, "{} ?", self.name) | ||
| } | ||
| } else { | ||
| write!( | ||
| f, | ||
| "{} {}", | ||
| self.name, | ||
| format_version(self.version.as_ref().unwrap(), false) | ||
| ) | ||
| let version_str = format_version(self.version.as_ref().unwrap(), false); | ||
| if let Some(ref pm) = self.package_manager { | ||
| write!(f, "{} {} ({})", self.name, version_str, pm) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dont use () but just prefix the name with "via" |
||
| } else { | ||
| write!(f, "{} {}", self.name, version_str) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,97 @@ use crate::binary::Binary; | |
| use crate::error::DiscoveryError; | ||
| use log::{info, warn}; | ||
| use miette::Result; | ||
| use std::path::Path; | ||
|
|
||
| /// Detect which package manager is responsible for managing a binary based on its path | ||
| fn detect_package_manager(binary_path: &Path) -> Option<String> { | ||
| let path_str = binary_path.to_string_lossy(); | ||
| let path_str_lower = path_str.to_lowercase(); | ||
|
|
||
| // Check if it's a cargo binary specifically | ||
| if path_str_lower.contains("/.cargo/bin/") { | ||
| return Some("cargo".to_string()); | ||
| } | ||
| // Check if it's other rustup toolchain binaries | ||
| if path_str_lower.contains("/.rustup/") { | ||
| return Some("rustup".to_string()); | ||
| } | ||
|
|
||
| // Check for Homebrew (macOS/Linux) | ||
| if path_str_lower.starts_with("/opt/homebrew/") | ||
| || path_str_lower.starts_with("/usr/local/cellar/") | ||
| || (path_str_lower.contains("/usr/local/") && path_str_lower.contains("homebrew")) | ||
| { | ||
| return Some("homebrew".to_string()); | ||
| } | ||
|
|
||
| // Check for npm global installs | ||
| if path_str_lower.contains("/node_modules/.bin/") | ||
| || path_str_lower.contains("/npm/") | ||
| || path_str_lower.contains("/.npm/") | ||
| || path_str_lower.contains("/npm-global/") | ||
| { | ||
| return Some("npm".to_string()); | ||
| } | ||
|
|
||
| // Check for Go binaries | ||
| if path_str_lower.contains("/go/bin/") || path_str_lower.contains("/gopath/bin/") { | ||
| return Some("go".to_string()); | ||
| } | ||
|
|
||
| // Check for Python pip/pipx installs | ||
| if path_str_lower.contains("/.local/bin/") | ||
| || path_str_lower.contains("/python") | ||
| || path_str_lower.contains("/pip/") | ||
| || path_str_lower.contains("/pipx/") | ||
| { | ||
| return Some("pip".to_string()); | ||
| } | ||
|
|
||
| // DEV: adding ? to these because i havnt tested them yet | ||
| // Check for snap packages | ||
| if path_str_lower.contains("/snap/") { | ||
| return Some("snap?".to_string()); | ||
| } | ||
|
|
||
| // Check for flatpak | ||
| if path_str_lower.contains("/flatpak/") { | ||
| return Some("flatpak?".to_string()); | ||
| } | ||
|
|
||
| // Check for AppImage | ||
| if path_str_lower.contains("/appimage/") || path_str_lower.ends_with(".appimage") { | ||
| return Some("appimage?".to_string()); | ||
| } | ||
|
|
||
| // check for bun | ||
| if path_str_lower.contains("/bun/") || path_str_lower.contains("/.bun/") { | ||
| return Some("bun".to_string()); | ||
| } | ||
|
|
||
| // check for deno | ||
| if path_str_lower.contains("/deno/") || path_str_lower.contains("/.deno/") { | ||
| return Some("deno".to_string()); | ||
| } | ||
|
|
||
| // check for yarn | ||
| if path_str_lower.contains("/yarn/") || path_str_lower.contains("/.yarn/") { | ||
| return Some("yarn".to_string()); | ||
| } | ||
|
|
||
| // Check for system package managers (dpkg/apt on Debian/Ubuntu, etc.) | ||
| // This should be last as it's the most generic | ||
| // if path_str_lower.starts_with("/usr/bin/") | ||
| // || path_str_lower.starts_with("/bin/") | ||
| // || path_str_lower.starts_with("/usr/local/bin/") | ||
| // || path_str_lower.starts_with("/sbin/") | ||
| // || path_str_lower.starts_with("/usr/sbin/") | ||
| // { | ||
| // return Some("system".to_string()); | ||
| // } | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add more managers, like uv, bun, yarn and pnpm |
||
| None | ||
| } | ||
|
|
||
| pub fn partition_binaries( | ||
| binaries_to_check: Vec<Binary<'_>>, | ||
|
|
@@ -16,9 +107,11 @@ pub fn partition_binaries( | |
| for binary in binaries_to_check { | ||
| let name = binary.name.as_ref(); | ||
| match which::which(name) { | ||
| Ok(_) => { | ||
| Ok(path) => { | ||
| info!(SCOPE = "which", bin = name; "found"); | ||
| available.push(binary); | ||
| let package_manager = detect_package_manager(&path); | ||
| let updated_binary = Binary::new_with_package_manager(binary.name, package_manager); | ||
| available.push(updated_binary); | ||
| } | ||
| Err(err) => { | ||
| info!(SCOPE = "which", bin = name; "not found"); | ||
|
|
@@ -30,7 +123,7 @@ pub fn partition_binaries( | |
| return Err( | ||
| DiscoveryError::BinaryCheck { | ||
| name: name.to_string(), | ||
| source: std::io::Error::new(std::io::ErrorKind::Other, err), | ||
| source: std::io::Error::other(err), | ||
| } | ||
| .into(), | ||
| ); | ||
|
|
@@ -44,9 +137,57 @@ pub fn partition_binaries( | |
| #[cfg(test)] | ||
| mod tests { | ||
| use beef::Cow; | ||
| use std::path::Path; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_detect_package_manager() { | ||
| // Test rustup/cargo detection | ||
| assert_eq!( | ||
| detect_package_manager(Path::new("/home/user/.cargo/bin/cargo")), | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here... |
||
| Some("cargo".to_string()) | ||
| ); | ||
| assert_eq!( | ||
| detect_package_manager(Path::new("/home/user/.rustup/toolchains/stable/bin/rustc")), | ||
| Some("rustup".to_string()) | ||
| ); | ||
|
|
||
| // Test system package detection | ||
| assert_eq!(detect_package_manager(Path::new("/usr/bin/grep")), None); | ||
| assert_eq!(detect_package_manager(Path::new("/bin/ls")), None); | ||
|
|
||
| // Test homebrew detection | ||
| assert_eq!( | ||
| detect_package_manager(Path::new("/opt/homebrew/bin/brew")), | ||
| Some("homebrew".to_string()) | ||
| ); | ||
|
|
||
| // Test npm detection | ||
| assert_eq!( | ||
| detect_package_manager(Path::new("/usr/local/lib/node_modules/.bin/npm")), | ||
| Some("npm".to_string()) | ||
| ); | ||
|
|
||
| // Test go detection | ||
| assert_eq!( | ||
| detect_package_manager(Path::new("/home/user/go/bin/gofmt")), | ||
| Some("go".to_string()) | ||
| ); | ||
|
|
||
| // Test pip detection | ||
| assert_eq!( | ||
| detect_package_manager(Path::new("/home/user/.local/bin/pip")), | ||
| Some("pip".to_string()) | ||
| ); | ||
|
|
||
| // Test unknown path | ||
| assert_eq!( | ||
| detect_package_manager(Path::new("/some/unknown/path/binary")), | ||
| None | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_partition_binaries() { | ||
| let cargo_exists = which::which("cargo").is_ok(); | ||
|
|
@@ -69,6 +210,9 @@ mod tests { | |
| if cargo_exists { | ||
| assert_eq!(available.len(), 1); | ||
| assert_eq!(available[0].name, "cargo"); | ||
| // Check that package manager was detected | ||
| assert!(available[0].package_manager.is_some()); | ||
| assert_eq!(available[0].package_manager.as_ref().unwrap(), "cargo"); | ||
| assert_eq!(not_available.len(), 1); | ||
| assert_eq!( | ||
| not_available[0].name, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont use () but just prefix the name with "via"