From 6465af384ba82c76eda01a98b8e6189015d5393e Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Wed, 26 Jun 2024 15:02:22 +0200 Subject: [PATCH] docs: command help (#332) --- README.md | 67 +++++++++++++++++++++++++++++++++-- codegen/Cargo.toml | 4 +++ codegen/src/command_help.rs | 32 +++++++++++++++++ codegen/src/main.rs | 3 ++ codegen/src/readme_tooling.rs | 16 +++++---- justfile | 3 +- src/commands/format.rs | 4 +-- 7 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 codegen/src/command_help.rs diff --git a/README.md b/README.md index 9df835f9..09b86bc7 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,73 @@ cargo install --path ./mdsf --bin mdsf mdsf format ``` + + +``` +Run formatters on input files + +Usage: mdsf format [OPTIONS] + +Arguments: + Path to file or directory + +Options: + --config Path to config + --debug Log stdout and stderr of formatters + --log-level [possible values: trace, debug, info, warn, error, off] + --threads Amount of threads to use. Defaults to 0 (auto) + -h, --help Print help + -V, --version Print version +``` + + + +### Verify code is formatted + +```shell +mdsf verify +``` + + + +``` +Verify files are formatted + +Usage: mdsf verify [OPTIONS] + +Arguments: + Path to file or directory + +Options: + --config Path to config + --debug Log stdout and stderr of formatters + --log-level [possible values: trace, debug, info, warn, error, off] + --threads Amount of threads to use. Defaults to 0 (auto) + -h, --help Print help + -V, --version Print version +``` + + + ### Shell completions Shell completions can be generated using `mdsf completions `. + + +``` +Usage: mdsf completions + +Arguments: + [possible values: bash, elvish, fish, powershell, zsh] + +Options: + -h, --help Print help + -V, --version Print version +``` + + + #### Bash Add the following to your `.bashrc`. @@ -102,7 +165,7 @@ mdsf init > > Only tools that are already installed will be used. - + `mdsf` currently supports 146 tools. @@ -255,7 +318,7 @@ mdsf init | zigfmt | [https://ziglang.org/](https://ziglang.org/) | | zprint | [https://github.com/kkinnear/zprint](https://github.com/kkinnear/zprint) | - + ## Acknowledgement diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index c9aa915b..d7eed756 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -7,6 +7,10 @@ license = "MIT" readme = "../README.md" keywords = [] categories = [] +authors = ["Mads Hougesen "] +repository = "https://github.com/hougesen/mdsf" +homepage = "https://github.com/hougesen/mdsf" +publish = false [dependencies] anyhow = "1.0.86" diff --git a/codegen/src/command_help.rs b/codegen/src/command_help.rs new file mode 100644 index 00000000..5caf180c --- /dev/null +++ b/codegen/src/command_help.rs @@ -0,0 +1,32 @@ +use crate::readme_tooling::update_readme; + +fn execute_command(name: &str) -> std::io::Result { + let mut x = std::process::Command::new("cargo"); + x.arg("build"); + x.current_dir("../"); + x.output()?; + + std::process::Command::new("../target/debug/mdsf") + .arg(name) + .arg("--help") + .output() +} + +fn update_command(name: &str) -> anyhow::Result<()> { + let help = String::from_utf8(execute_command(name)?.stdout)?; + + update_readme( + &format!("{name}-command-help"), + &format!("```\n{}\n```", help.trim()), + )?; + + anyhow::Ok(()) +} + +pub fn generate() -> anyhow::Result<()> { + update_command("format")?; + update_command("verify")?; + update_command("completions")?; + + anyhow::Ok(()) +} diff --git a/codegen/src/main.rs b/codegen/src/main.rs index f227ef5f..c108c7c6 100644 --- a/codegen/src/main.rs +++ b/codegen/src/main.rs @@ -1,6 +1,7 @@ use anyhow::{Ok, Result}; mod cargo; +mod command_help; mod language_to_filetype; mod readme_tooling; mod schema; @@ -12,5 +13,7 @@ fn main() -> Result<()> { readme_tooling::generate()?; + command_help::generate()?; + Ok(()) } diff --git a/codegen/src/readme_tooling.rs b/codegen/src/readme_tooling.rs index 7a12bd18..725b15e1 100644 --- a/codegen/src/readme_tooling.rs +++ b/codegen/src/readme_tooling.rs @@ -110,20 +110,22 @@ fn create_table(schema: Vec) -> String { lines.insert( 0, - format!("`mdsf` currently supports {} tools.\n", tool_count), + format!("`mdsf` currently supports {tool_count} tools.\n"), ); lines.join("\n") } -fn update_readme(table: &str) -> Result<()> { +pub fn update_readme(key: &str, value: &str) -> Result<()> { let readme = std::fs::read_to_string("../README.md")?; - let update = format!("\n\n{table}\n\n"); + let update = format!("\n\n{value}\n\n"); - let re = RegexBuilder::new( - r"()[^{}]*", - ).multi_line( true ) .build()?; + let re = RegexBuilder::new( + format!(r"()[^{{}}]*",).as_str(), + ) + .multi_line(true) + .build()?; let updated = re.replace(&readme, update); @@ -141,7 +143,7 @@ pub fn generate() -> Result<()> { let table = create_table(schema.definitions.tooling.one_of); - update_readme(&table)?; + update_readme("supported-tools", &table)?; Ok(()) } diff --git a/justfile b/justfile index 0b73e53f..7d6d92c9 100644 --- a/justfile +++ b/justfile @@ -34,6 +34,7 @@ codegen: format: cargo fmt + (cd codegen && cargo fmt) just --fmt --unstable . npx --yes prettier@latest --write --cache . cargo run -- format tests @@ -51,7 +52,7 @@ precommit: just test just changelog just format - typos --exclude src/generated.rs . + typos --exclude src/generated.rs --exclude CHANGELOG.md . publish: just build diff --git a/src/commands/format.rs b/src/commands/format.rs index 4fe913d1..9547cbb0 100644 --- a/src/commands/format.rs +++ b/src/commands/format.rs @@ -10,7 +10,7 @@ use threadpool::ThreadPool; const MDSF_IGNORE_FILE_NAME: &str = ".mdsfignore"; #[inline] -fn detemine_threads_to_use(argument: &Option) -> usize { +fn determine_threads_to_use(argument: &Option) -> usize { if let Some(thread_arg) = argument { if thread_arg > &0 { return thread_arg.to_owned(); @@ -58,7 +58,7 @@ pub fn run(args: FormatCommandArguments, dry_run: bool) -> Result<(), MdsfError> let md_ext = OsStr::from("md"); - let thread_count = detemine_threads_to_use(&args.threads).max(1); + let thread_count = determine_threads_to_use(&args.threads).max(1); let pool = ThreadPool::new(thread_count);