|
12 | 12 | use crate::detectors::{detect_all, is_tool_installed, DetectedRunner, Ecosystem}; |
13 | 13 | use crate::error::RunError; |
14 | 14 | use crate::output; |
| 15 | +use crate::validators::CommandSupport; |
15 | 16 | use std::collections::HashMap; |
16 | 17 | use std::path::{Path, PathBuf}; |
17 | 18 | use std::process::{Command, ExitStatus, Stdio}; |
@@ -138,6 +139,55 @@ pub fn check_conflicts( |
138 | 139 | Ok(runners[0].clone()) |
139 | 140 | } |
140 | 141 |
|
| 142 | +pub fn select_runner( |
| 143 | + runners: &[DetectedRunner], |
| 144 | + command: &str, |
| 145 | + working_dir: &Path, |
| 146 | + verbose: bool, |
| 147 | +) -> Result<DetectedRunner, RunError> { |
| 148 | + if runners.is_empty() { |
| 149 | + return Err(RunError::RunnerNotFound(0)); |
| 150 | + } |
| 151 | + |
| 152 | + let mut supported_runners: Vec<&DetectedRunner> = Vec::new(); |
| 153 | + let mut unknown_runners: Vec<&DetectedRunner> = Vec::new(); |
| 154 | + |
| 155 | + for runner in runners { |
| 156 | + match runner.supports_command(command, working_dir) { |
| 157 | + CommandSupport::Supported => { |
| 158 | + if verbose { |
| 159 | + output::info(&format!("{} supports command '{}'", runner.name, command)); |
| 160 | + } |
| 161 | + supported_runners.push(runner); |
| 162 | + } |
| 163 | + CommandSupport::NotSupported => { |
| 164 | + if verbose { |
| 165 | + output::info(&format!( |
| 166 | + "{} does not support command '{}'", |
| 167 | + runner.name, command |
| 168 | + )); |
| 169 | + } |
| 170 | + } |
| 171 | + CommandSupport::Unknown => { |
| 172 | + unknown_runners.push(runner); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if let Some(runner) = supported_runners.first() { |
| 178 | + return Ok((*runner).clone()); |
| 179 | + } |
| 180 | + |
| 181 | + if let Some(runner) = unknown_runners.first() { |
| 182 | + return Ok((*runner).clone()); |
| 183 | + } |
| 184 | + |
| 185 | + Err(RunError::CommandNotSupported( |
| 186 | + command.to_string(), |
| 187 | + runners.iter().map(|r| r.name.clone()).collect(), |
| 188 | + )) |
| 189 | +} |
| 190 | + |
141 | 191 | /// Execute a command with the detected runner |
142 | 192 | pub fn execute( |
143 | 193 | runner: &DetectedRunner, |
|
0 commit comments