Skip to content

Commit b4e2745

Browse files
authored
Github Actions improvements for write-rustdoc-hide-lines (v2) (#1101)
1 parent 320bc77 commit b4e2745

File tree

1 file changed

+30
-27
lines changed
  • write-rustdoc-hide-lines/src

1 file changed

+30
-27
lines changed

write-rustdoc-hide-lines/src/main.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
use std::{env, path::PathBuf, process::ExitCode};
22
use write_rustdoc_hide_lines::formatter;
33

4-
/// Generates `hide_lines` annotations to Rust code blocks.
5-
///
6-
/// In most cases you can just call `write_rustdoc_hide_lines.sh`, which will automatically handle
7-
/// formatting all files.
8-
///
9-
/// ```shell
10-
/// $ cd write-rustdoc-hide-lines
11-
/// $ ./write_rustdoc_hide_lines.sh
12-
/// ```
13-
///
14-
/// You can also run the executable manually.
15-
///
16-
/// ```shell
17-
/// $ cd write-rustdoc-hide-lines
18-
///
19-
/// # Format one folder.
20-
/// $ cargo run -- format ../content/learn/book
21-
///
22-
/// # Format multiple folders.
23-
/// $ cargo run -- format ../content/learn/book ../content/learn/quick-start
24-
///
25-
/// # Check one folder, but don't overwrite it.
26-
/// $ cargo run -- check ../content/learn/book
27-
/// ```
284
fn main() -> ExitCode {
295
// The first argument is usually the executable path, so we skip that to just get arguments.
306
let mut args = env::args().skip(1);
@@ -55,28 +31,55 @@ fn check(folders: impl Iterator<Item = PathBuf> + ExactSizeIterator) -> ExitCode
5531
// An aggregate list of all unformatted files, empty by default.
5632
let mut unformatted_files = Vec::new();
5733

34+
// Detect if we're running through Github Actions or not.
35+
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
36+
let is_ci = env::var("GITHUB_ACTIONS").is_ok_and(|x| x == "true");
37+
5838
for folder in folders {
59-
println!("\nChecking folder {:?}", folder);
39+
if is_ci {
40+
// Create an collapsible group for all files checked.
41+
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines
42+
println!("::group::Checking folder {:?}", folder);
43+
} else {
44+
println!("\nChecking folder {:?}", folder);
45+
}
6046

6147
// Checks folders, exiting early if an error occurred.
6248
match formatter::check(&folder) {
6349
// Merge new unformatted files into existing unformatted files.
6450
Ok(mut unformatted) => unformatted_files.append(&mut unformatted),
6551
Err(error) => {
52+
if is_ci {
53+
// End group if an error occurred, so it is not hidden.
54+
println!("::endgroup::");
55+
}
56+
6657
eprintln!("Error: {}", error);
6758

6859
return ExitCode::FAILURE;
6960
}
7061
}
62+
63+
if is_ci {
64+
println!("::endgroup::");
65+
}
7166
}
7267

7368
if !unformatted_files.is_empty() {
74-
eprintln!("\nThe following files are not formatted:");
69+
println!("\nThe following files are not formatted:");
7570

7671
for path in unformatted_files {
77-
eprintln!("- {:?}", path);
72+
if is_ci {
73+
// Print custom error message, formatted in Github Actions.
74+
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message
75+
println!("::error file={0:?},title=File is not formatted with correct hide-lines annotations::- {0:?}", path);
76+
} else {
77+
println!("- {:?}", path);
78+
}
7879
}
7980

81+
println!("\nRun write_rustdoc_hide_lines.sh to automatically fix these errors.");
82+
8083
ExitCode::FAILURE
8184
} else {
8285
println!("All files are properly formatted. :)");

0 commit comments

Comments
 (0)