ant_build_info/
lib.rs

1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use chrono::Utc;
10use tracing::debug;
11
12/// Git information separated by slashes: `<sha> / <branch> / <describe>`
13pub const fn git_info() -> &'static str {
14    concat!(
15        env!("VERGEN_GIT_BRANCH"),
16        " / ",
17        env!("VERGEN_GIT_SHA"),
18        " / ",
19        env!("VERGEN_BUILD_DATE")
20    )
21}
22
23/// Annotated tag description, or fall back to abbreviated commit object.
24pub const fn git_describe() -> &'static str {
25    env!("VERGEN_GIT_DESCRIBE")
26}
27
28/// The current git branch.
29pub const fn git_branch() -> &'static str {
30    env!("VERGEN_GIT_BRANCH")
31}
32
33/// Shortened SHA-1 hash.
34pub const fn git_sha() -> &'static str {
35    env!("VERGEN_GIT_SHA")
36}
37
38/// Nightly version format: YYYY.MM.DD
39pub fn nightly_version() -> String {
40    let now = Utc::now();
41    now.format("%Y.%m.%d").to_string()
42}
43
44/// Git information for nightly builds: `<date> / <branch> / <sha>`
45pub fn nightly_git_info() -> String {
46    format!("{} / {} / {}", nightly_version(), git_branch(), git_sha(),)
47}
48
49pub fn package_version() -> String {
50    format!(
51        "{}.{}.{}.{}",
52        env!("RELEASE_YEAR"),
53        env!("RELEASE_MONTH"),
54        env!("RELEASE_CYCLE"),
55        env!("RELEASE_CYCLE_COUNTER")
56    )
57}
58
59pub fn full_version_info(
60    app_name: &str,
61    crate_version: &str,
62    protocol_version: Option<&str>,
63) -> String {
64    let mut info = format!("{app_name} v{crate_version}");
65
66    if let Some(version) = protocol_version {
67        info.push_str(&format!("\nNetwork version: {version}"));
68    }
69
70    info.push_str(&format!(
71        "\nPackage version: {}\nGit info: {}",
72        package_version(),
73        git_info()
74    ));
75
76    info
77}
78
79pub fn full_nightly_version_info(app_name: &str, protocol_version: Option<&str>) -> String {
80    let mut info = format!("{app_name} -- Nightly Release {}", nightly_version(),);
81    if let Some(version) = protocol_version {
82        info.push_str(&format!("\nNetwork version: {version}"));
83    }
84    info.push_str(&format!("\nGit info: {} / {}", git_branch(), git_sha(),));
85    info
86}
87
88pub fn version_string(
89    app_name: &str,
90    crate_version: &str,
91    protocol_version: Option<&str>,
92) -> String {
93    if cfg!(feature = "nightly") {
94        full_nightly_version_info(app_name, protocol_version)
95    } else {
96        full_version_info(app_name, crate_version, protocol_version)
97    }
98}
99
100pub fn log_version_info(crate_version: &str, protocol_version: &str) {
101    if cfg!(feature = "nightly") {
102        debug!("nightly build info: {}", nightly_git_info());
103        debug!("network version: {protocol_version}");
104    } else {
105        debug!("version: {crate_version}");
106        debug!("network version: {protocol_version}");
107        debug!("package version: {}", package_version());
108        debug!("git info: {}", git_info());
109    }
110}