introduce dynamic generation of badges

This commit is contained in:
Sam Reis 2018-02-14 10:49:08 +11:00
parent 4bdebcaaad
commit 8b7c89bedd
11 changed files with 56 additions and 43 deletions

View file

@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Sam Rijs <srijs@airpost.net>"]
[dependencies]
badge = "0.1.0"
base64 = "0.9.0"
failure = "0.1.1"
futures = "0.1.18"

View file

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="144" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="144" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h85v20H0z"/><path fill="#dfb317" d="M85 0h59v20H85z"/><path fill="url(#b)" d="M0 0h144v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"><text x="435" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="750">dependencies</text><text x="435" y="140" transform="scale(.1)" textLength="750">dependencies</text><text x="1135" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="490">outdated</text><text x="1135" y="140" transform="scale(.1)" textLength="490">outdated</text></g> </svg>

Before

Width:  |  Height:  |  Size: 974 B

View file

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="146" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="146" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h85v20H0z"/><path fill="#9f9f9f" d="M85 0h61v20H85z"/><path fill="url(#b)" d="M0 0h146v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"><text x="435" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="750">dependencies</text><text x="435" y="140" transform="scale(.1)" textLength="750">dependencies</text><text x="1145" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">unknown</text><text x="1145" y="140" transform="scale(.1)" textLength="510">unknown</text></g> </svg>

Before

Width:  |  Height:  |  Size: 972 B

View file

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="152" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="152" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h85v20H0z"/><path fill="#4c1" d="M85 0h67v20H85z"/><path fill="url(#b)" d="M0 0h152v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"><text x="435" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="750">dependencies</text><text x="435" y="140" transform="scale(.1)" textLength="750">dependencies</text><text x="1175" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="570">up to date</text><text x="1175" y="140" transform="scale(.1)" textLength="570">up to date</text></g> </svg>

Before

Width:  |  Height:  |  Size: 975 B

View file

@ -3,6 +3,7 @@
#![feature(ip_constructors)]
#![feature(proc_macro)]
extern crate badge;
extern crate base64;
#[macro_use] extern crate failure;
#[macro_use] extern crate futures;

View file

@ -1,12 +1,3 @@
//pub mod templates;
pub static BADGE_UPTODATE_SVG: &'static [u8; 975] =
include_bytes!("../../assets/badges/up-to-date.svg");
pub static BADGE_OUTDATED_SVG: &'static [u8; 974] =
include_bytes!("../../assets/badges/outdated.svg");
pub static BADGE_UNKNOWN_SVG: &'static [u8; 972] =
include_bytes!("../../assets/badges/unknown.svg");
pub static STATIC_STYLE_CSS: &'static str =
include_str!(concat!(env!("OUT_DIR"), "/style.css"));
pub static STATIC_FAVICON_PNG: &'static [u8; 1338] =

View file

@ -148,7 +148,7 @@ impl Server {
fn status_format_analysis(analysis_outcome: Option<AnalyzeDependenciesOutcome>, format: StatusFormat, repo_path: RepoPath) -> Response {
match format {
StatusFormat::Svg =>
views::status_svg(analysis_outcome),
views::badge::response(analysis_outcome.as_ref()),
StatusFormat::Html =>
views::html::status::render(analysis_outcome, repo_path)
}

43
src/server/views/badge.rs Normal file
View file

@ -0,0 +1,43 @@
use badge::{Badge, BadgeOptions};
use hyper::Response;
use hyper::header::ContentType;
use ::engine::AnalyzeDependenciesOutcome;
pub fn svg(analysis_outcome: Option<&AnalyzeDependenciesOutcome>) -> Vec<u8> {
let opts = match analysis_outcome {
Some(outcome) => {
if outcome.any_outdated() {
BadgeOptions {
subject: "dependencies".into(),
status: "outdated".into(),
color: "#dfb317".into()
}
} else {
BadgeOptions {
subject: "dependencies".into(),
status: "up to date".into(),
color: "#4c1".into()
}
}
},
None => {
BadgeOptions {
subject: "dependencies".into(),
status: "unknown".into(),
color: "#9f9f9f".into()
}
}
};
Badge::new(opts)
.expect("failed to create badge")
.to_svg()
.into_bytes()
}
pub fn response(analysis_outcome: Option<&AnalyzeDependenciesOutcome>) -> Response {
Response::new()
.with_header(ContentType("image/svg+xml;charset=utf-8".parse().unwrap()))
.with_body(svg(analysis_outcome))
}

View file

@ -6,7 +6,8 @@ use ordermap::OrderMap;
use ::engine::AnalyzeDependenciesOutcome;
use ::models::crates::{CrateName, AnalyzedDependency, AnalyzedDependencies};
use ::models::repo::{RepoSite, RepoPath};
use ::server::assets;
use super::super::badge;
fn dependency_tables(crate_name: CrateName, deps: AnalyzedDependencies) -> Markup {
html! {
@ -125,13 +126,14 @@ fn render_success(analysis_outcome: AnalyzeDependenciesOutcome, repo_path: RepoP
let status_base_url = format!("{}/{}", &super::SELF_BASE_URL as &str, self_path);
let site_icon = get_site_icon(&repo_path.site);
let (hero_class, status_asset) = if analysis_outcome.any_outdated() {
("is-warning", assets::BADGE_OUTDATED_SVG.as_ref())
} else {
("is-success", assets::BADGE_UPTODATE_SVG.as_ref())
};
let status_badge = badge::svg(Some(&analysis_outcome));
let status_data_url = format!("data:image/svg+xml;base64,{}", Base64Display::standard(&status_badge));
let status_data_url = format!("data:image/svg+xml;base64,{}", Base64Display::standard(status_asset));
let hero_class = if analysis_outcome.any_outdated() {
"is-warning"
} else {
"is-success"
};
html! {
section class=(format!("hero {}", hero_class)) {

View file

@ -1,4 +1,2 @@
pub mod html;
mod status_svg;
pub use self::status_svg::status_svg;
pub mod badge;

View file

@ -1,20 +0,0 @@
use hyper::Response;
use hyper::header::ContentType;
use ::server::assets;
use ::engine::AnalyzeDependenciesOutcome;
pub fn status_svg(analysis_outcome: Option<AnalyzeDependenciesOutcome>) -> Response {
let mut response = Response::new()
.with_header(ContentType("image/svg+xml;charset=utf-8".parse().unwrap()));
if let Some(outcome) = analysis_outcome {
if outcome.any_outdated() {
response.set_body(assets::BADGE_OUTDATED_SVG.to_vec());
} else {
response.set_body(assets::BADGE_UPTODATE_SVG.to_vec());
}
} else {
response.set_body(assets::BADGE_UNKNOWN_SVG.to_vec());
}
response
}