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
use crate::utils::io;
use crate::utils::pckg;
use duckscript::types::command::Commands;
use duckscript::types::command::{Command, CommandResult};
use duckscript::types::instruction::Instruction;
use duckscript::types::runtime::StateValue;
use std::collections::HashMap;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "SDKDocsGen")
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}
fn requires_context(&self) -> bool {
true
}
fn run_with_context(
&self,
arguments: Vec<String>,
_state: &mut HashMap<String, StateValue>,
_variables: &mut HashMap<String, String>,
_output_variable: Option<String>,
_instructions: &Vec<Instruction>,
commands: &mut Commands,
_line: usize,
) -> CommandResult {
if arguments.is_empty() {
CommandResult::Error("Documentation output directory not provided.".to_string())
} else {
let (file, prefix) = if arguments.len() == 1 {
(arguments[0].clone(), "".to_string())
} else {
(arguments[1].clone(), arguments[0].clone())
};
let names = commands.get_all_command_names();
let mut buffer = String::new();
buffer.push_str("# Table of Contents\n");
for name in &names {
if name.starts_with(&prefix) {
match commands.get(name) {
Some(command) => {
if !command.help().is_empty() {
let aliases = command.aliases();
let aliases_line = if !aliases.is_empty() {
let mut aliases_docs_buffer = aliases.join(", ");
aliases_docs_buffer.insert_str(0, " (");
aliases_docs_buffer.push_str(")");
aliases_docs_buffer
} else {
"".to_string()
};
buffer.push_str(&format!(
"* [{}{}](#{})\n",
name,
aliases_line,
name.replace(":", "_"),
));
}
}
None => {
return CommandResult::Error(format!("Command: {} not found", name));
}
};
} else {
println!("Command: {} skipped.", &name);
}
}
buffer.push_str("\n");
for name in &names {
if name.starts_with(&prefix) {
let command = match commands.get(name) {
Some(command) => command,
None => {
return CommandResult::Error(format!("Command: {} not found", name));
}
};
let help = command.help();
if !help.is_empty() {
let aliases = command.aliases();
let aliases_docs = if !aliases.is_empty() {
let mut aliases_docs_buffer = String::from("\n\n#### Aliases:\n");
let mut first = true;
for alias in aliases {
if first {
first = false;
} else {
aliases_docs_buffer.push_str(", ");
}
aliases_docs_buffer.push_str(&alias);
}
aliases_docs_buffer
} else {
"".to_string()
};
buffer.push_str(&format!(
"\n<a name=\"{}\"></a>\n## {}\n{}{}\n",
name.replace(":", "_"),
name,
help,
aliases_docs
));
}
}
}
buffer.push_str(include_str!("footer.md"));
match io::write_text_file(&file, &buffer) {
Ok(_) => CommandResult::Continue(Some(file)),
Err(error) => CommandResult::Error(error.to_string()),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}