diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..28e73ed --- /dev/null +++ b/build.rs @@ -0,0 +1,35 @@ +use std::{env, fs, path::Path, process::Command}; + +fn command_output(command: &str, args: &[&str]) -> String { + Command::new(command) + .args(args) + .output() + .ok() + .filter(|output| output.status.success()) + .map(|output| String::from_utf8_lossy(&output.stdout).trim().to_owned()) + .filter(|output| !output.is_empty()) + .unwrap_or_else(|| "unknown".to_owned()) +} + +fn main() { + let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR is set by Cargo"); + let version = env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "unknown".to_owned()); + let rustc = env::var("RUSTC").unwrap_or_else(|_| "rustc".to_owned()); + let rustc_version = command_output(&rustc, &["--version"]); + let target = env::var("TARGET").unwrap_or_else(|_| "unknown".to_owned()); + + println!("cargo:rustc-env=TG_BOT_RUSTC_VERSION={rustc_version}"); + println!("cargo:rustc-env=TG_BOT_TARGET={target}"); + + let version_text = format!( + "{}\nbuild-time: {}\ncommit: {}\ntarget: {}\nrustc: {}", + version, + command_output("date", &["-u", "+%Y-%m-%d %H:%M:%S UTC"]), + command_output("git", &["rev-parse", "--short", "HEAD"]), + target, + rustc_version, + ); + + fs::write(Path::new(&out_dir).join("version.txt"), version_text) + .expect("failed to write generated version information"); +} diff --git a/src/commands/version.rs b/src/commands/version.rs index 23f7a04..2f9797f 100644 --- a/src/commands/version.rs +++ b/src/commands/version.rs @@ -1,16 +1,29 @@ -use crate::{Context, Error}; -use tracing::{trace, debug}; use crate::messaging::trace_message; +use crate::{Context, Error}; +use tracing::{debug, trace}; + +const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/version.txt")); /// Get the bot's current version #[poise::command(slash_command, prefix_command)] pub async fn version(ctx: Context<'_>) -> Result<(), Error> { - trace!("version command called by user {} in guild {}", ctx.author().id.get(), ctx.guild_id().unwrap().get()); - trace!("Loading version from environment"); - let version = std::env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "Unknown".to_string()); - let msg = format!("Current version: {}", version); - trace_message(msg.clone(), ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await; + trace!( + "version command called by user {} in guild {}", + ctx.author().id.get(), + ctx.guild_id().unwrap().get() + ); + trace!("Loading embedded version information"); + let msg = format!("Current version:\n{VERSION}"); + trace_message( + msg.clone(), + ctx.channel_id().to_string(), + ctx.guild_id().unwrap().to_string(), + ) + .await; ctx.say(msg).await?; - debug!("Version command performed for user {} with version {}", ctx.author().name, version); + debug!( + "Version command performed for user {} with version information", + ctx.author().name + ); Ok(()) -} \ No newline at end of file +}