use std::collections::BTreeMap;
use std::io::Write;
use std::path::PathBuf;
use mortise_core::{
AppSnapshot, Check, CoverageSettings, Detail, Finding, ReadCache, ReadError, ReadOptions,
RulesSource, Severity, read_app_cached,
};
use crate::{printable, say, strings};
pub(crate) struct Options {
pub(crate) path: PathBuf,
pub(crate) coverage: Option<PathBuf>,
pub(crate) coverage_limit: Option<u8>,
pub(crate) format: Format,
pub(crate) fail_on_warnings: bool,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Format {
Text,
Json,
Github,
}
pub(crate) fn run(options: &Options) -> u8 {
let report = options
.coverage
.as_ref()
.map(|path| std::path::absolute(path).unwrap_or_else(|_| path.clone()));
let read_options = ReadOptions {
coverage_given: CoverageSettings {
limit: options.coverage_limit,
report,
},
..ReadOptions::default()
};
let app = match read_app_cached(
&options.path,
&BTreeMap::new(),
&read_options,
&mut ReadCache::default(),
) {
Ok(app) => app,
Err(error @ ReadError::CoverageReport { .. }) => {
say(&strings::unreadable_report(&error.to_string()));
return crate::USAGE;
}
Err(error) => {
say(&strings::not_an_app("check", &error.to_string()));
return crate::NOT_AN_APP;
}
};
if let RulesSource::Invalid(message) = &app.rules_source {
say(&strings::invalid_rules_file(message));
}
if let Some(message) = &app.coverage_ignored {
say(&strings::coverage_ignored(message));
}
let findings = &app.findings;
let fails = findings.problems() > 0 || (options.fail_on_warnings && findings.warnings() > 0);
let report = match options.format {
Format::Json => {
let mut json = serde_json::to_string_pretty(findings).expect("findings are plain data");
json.push('\n');
json
}
Format::Text | Format::Github => {
let mut text = String::new();
for line in findings
.list
.iter()
.flat_map(|finding| lines(finding, &app))
{
text.push_str(&printable(&match options.format {
Format::Github => line.github(),
_ => line.text(),
}));
text.push('\n');
}
let (rules, checks) = findings.rules_and_checks();
text.push_str(&strings::summary(
findings.problems(),
findings.warnings(),
app.modules.len(),
rules,
checks,
));
text.push('\n');
text
}
};
let mut out = std::io::stdout().lock();
match out.write_all(report.as_bytes()).and_then(|()| out.flush()) {
Err(error) if error.kind() != std::io::ErrorKind::BrokenPipe => {
say(&strings::not_written(&error.to_string()));
return crate::NOT_WRITTEN;
}
_ => {}
}
match (fails, app.rules.fail_ci) {
(false, _) => crate::PASSED,
(true, true) => crate::FAILED,
(true, false) => {
say(strings::CI_FAIL_OFF);
crate::PASSED
}
}
}
struct Line {
severity: Severity,
check: Check,
path: String,
line: Option<u32>,
file: bool,
sentence: String,
}
impl Line {
fn text(&self) -> String {
let place = match self.line {
Some(line) => format!("{}:{line}", self.path),
None => self.path.clone(),
};
format!(
"{place}: {}: {}",
strings::severity(self.severity),
self.sentence
)
}
fn github(&self) -> String {
let command = match self.severity {
Severity::Problem => "error",
Severity::Warning => "warning",
};
let mut properties = Vec::new();
if self.file {
properties.push(format!("file={}", property(&self.path)));
if let Some(line) = self.line {
properties.push(format!("line={line}"));
}
}
properties.push(format!("title={}", property(strings::check(self.check))));
format!(
"::{command} {}::{}",
properties.join(","),
data(&self.sentence)
)
}
}
fn lines(finding: &Finding, app: &AppSnapshot) -> Vec<Line> {
let module = finding.module.as_deref().unwrap_or_default();
let line = |path: &str, line: Option<u32>, file: bool, sentence: String| Line {
severity: finding.severity,
check: finding.check,
path: path.to_owned(),
line,
file,
sentence,
};
let folder = app
.module(module)
.map(|found| app.path_shown(&found.dir))
.unwrap_or_else(|| module.to_owned());
let in_folder = |sentence: String| line(&folder, None, false, sentence);
match &finding.detail {
Detail::CircularUse { files } => {
let sentence = strings::circle(files);
let first = app
.circle_neighbour(module)
.and_then(|to| app.link(module, to))
.and_then(|link| link.refs.first());
vec![match first {
Some(first) => line(&first.file, Some(first.line), true, sentence),
None => in_folder(sentence),
}]
}
Detail::ReadsInternals { of, .. } => {
let mut files: BTreeMap<&str, (u32, Vec<&str>)> = BTreeMap::new();
let refs = app.link(module, of).map(|link| link.refs.as_slice());
for reference in refs.unwrap_or_default().iter().filter(|r| r.internal) {
let (first, classes) = files
.entry(&reference.file)
.or_insert((reference.line, Vec::new()));
*first = (*first).min(reference.line);
if !classes.contains(&reference.class.as_str()) {
classes.push(&reference.class);
}
}
let folders = &app.rules.public_folders;
let accepted = app.module(of).is_some_and(|of| of.surface.is_some());
files
.into_iter()
.map(|(file, (first, mut classes))| {
classes.sort_unstable();
let sentence =
strings::reads_internals(module, of, &classes, folders, accepted);
line(file, Some(first), true, sentence)
})
.collect()
}
Detail::InvalidManifest {
file,
message,
line: at,
} => {
let at = u32::try_from(*at).ok();
vec![line(
file,
at,
true,
strings::invalid_manifest(module, message),
)]
}
Detail::NoManifestName { file } => {
vec![line(file, None, true, strings::no_manifest_name(module))]
}
Detail::NamespaceMismatch { namespace, folder } => vec![line(
folder,
None,
false,
strings::namespace_mismatch(module, namespace, folder),
)],
Detail::StaleStatus { entry, file } => {
vec![line(file, None, true, strings::stale_status(entry, file))]
}
Detail::LowCoverage { percent, limit } => {
vec![in_folder(strings::low_coverage(module, *percent, *limit))]
}
Detail::MissingTranslations {
locale,
keys,
files,
..
} => vec![in_folder(strings::missing_translations(
module, locale, *keys, *files,
))],
}
}
fn data(text: &str) -> String {
text.replace('%', "%25")
.replace('\r', "%0D")
.replace('\n', "%0A")
}
fn property(text: &str) -> String {
data(text).replace(':', "%3A").replace(',', "%2C")
}