diff --git a/Cargo.toml b/Cargo.toml
index 5bbceb2..94eabf9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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]
diff --git a/badges/coverage.svg b/badges/coverage.svg
new file mode 100644
index 0000000..e6a15fe
--- /dev/null
+++ b/badges/coverage.svg
@@ -0,0 +1 @@
+
diff --git a/badges/test.svg b/badges/test.svg
index 103267b..a7859aa 100644
--- a/badges/test.svg
+++ b/badges/test.svg
@@ -1 +1 @@
-
+
diff --git a/badges/version.svg b/badges/version.svg
new file mode 100644
index 0000000..c28c459
--- /dev/null
+++ b/badges/version.svg
@@ -0,0 +1 @@
+
diff --git a/src/main.rs b/src/main.rs
index 66500d8..91fa7f2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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(())
}