Skip to content

Commit

Permalink
work for issue #86 #68
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed Mar 19, 2022
1 parent bfd5d75 commit 3457d59
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadow-rs"
version = "0.9.0"
version = "0.10.0"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "A build-time information stored in your rust project"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ fn main() {
println!("git_status_file:{}", shadow_rs::git_status_file()); // get current project statue file. e.g ' * examples/builtin_fn.rs (dirty)'

//shadow-rs built in const
println!("{}", build::version()); // the version (description binary detail information)
println!("{}", build::clap_long_version()); // usually used by clap crates version() (description binary detail information)
println!("{}", build::VERSION); // the version (description binary detail information)
println!("{}", build::CLAP_LONG_VERSION); // usually used by clap crates version() (description binary detail information)
println!("{}", build::PKG_VERSION); // current package version. e.g. '1.3.15-beta2'
println!("{}", build::PKG_VERSION_MAJOR); //current package major version. e.g. '1'
println!("{}", build::PKG_VERSION_MINOR); //current package minor version. e.g. '3'
Expand Down Expand Up @@ -155,8 +155,8 @@ with [`clap`](https://github.com/baoyachi/shadow-rs/blob/master/example_shadow/s

| const/fn | example |
| ------ | ------ |
| version() | support mini version information.It's use easy. |
| clap_long_version() | support mini version information for clap.It's use easy. |
| VERSION | support mini version information.It's use easy. |
| CLAP_LONG_VERSION | support mini version information for clap.It's use easy. |
| BRANCH | master/develop |
| TAG | v1.0.0 |
| SHORT_COMMIT | 8405e28e |
Expand Down
4 changes: 2 additions & 2 deletions example_shadow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fn main() {

#[allow(dead_code)]
pub fn print_build() {
println!("clap_long_version:{}", build::clap_long_version());
println!("version:{}", build::version());
println!("version:{}", build::VERSION);
println!("version:{}", build::CLAP_LONG_VERSION);
println!("pkg_version:{}", build::PKG_VERSION);
println!("pkg_version_major:{}", build::PKG_VERSION_MAJOR);
println!("pkg_version_minor:{}", build::PKG_VERSION_MINOR);
Expand Down
27 changes: 15 additions & 12 deletions src/gen_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ macro_rules! gen_const {
}

const VERSION_BRANCH_CONST: &str = r##"#[allow(dead_code)]
pub const VERSION:&'static str = shadow_rs::const_format::concatcp!(r#"
pub const VERSION:&'static str = shadow_rs::const_format::formatcp!(r#"
pkg_version:{}
branch:{}
commit_hash:{}
Expand All @@ -16,7 +16,7 @@ build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, R
);"##;

const VERSION_TAG_CONST: &str = r##"#[allow(dead_code)]
pub const VERSION:&'static str = shadow_rs::const_format::concatcp!(r#"
pub const VERSION:&'static str = shadow_rs::const_format::formatcp!(r#"
pkg_version:{}
tag:{}
commit_hash:{}
Expand All @@ -26,48 +26,51 @@ build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST

const CLAP_VERSION_BRANCH_CONST: &str = r##"#[allow(dead_code)]
#[deprecated = "Replaced with `CLAP_LONG_VERSION`"]
pub const CLAP_VERSION:&'static str = shadow_rs::const_format::concatcp!(r#"
pub const CLAP_VERSION:&'static str = shadow_rs::const_format::formatcp!(r#"{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##;

const CLAP_VERSION_TAG_CONST: &str = r##"#[allow(dead_code)]
#[deprecated = "Replaced with `clap_long_version`"]
pub const CLAP_VERSION:&'static str = shadow_rs::const_format::concatcp!(r#"
#[deprecated = "Replaced with `CLAP_LONG_VERSION`"]
pub const CLAP_VERSION:&'static str = shadow_rs::const_format::formatcp!(r#"{}
tag:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##;

const CLAP_LONG_VERSION_BRANCH_CONST: &str = r##"#[allow(dead_code)]
pub const CLAP_LONG_VERSION:&'static str = shadow_rs::const_format::concatcp!(r#"
pub const CLAP_LONG_VERSION:&'static str = shadow_rs::const_format::formatcp!(r#"{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##;

const CLAP_LONG_VERSION_TAG_CONST: &str = r##"#[allow(dead_code)]
pub const CLAP_LONG_VERSION:&'static str = shadow_rs::const_format::concatcp!(r#"
pub const CLAP_LONG_VERSION:&'static str = shadow_rs::const_format::formatcp!(r#"{}
tag:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
);"##;

const VERSION_CONST_DESC: &str =
r#"/// The common version const. It's so easy to use this const."#;
const VERSION_CONST_DESC: &str = r#"/// The common version const. It's so easy to use this const."#;

const CLAP_VERSION_CONST_DESC: &str =
r#"/// The common version const. It's so easy to use this const with CLAP_VERSION."#;

const CLAP_LONG_VERSION_CONST_DESC: &str =
r#"/// The common version const. It's so easy to use this const with CLAP_VERSION."#;

gen_const!(version_branch_const, VERSION_CONST_DESC, VERSION_BRANCH_CONST);
gen_const!(
version_branch_const,
VERSION_CONST_DESC,
VERSION_BRANCH_CONST
);

gen_const!(version_tag_const, VERSION_CONST_DESC, VERSION_TAG_CONST);

Expand All @@ -93,8 +96,8 @@ gen_const!(
CLAP_LONG_VERSION_TAG_CONST
);

pub(crate) const BUILD_FN_VERSION: &str = "version";
pub(crate) const BUILD_FN_CLAP_LONG_VERSION: &str = "clap_long_version";
pub(crate) const BUILD_CONST_VERSION: &str = "VERSION";
pub(crate) const BUILD_CONST_CLAP_LONG_VERSION: &str = "CLAP_LONG_VERSION";

#[cfg(test)]
mod tests {
Expand Down
21 changes: 11 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
//! println!("git_clean:{}", shadow_rs::git_clean()); // get current project clean. e.g 'true/false'
//! println!("git_status_file:{}", shadow_rs::git_status_file()); // get current project statue file. e.g ' * examples/builtin_fn.rs (dirty)'
//!
//! println!("{}",build::version()); //print version() function
//! println!("{}",build::VERSION); //print version const
//! println!("{}",build::CLAP_LONG_VERSION); //print CLAP_LONG_VERSION const
//! println!("{}",build::BRANCH); //master
//! println!("{}",build::SHORT_COMMIT);//8405e28e
//! println!("{}",build::COMMIT_HASH);//8405e28e64080a09525a6cf1b07c22fcaf71a5c5
Expand Down Expand Up @@ -144,10 +145,10 @@
pub extern crate const_format;

mod build;
mod gen_const;
mod ci;
mod env;
mod err;
mod gen_const;
mod git;
mod time;

Expand Down Expand Up @@ -176,8 +177,8 @@ use std::path::Path;

use crate::gen_const::{
clap_long_version_branch_const, clap_long_version_tag_const, clap_version_branch_const,
clap_version_tag_const, version_branch_const, version_tag_const, BUILD_FN_CLAP_LONG_VERSION,
BUILD_FN_VERSION,
clap_version_tag_const, version_branch_const, version_tag_const, BUILD_CONST_CLAP_LONG_VERSION,
BUILD_CONST_VERSION,
};
use chrono::Local;
pub use err::{SdResult, ShadowError};
Expand Down Expand Up @@ -338,9 +339,9 @@ impl Shadow {
self.gen_const()?;

//write version function
let build_fn = self.gen_version()?;
let gen_version = self.gen_version()?;

self.gen_build_in(build_fn)?;
self.gen_build_in(gen_version)?;

Ok(())
}
Expand Down Expand Up @@ -436,10 +437,10 @@ impl Shadow {
writeln!(&self.f, "{}\n", clap_ver_fn)?;
writeln!(&self.f, "{}\n", clap_long_ver_fn)?;

Ok(vec![BUILD_FN_VERSION, BUILD_FN_CLAP_LONG_VERSION])
Ok(vec![BUILD_CONST_VERSION, BUILD_CONST_CLAP_LONG_VERSION])
}

fn gen_build_in(&self, build_fn: Vec<&'static str>) -> SdResult<()> {
fn gen_build_in(&self, gen_const: Vec<&'static str>) -> SdResult<()> {
let mut print_val = String::from("\n");

// append gen const
Expand All @@ -449,8 +450,8 @@ impl Shadow {
}

// append gen fn
for k in build_fn {
let tmp = format!(r#"{}println!("{}:{{}}\n", {}());{}"#, "\t", k, k, "\n");
for k in gen_const {
let tmp = format!(r#"{}println!("{}:{{}}\n", {});{}"#, "\t", k, k, "\n");
print_val.push_str(tmp.as_str());
}

Expand Down

0 comments on commit 3457d59

Please sign in to comment.