cli/cli_commands/
print_steps.rs

1//! # print_steps
2//!
3//! Prints the execution plan in multiple formats.
4//!
5
6#[cfg(test)]
7#[path = "print_steps_test.rs"]
8mod print_steps_test;
9
10use crate::error::CargoMakeError;
11use std::io;
12
13use crate::execution_plan::ExecutionPlanBuilder;
14use crate::types::{Config, CrateInfo, ExecutionPlan};
15use regex::Regex;
16
17#[derive(Debug)]
18enum PrintFormat {
19    /// The default format
20    Default,
21    /// Prints a short description of the task
22    ShortDescription,
23}
24
25impl PartialEq for PrintFormat {
26    fn eq(&self, other: &PrintFormat) -> bool {
27        match self {
28            PrintFormat::Default => match other {
29                PrintFormat::Default => true,
30                _ => false,
31            },
32            PrintFormat::ShortDescription => match other {
33                PrintFormat::ShortDescription => true,
34                _ => false,
35            },
36        }
37    }
38}
39
40fn get_format_type(output_format: &str) -> PrintFormat {
41    if output_format == "short-description" {
42        PrintFormat::ShortDescription
43    } else {
44        PrintFormat::Default
45    }
46}
47
48fn print_short_description(
49    output_buffer: &mut impl io::Write,
50    execution_plan: &ExecutionPlan,
51) -> io::Result<()> {
52    let mut counter = 1;
53    for step in &execution_plan.steps {
54        let task = &step.config;
55        let description = match &task.description {
56            Some(value) => value,
57            None => "no description",
58        };
59        writeln!(
60            output_buffer,
61            "{}. {} - {}",
62            counter, &step.name, &description
63        )?;
64
65        counter = counter + 1;
66    }
67    Ok(())
68}
69
70fn print_default(
71    output_buffer: &mut impl io::Write,
72    execution_plan: &ExecutionPlan,
73) -> io::Result<()> {
74    writeln!(output_buffer, "{:#?}", &execution_plan)
75}
76
77/// Only prints the execution plan
78pub fn print(
79    output_buffer: &mut impl io::Write,
80    config: &Config,
81    task: &str,
82    output_format: &str,
83    disable_workspace: bool,
84    skip_tasks_pattern: &Option<String>,
85    crateinfo: &CrateInfo,
86    skip_init_end_tasks: bool,
87) -> Result<(), CargoMakeError> {
88    let skip_tasks_pattern_regex = match skip_tasks_pattern {
89        Some(ref pattern) => match Regex::new(pattern) {
90            Ok(reg) => Some(reg),
91            Err(_) => {
92                warn!("Invalid skip tasks pattern provided: {}", pattern);
93                None
94            }
95        },
96        None => None,
97    };
98
99    let execution_plan = ExecutionPlanBuilder {
100        crate_info: Some(crateinfo),
101        disable_workspace,
102        skip_tasks_pattern: skip_tasks_pattern_regex.as_ref(),
103        skip_init_end_tasks,
104        ..ExecutionPlanBuilder::new(&config, &task)
105    }
106    .build()?;
107    debug!("Created execution plan: {:#?}", &execution_plan);
108
109    let print_format = get_format_type(&output_format);
110
111    match print_format {
112        PrintFormat::ShortDescription => print_short_description(output_buffer, &execution_plan)?,
113        PrintFormat::Default => print_default(output_buffer, &execution_plan)?,
114    };
115    Ok(())
116}