mirror of
https://github.com/Nostalgica-Reverie/Content-Monorepo.git
synced 2026-05-09 00:24:15 +00:00
chore(ci): update and improve linters
This commit is contained in:
@@ -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::<serde_json::Value>(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::<serde_json::Value>(&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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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::<toml::Value>()
|
||||
.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::<toml::Value>() {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Vec<String>, 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)
|
||||
}
|
||||
}
|
||||
|
||||
60
src/actions/somnus/core/src/linter.rs
Normal file
60
src/actions/somnus/core/src/linter.rs
Normal file
@@ -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<M, P>(
|
||||
kind: &str,
|
||||
matches: M,
|
||||
parse: P,
|
||||
) -> Result<LintReport, SomnusError>
|
||||
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 })
|
||||
}
|
||||
Reference in New Issue
Block a user