First draft

This commit is contained in:
Dandaedre 2024-11-05 17:47:44 +01:00
parent 11c4989291
commit ceb37562fa
5 changed files with 111 additions and 0 deletions

5
.gitignore vendored
View File

@ -14,3 +14,8 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# Added by cargo
/target

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "rust_autobadges"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.92"
regex = "1.11.1"
xshell = "0.2.6"

1
badges/release.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="94" height="18" role="img" aria-label="release: v0.1.0"><title>release: v0.1.0</title><linearGradient id="bms-162829b957040378" 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-162829b957040378"><rect width="94" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-162829b957040378)"><rect width="49" height="18" fill="#555"/><rect x="49" width="45" height="18" fill="#fe7d37"/><rect width="94" height="18" fill="url(#bms-162829b957040378)"/></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="255" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="390">release</text><text x="255" y="130" transform="scale(.1)" fill="#fff" textLength="390">release</text><text aria-hidden="true" x="705" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="350">v0.1.0</text><text x="705" y="130" transform="scale(.1)" fill="#fff" textLength="350">v0.1.0</text></g></svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

1
badges/test.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="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>

After

Width:  |  Height:  |  Size: 1.3 KiB

95
src/main.rs Normal file
View File

@ -0,0 +1,95 @@
use std::{fs::File, io::Write, process::Command};
use regex::Regex;
use xshell::Shell;
fn main() -> anyhow::Result<()> {
let sh = Shell::new()?;
sh.create_dir("badges")?;
// run_test(&sh)?;
run_version(&sh)?;
run_coverage(&sh)?;
Ok(())
}
fn run_coverage(sh: &Shell) -> Result<(), anyhow::Error> {
let _ = sh;
// 84.78% coverage, 78/92 lines covered, -15.22% change in coverage
println!("Mesuring coverage...");
// cmd!(sh, "cargo tarpaulin").quiet().ignore_stdout().run()?;
let output = Command::new("cargo")
.args(vec!["tarpaulin", "-o", "html"])
.output()?;
match &output.status.success() {
true => write_badge("test", "passing", "green", "test")?,
_ => write_badge("test", "failing", "red", "test")?,
};
let test = String::from_utf8(output.stdout)?;
// println!("{}", test);
let re = Regex::new(r"(\d+.\d+)% coverage, ").unwrap();
let coverage = re.captures(&test);
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"
} else {
"red"
};
write_badge("test-coverage", &format!("{:.0}%", v), color, "coverage")?;
println!(" Done ! ({:?}%)", v);
} else {
write_badge("test-coverage", "N/A", "red", "coverage")?;
println!(" Failed! (Couldn't find coverage)");
}
Ok(())
}
fn run_version(sh: &Shell) -> Result<(), anyhow::Error> {
println!("Finding version...");
let manifest = sh.read_file("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")?;
println!(" Done!");
Ok(())
}
// fn run_test(sh: &Shell) -> Result<(), anyhow::Error> {
// print!("Running tests...");
// let test_status = cmd!(sh, "cargo test --").ignore_stdout().quiet().run();
// if let Err(e) = test_status {
// println!("failed!");
// println!("{:?}", e);
// write_badge("test", "failing", "test")?;
// } else {
// println!("succes!");
// write_badge("test", "passed", "test")?;
// };
// println!("done!");
// Ok(())
// }
fn write_badge(name: &str, value: &str, color: &str, test_name: &str) -> Result<(), anyhow::Error> {
let output = Command::new("badge-maker")
.args(vec!["-s", "plastic", name, value, "-c", color])
.output()?;
let mut f = File::create(format!("badges/{}.svg", 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(())
}