Skip to content

Commit

Permalink
Merge branch 'report-code-cli' into test-report-code-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaIalangi authored Aug 5, 2024
2 parents 81fd978 + 7bd2b57 commit 4484ca9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ permissions:
jobs:
contracts:
name: Contracts
uses: multiversx/mx-sc-actions/.github/workflows/contracts.yml@781c32994b8c6673b53ba204187e59978efe8fcb
uses: multiversx/mx-sc-actions/.github/workflows/contracts.yml@3ccc32c6283f3e7106b324b742d10aae28827c15
with:
rust-toolchain: stable
path-to-sc-meta: framework/meta
Expand Down
5 changes: 3 additions & 2 deletions framework/meta/src/cli/cli_args_standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ pub struct CodeReportArgs {
#[arg(short, long, verbatim_doc_comment)]
pub format: Option<OutputFormat>,

/// Compare two reports. Output available only for Markdown format
/// Compares 2 reports in JSON format and the output is printed in Markdown format.
/// If one argument with JSON format is given then it compares with itself.
#[arg(short, long, verbatim_doc_comment)]
pub compare: Option<PathBuf>,
pub compare: Vec<PathBuf>,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
Expand Down
6 changes: 5 additions & 1 deletion framework/meta/src/cmd/code_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub fn code_report(args: &CodeReportArgs) {
path,
args.output.to_str().unwrap(),
args.format.as_ref().unwrap_or(&OutputFormat::default()),
args.compare.clone().unwrap_or_default().to_str().unwrap(),
args.compare
.clone()
.into_iter()
.map(|path| path.to_string_lossy().into_owned())
.collect(),
);
}
4 changes: 2 additions & 2 deletions framework/meta/src/cmd/code_report/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ pub(crate) fn parse_into_code_report_json(
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)
format!("{} :arrow_right: {} :red_circle:", compared_size, size)
},
std::cmp::Ordering::Less => {
format!("{} :arrow-right: {} :green-circle:", compared_size, size)
format!("{} :arrow_right: {} :green_circle:", compared_size, size)
},
std::cmp::Ordering::Equal => {
format!("{}", size)
Expand Down
44 changes: 32 additions & 12 deletions framework/meta/src/cmd/code_report/generate_report.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fs::{read_dir, File},
io::Write,
io::{BufReader, Write},
path::PathBuf,
process::Command,
};
Expand All @@ -13,16 +13,31 @@ use multiversx_sc_meta_lib::{

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()]);
pub fn run_code_report(
path: &str,
output_path: &str,
output_format: &OutputFormat,
compare: Vec<String>,
) {
let reports = if compare.is_empty() {
generate_new_report(path)
} else {
let file = File::open(&compare[0]).expect("file not found");
let reader = BufReader::new(file);

serde_json::from_reader(reader).expect("Cannot deserialize")
};

let reports = extract_report(directors);
let mut compared_to = String::new();
if compare.len() == 2 {
compare[1].clone_into(&mut compared_to);
}

let mut file = create_file(output_path);

match output_format {
OutputFormat::Markdown => {
let mut render_code_report = CodeReportRender::new(&mut file, compare, &reports);
let mut render_code_report = CodeReportRender::new(&mut file, &compared_to, &reports);
render_code_report.render_report();
},
OutputFormat::Json => {
Expand All @@ -32,6 +47,12 @@ pub fn run_code_report(path: &str, output_path: &str, output_format: &OutputForm
};
}

fn generate_new_report(path: &str) -> Vec<CodeReportJson> {
let directors = RelevantDirectories::find_all(path, &["".to_owned()]);

extract_report(directors)
}

fn extract_report(directors: RelevantDirectories) -> Vec<CodeReportJson> {
let mut reports: Vec<CodeReportJson> = Vec::new();

Expand Down Expand Up @@ -99,14 +120,13 @@ fn create_file(file_path: &str) -> File {
File::create(file_path).expect("could not write report file")
}

fn sanitize_output_path_from_report(reports: &mut Vec<CodeReportJson>) {
for report in reports {
let new_path = report
fn sanitize_output_path_from_report(reports: &mut [CodeReportJson]) {
reports.iter_mut().for_each(|report| {
report.path = report
.path
.split('/')
.last()
.unwrap_or_else(|| &report.path);

report.path = new_path.to_owned();
}
.unwrap_or(&report.path)
.to_string();
})
}
15 changes: 6 additions & 9 deletions framework/meta/src/cmd/code_report/render_code_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ impl<'a> CodeReportRender<'a> {
}

fn render_header(&mut self) {
if !self.compared_path_file.is_empty() {
self.writeln(format!(
"Contract comparison with {}",
self.compared_path_file
))
}

self.writeln("| Path                                                         |                                     size |                  has-allocator |                     has-format |");
self.writeln("| :-- | --: | --: | --: |");
}
Expand Down Expand Up @@ -95,8 +88,12 @@ impl<'a> CodeReportRender<'a> {
// TODO this is only one time compared. Decide whether to exist or not
parse_into_code_report_json(&mut compared_file_reader)
} else {
serde_json::from_reader(compared_file_reader)
.unwrap_or_else(|_| panic!("Cannot deserialize into code report structure."))
serde_json::from_reader(compared_file_reader).unwrap_or_else(|_| {
self.render_reports();
self.writeln("\n:warning: Could not compare the 2 versions because the deserialization process into the code report structure failed. :warning:");

Vec::new()
})
};

for report in self.reports.iter() {
Expand Down

0 comments on commit 4484ca9

Please sign in to comment.