|
| 1 | +use crate::command_utils::{capture_command_output, run_command, CommandOutput}; |
| 2 | +use crate::errors::KeeperError; |
| 3 | +use crate::models::Task; |
| 4 | +use crate::task; |
| 5 | +use error_stack::Result; |
| 6 | +use std::io::BufRead; |
| 7 | +use which::which; |
| 8 | + |
| 9 | +pub fn is_available() -> bool { |
| 10 | + std::env::current_dir() |
| 11 | + .map(|dir| dir.join("Gruntfile.js").exists() || dir.join("Gruntfile").exists()) |
| 12 | + .unwrap_or(false) |
| 13 | +} |
| 14 | + |
| 15 | +pub fn is_command_available() -> bool { |
| 16 | + which("grunt").is_ok() |
| 17 | +} |
| 18 | + |
| 19 | +pub fn list_tasks() -> Result<Vec<Task>, KeeperError> { |
| 20 | + let grunt_help_output = capture_command_output("gulp", &["--help"]) |
| 21 | + .map(|output| String::from_utf8(output.stdout).unwrap_or("".to_owned()))?; |
| 22 | + let lines = grunt_help_output.lines().collect::<Vec<&str>>(); |
| 23 | + // extract lines between "Available tasks" and the next empty line |
| 24 | + let start_index = lines |
| 25 | + .iter() |
| 26 | + .position(|&line| line.trim() == "Available tasks") |
| 27 | + .map(|i| i + 1) |
| 28 | + .unwrap_or(0); |
| 29 | + let end_index = lines[start_index..] |
| 30 | + .iter() |
| 31 | + .position(|&line| line.trim().is_empty()) |
| 32 | + .map(|i| start_index + i) |
| 33 | + .unwrap_or(lines.len()); |
| 34 | + let tasks: Vec<Task> = lines[start_index..end_index] |
| 35 | + .iter() |
| 36 | + .map(|line| { |
| 37 | + let parts = line.trim().splitn(2, ' ').collect::<Vec<&str>>(); |
| 38 | + let description = if parts.len() > 1 { parts[1].trim() } else { "" }; |
| 39 | + task!(parts[0], "grunt", description) |
| 40 | + }) |
| 41 | + .collect(); |
| 42 | + Ok(tasks) |
| 43 | +} |
| 44 | + |
| 45 | +pub fn run_task( |
| 46 | + task: &str, |
| 47 | + task_args: &[&str], |
| 48 | + global_args: &[&str], |
| 49 | + verbose: bool, |
| 50 | +) -> Result<CommandOutput, KeeperError> { |
| 51 | + let mut args = vec![]; |
| 52 | + args.extend(global_args); |
| 53 | + args.push(task); |
| 54 | + args.extend(task_args); |
| 55 | + run_command("grunt", &args, verbose) |
| 56 | +} |
0 commit comments