-
Notifications
You must be signed in to change notification settings - Fork 129
Add TODO checker to cg_clif #1632
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| use std::ffi::OsStr; | ||
| use std::fs; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::process::Command; | ||
|
|
||
| const EXTENSIONS: &[&str] = | ||
| &["rs", "py", "js", "sh", "c", "cpp", "h", "md", "css", "ftl", "toml", "yml", "yaml"]; | ||
| const SKIP_FILES: &[&str] = &["build_system/todo.rs", ".github/workflows/main.yml"]; | ||
|
|
||
| fn has_supported_extension(path: &Path) -> bool { | ||
| path.extension().is_some_and(|ext| EXTENSIONS.iter().any(|e| ext == OsStr::new(e))) | ||
| } | ||
|
|
||
| fn is_editor_temp(path: &Path) -> bool { | ||
| path.file_name().is_some_and(|name| name.to_string_lossy().starts_with(".#")) | ||
| } | ||
|
|
||
| fn list_tracked_files() -> Result<Vec<PathBuf>, String> { | ||
| let output = Command::new("git") | ||
| .args(["ls-files", "-z"]) | ||
| .output() | ||
| .map_err(|e| format!("Failed to run `git ls-files`: {e}"))?; | ||
|
|
||
| if !output.status.success() { | ||
| let stderr = String::from_utf8_lossy(&output.stderr); | ||
| return Err(format!("`git ls-files` failed: {stderr}")); | ||
| } | ||
|
|
||
| let mut files = Vec::new(); | ||
| for entry in output.stdout.split(|b| *b == 0) { | ||
| if entry.is_empty() { | ||
| continue; | ||
| } | ||
| let path = std::str::from_utf8(entry) | ||
| .map_err(|_| "Non-utf8 path returned by `git ls-files`".to_string())?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These can also use unwrap. |
||
| files.push(PathBuf::from(path)); | ||
| } | ||
|
|
||
| Ok(files) | ||
| } | ||
|
|
||
| pub(crate) fn run() -> Result<(), String> { | ||
| let files = list_tracked_files()?; | ||
| let mut errors = Vec::new(); | ||
| // Avoid embedding the task marker in source so greps only find real occurrences. | ||
| let todo_marker = "todo".to_ascii_uppercase(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are both excluding this file from the todo check and also avoid the literal string TODO. One of the two seems redundant. |
||
|
|
||
| for file in files { | ||
| if is_editor_temp(&file) { | ||
| continue; | ||
| } | ||
|
Comment on lines
+49
to
+51
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is already excluded by |
||
| if SKIP_FILES.iter().any(|skip| file.ends_with(Path::new(skip))) { | ||
| continue; | ||
| } | ||
| if !has_supported_extension(&file) { | ||
| continue; | ||
| } | ||
|
|
||
| let bytes = | ||
| fs::read(&file).map_err(|e| format!("Failed to read `{}`: {e}", file.display()))?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to do error handling. Just unwrap. The build system doesn't have to be robust against hardware/OS issues. It isn't robust in other places either. |
||
| let Ok(contents) = std::str::from_utf8(&bytes) else { | ||
| continue; | ||
| }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All source files should be valid UTF-8, so unwrap here is fine. |
||
|
|
||
| for (i, line) in contents.split('\n').enumerate() { | ||
| let trimmed = line.trim(); | ||
| if trimmed.contains(&todo_marker) { | ||
| errors.push(format!( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can immediately print the error and increment an error counter. |
||
| "{}:{}: {} is used for tasks that should be done before merging a PR; if you want to leave a message in the codebase use FIXME", | ||
| file.display(), | ||
| i + 1, | ||
| todo_marker | ||
| )); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if errors.is_empty() { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| for err in &errors { | ||
| eprintln!("{err}"); | ||
| } | ||
|
|
||
| Err(format!("found {} {}(s)", errors.len(), todo_marker)) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make
todo::run()return!and move theprocess::exit()into it?