Skip to content

Commit

Permalink
cli code report - impl changes, integrate in a structure
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaIalangi committed Jul 20, 2024
1 parent 02a8a64 commit b0a2d11
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 265 deletions.
1 change: 0 additions & 1 deletion contracts/benchmarks/report.json

This file was deleted.

6 changes: 3 additions & 3 deletions framework/meta/src/cli/cli_args_standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ pub struct CodeReportArgs {

/// Output file path
#[arg(short, long, verbatim_doc_comment)]
pub output: Option<String>,
pub output: PathBuf,

/// Output format
#[arg(short, long, verbatim_doc_comment)]
pub format: Option<OutputFormat>,

/// Compare
/// Compare two reports. Output available only for Markdown format
#[arg(short, long, verbatim_doc_comment)]
pub compare: Option<String>,
pub compare: Option<PathBuf>,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
Expand Down
5 changes: 3 additions & 2 deletions framework/meta/src/cmd/code_report.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod compare;
pub mod generate_report;
pub mod render_code_report;

Expand All @@ -14,8 +15,8 @@ pub fn code_report(args: &CodeReportArgs) {

run_code_report(
path,
args.output.as_ref().unwrap_or(&"./report.md".to_string()),
args.output.to_str().unwrap(),
args.format.as_ref().unwrap_or(&OutputFormat::default()),
&args.compare.clone().unwrap_or_default(),
args.compare.clone().unwrap_or_default().to_str().unwrap(),
);
}
87 changes: 87 additions & 0 deletions framework/meta/src/cmd/code_report/compare.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::io::BufRead;

use multiversx_sc_meta_lib::code_report_json::CodeReportJson;

pub(crate) fn parse_into_code_report_json(
compared_file_reader: &mut dyn BufRead,
) -> Vec<CodeReportJson> {
let lines = compared_file_reader.lines().skip(2);

let mut compared_reports: Vec<CodeReportJson> = Vec::new();

for line in lines {
match line {
Ok(l) => {
let columns: Vec<String> = l
.split('|')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect();

if columns.len() == 4 {
compared_reports.push(CodeReportJson {
path: columns[0]
.split('/')
.last()
.unwrap_or_else(|| &columns[0])
.to_owned(),
size: columns[1].parse::<usize>().unwrap(),
has_allocator: columns[2].parse::<bool>().unwrap(),
has_panic: columns[3].to_owned(),
})
}
},
Err(_) => return compared_reports,
}
}

compared_reports
}

pub(crate) fn size_status_after_comparing(size: usize, compared_size: usize) -> String {
match size.cmp(&compared_size) {
std::cmp::Ordering::Greater => {
format!("{} :arrow-right: {} :red-circle:", compared_size, size)
},
std::cmp::Ordering::Less => {
format!("{} :arrow-right: {} :green-circle:", compared_size, size)
},
std::cmp::Ordering::Equal => {
format!("{}", size)
},
}
}

pub(crate) fn allocator_status_after_comparing(
has_allocator: bool,
compared_has_allocator: bool,
) -> String {
if compared_has_allocator == has_allocator {
return format!("{}", has_allocator);
}

let allocator_status = format!("{} :arrow-right: {}", compared_has_allocator, has_allocator);

if !has_allocator {
format!("{allocator_status} :green-circle:")
} else {
format!("{allocator_status} :red-circle:")
}
}

pub(crate) fn panic_status_after_comparing(
has_panic: &String,
compared_has_panic: &String,
) -> String {
if has_panic == compared_has_panic {
return has_panic.to_string();
}

let panic_status = format!("{} :arrow-right: {}", compared_has_panic, has_panic);

if has_panic == "none" {
return format!("{panic_status} :green-circle:");
}

panic_status
}
22 changes: 13 additions & 9 deletions framework/meta/src/cmd/code_report/generate_report.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
fs::{self, read_dir, File},
fs::{read_dir, File},
io::Write,
path::PathBuf,
process::Command,
};
Expand All @@ -10,26 +11,25 @@ use multiversx_sc_meta_lib::{
self, code_report_json::CodeReportJson, mxsc_file_json::MxscFileJson,
};

use super::render_code_report::render_report;
use super::render_code_report::CodeReportRender;

pub fn run_code_report(path: &str, output_path: &str, output_format: &OutputFormat, compare: &str) {
let directors = RelevantDirectories::find_all(path, &["".to_owned()]);

let reports = extract_report(directors);
let mut output = String::new();

let mut file = create_file(output_path);

match output_format {
OutputFormat::Markdown => {
render_report(&mut output, &reports, compare);
let mut render_code_report = CodeReportRender::new(&mut file, compare, &reports);
render_code_report.render_report();
},
OutputFormat::Json => {
output = serde_json::to_string(&reports).unwrap();
let json_output = serde_json::to_string(&reports).unwrap();
file.write_all(json_output.as_bytes()).unwrap();
},
};

let Ok(_) = fs::write(output_path, output) else {
return;
};
}

fn extract_report(directors: RelevantDirectories) -> Vec<CodeReportJson> {
Expand Down Expand Up @@ -92,3 +92,7 @@ fn extract_reports(path: &PathBuf, reports: &mut Vec<CodeReportJson>) {
reports.push(data.report.code_report);
}
}

fn create_file(file_path: &str) -> File {
File::create(file_path).expect("could not write report file")
}
Loading

0 comments on commit b0a2d11

Please sign in to comment.