Skip to content

Commit

Permalink
project: check for BOM (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson authored Oct 9, 2024
1 parent 81baec3 commit 037d696
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 9 deletions.
3 changes: 2 additions & 1 deletion bin/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
context::{self, Context},
error::Error,
executor::Executor,
modules::{pbo::Collapse, Binarize, Files, Hooks, Rapifier, SQFCompiler},
modules::{bom::BOMCheck, pbo::Collapse, Binarize, Files, Hooks, Rapifier, SQFCompiler},
report::Report,
};

Expand Down Expand Up @@ -96,6 +96,7 @@ pub fn executor(ctx: Context, matches: &ArgMatches) -> Executor {

executor.collapse(Collapse::No);

executor.add_module(Box::<BOMCheck>::default());
executor.add_module(Box::<Hooks>::default());
if matches.get_one::<bool>("no-rap") != Some(&true) {
executor.add_module(Box::<Rapifier>::default());
Expand Down
3 changes: 2 additions & 1 deletion bin/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
context::Context,
error::Error,
executor::Executor,
modules::{pbo::Collapse, Binarize, Hooks, Rapifier, SQFCompiler},
modules::{bom::BOMCheck, pbo::Collapse, Binarize, Hooks, Rapifier, SQFCompiler},
report::Report,
};

Expand All @@ -28,6 +28,7 @@ pub fn execute() -> Result<Report, Error> {

executor.collapse(Collapse::Yes);

executor.add_module(Box::<BOMCheck>::default());
executor.add_module(Box::<Hooks>::default());
executor.add_module(Box::<Rapifier>::default());
executor.add_module(Box::<SQFCompiler>::default());
Expand Down
5 changes: 4 additions & 1 deletion bin/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::{
context::Context,
error::Error,
executor::Executor,
modules::{pbo::Collapse, Binarize, FilePatching, Files, Hooks, Rapifier, SQFCompiler},
modules::{
bom::BOMCheck, pbo::Collapse, Binarize, FilePatching, Files, Hooks, Rapifier, SQFCompiler,
},
report::Report,
};

Expand Down Expand Up @@ -136,6 +138,7 @@ pub fn context(

executor.collapse(Collapse::Yes);

executor.add_module(Box::<BOMCheck>::default());
executor.add_module(Box::<Hooks>::default());
if rapify && matches.get_one::<bool>("no-rap") != Some(&true) {
executor.add_module(Box::<Rapifier>::default());
Expand Down
75 changes: 75 additions & 0 deletions bin/src/modules/bom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::{io::Read, path::PathBuf, sync::Arc};

use hemtt_workspace::reporting::Code;

use crate::{context::Context, report::Report};

use super::Module;

#[derive(Default)]
pub struct BOMCheck {}

impl Module for BOMCheck {
fn name(&self) -> &'static str {
"BOM Check"
}

fn check(&self, ctx: &Context) -> Result<Report, crate::Error> {
fn files_to_check(root: &PathBuf) -> Vec<PathBuf> {
const IGNORED_EXTENSIONS: [&str; 4] = ["p3d", "rtm", "bin", "paa"];
walkdir::WalkDir::new(root)
.into_iter()
.filter_map(std::result::Result::ok)
.filter(|e| e.file_type().is_file())
.filter(|e| !e.path().display().to_string().contains(".hemttout"))
.filter(|e| {
e.path().extension().map_or(false, |e| {
!IGNORED_EXTENSIONS.contains(&e.to_str().unwrap_or_default())
})
})
.map(|e| e.path().to_path_buf())
.collect::<Vec<_>>()
}
let mut report = Report::new();
for folder in ["addons", "optionals"] {
let folder = ctx.project_folder().join(folder);
if !folder.exists() {
continue;
}
let files = files_to_check(&folder);
for path in files {
let mut buffer = [0; 3];
let mut file = std::fs::File::open(&path)?;
if file.read_exact(&mut buffer).is_err() {
continue;
}
if buffer == [0xEF, 0xBB, 0xBF] {
report.push(Arc::new(BOMError {
file: {
path.display()
.to_string()
.strip_prefix(&ctx.project_folder().display().to_string())
.unwrap_or_default()
.to_string()
},
}));
}
}
}
Ok(report)
}
}

struct BOMError {
file: String,
}

impl Code for BOMError {
fn ident(&self) -> &'static str {
"BOM"
}

fn message(&self) -> String {
format!("File `{}` has a BOM marker", self.file)
}
}
1 change: 1 addition & 0 deletions bin/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{context::Context, error::Error, report::Report};
pub mod asc;

pub mod archive;
pub mod bom;
pub mod hook;
pub mod pbo;

Expand Down
2 changes: 1 addition & 1 deletion bin/tests/alpha/.hemtt/project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ git_hash = 0
[asc]
enabled = true

[hemtt.launch]
[hemtt.launch.default]
dlc = [
"ws"
]
Expand Down
10 changes: 5 additions & 5 deletions libs/workspace/src/reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ pub trait Code: Send + Sync {

/// A diagnostic for the LSP / terminal
fn diagnostic(&self) -> Option<Diagnostic> {
let token = self.token()?;
let mut diag = Diagnostic::new(self.ident(), self.message())
.with_label(
let mut diag = Diagnostic::new(self.ident(), self.message()).set_severity(self.severity());
if let Some(token) = self.token() {
diag = diag.with_label(
Label::primary(token.position().path().clone(), token.position().span())
.with_message(self.label_message()),
)
.set_severity(self.severity());
);
}
if let Some(note) = self.note() {
diag = diag.with_note(note);
}
Expand Down

0 comments on commit 037d696

Please sign in to comment.