2020-05-30 18:54:54 -05:00
|
|
|
//! Generates `assists.md` documentation.
|
|
|
|
|
2020-10-19 12:58:32 -05:00
|
|
|
use std::{fmt, path::PathBuf};
|
2020-05-30 18:54:54 -05:00
|
|
|
|
2021-03-08 12:39:09 -06:00
|
|
|
use xshell::write_file;
|
|
|
|
|
2020-05-30 18:54:54 -05:00
|
|
|
use crate::{
|
2021-03-08 12:39:09 -06:00
|
|
|
codegen::{extract_comment_blocks_with_empty_lines, Location, PREAMBLE},
|
2020-05-30 18:54:54 -05:00
|
|
|
project_root, rust_files, Result,
|
|
|
|
};
|
|
|
|
|
2021-03-08 12:25:44 -06:00
|
|
|
pub(crate) fn generate_feature_docs() -> Result<()> {
|
2020-05-30 18:54:54 -05:00
|
|
|
let features = Feature::collect()?;
|
|
|
|
let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
|
2020-08-17 08:49:46 -05:00
|
|
|
let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
|
2020-05-30 18:54:54 -05:00
|
|
|
let dst = project_root().join("docs/user/generated_features.adoc");
|
2021-03-08 12:39:09 -06:00
|
|
|
write_file(&dst, &contents)?;
|
2020-05-30 18:54:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Feature {
|
|
|
|
id: String,
|
2020-05-31 08:02:12 -05:00
|
|
|
location: Location,
|
2020-05-30 18:54:54 -05:00
|
|
|
doc: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Feature {
|
|
|
|
fn collect() -> Result<Vec<Feature>> {
|
|
|
|
let mut res = Vec::new();
|
2021-01-21 07:37:08 -06:00
|
|
|
for path in rust_files() {
|
2020-05-30 18:54:54 -05:00
|
|
|
collect_file(&mut res, path)?;
|
|
|
|
}
|
|
|
|
res.sort_by(|lhs, rhs| lhs.id.cmp(&rhs.id));
|
|
|
|
return Ok(res);
|
|
|
|
|
|
|
|
fn collect_file(acc: &mut Vec<Feature>, path: PathBuf) -> Result<()> {
|
2020-10-19 12:58:32 -05:00
|
|
|
let text = xshell::read_file(&path)?;
|
2020-05-30 18:54:54 -05:00
|
|
|
let comment_blocks = extract_comment_blocks_with_empty_lines("Feature", &text);
|
|
|
|
|
|
|
|
for block in comment_blocks {
|
|
|
|
let id = block.id;
|
2020-10-05 13:22:44 -05:00
|
|
|
if let Err(msg) = is_valid_feature_name(&id) {
|
|
|
|
panic!("invalid feature name: {:?}:\n {}", id, msg)
|
|
|
|
}
|
2020-05-30 18:54:54 -05:00
|
|
|
let doc = block.contents.join("\n");
|
2020-05-31 08:36:20 -05:00
|
|
|
let location = Location::new(path.clone(), block.line);
|
|
|
|
acc.push(Feature { id, location, doc })
|
2020-05-30 18:54:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:22:44 -05:00
|
|
|
fn is_valid_feature_name(feature: &str) -> Result<(), String> {
|
2020-05-31 02:45:41 -05:00
|
|
|
'word: for word in feature.split_whitespace() {
|
2020-05-31 04:29:19 -05:00
|
|
|
for &short in ["to", "and"].iter() {
|
2020-05-31 02:45:41 -05:00
|
|
|
if word == short {
|
|
|
|
continue 'word;
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 04:29:19 -05:00
|
|
|
for &short in ["To", "And"].iter() {
|
2020-05-31 02:45:41 -05:00
|
|
|
if word == short {
|
2020-10-05 13:22:44 -05:00
|
|
|
return Err(format!("Don't capitalize {:?}", word));
|
2020-05-31 02:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !word.starts_with(char::is_uppercase) {
|
2020-10-05 13:22:44 -05:00
|
|
|
return Err(format!("Capitalize {:?}", word));
|
2020-05-31 02:45:41 -05:00
|
|
|
}
|
|
|
|
}
|
2020-10-05 13:22:44 -05:00
|
|
|
Ok(())
|
2020-05-31 02:45:41 -05:00
|
|
|
}
|
|
|
|
|
2020-05-30 18:54:54 -05:00
|
|
|
impl fmt::Display for Feature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-05-31 08:02:12 -05:00
|
|
|
writeln!(f, "=== {}\n**Source:** {}\n{}", self.id, self.location, self.doc)
|
2020-05-30 18:54:54 -05:00
|
|
|
}
|
|
|
|
}
|