1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use chrono::{DateTime, FixedOffset, Months, Utc};
use console::style;
use once_cell::sync::Lazy;

use crate::env::RTX_HIDE_UPDATE_WARNING;

pub mod built_info {
    include!(concat!(env!("OUT_DIR"), "/built.rs"));
}

pub static BUILD_TIME: Lazy<DateTime<FixedOffset>> =
    Lazy::new(|| DateTime::parse_from_rfc2822(built_info::BUILT_TIME_UTC).unwrap());

#[allow(dead_code)]
pub fn init() {
    if !*RTX_HIDE_UPDATE_WARNING
        && BUILD_TIME.checked_add_months(Months::new(12)).unwrap() < Utc::now()
    {
        eprintln!("{}", render_outdated_message());
    }
}

fn render_outdated_message() -> String {
    let rtx = style("rtx").dim().for_stderr();
    let mut output = vec![];
    output.push(format!(
        "{rtx} rtx has not been updated in over a year. Please update to the latest version."
    ));
    if cfg!(any(
        feature = "self_update",
        feature = "alpine",
        feature = "brew",
        feature = "deb",
        feature = "rpm",
    )) {
        output.push(format!("{rtx} update with: `rtx self-update`"));
    }
    output.push(format!(
        "{rtx} To hide this warning, set RTX_HIDE_OUTDATED_BUILD=1."
    ));

    output.join("\n")
}