Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion bake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 11 additions & 3 deletions context/markdown-normalization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions releases.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn normalize_files_with(
normalize: impl Fn(&str) -> std::result::Result<String, Message>,
) -> Result<usize> {
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;
Expand Down
2 changes: 1 addition & 1 deletion src/file_system_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
) -> bake::Result<String> {
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()
))
}

Expand Down
45 changes: 35 additions & 10 deletions tests/markdown/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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"
);
}
Loading