Skip to content

Commit

Permalink
follow review opinion
Browse files Browse the repository at this point in the history
  • Loading branch information
KaoImin committed Aug 7, 2023
1 parent 8b94bab commit eeb04e6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
11 changes: 6 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
let commit_id = std::process::Command::new("git")
if let Some(commit_id) = std::process::Command::new("git")
.args([
"describe",
"--always",
Expand All @@ -8,8 +8,9 @@ fn main() {
"--abbrev=7",
])
.output()
.map(|r| String::from_utf8(r.stdout).unwrap())
.unwrap();

println!("cargo:rustc-env=AXON_COMMIT_ID={}", commit_id);
.ok()
.and_then(|r| String::from_utf8(r.stdout).ok())
{
println!("cargo:rustc-env=AXON_COMMIT_ID={}", commit_id);
}
}
20 changes: 19 additions & 1 deletion common/version/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::fmt::{Display, Formatter};
use std::str::FromStr;

pub const DEFAULT_COMMIT_ID: &str = "unknown";

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version {
inner: semver::Version,
Expand All @@ -22,7 +24,9 @@ impl Display for Version {

impl Version {
pub fn new(ver: &str) -> Self {
Version::from_str(ver).unwrap_or_else(|e| panic!("Parse version error {:?}", e))
Version::from_str(ver)
.unwrap_or_else(|e| panic!("Parse version error {:?}", e))
.set_commit_id(DEFAULT_COMMIT_ID)
}

pub fn new_with_commit_id(ver: &str, commit_id: &str) -> Self {
Expand All @@ -40,3 +44,17 @@ impl Version {
self
}
}

#[cfg(test)]
mod tests {
use semver::BuildMetadata;
use std::str::FromStr;

use crate::DEFAULT_COMMIT_ID;

#[test]
fn test_parse_default_commit_id() {
let build = BuildMetadata::from_str(DEFAULT_COMMIT_ID);
assert_eq!(build.unwrap().as_str(), DEFAULT_COMMIT_ID);
}
}
16 changes: 9 additions & 7 deletions core/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ pub struct CheckingVersionError {
pub type Result<T, E = Error> = std::result::Result<T, E>;

pub struct AxonCli {
version: Version,
matches: ArgMatches,
kernel_version: Version,
application_version: Version,
matches: ArgMatches,
}

impl AxonCli {
pub fn init(axon_version: Version) -> Self {
pub fn init(kernel_version: Version, application_version: Version) -> Self {
let matches = Command::new("axon")
.version(axon_version.to_string())
.version(kernel_version.to_string())
.subcommand_required(true)
.subcommand(
Command::new("run")
Expand All @@ -78,7 +79,8 @@ impl AxonCli {
);

AxonCli {
version: axon_version,
kernel_version,
application_version,
matches: matches.get_matches(),
}
}
Expand Down Expand Up @@ -112,7 +114,7 @@ impl AxonCli {

register_log(&config);

Axon::new(self.version.to_string(), config, genesis)
Axon::new(self.application_version.to_string(), config, genesis)
.run(key_provider)
.map_err(Error::Running)
}
Expand All @@ -128,7 +130,7 @@ impl AxonCli {
// Won't panic because parent of data_path_for_version() is data_path.
check_version(
&config.data_path_for_version(),
&self.version,
&self.kernel_version,
latest_compatible_version(),
)
}
Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ use core_executor::FEE_ALLOCATOR;
pub fn run(
fee_allocator: impl FeeAllocate + 'static,
key_provider: impl KeyProvider,
version: &'static str,
app_version: &'static str,
) -> Result<()> {
FEE_ALLOCATOR.swap(Arc::new(Box::new(fee_allocator)));
AxonCli::init(version.parse().unwrap()).start_with_custom_key_provider(Some(key_provider))

AxonCli::init(
clap::crate_version!()
.parse()
.expect("Parse kernel version"),
app_version.parse().expect("Parse application version"),
)
.start_with_custom_key_provider(Some(key_provider))
}
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use common_version::Version;
use core_cli::AxonCli;

fn main() {
let version = Version::new_with_commit_id(crate_version!(), env!("AXON_COMMIT_ID"));
let crate_version = crate_version!();
let kernel_version = option_env!("AXON_COMMIT_ID")
.map(|commit_id| Version::new_with_commit_id(crate_version, commit_id))
.unwrap_or_else(|| Version::new(crate_version));

if let Err(e) = AxonCli::init(version).start() {
if let Err(e) = AxonCli::init(kernel_version.clone(), kernel_version).start() {
eprintln!("Error {e}");
std::process::exit(1);
}
Expand Down

0 comments on commit eeb04e6

Please sign in to comment.