mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
feat: Generate admin command documentation
The first part of getting admin command docs on the website. There's also the beginnings of manpage generation here, although it's kinda sus and I'm not sure how it's supposed to work. I'll leave that to anyone who wants to package it. We introduce the beginings of the xtask pattern here - we do a lot of file generation, I thought it would be best to avoid doing that on every compilation. It also helps avoid lots of runtime deps. We'll need to document generating this stuff & probably add pre-commit hooks for it, though.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
[package]
|
||||
name = "xtask-admin-command"
|
||||
authors.workspace = true
|
||||
categories.workspace = true
|
||||
description.workspace = true
|
||||
edition.workspace = true
|
||||
homepage.workspace = true
|
||||
keywords.workspace = true
|
||||
license.workspace = true
|
||||
readme.workspace = true
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
clap-markdown = "0.1.5"
|
||||
clap_builder = { version = "4.5.38", default-features = false }
|
||||
clap_mangen = "0.2"
|
||||
|
||||
conduwuit-admin.workspace = true
|
||||
|
||||
# Hack to prevent rebuilds
|
||||
conduwuit.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -0,0 +1,63 @@
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::{self, Write},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use clap_builder::{Command, CommandFactory};
|
||||
use conduwuit_admin::AdminCommand;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut args = std::env::args().skip(1);
|
||||
let task = args.next();
|
||||
match task {
|
||||
| None => todo!(),
|
||||
| Some(t) => match t.as_str() {
|
||||
| "man" => {
|
||||
let dir = Path::new("./admin-man");
|
||||
gen_manpages(dir)?;
|
||||
},
|
||||
| "md" => {
|
||||
let command = AdminCommand::command().name("admin");
|
||||
|
||||
let res = clap_markdown::help_markdown_command_custom(
|
||||
&command,
|
||||
&clap_markdown::MarkdownOptions::default().show_footer(false),
|
||||
)
|
||||
.replace("\n\r", "\n")
|
||||
.replace("\r\n", "\n")
|
||||
.replace(" \n", "\n");
|
||||
|
||||
let mut file = File::create(Path::new("./docs/admin_reference.md"))?;
|
||||
Write::write_all(&mut file, res.trim_end().as_bytes())?;
|
||||
file.write(b"\n")?;
|
||||
},
|
||||
| invalid => return Err(format!("Invalid task name: {invalid}").into()),
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gen_manpages(dir: &Path) -> Result<(), io::Error> {
|
||||
fn r#gen(dir: &Path, c: &Command, prefix: Option<&str>) -> Result<(), io::Error> {
|
||||
fs::create_dir_all(dir)?;
|
||||
let sub_name = c.get_display_name().unwrap_or_else(|| c.get_name());
|
||||
let name = if let Some(prefix) = prefix {
|
||||
format!("{prefix}-{sub_name}")
|
||||
} else {
|
||||
sub_name.to_owned()
|
||||
};
|
||||
|
||||
let mut out = File::create(dir.join(format!("{name}.1")))?;
|
||||
let clap_mangen = clap_mangen::Man::new(c.to_owned().disable_help_flag(true));
|
||||
clap_mangen.render(&mut out)?;
|
||||
|
||||
for sub in c.get_subcommands() {
|
||||
r#gen(&dir.join(sub_name), sub, Some(&name))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
r#gen(dir, &AdminCommand::command().name("admin"), None)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "xtask"
|
||||
authors.workspace = true
|
||||
categories.workspace = true
|
||||
description.workspace = true
|
||||
edition.workspace = true
|
||||
homepage.workspace = true
|
||||
keywords.workspace = true
|
||||
license.workspace = true
|
||||
readme.workspace = true
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
clap.workspace = true
|
||||
# Required for working with JSON output from cargo metadata
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -0,0 +1,11 @@
|
||||
use std::{env, process::Command};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut child = Command::new("cargo").args(["run", "--package", "xtask-admin-command", "--"].into_iter().map(ToOwned::to_owned).chain(env::args().skip(2)))
|
||||
// .stdout(Stdio::piped())
|
||||
// .stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("failed to execute child");
|
||||
child.wait()?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user