|
1 | 1 | use std::{env, path::PathBuf, process::ExitCode}; |
2 | 2 | use write_rustdoc_hide_lines::formatter; |
3 | 3 |
|
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 | | -/// ``` |
28 | 4 | fn main() -> ExitCode { |
29 | 5 | // The first argument is usually the executable path, so we skip that to just get arguments. |
30 | 6 | let mut args = env::args().skip(1); |
@@ -55,28 +31,55 @@ fn check(folders: impl Iterator<Item = PathBuf> + ExactSizeIterator) -> ExitCode |
55 | 31 | // An aggregate list of all unformatted files, empty by default. |
56 | 32 | let mut unformatted_files = Vec::new(); |
57 | 33 |
|
| 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 | + |
58 | 38 | 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 | + } |
60 | 46 |
|
61 | 47 | // Checks folders, exiting early if an error occurred. |
62 | 48 | match formatter::check(&folder) { |
63 | 49 | // Merge new unformatted files into existing unformatted files. |
64 | 50 | Ok(mut unformatted) => unformatted_files.append(&mut unformatted), |
65 | 51 | Err(error) => { |
| 52 | + if is_ci { |
| 53 | + // End group if an error occurred, so it is not hidden. |
| 54 | + println!("::endgroup::"); |
| 55 | + } |
| 56 | + |
66 | 57 | eprintln!("Error: {}", error); |
67 | 58 |
|
68 | 59 | return ExitCode::FAILURE; |
69 | 60 | } |
70 | 61 | } |
| 62 | + |
| 63 | + if is_ci { |
| 64 | + println!("::endgroup::"); |
| 65 | + } |
71 | 66 | } |
72 | 67 |
|
73 | 68 | if !unformatted_files.is_empty() { |
74 | | - eprintln!("\nThe following files are not formatted:"); |
| 69 | + println!("\nThe following files are not formatted:"); |
75 | 70 |
|
76 | 71 | 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 | + } |
78 | 79 | } |
79 | 80 |
|
| 81 | + println!("\nRun write_rustdoc_hide_lines.sh to automatically fix these errors."); |
| 82 | + |
80 | 83 | ExitCode::FAILURE |
81 | 84 | } else { |
82 | 85 | println!("All files are properly formatted. :)"); |
|
0 commit comments