1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! # print_steps
//!
//! Prints the execution plan in multiple formats.
//!

#[cfg(test)]
#[path = "print_steps_test.rs"]
mod print_steps_test;

use crate::error::CargoMakeError;
use std::io;

use crate::execution_plan::ExecutionPlanBuilder;
use crate::types::{Config, CrateInfo, ExecutionPlan};
use regex::Regex;

#[derive(Debug)]
enum PrintFormat {
    /// The default format
    Default,
    /// Prints a short description of the task
    ShortDescription,
}

impl PartialEq for PrintFormat {
    fn eq(&self, other: &PrintFormat) -> bool {
        match self {
            PrintFormat::Default => match other {
                PrintFormat::Default => true,
                _ => false,
            },
            PrintFormat::ShortDescription => match other {
                PrintFormat::ShortDescription => true,
                _ => false,
            },
        }
    }
}

fn get_format_type(output_format: &str) -> PrintFormat {
    if output_format == "short-description" {
        PrintFormat::ShortDescription
    } else {
        PrintFormat::Default
    }
}

fn print_short_description(
    output_buffer: &mut impl io::Write,
    execution_plan: &ExecutionPlan,
) -> io::Result<()> {
    let mut counter = 1;
    for step in &execution_plan.steps {
        let task = &step.config;
        let description = match &task.description {
            Some(value) => value,
            None => "no description",
        };
        writeln!(
            output_buffer,
            "{}. {} - {}",
            counter, &step.name, &description
        )?;

        counter = counter + 1;
    }
    Ok(())
}

fn print_default(
    output_buffer: &mut impl io::Write,
    execution_plan: &ExecutionPlan,
) -> io::Result<()> {
    writeln!(output_buffer, "{:#?}", &execution_plan)
}

/// Only prints the execution plan
pub fn print(
    output_buffer: &mut impl io::Write,
    config: &Config,
    task: &str,
    output_format: &str,
    disable_workspace: bool,
    skip_tasks_pattern: &Option<String>,
    crateinfo: &CrateInfo,
    skip_init_end_tasks: bool,
) -> Result<(), CargoMakeError> {
    let skip_tasks_pattern_regex = match skip_tasks_pattern {
        Some(ref pattern) => match Regex::new(pattern) {
            Ok(reg) => Some(reg),
            Err(_) => {
                warn!("Invalid skip tasks pattern provided: {}", pattern);
                None
            }
        },
        None => None,
    };

    let execution_plan = ExecutionPlanBuilder {
        crate_info: Some(crateinfo),
        disable_workspace,
        skip_tasks_pattern: skip_tasks_pattern_regex.as_ref(),
        skip_init_end_tasks,
        ..ExecutionPlanBuilder::new(&config, &task)
    }
    .build()?;
    debug!("Created execution plan: {:#?}", &execution_plan);

    let print_format = get_format_type(&output_format);

    match print_format {
        PrintFormat::ShortDescription => print_short_description(output_buffer, &execution_plan)?,
        PrintFormat::Default => print_default(output_buffer, &execution_plan)?,
    };
    Ok(())
}