Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github Actions improvements for write-rustdoc-hide-lines (v2) #1101

Merged
merged 8 commits into from Mar 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 30 additions & 27 deletions write-rustdoc-hide-lines/src/main.rs
@@ -1,30 +1,6 @@
use std::{env, path::PathBuf, process::ExitCode};
use write_rustdoc_hide_lines::formatter;

/// Generates `hide_lines` annotations to Rust code blocks.
///
/// In most cases you can just call `write_rustdoc_hide_lines.sh`, which will automatically handle
/// formatting all files.
///
/// ```shell
/// $ cd write-rustdoc-hide-lines
/// $ ./write_rustdoc_hide_lines.sh
/// ```
///
/// You can also run the executable manually.
///
/// ```shell
/// $ cd write-rustdoc-hide-lines
///
/// # Format one folder.
/// $ cargo run -- format ../content/learn/book
///
/// # Format multiple folders.
/// $ cargo run -- format ../content/learn/book ../content/learn/quick-start
///
/// # Check one folder, but don't overwrite it.
/// $ cargo run -- check ../content/learn/book
/// ```
fn main() -> ExitCode {
// The first argument is usually the executable path, so we skip that to just get arguments.
let mut args = env::args().skip(1);
Expand Down Expand Up @@ -55,28 +31,55 @@ fn check(folders: impl Iterator<Item = PathBuf> + ExactSizeIterator) -> ExitCode
// An aggregate list of all unformatted files, empty by default.
let mut unformatted_files = Vec::new();

// Detect if we're running through Github Actions or not.
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
let is_ci = env::var("GITHUB_ACTIONS").is_ok_and(|x| x == "true");

for folder in folders {
println!("\nChecking folder {:?}", folder);
if is_ci {
// Create an collapsible group for all files checked.
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines
println!("::group::Checking folder {:?}", folder);
} else {
println!("\nChecking folder {:?}", folder);
}

// Checks folders, exiting early if an error occurred.
match formatter::check(&folder) {
// Merge new unformatted files into existing unformatted files.
Ok(mut unformatted) => unformatted_files.append(&mut unformatted),
Err(error) => {
if is_ci {
// End group if an error occurred, so it is not hidden.
println!("::endgroup::");
}

eprintln!("Error: {}", error);

return ExitCode::FAILURE;
}
}

if is_ci {
println!("::endgroup::");
}
}

if !unformatted_files.is_empty() {
eprintln!("\nThe following files are not formatted:");
println!("\nThe following files are not formatted:");

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

println!("\nRun write_rustdoc_hide_lines.sh to automatically fix these errors.");

ExitCode::FAILURE
} else {
println!("All files are properly formatted. :)");
Expand Down