|
| 1 | +use crate::binary_installer::ensure_binary_installed; |
| 2 | +use crate::executor::{ToolInstallStatus, ToolStatus}; |
| 3 | +use crate::prelude::*; |
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +pub const SAMPLY_COMMAND: &str = "samply"; |
| 7 | +pub const SAMPLY_VERSION: &str = "0.13.1"; |
| 8 | + |
| 9 | +pub fn get_samply_status() -> ToolStatus { |
| 10 | + let tool_name = SAMPLY_COMMAND.to_string(); |
| 11 | + |
| 12 | + let is_available = Command::new("which") |
| 13 | + .arg(SAMPLY_COMMAND) |
| 14 | + .output() |
| 15 | + .is_ok_and(|output| output.status.success()); |
| 16 | + if !is_available { |
| 17 | + return ToolStatus { |
| 18 | + tool_name, |
| 19 | + status: ToolInstallStatus::NotInstalled, |
| 20 | + }; |
| 21 | + } |
| 22 | + |
| 23 | + let Ok(version_output) = Command::new(SAMPLY_COMMAND).arg("--version").output() else { |
| 24 | + return ToolStatus { |
| 25 | + tool_name, |
| 26 | + status: ToolInstallStatus::NotInstalled, |
| 27 | + }; |
| 28 | + }; |
| 29 | + |
| 30 | + if !version_output.status.success() { |
| 31 | + return ToolStatus { |
| 32 | + tool_name, |
| 33 | + status: ToolInstallStatus::NotInstalled, |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + let version = String::from_utf8_lossy(&version_output.stdout) |
| 38 | + .trim() |
| 39 | + .to_string(); |
| 40 | + |
| 41 | + let expected = semver::Version::parse(SAMPLY_VERSION).unwrap(); |
| 42 | + if let Some(version_str) = version.split_once(' ').map(|(_, v)| v.trim()) { |
| 43 | + if let Ok(installed) = semver::Version::parse(version_str) { |
| 44 | + if installed < expected { |
| 45 | + return ToolStatus { |
| 46 | + tool_name, |
| 47 | + status: ToolInstallStatus::IncorrectVersion { |
| 48 | + version, |
| 49 | + message: format!("version too old, expecting {SAMPLY_VERSION} or higher"), |
| 50 | + }, |
| 51 | + }; |
| 52 | + } |
| 53 | + return ToolStatus { |
| 54 | + tool_name, |
| 55 | + status: ToolInstallStatus::Installed { version }, |
| 56 | + }; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + ToolStatus { |
| 61 | + tool_name, |
| 62 | + status: ToolInstallStatus::IncorrectVersion { |
| 63 | + version, |
| 64 | + message: "could not parse version".to_string(), |
| 65 | + }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +pub async fn install_samply() -> Result<()> { |
| 70 | + let get_samply_installer_url = || { |
| 71 | + format!( |
| 72 | + "https://github.com/mstange/samply/releases/download/samply-v{SAMPLY_VERSION}/samply-installer.sh" |
| 73 | + ) |
| 74 | + }; |
| 75 | + |
| 76 | + ensure_binary_installed(SAMPLY_COMMAND, SAMPLY_VERSION, get_samply_installer_url).await |
| 77 | +} |
0 commit comments