Skip to content

Commit

Permalink
feat(l1, l2, levm): print loc diff in loc report message (#1342)
Browse files Browse the repository at this point in the history
Resolves #1300
  • Loading branch information
ilitteri authored Nov 28, 2024
1 parent 3447b46 commit 2036de8
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 19 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/loc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Weekly LoC
name: Daily Lines of Code Report

on:
schedule:
Expand All @@ -25,10 +25,30 @@ jobs:
- name: Add Rust Cache
uses: Swatinem/rust-cache@v2

- name: Restore cache
id: cache-loc-report
uses: actions/cache@v3
with:
path: loc_report.json
key: loc-report-${{ github.ref_name }}
restore-keys: |
loc-report-
- name: Rename cached loc_report.json to loc_report.json.old
if: steps.cache-loc-report.outputs.cache-hit == 'true'
run: mv loc_report.json loc_report.json.old

- name: Generate the loc report
run: |
make loc
- name: Save new loc_report.json to cache
if: success()
uses: actions/cache@v3
with:
path: loc_report.json
key: loc-report-${{ github.ref_name }}

- name: Post results in summary
run: |
echo "# `ethrex` lines of code report" >> $GITHUB_STEP_SUMMARY
Expand Down
3 changes: 2 additions & 1 deletion cmd/loc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ edition.workspace = true

[dependencies]
tokei = "12.1.2"
colored = "2.1.0"
serde = "1.0.215"
serde_json = "1.0.133"
110 changes: 93 additions & 17 deletions cmd/loc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tokei::{Config, LanguageType, Languages};

const CARGO_MANIFEST_DIR: &str = std::env!("CARGO_MANIFEST_DIR");

#[derive(Default, Serialize, Deserialize, Clone, Copy)]
pub struct LinesOfCodeReport {
ethrex: usize,
ethrex_l1: usize,
ethrex_l2: usize,
levm: usize,
}

fn main() {
let ethrex = PathBuf::from(CARGO_MANIFEST_DIR).join("../../");
let levm = PathBuf::from(CARGO_MANIFEST_DIR).join("../../crates/vm");
Expand All @@ -22,41 +31,108 @@ fn main() {
languages.get_statistics(&[ethrex_l2], &["tests"], &config);
let ethrex_l2_loc = &languages.get(&LanguageType::Rust).unwrap();

let new_report = LinesOfCodeReport {
ethrex: ethrex_loc.code,
ethrex_l1: ethrex_loc.code - ethrex_l2_loc.code - levm_loc.code,
ethrex_l2: ethrex_l2_loc.code,
levm: levm_loc.code,
};

std::fs::write(
"loc_report.json",
serde_json::to_string(&new_report).unwrap(),
)
.expect("loc_report.json could not be written");

let old_report: LinesOfCodeReport = std::fs::read_to_string("loc_report.json.old")
.map(|s| serde_json::from_str(&s).unwrap())
.unwrap_or_default();

std::fs::write(
"loc_report_slack.txt",
slack_message(ethrex_loc.code, ethrex_l2_loc.code, levm_loc.code),
slack_message(old_report, new_report),
)
.unwrap();
std::fs::write(
"loc_report_github.txt",
github_step_summary(ethrex_loc.code, ethrex_l2_loc.code, levm_loc.code),
github_step_summary(old_report, new_report),
)
.unwrap();
}

fn slack_message(ethrex_loc: usize, ethrex_l2_loc: usize, levm_loc: usize) -> String {
fn slack_message(old_report: LinesOfCodeReport, new_report: LinesOfCodeReport) -> String {
let ethrex_l1_diff = new_report.ethrex_l1.abs_diff(old_report.ethrex_l1);
let ethrex_l2_diff = new_report.ethrex_l2.abs_diff(old_report.ethrex_l2);
let levm_diff = new_report.levm.abs_diff(old_report.levm);
let ethrex_diff_total = ethrex_l1_diff + ethrex_l2_diff + levm_diff;

format!(
r#"*ethrex L1:* {}\n*ethrex L2:* {}\n*levm:* {}\n*ethrex (total):* {}"#,
ethrex_loc - ethrex_l2_loc - levm_loc,
ethrex_l2_loc,
levm_loc,
ethrex_loc,
r#"*ethrex L1:* {} {}\n*ethrex L2:* {} {}\n*levm:* {} {}\n*ethrex (total):* {} {}"#,
new_report.ethrex_l1,
if new_report.ethrex > old_report.ethrex {
format!("(+{ethrex_l1_diff})")
} else {
format!("(-{ethrex_l1_diff})")
},
new_report.ethrex_l2,
if new_report.ethrex_l2 > old_report.ethrex_l2 {
format!("(+{ethrex_l2_diff})")
} else {
format!("(-{ethrex_l2_diff})")
},
new_report.levm,
if new_report.levm > old_report.levm {
format!("(+{levm_diff})")
} else {
format!("(-{levm_diff})")
},
new_report.ethrex,
if new_report.ethrex > old_report.ethrex {
format!("(+{ethrex_diff_total})")
} else {
format!("(-{ethrex_diff_total})")
},
)
}

fn github_step_summary(ethrex_loc: usize, ethrex_l2_loc: usize, levm_loc: usize) -> String {
fn github_step_summary(old_report: LinesOfCodeReport, new_report: LinesOfCodeReport) -> String {
let ethrex_l1_diff = new_report.ethrex_l1.abs_diff(old_report.ethrex_l1);
let ethrex_l2_diff = new_report.ethrex_l2.abs_diff(old_report.ethrex_l2);
let levm_diff = new_report.levm.abs_diff(old_report.levm);
let ethrex_diff_total = ethrex_l1_diff + ethrex_l2_diff + levm_diff;

format!(
r#"```
ethrex loc summary
====================
ethrex L1: {}
ethrex L2: {}
levm: {}
ethrex (total): {}
ethrex L1: {} {}
ethrex L2: {} {}
levm: {} ({})
ethrex (total): {} {}
```"#,
ethrex_loc - ethrex_l2_loc - levm_loc,
ethrex_l2_loc,
levm_loc,
ethrex_loc,
new_report.ethrex_l1,
if new_report.ethrex > old_report.ethrex {
format!("(+{ethrex_l1_diff})")
} else {
format!("(-{ethrex_l1_diff})")
},
new_report.ethrex_l2,
if new_report.ethrex_l2 > old_report.ethrex_l2 {
format!("(+{ethrex_l2_diff})")
} else {
format!("(-{ethrex_l2_diff})")
},
new_report.levm,
if new_report.levm > old_report.levm {
format!("(+{levm_diff})")
} else {
format!("(-{levm_diff})")
},
new_report.ethrex,
if new_report.ethrex > old_report.ethrex {
format!("(+{ethrex_diff_total})")
} else {
format!("(-{ethrex_diff_total})")
},
)
}

0 comments on commit 2036de8

Please sign in to comment.