diff --git a/.github/workflows/loc.yaml b/.github/workflows/loc.yaml index 4724c3f25..44a58b929 100644 --- a/.github/workflows/loc.yaml +++ b/.github/workflows/loc.yaml @@ -1,4 +1,4 @@ -name: Weekly LoC +name: Daily Lines of Code Report on: schedule: @@ -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 diff --git a/cmd/loc/Cargo.toml b/cmd/loc/Cargo.toml index 55addc9d9..8c81c97b3 100644 --- a/cmd/loc/Cargo.toml +++ b/cmd/loc/Cargo.toml @@ -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" diff --git a/cmd/loc/src/main.rs b/cmd/loc/src/main.rs index 3eb15c074..506ea752d 100644 --- a/cmd/loc/src/main.rs +++ b/cmd/loc/src/main.rs @@ -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"); @@ -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})") + }, ) }