Adds badge and new indicatif bar

This commit is contained in:
Dandaedre 2025-03-10 12:23:29 -05:00
parent 876415f075
commit df7cb80093
5 changed files with 119 additions and 29 deletions

View File

@ -5,5 +5,10 @@ edition = "2021"
[dependencies]
anyhow = "1.0.92"
clap = { version = "4.5.31", features = ["derive"] }
console = "0.15.11"
indicatif = "0.17.11"
regex = "1.11.1"
xshell = "0.2.6"
[lints.rust]

1
badges/coverage.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="116" height="18" role="img" aria-label="test-coverage: 0%"><title>test-coverage: 0%</title><linearGradient id="bms-1031a6650666d425" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-1031a6650666d425"><rect width="116" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-1031a6650666d425)"><rect width="87" height="18" fill="#555"/><rect x="87" width="29" height="18" fill="#e05d44"/><rect width="116" height="18" fill="url(#bms-1031a6650666d425)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="445" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="770">test-coverage</text><text x="445" y="130" transform="scale(.1)" fill="#fff" textLength="770">test-coverage</text><text aria-hidden="true" x="1005" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="190">0%</text><text x="1005" y="130" transform="scale(.1)" fill="#fff" textLength="190">0%</text></g></svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="80" height="18" role="img" aria-label="test: passed"><title>test: passed</title><linearGradient id="bms-cb03fce1f5245dce" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-cb03fce1f5245dce"><rect width="80" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-cb03fce1f5245dce)"><rect width="31" height="18" fill="#555"/><rect x="31" width="49" height="18" fill="#e05d44"/><rect width="80" height="18" fill="url(#bms-cb03fce1f5245dce)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="165" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="210">test</text><text x="165" y="130" transform="scale(.1)" fill="#fff" textLength="210">test</text><text aria-hidden="true" x="545" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="390">passed</text><text x="545" y="130" transform="scale(.1)" fill="#fff" textLength="390">passed</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="82" height="18" role="img" aria-label="test: passing"><title>test: passing</title><linearGradient id="bms-5402c39dce014bd3" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-5402c39dce014bd3"><rect width="82" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-5402c39dce014bd3)"><rect width="31" height="18" fill="#555"/><rect x="31" width="51" height="18" fill="#97ca00"/><rect width="82" height="18" fill="url(#bms-5402c39dce014bd3)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="165" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="210">test</text><text x="165" y="130" transform="scale(.1)" fill="#fff" textLength="210">test</text><text aria-hidden="true" x="555" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="410">passing</text><text x="555" y="130" transform="scale(.1)" fill="#fff" textLength="410">passing</text></g></svg>

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

