From 9bede6b72f5632ca0fc7d6a56ffd4f869f00c3a0 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 5 Oct 2026 22:19:40 +1300 Subject: [PATCH] Accept positional paths in Markdown normalization. --- Cargo.lock | 16 +++++------ Cargo.toml | 2 +- bake/Cargo.toml | 2 +- context/markdown-normalization.md | 14 +++++++--- readme.md | 2 +- releases.md | 4 +++ src/file_system.rs | 2 +- src/file_system_tests.rs | 2 +- src/lib.rs | 10 +++---- tests/markdown/task.rs | 45 ++++++++++++++++++++++++------- 10 files changed, 67 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6848e9d..5ccfa2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,9 +19,9 @@ dependencies = [ [[package]] name = "bake" -version = "0.18.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbed34a1e40a3a5893a12c47de45006de3ae53112db64c0919a6e49c1a38d7e0" +checksum = "2f96c84418680954a2d9c8fe9afffb35aafb0b7086cb3cc141d613a2f0a74b42" dependencies = [ "bake-macros", "linkme", @@ -72,9 +72,9 @@ dependencies = [ [[package]] name = "bake-macros" -version = "0.18.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b801275922e3bbc727ecdbb809f1a82591744cad907c9e915bdfa9b28bf001f" +checksum = "696dc147efb72a305f77a058388c94e651cd3586222a0ccfe3fcf145c7143242" dependencies = [ "proc-macro2", "quote", @@ -101,9 +101,9 @@ dependencies = [ [[package]] name = "bake-readme" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0ddf47d6d04f3452746fadd69e5900b1fb2c41cd1e0995cbc1c4ba2fd63af96" +checksum = "7f9ab6aa18fb34ab56c06cb49d87f7eb19cd6bfe16d8553856fbdd6380f39634" dependencies = [ "bake", "serde_json", @@ -113,9 +113,9 @@ dependencies = [ [[package]] name = "bake-releases" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4be2cb3610ff7e22b9d926982e79909c4a936c54f2d1196ab4dd14c95fb8a20" +checksum = "54d725086b889120c08f4160d40ac117a029d41f5e0d96c4174922abfac61d83" dependencies = [ "bake", "socketry-markdown 0.1.1", diff --git a/Cargo.toml b/Cargo.toml index 7b08c35..5bddc10 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ manifest = "bake/Cargo.toml" reviewers = ["socketry/managers"] [dependencies] -bake = "0" +bake = ">=0.19.0" socketry-markdown = "0.2.0" [dev-dependencies] diff --git a/bake/Cargo.toml b/bake/Cargo.toml index cdc62fd..24eef03 100644 --- a/bake/Cargo.toml +++ b/bake/Cargo.toml @@ -5,6 +5,6 @@ edition = "2024" publish = false [dependencies] -bake = "0.18.0" +bake = ">=0.19.0" bake-markdown = { path = ".." } socketry-project = ">=0.3.3" diff --git a/context/markdown-normalization.md b/context/markdown-normalization.md index 9cb8a8c..f11d52a 100644 --- a/context/markdown-normalization.md +++ b/context/markdown-normalization.md @@ -15,11 +15,19 @@ their delimiters and content remain intact. The serializer canonicalizes line endings to LF and emits a trailing newline for non-empty documents. The normalizer does not wrap paragraphs to a target line width. -The task accepts one or more repeatable `--path` arguments. Paths are resolved -relative to the Bake project root: +The task accepts one or more positional paths, resolved relative to the Bake +project root. For example, the shell expands a glob before Bake receives the +paths: ```sh -cargo bake markdown:normalize --path readme.md --path context/guide.md +cargo bake markdown:normalize **/*.md +``` + +Use `::` before another task because the variadic path list consumes all +positional values up to the end of the command: + +```sh +cargo bake markdown:normalize **/*.md :: null ``` It writes a file only when the serialized content differs from its source. diff --git a/readme.md b/readme.md index dbbbfcd..023929b 100644 --- a/readme.md +++ b/readme.md @@ -26,7 +26,7 @@ use bake_markdown as _; Normalize one or more files relative to the project root: ```sh -cargo bake markdown:normalize --path readme.md --path context/guide.md +cargo bake markdown:normalize readme.md context/guide.md ``` Rust callers can use `bake_markdown::normalize_document` directly. diff --git a/releases.md b/releases.md index 6f4377e..d9d1f22 100644 --- a/releases.md +++ b/releases.md @@ -1,5 +1,9 @@ # Releases +## Unreleased + +- Accept Markdown paths as positional arguments to `markdown:normalize`. + ## v0.1.0 - Add the `markdown:normalize` task to join soft source line breaks in Markdown diff --git a/src/file_system.rs b/src/file_system.rs index e2c0180..63acec9 100644 --- a/src/file_system.rs +++ b/src/file_system.rs @@ -37,7 +37,7 @@ fn normalize_files_with( normalize: impl Fn(&str) -> std::result::Result, ) -> Result { if paths.is_empty() { - return Err(Error::new("provide one or more --path arguments")); + return Err(Error::new("provide one or more file paths")); } let mut changed = 0; diff --git a/src/file_system_tests.rs b/src/file_system_tests.rs index bdb702e..57ccb63 100644 --- a/src/file_system_tests.rs +++ b/src/file_system_tests.rs @@ -53,7 +53,7 @@ fn rejects_an_empty_path_list() { ) .unwrap_err(); - assert_eq!(error.to_string(), "provide one or more --path arguments"); + assert_eq!(error.to_string(), "provide one or more file paths"); } #[test] diff --git a/src/lib.rs b/src/lib.rs index a173b64..d9b76ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,18 +60,16 @@ mod file_system; use std::path::PathBuf; -/// Normalize one or more Markdown files beneath the Bake project root. +/// Normalize Markdown files beneath the Bake project root. #[bake::task] pub fn normalize( context: &mut bake::Context, - #[bake(help = "Repeat for each Markdown file, relative to the project root.")] path: Vec< - PathBuf, - >, + #[bake(positional, help = "Markdown files, relative to the project root.")] paths: Vec, ) -> bake::Result { - let changed = file_system::normalize_files(context.root(), &path)?; + let changed = file_system::normalize_files(context.root(), &paths)?; Ok(format!( "Normalized {changed} of {} Markdown files", - path.len() + paths.len() )) } diff --git a/tests/markdown/task.rs b/tests/markdown/task.rs index ebf2cdb..5eebc6f 100644 --- a/tests/markdown/task.rs +++ b/tests/markdown/task.rs @@ -27,14 +27,11 @@ fn registers_and_normalizes_multiple_files_from_the_project_root() { .any(|task| task.name() == "markdown:normalize") ); let help = registry.help(Some("markdown:normalize")).unwrap(); - assert!(help.contains("--path value")); - assert!(help.contains("repeatable")); + assert!(help.contains("paths...")); + assert!(help.contains("Markdown files, relative to the project root.")); let mut context = registry.context(directory.path()); let result = context - .call( - "markdown:normalize", - &["--path", "first.md", "--path", "second.md"], - ) + .call("markdown:normalize", &["first.md", "second.md"]) .unwrap(); assert_eq!(result.as_str(), Some("Normalized 2 of 2 Markdown files")); @@ -55,9 +52,37 @@ fn rejects_a_task_call_without_paths() { let error = context.call("markdown:normalize", &[]).unwrap_err(); - assert!( - error - .to_string() - .contains("provide one or more --path arguments") + assert!(error.to_string().contains("provide one or more file paths")); +} + +#[test] +fn stops_collecting_paths_at_the_task_chain_separator() { + let directory = tempfile::tempdir().unwrap(); + fs::write( + directory.path().join("first.md"), + "First line\ncontinues.\n", + ) + .unwrap(); + fs::write( + directory.path().join("second.md"), + "Second line\ncontinues.\n", + ) + .unwrap(); + + let arguments = + ["markdown:normalize", "first.md", "second.md", "::", "null"].map(str::to_owned); + let output = Registry::discover() + .unwrap() + .run_arguments(directory.path(), &arguments) + .unwrap(); + + assert_eq!(output, ""); + assert_eq!( + fs::read_to_string(directory.path().join("first.md")).unwrap(), + "First line continues.\n" + ); + assert_eq!( + fs::read_to_string(directory.path().join("second.md")).unwrap(), + "Second line continues.\n" ); }