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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! # list_steps
//!
//! Lists all known tasks in multiple formats.
//! Or can list tasks based on a category
//!

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

use crate::error::CargoMakeError;
use crate::execution_plan;
use crate::io;
use crate::types::{Config, DeprecationInfo};
use std::collections::{BTreeMap, BTreeSet};

pub fn run(
    config: &Config,
    output_format: &str,
    output_file: &Option<String>,
    category: &Option<String>,
    hide_uninteresting: bool,
) -> Result<(), CargoMakeError> {
    let output = create_list(&config, output_format, category, hide_uninteresting)?;

    match output_file {
        Some(file) => {
            io::write_text_file(&file, &output);
            ()
        }
        None => print!("{}", output),
    };
    Ok(())
}

pub(crate) fn create_list(
    config: &Config,
    output_format: &str,
    category_filter: &Option<String>,
    hide_uninteresting: bool,
) -> Result<String, CargoMakeError> {
    // category -> actual_task -> description
    let mut categories: BTreeMap<String, BTreeMap<String, String>> = BTreeMap::new();
    // actual_task -> aliases
    let mut aliases: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();

    // iterate over all tasks to build categories and aliases
    for key in config.tasks.keys() {
        let actual_task_name = execution_plan::get_actual_task_name(&config, &key)?;

        let task = execution_plan::get_normalized_task(&config, &actual_task_name, true)?;

        let is_private = match task.private {
            Some(private) => private,
            None => false,
        };

        let skip_task = if is_private {
            true
        } else if hide_uninteresting {
            key.contains("pre-")
                || key.contains("post-")
                || key == "init"
                || key == "end"
                || key == "empty"
        } else {
            false
        };

        if !skip_task {
            let category = match task.category {
                Some(value) => value,
                None => "No Category".to_string(),
            };

            if category_filter
                .as_ref()
                .map_or(false, |value| value != &category)
            {
                continue;
            }

            if &actual_task_name != key {
                aliases
                    .entry(actual_task_name)
                    .or_default()
                    .insert(key.clone());
                continue;
            }

            let description = match task.description {
                Some(value) => value,
                None => "No Description.".to_string(),
            };

            let deprecated_message = match task.deprecated {
                Some(deprecated) => match deprecated {
                    DeprecationInfo::Boolean(value) => {
                        if value {
                            " (deprecated)".to_string()
                        } else {
                            "".to_string()
                        }
                    }
                    DeprecationInfo::Message(ref message) => {
                        let mut buffer = " (deprecated - ".to_string();
                        buffer.push_str(message);
                        buffer.push_str(")");

                        buffer
                    }
                },
                None => "".to_string(),
            };

            let mut text = String::from(description);
            text.push_str(&deprecated_message);

            categories
                .entry(category)
                .or_default()
                .insert(key.clone(), text);
        }
    }

    // build the task list output string
    let single_page_markdown = output_format == "markdown-single-page";
    let markdown = single_page_markdown
        || output_format == "markdown"
        || output_format == "markdown-sub-section";
    let just_task_name = output_format == "autocomplete";

    let mut buffer = String::new();
    if single_page_markdown {
        buffer.push_str(&format!("# Task List\n\n"));
    }

    let post_key = if markdown { "**" } else { "" };
    for (category, tasks) in &categories {
        if category_filter
            .as_ref()
            .map_or(false, |value| value != category)
        {
            continue;
        }

        if !just_task_name {
            if single_page_markdown {
                buffer.push_str(&format!("## {}\n\n", category));
            } else if markdown {
                buffer.push_str(&format!("#### {}\n\n", category));
            } else {
                buffer.push_str(&format!("{}\n----------\n", category));
            }
        }

        for (key, description) in tasks {
            if markdown {
                buffer.push_str(&format!("* **"));
            }

            let aliases = if let Some(aliases) = aliases.remove(key) {
                if just_task_name {
                    aliases.into_iter().collect::<Vec<String>>().join(" ")
                } else {
                    format!(
                        " [aliases: {}]",
                        aliases.into_iter().collect::<Vec<String>>().join(", ")
                    )
                }
            } else {
                "".to_string()
            };

            if just_task_name {
                buffer.push_str(&format!("{} {} ", &key, aliases));
            } else {
                buffer.push_str(&format!(
                    "{}{} - {}{}\n",
                    &key, &post_key, &description, aliases
                ));
            }
        }

        if !just_task_name {
            buffer.push('\n');
        }
    }

    Ok(buffer)
}