Skip to content

Commit

Permalink
chore: standardise versioning for binaries
Browse files Browse the repository at this point in the history
The RFC for the release process stipulated there should be four versioning arguments:
* --version
* --crate-version
* --package-version
* --network-version

Here, `--crate-version` is the semantic version of the crate, `--package-version` is the release
cycle version, e.g., `2024.09.1.1`, and `--network-version` prints the compatible network protocol.
The `--version` argument will then print all of this information. The `--package-version` argument
does not apply to the nightly release.

The approach for printing the version information is to provide our own `version` flag, by using the
`disable_version_flag` attribute with `clap`. If you want to use `clap`, all the information needs
to be completely static and available at compile time. This is convoluted, especially for things
like the network protocol version and the crate version, and it would have led to a lot of
repetition. We can avoid the difficulty by providing the information dynamically, and we can use the
`sn_build_info` crate to define the version string once. On binaries that used subcommands, for the
versioning to work correctly, the subcommand needs to be made optional.

The `get_bin_version` helper used by the node manager was updated parse the new versioning
information from both stable and nightly releases. Now, the function can handle different
`--version` output formats by extracting the version number for stable releases prefixed with a 'v'
and the date for nightly releases.

Since we are going to introduce a nightly build, this commit also introduces a nightly feature,
which will control what versioning information is used. For the nightly, the binaries will be
versioned with the date on which they are built. The `--package-version` argument does not apply to
the nightly release; it is not necessary if all the binaries have the same version, which is the
date.
  • Loading branch information
jacderida committed Sep 24, 2024
1 parent 2238eeb commit 7639624
Show file tree
Hide file tree
Showing 25 changed files with 649 additions and 137 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions nat-detection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ version = "0.2.5"
name = "nat-detection"
path = "src/main.rs"