1
badges/version.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="90" height="18" role="img" aria-label="version: 0.1.0"><title>version: 0.1.0</title><linearGradient id="bms-0431e50b7af7883f" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-0431e50b7af7883f"><rect width="90" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-0431e50b7af7883f)"><rect width="51" height="18" fill="#555"/><rect x="51" width="39" height="18" fill="#fe7d37"/><rect width="90" height="18" fill="url(#bms-0431e50b7af7883f)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="265" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="410">version</text><text x="265" y="130" transform="scale(.1)" fill="#fff" textLength="410">version</text><text aria-hidden="true" x="695" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="290">0.1.0</text><text x="695" y="130" transform="scale(.1)" fill="#fff" textLength="290">0.1.0</text></g></svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -1,33 +1,87 @@
//! Basic badge generation using badge-maker and cargo-tarpaulin
use console::style;
use indicatif::ProgressBar;
use std::time::{Duration, Instant};
use std::{fs::File, io::Write, process::Command};
use anyhow::Context;
use clap::Parser;
use regex::Regex;
use xshell::Shell;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct CLIArgs {
#[arg(short, long)]
skip_clean: bool,
#[arg(short, long, default_value_t = String::from("orange"))]
/// The color used to use on the version badge
version_color: String,
#[arg(short, long, default_value_t = String::from("red"))]
/// The color used to indicate failure on the test badges
failing_color: String,
#[arg(short='S', long, default_value_t = String::from("green"))]
/// The color used to indicate success on the badges
success_color: String,
/// The color used on a high coverage
#[arg(long, default_value_t = String::from("green"))]
high_coverage_color: String,
/// The color used on a mid coverage
#[arg(long, default_value_t = String::from("orange"))]
mid_coverage_color: String,
/// The color used on a low coverage
#[arg(long, default_value_t = String::from("red"))]
low_coverage_color: String,
/// The percent indicating a high coverage
#[arg(long, default_value_t = 1.)]
high_coverage_percent: f64,
/// The percent indicating a mid coverage
#[arg(long, default_value_t = 0.8)]
mid_coverage_percent: f64,
#[arg(short, long, default_value_t = String::from("badges"))]
/// The output directory where all the badges will be saved
output: String,
}
// TODO: No anyhow result, first print a conprehensive error, then return an error
// : To avoid code reuse, a custom pb with a behaviour on error would be nice
// TODO: Better than manual alignment
#[cfg(not(tarpaulin_include))]
fn main() -> anyhow::Result<()> {
let args = CLIArgs::parse();
let sh = Shell::new()?;
sh.create_dir("badges")?;
// run_test(&sh)?;
run_version(&sh)?;
run_coverage(&sh)?;
run_version(&sh, ProgressBar::new_spinner(), &args)?;
run_coverage(&sh, ProgressBar::new_spinner(), &args)?;
Ok(())
}
fn run_coverage(sh: &Shell) -> Result<(), anyhow::Error> {
fn run_coverage(sh: &Shell, pb: ProgressBar, args: &CLIArgs) -> Result<(), anyhow::Error> {
let _ = sh;
let started = Instant::now();
// 84.78% coverage, 78/92 lines covered, -15.22% change in coverage
println!("Mesuring coverage...");
let pb = pb.with_message(format!(
"{:>10} Mesuring coverage...",
style("[Coverage]").green().bold()
));
pb.enable_steady_tick(Duration::from_millis(100));
// cmd!(sh, "cargo tarpaulin").quiet().ignore_stdout().run()?;
let output = Command::new("cargo")
.args(vec!["tarpaulin", "-o", "html"])
.args(vec!["tarpaulin", "-o", "html", "--skip-clean"])
.output()?;
match &output.status.success() {
true => write_badge("test", "passing", "green", "test")?,
_ => write_badge("test", "failing", "red", "test")?,
true => write_badge("test", "passing", &args.success_color, "test", args)?,
false => write_badge("test", "failing", &args.failing_color, "test", args)?,
};
let test = String::from_utf8(output.stdout)?;
@ -37,33 +91,59 @@ fn run_coverage(sh: &Shell) -> Result<(), anyhow::Error> {
if let Some(c) = coverage {
let v = c.get(1).unwrap().as_str();
// println!("{}", v);
let v: f64 = v.parse()?;
let color = if v == 100. {
"green"
} else if v > 80. {
"orange"
let coverage_value: f64 = v.parse()?;
let color = if coverage_value >= args.high_coverage_percent {
&args.high_coverage_color
} else if coverage_value > args.mid_coverage_percent {
&args.mid_coverage_color
} else {
"red"
&args.low_coverage_color
};
write_badge("test-coverage", &format!("{:.0}%", v), color, "coverage")?;
println!(" Done ! ({:?}%)", v);
write_badge(
"test-coverage",
&format!("{coverage_value:.0}%"),
color,
"coverage",
args,
)?;
pb.finish_with_message(format!(
"{:>10} Found {coverage_value:.2}% - Badge done ({:.2}s)",
style("[Coverage]").bold().green(),
started.elapsed().as_secs_f32()
));
} else {
write_badge("test-coverage", "N/A", "red", "coverage")?;
println!(" Failed! (Couldn't find coverage)");
write_badge("test-coverage", "N/A", "red", "coverage", args)?;
pb.finish_with_message(format!(
"{:<10} Couldn't find coverage - Badge failed ({:.2}s)",
style("[Coverage]").bold().red(),
started.elapsed().as_secs_f32()
));
}
Ok(())
}
fn run_version(sh: &Shell) -> Result<(), anyhow::Error> {
println!("Finding version...");
fn run_version(sh: &Shell, pb: ProgressBar, args: &CLIArgs) -> Result<(), anyhow::Error> {
let started = Instant::now();
let pb = pb.with_message(format!(
"{:>10} Finding vesion...",
style("[Version]").bold().green()
));
// println!("Finding version...");
let manifest = sh.read_file("Cargo.toml").context("Opening Cargo.toml")?;
let version = manifest
.split_once("version = \"")
.and_then(|it| it.1.split_once('\"'))
.map(|it| it.0)
.ok_or_else(|| anyhow::format_err!("can't find version field in the manifest"))?;
write_badge("version", version, "orange", "version").context("Writing version badge")?;
println!(" Done!");
write_badge("version", version, &args.version_color, "version", args)
.context("Writing version badge")?;
// println!(" Done!");
pb.finish_with_message(format!(
"{:>10} Found {version} - Badge done ({}ms).",
style("[Version]").bold().green(),
started.elapsed().as_millis()
));
Ok(())
}
@ -82,16 +162,19 @@ fn run_version(sh: &Shell) -> Result<(), anyhow::Error> {
// Ok(())
// }
fn write_badge(name: &str, value: &str, color: &str, test_name: &str) -> Result<(), anyhow::Error> {
/// Write the badge to a specific directory using badge-maker
fn write_badge(
name: &str,
value: &str,
color: &str,
test_name: &str,
args: &CLIArgs,
) -> Result<(), anyhow::Error> {
let output = Command::new("badge-maker")
.args(vec!["-s", "plastic", name, value, "-c", color])
.output()
.context("Running badge-maker to export the badge")?;
let mut f = File::create(format!("badges/{}.svg", test_name)).unwrap();
let mut f = File::create(format!("{}/{}.svg", args.output, test_name)).unwrap();
f.write_all(&output.stdout)?;
// if let Ok(badge_out) = cmd!(sh, "badge-maker -s plastic build failed -c red")
// .quiet()
// .run()
// {};
Ok(())
}