| 1 |
use clap::ValueEnum; |
| 2 |
use clap_complete::Shell; |
| 3 |
use anyhow::Result; |
| 4 |
|
| 5 |
use sequoia_man::asset_out_dir; |
| 6 |
|
| 7 |
pub mod cli { |
| 8 |
#![allow(unused_macros)] |
| 9 |
include!("src/macros.rs"); |
| 10 |
include!("src/cli/mod.rs"); |
| 11 |
} |
| 12 |
|
| 13 |
fn main() { |
| 14 |
println!("cargo:rerun-if-changed=build.rs"); |
| 15 |
|
| 16 |
// Generate subplot tests. |
| 17 |
#[cfg(feature = "subplot")] |
| 18 |
subplot_build::codegen("sq.subplot") |
| 19 |
.expect("failed to generate code with Subplot"); |
| 20 |
|
| 21 |
let mut sq = cli::build(false); |
| 22 |
generate_shell_completions(&mut sq).unwrap(); |
| 23 |
|
| 24 |
generate_man_pages(&mut sq).expect("can generate man pages"); |
| 25 |
|
| 26 |
lint_help_texts(&sq).unwrap(); |
| 27 |
} |
| 28 |
|
| 29 |
/// Generates shell completions. |
| 30 |
fn generate_shell_completions(sq: &mut clap::Command) -> Result<()> { |
| 31 |
let path = asset_out_dir("shell-completions")?; |
| 32 |
|
| 33 |
for shell in Shell::value_variants() { |
| 34 |
clap_complete::generate_to(*shell, sq, "sq", &path)?; |
| 35 |
}; |
| 36 |
|
| 37 |
println!("cargo:warning=shell completions written to {}", path.display()); |
| 38 |
Ok(()) |
| 39 |
} |
| 40 |
|
| 41 |
/// Generates man pages. |
| 42 |
fn generate_man_pages(cli: &mut clap::Command) -> Result<()> { |
| 43 |
let version = env!("CARGO_PKG_VERSION"); |
| 44 |
let mut builder = sequoia_man::man::Builder::new(cli, version, None); |
| 45 |
builder.see_also( |
| 46 |
&[ "For the full documentation see <https://book.sequoia-pgp.org/>." ]); |
| 47 |
|
| 48 |
let man_pages = asset_out_dir("man-pages")?; |
| 49 |
|
| 50 |
sequoia_man::generate_man_pages(&man_pages, &builder).unwrap(); |
| 51 |
Ok(()) |
| 52 |
} |
| 53 |
|
| 54 |
/// Lints the help texts. |
| 55 |
fn lint_help_texts(sq: &clap::Command) -> Result<()> { |
| 56 |
let mut lints = Vec::new(); |
| 57 |
|
| 58 |
walk(&mut Vec::new(), sq, |
| 59 |
&mut |path: &[&str], c: &clap::Command| { |
| 60 |
let top_level = path.len() == 1; |
| 61 |
let path = path.join(" "); |
| 62 |
lint_short_long(&mut lints, &path, |
| 63 |
c.get_about(), c.get_long_about()); |
| 64 |
|
| 65 |
for arg in c.get_arguments() |
| 66 |
.filter(|a| a.get_id().as_str() != "help") |
| 67 |
.filter(|a| ! a.is_global_set() || top_level) |
| 68 |
{ |
| 69 |
let slug = format!("{} {}", path, |
| 70 |
if let Some(l) = arg.get_long() { |
| 71 |
format!("--{}", l) |
| 72 |
} else if let Some(s) = arg.get_short() { |
| 73 |
format!("-{}", s) |
| 74 |
} else { |
| 75 |
arg.get_id().as_str().to_string() |
| 76 |
}); |
| 77 |
lint_short_long(&mut lints, &slug, |
| 78 |
arg.get_help(), arg.get_long_help()); |
| 79 |
} |
| 80 |
|
| 81 |
Ok(()) |
| 82 |
}, |
| 83 |
&mut |_, _, _| Ok(()))?; |
| 84 |
|
| 85 |
if lints.is_empty() { |
| 86 |
Ok(()) |
| 87 |
} else { |
| 88 |
println!("cargo:warning=Linting help texts found {} issues", |
| 89 |
lints.len()); |
| 90 |
println!("cargo:warning="); |
| 91 |
|
| 92 |
for lint in lints { |
| 93 |
println!("cargo:warning=lint: {}", lint); |
| 94 |
} |
| 95 |
|
| 96 |
Err(anyhow::anyhow!("linting help texts failed")) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
use clap::builder::StyledStr; |
| 101 |
|
| 102 |
/// Lints short and long about and help texts. |
| 103 |
fn lint_short_long(lints: &mut Vec<String>, |
| 104 |
slug: &str, |
| 105 |
short: Option<&StyledStr>, |
| 106 |
long: Option<&StyledStr>) |
| 107 |
{ |
| 108 |
let short = if let Some(short) = short { |
| 109 |
short.to_string() |
| 110 |
} else { |
| 111 |
lints.push(format!("{}: no short help", slug)); |
| 112 |
return; |
| 113 |
}; |
| 114 |
|
| 115 |
if short.contains(". ") { |
| 116 |
lints.push(format!("{}: short contains more than one sentence", slug)); |
| 117 |
} |
| 118 |
|
| 119 |
if short.ends_with(".") { |
| 120 |
lints.push(format!("{}: short ends in period", slug)); |
| 121 |
} |
| 122 |
|
| 123 |
let long = if let Some(long) = long { |
| 124 |
long.to_string() |
| 125 |
} else { |
| 126 |
return; |
| 127 |
}; |
| 128 |
|
| 129 |
if ! long.starts_with(&format!("{}\n\n", short)) { |
| 130 |
lints.push(format!("\ |
| 131 |
{}: long help doesn't start with subject (short help + \\n\\n) |
| 132 |
long help: {} |
| 133 |
short help: {}", |
| 134 |
slug, long, short)); |
| 135 |
} |
| 136 |
|
| 137 |
if long.split("\n").count() == 1 { |
| 138 |
lints.push(format!("{}: long help consists of a single line", slug)); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
fn walk<'sq, F, G, R>(path: &mut Vec<&'sq str>, c: &'sq clap::Command, |
| 143 |
fun0: &mut F, |
| 144 |
fun1: &mut G) |
| 145 |
-> Result<R> |
| 146 |
where |
| 147 |
F: FnMut(&[&str], &clap::Command) -> Result<()>, |
| 148 |
G: FnMut(&[&str], &clap::Command, &[R]) -> Result<R>, |
| 149 |
{ |
| 150 |
path.push(c.get_name()); |
| 151 |
(fun0)(&path, c)?; |
| 152 |
|
| 153 |
let mut r = Vec::new(); |
| 154 |
for s in c.get_subcommands().filter(|cmd| cmd.get_name() != "help") { |
| 155 |
r.push(walk(path, s, fun0, fun1)?); |
| 156 |
} |
| 157 |
|
| 158 |
let r = (fun1)(&path, c, &r)?; |
| 159 |
path.pop(); |
| 160 |
Ok(r) |
| 161 |
} |
| 162 |
|