From 705e9e073de3cfd44d467347021336a0dd701cab Mon Sep 17 00:00:00 2001 From: omo50 <144749186+omo50@users.noreply.github.com> Date: Sat, 18 Apr 2026 13:56:41 -0600 Subject: [PATCH] chore(ci): update and improve linters --- .../somnus/core/src/bin/json-linter.rs | 63 +++++++------------ .../somnus/core/src/bin/toml-linter.rs | 58 +++++++---------- src/actions/somnus/core/src/lib.rs | 30 +++++---- src/actions/somnus/core/src/linter.rs | 60 ++++++++++++++++++ 4 files changed, 125 insertions(+), 86 deletions(-) create mode 100644 src/actions/somnus/core/src/linter.rs diff --git a/src/actions/somnus/core/src/bin/json-linter.rs b/src/actions/somnus/core/src/bin/json-linter.rs index e9200c0c0..52f341fdc 100644 --- a/src/actions/somnus/core/src/bin/json-linter.rs +++ b/src/actions/somnus/core/src/bin/json-linter.rs @@ -1,47 +1,32 @@ -use std::fs; -use std::path::Path; -use somnus_core::get_changed_files; +use somnus_core::lint_changed_files; + +const WATCHED_PREFIXES: &[&str] = &["modpacks/", "datapacks/"]; +const WATCHED_EXTS: &[&str] = &[".json", ".mcmeta"]; fn main() { - if run() { - std::process::exit(1); - } -} - -pub fn run() -> bool { - let mut failed = false; - - let changed_files = match get_changed_files() { - Ok(files) => files, + let report = match lint_changed_files( + "json", + |path| { + WATCHED_PREFIXES.iter().any(|p| path.starts_with(p)) + && WATCHED_EXTS.iter().any(|e| path.ends_with(e)) + }, + |content| { + serde_json::from_str::(content) + .map(|_| ()) + .map_err(|e| e.to_string()) + }, + ) { + Ok(r) => r, Err(e) => { - eprintln!("::error::Failed to retrieve changed files: {}", e); - return true; + eprintln!("::error::lint harness failed: {e}"); + std::process::exit(1); } }; - for file_path in changed_files { - let path = Path::new(&file_path); - if !path.exists() { continue; } + println!("linted {} json file(s), {} failed.", report.checked, report.failed); - if file_path.ends_with(".json") || file_path.ends_with(".mcmeta") { - if ["modpacks/", "resourcepacks/", "datapacks/"] - .iter() - .any(|dir| file_path.starts_with(dir)) - { - println!("::group::Linting {}", file_path); - let content = fs::read_to_string(path).expect("Read Error"); - - if let Err(e) = serde_json::from_str::(&content) { - eprintln!("::error file={}::INVALID JSON: {}", file_path, e); - failed = true; - } - println!("::endgroup::"); - } - } + if !report.is_ok() { + eprintln!("fix yo json chud..."); + std::process::exit(1); } - - if failed { - eprintln!("Fix yo json chud..."); - } - failed -} \ No newline at end of file +} diff --git a/src/actions/somnus/core/src/bin/toml-linter.rs b/src/actions/somnus/core/src/bin/toml-linter.rs index 61ea66d00..be54403cc 100644 --- a/src/actions/somnus/core/src/bin/toml-linter.rs +++ b/src/actions/somnus/core/src/bin/toml-linter.rs @@ -1,42 +1,32 @@ -use std::fs; -use std::path::Path; -use somnus_core::get_changed_files; +use somnus_core::lint_changed_files; + +const WATCHED_PREFIXES: &[&str] = &["modpacks/", "datapacks/"]; fn main() { - if run() { - std::process::exit(1); - } -} - -pub fn run() -> bool { - let mut failed = false; - - let changed_files = match get_changed_files() { - Ok(files) => files, + let report = match lint_changed_files( + "toml", + |path| { + path.ends_with(".toml") + && WATCHED_PREFIXES.iter().any(|p| path.starts_with(p)) + }, + |content| { + content + .parse::() + .map(|_| ()) + .map_err(|e| e.to_string()) + }, + ) { + Ok(r) => r, Err(e) => { - eprintln!("::error::Failed to retrieve changed files: {}", e); - return true; + eprintln!("::error::lint harness failed: {e}"); + std::process::exit(1); } }; - for file_path in changed_files { - let path = Path::new(&file_path); - if !path.exists() { continue; } + println!("linted {} toml file(s), {} failed.", report.checked, report.failed); - if file_path.ends_with(".toml") { - println!("::group::Linting TOML: {}", file_path); - let content = fs::read_to_string(path).expect("Read Error"); - - if let Err(e) = content.parse::() { - eprintln!("::error file={}::INVALID TOML: {}", file_path, e); - failed = true; - } - println!("::endgroup::"); - } + if !report.is_ok() { + eprintln!("fix yo toml chud..."); + std::process::exit(1); } - - if failed { - eprintln!("Fix yo toml chud..."); - } - failed -} \ No newline at end of file +} diff --git a/src/actions/somnus/core/src/lib.rs b/src/actions/somnus/core/src/lib.rs index 6199e0036..bda255782 100644 --- a/src/actions/somnus/core/src/lib.rs +++ b/src/actions/somnus/core/src/lib.rs @@ -5,28 +5,32 @@ pub mod auditor; pub mod errors; pub mod state; pub mod logger; +pub mod linter; use git2::Repository; use crate::errors::SomnusError; pub fn get_changed_files() -> Result, SomnusError> { let repo = Repository::open(".")?; - + let head = repo.head()?.peel_to_tree()?; - - let parent = repo.find_commit(repo.head()?.target().unwrap())? + + let parent = repo + .find_commit(repo.head()?.target().unwrap())? .parent(0)? .tree()?; - let diff = repo.diff_tree_to_tree(Some(&parent), Some(&head), None)?; let mut changed_files = Vec::new(); - - diff.foreach(&mut |delta, _| { - if let Some(path) = delta.new_file().path() { - changed_files.push(path.to_string_lossy().to_string()); - } - true - }, None, None, None)?; - + diff.foreach( + &mut |delta, _| { + if let Some(path) = delta.new_file().path() { + changed_files.push(path.to_string_lossy().to_string()); + } + true + }, + None, + None, + None, + )?; Ok(changed_files) -} \ No newline at end of file +} diff --git a/src/actions/somnus/core/src/linter.rs b/src/actions/somnus/core/src/linter.rs new file mode 100644 index 000000000..4a75c2a77 --- /dev/null +++ b/src/actions/somnus/core/src/linter.rs @@ -0,0 +1,60 @@ +use crate::errors::SomnusError; +use crate::get_changed_files; +use std::fs; +use std::path::Path; + +pub struct LintReport { + pub checked: usize, + pub failed: usize, +} + +impl LintReport { + pub fn is_ok(&self) -> bool { + self.failed == 0 + } +} + +pub fn lint_changed_files( + kind: &str, + matches: M, + parse: P, +) -> Result +where + M: Fn(&str) -> bool, + P: Fn(&str) -> Result<(), String>, +{ + let changed = get_changed_files()?; + let mut checked = 0usize; + let mut failed = 0usize; + let kind_upper = kind.to_uppercase(); + + for file_path in changed { + if !matches(&file_path) { + continue; + } + let path = Path::new(&file_path); + if !path.exists() { + continue; + } + + println!("::group::Linting {kind}: {file_path}"); + checked += 1; + + match fs::read_to_string(path) { + Ok(content) => { + if let Err(msg) = parse(&content) { + eprintln!("::error file={file_path}::INVALID {kind_upper}: {msg}"); + failed += 1; + } + } + Err(e) => { + eprintln!("::error file={file_path}::Failed to read: {e}"); + failed += 1; + } + } + + println!("::endgroup::"); + } + + Ok(LintReport { checked, failed }) +}