[features]
nightly = []

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
clap-verbosity-flag = "2.2.0"
Expand All @@ -28,7 +31,9 @@ libp2p = { version = "0.54.1", features = [
"macros",
"upnp",
] }
sn_build_info = { path = "../sn_build_info", version = "0.1.13" }
sn_networking = { path = "../sn_networking", version = "0.18.2" }
sn_protocol = { path = "../sn_protocol", version = "0.17.9" }
tokio = { version = "1.32.0", features = ["full"] }
tracing = { version = "~0.1.26" }
tracing-log = "0.2.0"
Expand Down
39 changes: 37 additions & 2 deletions nat-detection/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const RETRY_INTERVAL: Duration = Duration::from_secs(10);
/// - 11: Public under UPnP
/// - 12: Private or Unknown NAT
#[derive(Debug, Parser)]
#[clap(version, author, verbatim_doc_comment)]
#[clap(disable_version_flag = true)]
struct Opt {
/// Port to listen on.
///
Expand All @@ -60,15 +60,50 @@ struct Opt {

#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity,

/// Print the crate version
#[clap(long)]
crate_version: bool,

/// Print the package version
#[clap(long)]
#[cfg(not(feature = "nightly"))]
package_version: bool,

/// Print version information.
#[clap(long)]
version: bool,
}

#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;

// Process command line arguments.
let opt = Opt::parse();

if opt.version {
println!(
"{}",
sn_build_info::version_string(
"Autonomi NAT Detection",
env!("CARGO_PKG_VERSION"),
None
)
);
return Ok(());
}

if opt.crate_version {
println!("Crate version: {}", env!("CARGO_PKG_VERSION"));
return Ok(());
}

#[cfg(not(feature = "nightly"))]
if opt.package_version {
println!("Package version: {}", sn_build_info::package_version());
return Ok(());
}

let registry = tracing_subscriber::registry().with(tracing_subscriber::fmt::layer());
// Use `RUST_LOG` if set, else use the verbosity flag (where `-vvvv` is trace level).
let _ = if std::env::var_os("RUST_LOG").is_some() {
Expand Down
5 changes: 5 additions & 0 deletions node-launchpad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ build = "build.rs"
name = "node-launchpad"
path = "src/bin/tui/main.rs"

[features]
nightly = []

[dependencies]
atty = "0.2.14"
better-panic = "0.3.0"
Expand Down Expand Up @@ -48,8 +51,10 @@ reqwest = { version = "0.12.2", default-features = false, features = [
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
signal-hook = "0.3.17"
sn_build_info = { path = "../sn_build_info", version = "0.1.13" }
sn-node-manager = { version = "0.10.4", path = "../sn_node_manager" }
sn_peers_acquisition = { version = "0.5.1", path = "../sn_peers_acquisition" }
sn_protocol = { path = "../sn_protocol", version = "0.17.9" }
sn-releases = "~0.2.6"
sn_service_management = { version = "0.3.12", path = "../sn_service_management" }
strip-ansi-escapes = "0.2.0"
Expand Down
40 changes: 38 additions & 2 deletions node-launchpad/src/bin/tui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use color_eyre::eyre::Result;
use node_launchpad::{
app::App,
config::configure_winsw,
utils::{initialize_logging, initialize_panic_handler, version},
utils::{initialize_logging, initialize_panic_handler},
};
#[cfg(target_os = "windows")]
use sn_node_manager::config::is_running_as_root;
Expand All @@ -25,7 +25,7 @@ use std::{env, path::PathBuf};
use tokio::task::LocalSet;

#[derive(Parser, Debug)]
#[command(author, version = version(), about)]
#[command(disable_version_flag = true)]
pub struct Cli {
#[arg(
short,
Expand Down Expand Up @@ -53,12 +53,48 @@ pub struct Cli {

#[command(flatten)]
pub(crate) peers: PeersArgs,

/// Print the crate version.
#[clap(long)]
crate_version: bool,

/// Print the package version.
#[clap(long)]
#[cfg(not(feature = "nightly"))]
package_version: bool,

/// Print the version.
#[clap(long)]
version: bool,
}

async fn tokio_main() -> Result<()> {
initialize_panic_handler()?;
let args = Cli::parse();

if args.version {
println!(
"{}",
sn_build_info::version_string(
"Autonomi Node Launchpad",
env!("CARGO_PKG_VERSION"),
None
)
);
return Ok(());
}

if args.crate_version {
println!("{}", env!("CARGO_PKG_VERSION"));
return Ok(());
}

#[cfg(not(feature = "nightly"))]
if args.package_version {
println!("{}", sn_build_info::package_version());
return Ok(());
}

info!("Starting app with args: {args:?}");
let mut app = App::new(
args.tick_rate,
Expand Down
24 changes: 0 additions & 24 deletions node-launchpad/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ use tracing_subscriber::{
self, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Layer,
};

const VERSION_MESSAGE: &str = concat!(
env!("CARGO_PKG_VERSION"),
"-",
env!("VERGEN_GIT_DESCRIBE"),
" (",
env!("VERGEN_BUILD_DATE"),
")"
);

pub fn initialize_panic_handler() -> Result<()> {
let (panic_hook, eyre_hook) = color_eyre::config::HookBuilder::default()
.panic_section(format!(
Expand Down Expand Up @@ -132,18 +123,3 @@ macro_rules! trace_dbg {
trace_dbg!(level: tracing::Level::DEBUG, $ex)
};
}

pub fn version() -> String {
let author = clap::crate_authors!();

let data_dir_path = get_launchpad_data_dir_path().unwrap().display().to_string();

format!(
"\
{VERSION_MESSAGE}
Authors: {author}
Data directory: {data_dir_path}"
)
}
3 changes: 3 additions & 0 deletions sn_auditor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local-discovery = [
"sn_peers_acquisition/local-discovery",
]
network-contacts = ["sn_peers_acquisition/network-contacts"]
nightly = []
open-metrics = ["sn_client/open-metrics"]
websockets = ["sn_client/websockets"]
svg-dag = ["graphviz-rust", "dag-collection"]
Expand All @@ -31,9 +32,11 @@ graphviz-rust = { version = "0.9.0", optional = true }
lazy_static = "1.4.0"
serde = { version = "1.0.133", features = ["derive", "rc"] }
serde_json = "1.0.108"
sn_build_info = { path = "../sn_build_info", version = "0.1.13" }
sn_client = { path = "../sn_client", version = "0.110.1" }
sn_logging = { path = "../sn_logging", version = "0.2.34" }
sn_peers_acquisition = { path = "../sn_peers_acquisition", version = "0.5.1" }
sn_protocol = { path = "../sn_protocol", version = "0.17.9" }
tiny_http = { version = "0.12", features = ["ssl-rustls"] }
tracing = { version = "~0.1.26" }
tokio = { version = "1.32.0", features = [
Expand Down
50 changes: 48 additions & 2 deletions sn_auditor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use dag_db::SpendDagDb;
use sn_client::Client;
use sn_logging::{Level, LogBuilder, LogFormat, LogOutputDest};
use sn_peers_acquisition::PeersArgs;
use sn_protocol::version::IDENTIFY_PROTOCOL_STR;
use std::collections::BTreeSet;
use std::path::PathBuf;
use tiny_http::{Response, Server};
Expand All @@ -27,7 +28,7 @@ use tiny_http::{Response, Server};
const BETA_REWARDS_BACKUP_INTERVAL_SECS: u64 = 20 * 60;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(disable_version_flag = true)]
struct Opt {
#[command(flatten)]
peers: PeersArgs,
Expand Down Expand Up @@ -70,14 +71,59 @@ struct Opt {
/// discord usernames of the beta participants
#[clap(short = 'k', long, value_name = "hex_secret_key")]
beta_encryption_key: Option<String>,

/// Print the crate version.
#[clap(long)]
pub crate_version: bool,

/// Print the network protocol version.
#[clap(long)]
pub protocol_version: bool,

/// Print the package version.
#[cfg(not(feature = "nightly"))]
#[clap(long)]
pub package_version: bool,

/// Print version information.
#[clap(long)]
version: bool,
}

#[tokio::main]
async fn main() -> Result<()> {
let opt = Opt::parse();

if opt.version {
println!(
"{}",
sn_build_info::version_string(
"Autonomi Auditor",
env!("CARGO_PKG_VERSION"),
Some(&IDENTIFY_PROTOCOL_STR)
)
);
return Ok(());
}

if opt.crate_version {
println!("{}", env!("CARGO_PKG_VERSION"));
return Ok(());
}

#[cfg(not(feature = "nightly"))]
if opt.package_version {
println!("{}", sn_build_info::package_version());
return Ok(());
}

if opt.protocol_version {
println!("{}", *IDENTIFY_PROTOCOL_STR);
return Ok(());
}

let log_builder = logging_init(opt.log_output_dest, opt.log_format)?;
let _log_handles = log_builder.initialize()?;

let beta_participants = load_and_update_beta_participants(opt.beta_participants)?;

let maybe_sk = if let Some(sk_str) = opt.beta_encryption_key {
Expand Down
8 changes: 8 additions & 0 deletions sn_build_info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ name = "sn_build_info"
readme = "README.md"
repository = "https://github.com/maidsafe/safe_network"
version = "0.1.13"
build = "build.rs"

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }

[features]
nightly = []

[lints]
workspace = true

[dependencies]
chrono = "0.4"
tracing = { version = "~0.1.26" }
Loading

0 comments on commit 7639624

Please sign in to comment.