From 88b3df21c294303fd6bdeab98649f4e590b0424e Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Mon, 20 Nov 2017 10:12:10 +0100 Subject: [PATCH] Refactor build information and add more information #44 --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/build_config.rs | 22 ++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 8 ++++++-- src/resources/about_resource.rs | 3 ++- src/site.rs | 7 ++++--- templates/about.html | 2 +- templates/layout.html | 2 +- 9 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 src/build_config.rs diff --git a/Cargo.lock b/Cargo.lock index be7f670..0af78f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -579,7 +579,7 @@ dependencies = [ [[package]] name = "sausagewiki" -version = "0.1.0" +version = "0.1.0-dev" dependencies = [ "bart 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "bart_derive 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index bd8f95e..85834a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sausagewiki" -version = "0.1.0" +version = "0.1.0-dev" description = "A wiki engine" authors = ["Magnus Hoff "] license = "GPL-3.0" diff --git a/src/build_config.rs b/src/build_config.rs new file mode 100644 index 0000000..8afc1dd --- /dev/null +++ b/src/build_config.rs @@ -0,0 +1,22 @@ +#![allow(dead_code)] + +pub const PROJECT_NAME: &str = env!("CARGO_PKG_NAME"); + +lazy_static! { + pub static ref VERSION: String = || -> String { + #[allow(unused_mut)] + let mut components = Vec::<&'static str>::new(); + + #[cfg(debug_assertions)] + components.push("debug"); + + if components.len() > 0 { + format!("{} ({})", env!("CARGO_PKG_VERSION"), components.join(" ")) + } else { + env!("CARGO_PKG_VERSION").to_string() + } + }(); + + pub static ref HTTP_SERVER: String = + format!("{}/{}", PROJECT_NAME, VERSION.as_str()); +} diff --git a/src/lib.rs b/src/lib.rs index 0a823a1..b99926e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,7 @@ extern crate titlecase; use std::net::{IpAddr, SocketAddr}; mod assets; +mod build_config; mod db; mod merge; mod mimes; diff --git a/src/main.rs b/src/main.rs index 7d04f33..972dd56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,12 @@ +#[macro_use] extern crate lazy_static; extern crate clap; extern crate sausagewiki; use std::net::IpAddr; +mod build_config; +use build_config::*; + const DATABASE: &str = "DATABASE"; const TRUST_IDENTITY: &str = "trust-identity"; const ADDRESS: &str = "address"; @@ -11,8 +15,8 @@ const PORT: &str = "port"; fn args<'a>() -> clap::ArgMatches<'a> { use clap::{App, Arg}; - App::new(env!("CARGO_PKG_NAME")) - .version(env!("CARGO_PKG_VERSION")) + App::new(PROJECT_NAME) + .version(VERSION.as_str()) .about(env!("CARGO_PKG_DESCRIPTION")) .arg(Arg::with_name(DATABASE) .help("Sets the database file to use") diff --git a/src/resources/about_resource.rs b/src/resources/about_resource.rs index ab3bd0c..b6e8975 100644 --- a/src/resources/about_resource.rs +++ b/src/resources/about_resource.rs @@ -3,6 +3,7 @@ use hyper; use hyper::header::ContentType; use hyper::server::*; +use build_config; use mimes::*; use site::Layout; use web::{Resource, ResponseFuture}; @@ -59,7 +60,7 @@ struct Template<'a> { } impl<'a> Template<'a> { - fn pkg_version(&self) -> &str { env!("CARGO_PKG_VERSION") } + fn version(&self) -> &str { &build_config::VERSION } } impl Resource for AboutResource { diff --git a/src/site.rs b/src/site.rs index ff12c3c..c1517e4 100644 --- a/src/site.rs +++ b/src/site.rs @@ -10,13 +10,14 @@ use hyper::server::*; use hyper; use assets::{StyleCss, SearchJs}; +use build_config; use web::Lookup; use wiki_lookup::WikiLookup; lazy_static! { static ref TEXT_HTML: mime::Mime = "text/html;charset=utf-8".parse().unwrap(); static ref SERVER: Server = - Server::new(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))); + Server::new(build_config::HTTP_SERVER.as_str()); } header! { (XIdentity, "X-Identity") => [String] } @@ -33,8 +34,8 @@ impl<'a, T: 'a + fmt::Display> Layout<'a, T> { pub fn style_css_checksum(&self) -> &str { StyleCss::checksum() } pub fn search_js_checksum(&self) -> &str { SearchJs::checksum() } - pub fn pkg_name(&self) -> &str { env!("CARGO_PKG_NAME") } - pub fn pkg_version(&self) -> &str { env!("CARGO_PKG_VERSION") } + pub fn project_name(&self) -> &str { build_config::PROJECT_NAME } + pub fn version(&self) -> &str { build_config::VERSION.as_str() } } #[derive(BartDisplay)] diff --git a/templates/about.html b/templates/about.html index cb61443..ab70a6d 100644 --- a/templates/about.html +++ b/templates/about.html @@ -4,7 +4,7 @@
-

This site is running version {{pkg_version()}} of Sausagewiki, a simple, +

This site is running version {{version()}} of Sausagewiki, a simple, self-contained wiki engine.

Copyright © 2017 Magnus Hovland Hoff.

diff --git a/templates/layout.html b/templates/layout.html index a4874e2..6e06283 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -6,7 +6,7 @@ {{#base}}{{/base}} - + {{>search_input.html}}