Skip to content

Commit

Permalink
docs: command help (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Jun 26, 2024
1 parent 32b51d0 commit 6465af3
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 12 deletions.
67 changes: 65 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,73 @@ cargo install --path ./mdsf --bin mdsf
mdsf format <NAME_OF_FOLDER_OR_FOLDER>
```

<!-- START_SECTION:format-command-help -->

```
Run formatters on input files
Usage: mdsf format [OPTIONS] <PATH>
Arguments:
<PATH> Path to file or directory
Options:
--config <CONFIG> Path to config
--debug Log stdout and stderr of formatters
--log-level <LOG_LEVEL> [possible values: trace, debug, info, warn, error, off]
--threads <THREADS> Amount of threads to use. Defaults to 0 (auto)
-h, --help Print help
-V, --version Print version
```

<!-- END_SECTION:format-command-help -->

### Verify code is formatted

```shell
mdsf verify <NAME_OF_FOLDER_OR_FOLDER>
```

<!-- START_SECTION:verify-command-help -->

```
Verify files are formatted
Usage: mdsf verify [OPTIONS] <PATH>
Arguments:
<PATH> Path to file or directory
Options:
--config <CONFIG> Path to config
--debug Log stdout and stderr of formatters
--log-level <LOG_LEVEL> [possible values: trace, debug, info, warn, error, off]
--threads <THREADS> Amount of threads to use. Defaults to 0 (auto)
-h, --help Print help
-V, --version Print version
```

<!-- END_SECTION:verify-command-help -->

### Shell completions

Shell completions can be generated using `mdsf completions <SHELL>`.

<!-- START_SECTION:completions-command-help -->

```
Usage: mdsf completions <SHELL>
Arguments:
<SHELL> [possible values: bash, elvish, fish, powershell, zsh]
Options:
-h, --help Print help
-V, --version Print version
```

<!-- END_SECTION:completions-command-help -->

#### Bash

Add the following to your `.bashrc`.
Expand Down Expand Up @@ -102,7 +165,7 @@ mdsf init
>
> Only tools that are already installed will be used.
<!-- START_SECTION:supported-languages -->
<!-- START_SECTION:supported-tools-->

`mdsf` currently supports 146 tools.

Expand Down Expand Up @@ -255,7 +318,7 @@ mdsf init
| zigfmt | [https://ziglang.org/](https://ziglang.org/) |
| zprint | [https://github.com/kkinnear/zprint](https://github.com/kkinnear/zprint) |

<!-- END_SECTION:supported-languages -->
<!-- END_SECTION:supported-tools-->

## Acknowledgement

Expand Down
4 changes: 4 additions & 0 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ license = "MIT"
readme = "../README.md"
keywords = []
categories = []
authors = ["Mads Hougesen <[email protected]>"]
repository = "https://github.com/hougesen/mdsf"
homepage = "https://github.com/hougesen/mdsf"
publish = false

[dependencies]
anyhow = "1.0.86"
Expand Down
32 changes: 32 additions & 0 deletions codegen/src/command_help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::readme_tooling::update_readme;

fn execute_command(name: &str) -> std::io::Result<std::process::Output> {
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(())
}
3 changes: 3 additions & 0 deletions codegen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{Ok, Result};

mod cargo;
mod command_help;
mod language_to_filetype;
mod readme_tooling;
mod schema;
Expand All @@ -12,5 +13,7 @@ fn main() -> Result<()> {

readme_tooling::generate()?;

command_help::generate()?;

Ok(())
}
16 changes: 9 additions & 7 deletions codegen/src/readme_tooling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,22 @@ fn create_table(schema: Vec<Tool>) -> 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!("<!-- START_SECTION:supported-languages -->\n\n{table}\n\n<!-- END_SECTION:supported-languages -->");
let update = format!("<!-- START_SECTION:{key} -->\n\n{value}\n\n<!-- END_SECTION:{key} -->");

let re = RegexBuilder::new(
r"(<!-- START_SECTION:supported-languages -->)[^{}]*<!-- END_SECTION:supported-languages -->",
).multi_line( true ) .build()?;
let re = RegexBuilder::new(
format!(r"(<!-- START_SECTION:{key} -->)[^{{}}]*<!-- END_SECTION:{key} -->",).as_str(),
)
.multi_line(true)
.build()?;

let updated = re.replace(&readme, update);

Expand All @@ -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(())
}
3 changes: 2 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use threadpool::ThreadPool;
const MDSF_IGNORE_FILE_NAME: &str = ".mdsfignore";

#[inline]
fn detemine_threads_to_use(argument: &Option<usize>) -> usize {
fn determine_threads_to_use(argument: &Option<usize>) -> usize {
if let Some(thread_arg) = argument {
if thread_arg > &0 {
return thread_arg.to_owned();
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 6465af3

Please sign in to comment.