From efe28fbf0c22c4cb3893eff9053c263540f37b90 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 00:39:20 +0800 Subject: [PATCH 01/15] add exec_current_branch worker for issue:https://github.com/baoyachi/shadow-rs/issues/37 --- src/git.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/git.rs b/src/git.rs index c6e5df1..ae7b20c 100644 --- a/src/git.rs +++ b/src/git.rs @@ -165,6 +165,15 @@ pub fn new_git( git.map } +fn exec_current_branch() -> Option { + Command::new("git") + .args(&["symbolic-ref", "--short", "HEAD", ]) + .output() + .map(|x| String::from_utf8(x.stdout).ok()) + .map(|x| x.map(|x| x.trim().to_string())) + .unwrap_or(None) +} + #[cfg(test)] mod tests { use super::*; From 88c27a72d8c05db4c7d23206d7137d2c8156d3c2 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 00:58:43 +0800 Subject: [PATCH 02/15] add git2_current_branch worker for issue:https://github.com/baoyachi/shadow-rs/issues/37 --- src/git.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/git.rs b/src/git.rs index ae7b20c..32d7983 100644 --- a/src/git.rs +++ b/src/git.rs @@ -2,9 +2,11 @@ use crate::build::{ConstType, ConstVal, ShadowConst}; use crate::ci::CIType; use crate::err::*; use chrono::{DateTime, Local, NaiveDateTime, Utc}; -use git2::Reference; +use git2::{Reference, Repository}; +use git2::Error as git2Error; use std::collections::HashMap; use std::process::Command; +use std::path::Path; pub const BRANCH: ShadowConst = "BRANCH"; pub(crate) const TAG: ShadowConst = "TAG"; @@ -31,7 +33,7 @@ impl Git { } } - fn init(&mut self, path: &std::path::Path, std_env: &HashMap) -> SdResult<()> { + fn init(&mut self, path: &Path, std_env: &HashMap) -> SdResult<()> { let repo = git2::Repository::discover(path)?; let reference = repo.head()?; @@ -165,7 +167,17 @@ pub fn new_git( git.map } -fn exec_current_branch() -> Option { +fn git_repo>(path: P) -> Result { + git2::Repository::discover(path) +} + +fn git2_current_branch(repo: &Repository) -> Option { + repo.head() + .map(|x| x.shorthand().map(|x| x.to_string())) + .unwrap_or(None) +} + +fn command_current_branch() -> Option { Command::new("git") .args(&["symbolic-ref", "--short", "HEAD", ]) .output() @@ -186,4 +198,15 @@ mod tests { let map = new_git(Path::new("./"), CIType::Github, &map); println!("map:{:?}", map); } + + #[test] + fn test_current_branch() { + let git2_branch = git_repo(".") + .map(|x| git2_current_branch(&x)) + .unwrap_or(None); + let command_branch = command_current_branch(); + assert!(git2_branch.is_some()); + assert!(command_branch.is_some()); + assert_eq!(command_branch, git2_branch) + } } From 1fe7b704c29aa0f9c957b8c19e7005dfdb571abf Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 01:07:11 +0800 Subject: [PATCH 03/15] add git_current_branch method. worker for issue:https://github.com/baoyachi/shadow-rs/issues/37 --- src/git.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/git.rs b/src/git.rs index 32d7983..9616c7a 100644 --- a/src/git.rs +++ b/src/git.rs @@ -2,11 +2,11 @@ use crate::build::{ConstType, ConstVal, ShadowConst}; use crate::ci::CIType; use crate::err::*; use chrono::{DateTime, Local, NaiveDateTime, Utc}; -use git2::{Reference, Repository}; use git2::Error as git2Error; +use git2::{Reference, Repository}; use std::collections::HashMap; -use std::process::Command; use std::path::Path; +use std::process::Command; pub const BRANCH: ShadowConst = "BRANCH"; pub(crate) const TAG: ShadowConst = "TAG"; @@ -167,6 +167,12 @@ pub fn new_git( git.map } +pub fn git_current_branch>(path: P) -> Option { + git_repo(path) + .map(|x| git2_current_branch(&x)) + .unwrap_or(command_current_branch()) +} + fn git_repo>(path: P) -> Result { git2::Repository::discover(path) } @@ -179,7 +185,7 @@ fn git2_current_branch(repo: &Repository) -> Option { fn command_current_branch() -> Option { Command::new("git") - .args(&["symbolic-ref", "--short", "HEAD", ]) + .args(&["symbolic-ref", "--short", "HEAD"]) .output() .map(|x| String::from_utf8(x.stdout).ok()) .map(|x| x.map(|x| x.trim().to_string())) @@ -207,6 +213,8 @@ mod tests { let command_branch = command_current_branch(); assert!(git2_branch.is_some()); assert!(command_branch.is_some()); - assert_eq!(command_branch, git2_branch) + assert_eq!(command_branch, git2_branch); + + assert_eq!(git_current_branch("."), git2_branch); } } From 48b5eaff5a1e2a9ea2c2e7d2dbfd98fd10c94fc6 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 01:16:07 +0800 Subject: [PATCH 04/15] replace get_branch worker for issue:https://github.com/baoyachi/shadow-rs/issues/37 --- src/git.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/git.rs b/src/git.rs index 9616c7a..cea051e 100644 --- a/src/git.rs +++ b/src/git.rs @@ -34,7 +34,7 @@ impl Git { } fn init(&mut self, path: &Path, std_env: &HashMap) -> SdResult<()> { - let repo = git2::Repository::discover(path)?; + let repo = git_repo(path)?; let reference = repo.head()?; let (branch, tag) = self.get_branch_tag(&reference, &std_env)?; @@ -85,7 +85,10 @@ impl Git { let mut tag = String::new(); //get branch - if let Some(v) = reference.shorthand() { + if let Some(v) = reference + .shorthand() + .map(|x| x.trim().to_string()) + .or_else(|| command_current_branch()) { branch = v.to_string(); } From 352deb6f9df7dc9c115a9993aaaa0b3548f116c9 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 01:17:14 +0800 Subject: [PATCH 05/15] cargo fmt --- src/git.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/git.rs b/src/git.rs index cea051e..89f4432 100644 --- a/src/git.rs +++ b/src/git.rs @@ -88,7 +88,8 @@ impl Git { if let Some(v) = reference .shorthand() .map(|x| x.trim().to_string()) - .or_else(|| command_current_branch()) { + .or_else(|| command_current_branch()) + { branch = v.to_string(); } From 3749f1df2278f895109c828279c260ad8299557c Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 01:34:50 +0800 Subject: [PATCH 06/15] add branch() method worker for issue:https://github.com/baoyachi/shadow-rs/issues/37 --- example_shadow/Cargo.toml | 6 ++++-- example_shadow/src/main.rs | 1 + src/git.rs | 13 ++++++++++--- src/lib.rs | 1 + 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/example_shadow/Cargo.toml b/example_shadow/Cargo.toml index 384c6af..2027dd7 100644 --- a/example_shadow/Cargo.toml +++ b/example_shadow/Cargo.toml @@ -9,7 +9,9 @@ build = "build.rs" [dependencies] clap = "2.33.3" -shadow-rs = "0.5" +#shadow-rs = "0.5" +shadow-rs = {path="../"} [build-dependencies] -shadow-rs = "0.5" +#shadow-rs = "0.5" +shadow-rs = {path="../"} \ No newline at end of file diff --git a/example_shadow/src/main.rs b/example_shadow/src/main.rs index 13be369..5b028e6 100644 --- a/example_shadow/src/main.rs +++ b/example_shadow/src/main.rs @@ -11,6 +11,7 @@ fn main() { .get_matches(); //USAGE: ./example_shadow -V println!("debug:{}", shadow_rs::is_debug()); //get build mode + println!("current_branch:{}", shadow_rs::branch()); //get current branch println!("tag:{}", build::TAG); println!("branch:{}", build::BRANCH); diff --git a/src/git.rs b/src/git.rs index 89f4432..80c7d3f 100644 --- a/src/git.rs +++ b/src/git.rs @@ -171,10 +171,17 @@ pub fn new_git( git.map } -pub fn git_current_branch>(path: P) -> Option { - git_repo(path) +/// get current repository git branch. +/// +/// When current repository exists git folder. +/// +/// This method try use `git2` crates get current branch. +/// If get error,then try use `Command` to get. +pub fn branch() -> String { + git_repo(".") .map(|x| git2_current_branch(&x)) .unwrap_or(command_current_branch()) + .unwrap_or(Default::default()) } fn git_repo>(path: P) -> Result { @@ -219,6 +226,6 @@ mod tests { assert!(command_branch.is_some()); assert_eq!(command_branch, git2_branch); - assert_eq!(git_current_branch("."), git2_branch); + assert_eq!(Some(branch()), git2_branch); } } diff --git a/src/lib.rs b/src/lib.rs index f7c1928..babdf81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,6 +143,7 @@ use std::path::Path; pub use channel::BuildRustChannel; use chrono::Local; pub use err::SdResult; +pub use git::branch; const SHADOW_RS: &str = "shadow.rs"; From eeda73942c08142fb0046cefb8c79d58f1b5f616 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 01:50:10 +0800 Subject: [PATCH 07/15] fix clouser --- src/git.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/git.rs b/src/git.rs index 80c7d3f..1889b9b 100644 --- a/src/git.rs +++ b/src/git.rs @@ -88,9 +88,9 @@ impl Git { if let Some(v) = reference .shorthand() .map(|x| x.trim().to_string()) - .or_else(|| command_current_branch()) + .or_else(command_current_branch) { - branch = v.to_string(); + branch = v } //get HEAD branch @@ -180,8 +180,8 @@ pub fn new_git( pub fn branch() -> String { git_repo(".") .map(|x| git2_current_branch(&x)) - .unwrap_or(command_current_branch()) - .unwrap_or(Default::default()) + .unwrap_or_else(|_|command_current_branch()) + .unwrap_or_default() } fn git_repo>(path: P) -> Result { From 53e1e64364f7ecdcd6258099ad6766f6c0d26437 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 01:50:33 +0800 Subject: [PATCH 08/15] cargo fmt --- src/git.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git.rs b/src/git.rs index 1889b9b..d5ae899 100644 --- a/src/git.rs +++ b/src/git.rs @@ -180,7 +180,7 @@ pub fn new_git( pub fn branch() -> String { git_repo(".") .map(|x| git2_current_branch(&x)) - .unwrap_or_else(|_|command_current_branch()) + .unwrap_or_else(|_| command_current_branch()) .unwrap_or_default() } From 0fe8383b12372ed55b3c1bacfd6eb72f8332e8d1 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 01:57:08 +0800 Subject: [PATCH 09/15] add semver method worker for issue:https://github.com/baoyachi/shadow-rs/issues/38 --- src/env.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/env.rs b/src/env.rs index 332ad57..932dabf 100644 --- a/src/env.rs +++ b/src/env.rs @@ -20,6 +20,10 @@ const CARGO_VERSION: ShadowConst = "CARGO_VERSION"; const CARGO_TREE: ShadowConst = "CARGO_TREE"; // const CARGO_METADATA: ShadowConst = "CARGO_METADATA"; const PKG_VERSION: ShadowConst = "PKG_VERSION"; +const PKG_VERSION_MAJOR: ShadowConst = "PKG_VERSION_MAJOR"; +const PKG_VERSION_MINOR: ShadowConst = "PKG_VERSION_MINOR"; +const PKG_VERSION_PATCH: ShadowConst = "PKG_VERSION_PATCH"; +const PKG_VERSION_PRE: ShadowConst = "PKG_VERSION_PRE"; impl SystemEnv { fn init(&mut self, std_env: &HashMap) -> SdResult<()> { @@ -74,6 +78,20 @@ impl SystemEnv { update_val(PKG_VERSION, v.to_string()); } + if let Some(v) = std_env.get("CARGO_PKG_VERSION_MAJOR") { + update_val(PKG_VERSION_MAJOR, v.to_string()); + } + + if let Some(v) = std_env.get("CARGO_PKG_VERSION_MINOR") { + update_val(PKG_VERSION_MINOR, v.to_string()); + } + if let Some(v) = std_env.get("CARGO_PKG_VERSION_PATCH") { + update_val(PKG_VERSION_PATCH, v.to_string()); + } + if let Some(v) = std_env.get("CARGO_PKG_VERSION_PRE") { + update_val(PKG_VERSION_PRE, v.to_string()); + } + Ok(()) } } @@ -134,6 +152,23 @@ pub fn new_system_env(std_env: &HashMap) -> HashMap Date: Thu, 14 Jan 2021 02:30:04 +0800 Subject: [PATCH 10/15] fxi doc --- README.md | 21 +++++++++++++++++---- example_shadow/Cargo.toml | 2 +- example_shadow/src/main.rs | 13 ++++++++++--- src/lib.rs | 6 +++++- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7787af1..ebaa6e9 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,14 @@ shadow!(build); ```rust fn main() { println!("{}", shadow_rs::is_debug()); // check if this is a debug build - + println!("{}", shadow_rs::branch()); // get current project git branch. e.g.'master' println!("{}", build::version()); // the 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' + println!("{}", build::PKG_VERSION_PATCH); //current package minor version. e.g. '15' + println!("{}", build::PKG_VERSION_PRE); //current package minor version. e.g. 'beta2' println!("{}", build::BRANCH); // the branch, e.g. 'master' println!("{}", build::TAG); // the tag, e.g. 'v1.0.0' println!("{}", build::SHORT_COMMIT); // short commit hash, e.g. '8405e28e' @@ -84,7 +90,6 @@ fn main() { println!("{}", build::RUST_VERSION); // rustc version e.g. 'rustc 1.45.0 (5c1f21c3b 2020-07-13)' println!("{}", build::RUST_CHANNEL); // rust toolchain e.g. 'stable-x86_64-apple-darwin (default)' println!("{}", build::CARGO_VERSION); // cargo version e.g. 'cargo 1.45.0 (744bd1fbb 2020-06-15)' - println!("{}", build::PKG_VERSION); // e.g. '0.3.13' println!("{}", build::CARGO_TREE); // e.g. the output of '$ cargo tree' println!("{}", build::PROJECT_NAME); // your project name, e.g. 'shadow-rs' @@ -96,10 +101,18 @@ fn main() { ## Clap Example And you can also use `shadow-rs` with [`clap`](https://github.com/baoyachi/shadow-rs/blob/master/example_shadow/src/main.rs). -## Support const table +## Support const,method in table + +#### shadow-rs support method. +| method | desc | +| ------ | ------ | +| is_debug() | check if this is a debug build.e.g.'true/false' | +| version() | support mini version information.It's use easy. | +| branch() | get current project branch.e.g.'master/develop' | + +#### shadow-rs support build const. | const | example | | ------ | ------ | -| version() | master/develop | | BRANCH | master/develop | | TAG | v1.0.0 | | SHORT_COMMIT | 8405e28e | diff --git a/example_shadow/Cargo.toml b/example_shadow/Cargo.toml index 2027dd7..a0e9855 100644 --- a/example_shadow/Cargo.toml +++ b/example_shadow/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "example_shadow" -version = "0.1.0" +version = "0.1.8-beta3" authors = ["baoyachi "] edition = "2018" build = "build.rs" diff --git a/example_shadow/src/main.rs b/example_shadow/src/main.rs index 5b028e6..0c3b337 100644 --- a/example_shadow/src/main.rs +++ b/example_shadow/src/main.rs @@ -10,8 +10,16 @@ fn main() { .version(build::version().as_str()) .get_matches(); //USAGE: ./example_shadow -V - println!("debug:{}", shadow_rs::is_debug()); //get build mode - println!("current_branch:{}", shadow_rs::branch()); //get current branch + // shadow-rs built in method + println!("is_debug:{}", shadow_rs::is_debug()); + println!("branch:{}", shadow_rs::branch()); + println!("version:{}", build::version()); + + println!("pkg_version:{}", build::PKG_VERSION); + println!("pkg_version_major:{}", build::PKG_VERSION_MAJOR); + println!("pkg_version_minor:{}", build::PKG_VERSION_MINOR); + println!("pkg_version_patch:{}", build::PKG_VERSION_PATCH); + println!("pkg_version_pre:{}", build::PKG_VERSION_PRE); println!("tag:{}", build::TAG); println!("branch:{}", build::BRANCH); @@ -25,7 +33,6 @@ fn main() { println!("rust_version:{}", build::RUST_VERSION); println!("rust_channel:{}", build::RUST_CHANNEL); println!("cargo_version:{}", build::CARGO_VERSION); - println!("pkg_version:{}", build::PKG_VERSION); println!("cargo_tree:{}", build::CARGO_TREE); println!("project_name:{}", build::PROJECT_NAME); diff --git a/src/lib.rs b/src/lib.rs index babdf81..855a87e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,11 @@ //! # Example //! //! ``` +//! pub const PKG_VERSION :&str = "1.3.8-beta3"; +//! pub const PKG_VERSION_MAJOR :&str = "1"; +//! pub const PKG_VERSION_MINOR :&str = "3"; +//! pub const PKG_VERSION_PATCH :&str = "8"; +//! pub const PKG_VERSION_PRE :&str = "beta3"; //! pub const RUST_VERSION :&str = "rustc 1.45.0 (5c1f21c3b 2020-07-13)"; //! pub const BUILD_RUST_CHANNEL :&str = "debug"; //! pub const COMMIT_AUTHOR :&str = "baoyachi"; @@ -46,7 +51,6 @@ //! pub const CARGO_VERSION :&str = "cargo 1.45.0 (744bd1fbb 2020-06-15)"; //! pub const BUILD_OS :&str = "macos-x86_64"; //! pub const COMMIT_HASH :&str = "386741540d73c194a3028b96b92fdeb53ca2788a"; -//! pub const PKG_VERSION :&str = "0.3.13"; //! ``` //! # Setup Guide //! From 9b410bd7d411f89c324830819d2bd7d9502ec658 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 02:32:37 +0800 Subject: [PATCH 11/15] upgrade 0.5.10 --- Cargo.toml | 2 +- example_shadow/Cargo.toml | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3ab6d21..cd131d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadow-rs" -version = "0.5.9" +version = "0.5.10" authors = ["baoyachi "] edition = "2018" description = "A build script write by Rust" diff --git a/example_shadow/Cargo.toml b/example_shadow/Cargo.toml index a0e9855..e9f55cc 100644 --- a/example_shadow/Cargo.toml +++ b/example_shadow/Cargo.toml @@ -9,9 +9,7 @@ build = "build.rs" [dependencies] clap = "2.33.3" -#shadow-rs = "0.5" -shadow-rs = {path="../"} +shadow-rs = "0.5" [build-dependencies] -#shadow-rs = "0.5" -shadow-rs = {path="../"} \ No newline at end of file +shadow-rs = "0.5" \ No newline at end of file From e8e6ab3628068028dcecfb9073656910371e3de4 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 02:35:16 +0800 Subject: [PATCH 12/15] update dep --- example_shadow/Cargo.toml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/example_shadow/Cargo.toml b/example_shadow/Cargo.toml index e9f55cc..a0e9855 100644 --- a/example_shadow/Cargo.toml +++ b/example_shadow/Cargo.toml @@ -9,7 +9,9 @@ build = "build.rs" [dependencies] clap = "2.33.3" -shadow-rs = "0.5" +#shadow-rs = "0.5" +shadow-rs = {path="../"} [build-dependencies] -shadow-rs = "0.5" \ No newline at end of file +#shadow-rs = "0.5" +shadow-rs = {path="../"} \ No newline at end of file From aa47a158cde949d6e450429eb5932fdb0cd7b587 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 10:50:05 +0800 Subject: [PATCH 13/15] update doc --- README.md | 84 ++++++++++++++++++++++---------------- example_shadow/src/main.rs | 2 +- examples/builtin_method.rs | 4 ++ src/lib.rs | 3 ++ 4 files changed, 57 insertions(+), 36 deletions(-) create mode 100644 examples/builtin_method.rs diff --git a/README.md b/README.md index ebaa6e9..a2bd39d 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ [![DepStatus](https://deps.rs/repo/github/baoyachi/shadow-rs/status.svg)](https://deps.rs/repo/github/baoyachi/shadow-rs) [![Gitter](https://badges.gitter.im/shadow-rs/community.svg)](https://gitter.im/shadow-rs/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) - `shadow-rs` allows you to recall properties of the build process and environment at runtime, including: * `Cargo.toml` project version @@ -24,11 +23,14 @@ You can use this tool to check in production exactly where a binary came from and how it was built. # Full Examples -* Check out the [shadow_example](https://github.com/baoyachi/shadow-rs/tree/master/example_shadow) for a simple demonstration of how `shadow-rs` might be used to provide build-time information at run-time. + +* Check out the [shadow_example](https://github.com/baoyachi/shadow-rs/tree/master/example_shadow) for a simple + demonstration of how `shadow-rs` might be used to provide build-time information at run-time. # Setup Guide ### 1) Modify `Cargo.toml` fields + Modify your `Cargo.toml` like so: ```TOML @@ -63,56 +65,65 @@ extern crate shadow_rs; shadow!(build); ``` -**Notice that the `shadow!` macro is provided the identifier `build`. You can now use this identifier to access build-time information.** +**Notice that the `shadow!` macro is provided the identifier `build`. You can now use this identifier to access +build-time information.** ### 4) Done. Use Shadow. ```rust fn main() { - println!("{}", shadow_rs::is_debug()); // check if this is a debug build - println!("{}", shadow_rs::branch()); // get current project git branch. e.g.'master' - println!("{}", build::version()); // the 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' - println!("{}", build::PKG_VERSION_PATCH); //current package minor version. e.g. '15' - println!("{}", build::PKG_VERSION_PRE); //current package minor version. e.g. 'beta2' - println!("{}", build::BRANCH); // the branch, e.g. 'master' - println!("{}", build::TAG); // the tag, e.g. 'v1.0.0' - println!("{}", build::SHORT_COMMIT); // short commit hash, e.g. '8405e28e' - println!("{}", build::COMMIT_HASH); // full commit hash, e.g. '8405e28e64080a09525a6cf1b07c22fcaf71a5c5' - println!("{}", build::COMMIT_DATE); // commit date, e.g. '2020-08-16 11:52:47' - println!("{}", build::COMMIT_AUTHOR); // commit author, e.g. 'baoyachi' - println!("{}", build::COMMIT_EMAIL); // commit email, e.g. 'example@gmail.com' - - println!("{}", build::BUILD_OS); // the OS that built the binary, e.g. 'macos-x86_64' - println!("{}", build::RUST_VERSION); // rustc version e.g. 'rustc 1.45.0 (5c1f21c3b 2020-07-13)' - println!("{}", build::RUST_CHANNEL); // rust toolchain e.g. 'stable-x86_64-apple-darwin (default)' - println!("{}", build::CARGO_VERSION); // cargo version e.g. 'cargo 1.45.0 (744bd1fbb 2020-06-15)' - println!("{}", build::CARGO_TREE); // e.g. the output of '$ cargo tree' - - println!("{}", build::PROJECT_NAME); // your project name, e.g. 'shadow-rs' - println!("{}", build::BUILD_TIME); // time when start build occurred, e.g. '2020-08-16 14:50:25' - println!("{}", build::BUILD_RUST_CHANNEL); // e.g. 'debug' + + //shadow-rs built in method + println!("{}", shadow_rs::is_debug()); // check if this is a debug build + println!("{}", shadow_rs::branch()); // get current project git branch. e.g.'master' + + //shadow-rs built in const + println!("{}", build::version()); // the 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' + println!("{}", build::PKG_VERSION_PATCH); //current package minor version. e.g. '15' + println!("{}", build::PKG_VERSION_PRE); //current package minor version. e.g. 'beta2' + println!("{}", build::BRANCH); // the branch, e.g. 'master' + println!("{}", build::TAG); // the tag, e.g. 'v1.0.0' + println!("{}", build::SHORT_COMMIT); // short commit hash, e.g. '8405e28e' + println!("{}", build::COMMIT_HASH); // full commit hash, e.g. '8405e28e64080a09525a6cf1b07c22fcaf71a5c5' + println!("{}", build::COMMIT_DATE); // commit date, e.g. '2020-08-16 11:52:47' + println!("{}", build::COMMIT_AUTHOR); // commit author, e.g. 'baoyachi' + println!("{}", build::COMMIT_EMAIL); // commit email, e.g. 'example@gmail.com' + + println!("{}", build::BUILD_OS); // the OS that built the binary, e.g. 'macos-x86_64' + println!("{}", build::RUST_VERSION); // rustc version e.g. 'rustc 1.45.0 (5c1f21c3b 2020-07-13)' + println!("{}", build::RUST_CHANNEL); // rust toolchain e.g. 'stable-x86_64-apple-darwin (default)' + println!("{}", build::CARGO_VERSION); // cargo version e.g. 'cargo 1.45.0 (744bd1fbb 2020-06-15)' + println!("{}", build::CARGO_TREE); // e.g. the output of '$ cargo tree' + + println!("{}", build::PROJECT_NAME); // your project name, e.g. 'shadow-rs' + println!("{}", build::BUILD_TIME); // time when start build occurred, e.g. '2020-08-16 14:50:25' + println!("{}", build::BUILD_RUST_CHANNEL); // e.g. 'debug' } ``` -## Clap Example -And you can also use `shadow-rs` with [`clap`](https://github.com/baoyachi/shadow-rs/blob/master/example_shadow/src/main.rs). +## Clap Example + +And you can also use `shadow-rs` +with [`clap`](https://github.com/baoyachi/shadow-rs/blob/master/example_shadow/src/main.rs). ## Support const,method in table -#### shadow-rs support method. +#### shadow-rs built in method. +* how to use 👉:[examples](https://github.com/baoyachi/shadow-rs/tree/master/examples) | method | desc | | ------ | ------ | | is_debug() | check if this is a debug build.e.g.'true/false' | -| version() | support mini version information.It's use easy. | | branch() | get current project branch.e.g.'master/develop' | #### shadow-rs support build const. +* how to use see 👉:[shadow_example](https://github.com/baoyachi/shadow-rs/tree/master/example_shadow) + | const | example | | ------ | ------ | +| version() | support mini version information.It's use easy. | | BRANCH | master/develop | | TAG | v1.0.0 | | SHORT_COMMIT | 8405e28e | @@ -130,10 +141,13 @@ And you can also use `shadow-rs` with [`clap`](https://github.com/baoyachi/shado | BUILD_TIME | 2020-08-16 14:50:25 | | BUILD_RUST_CHANNEL | debug/release | -If you have any questions, please create an [issue](https://github.com/baoyachi/shadow-rs/issues/new) so we may improve the documentation where it may be unclear. +If you have any questions, please create an [issue](https://github.com/baoyachi/shadow-rs/issues/new) so we may improve +the documentation where it may be unclear. ## People using Shadow -If you are using `shadow-rs`, please tell me! Or instead, consider making a note here: [Shadow Users Collection](https://github.com/baoyachi/shadow-rs/issues/19). + +If you are using `shadow-rs`, please tell me! Or instead, consider making a note +here: [Shadow Users Collection](https://github.com/baoyachi/shadow-rs/issues/19). diff --git a/example_shadow/src/main.rs b/example_shadow/src/main.rs index 0c3b337..4acc195 100644 --- a/example_shadow/src/main.rs +++ b/example_shadow/src/main.rs @@ -13,8 +13,8 @@ fn main() { // shadow-rs built in method println!("is_debug:{}", shadow_rs::is_debug()); println!("branch:{}", shadow_rs::branch()); - println!("version:{}", build::version()); + println!("version:{}", build::version()); println!("pkg_version:{}", build::PKG_VERSION); println!("pkg_version_major:{}", build::PKG_VERSION_MAJOR); println!("pkg_version_minor:{}", build::PKG_VERSION_MINOR); diff --git a/examples/builtin_method.rs b/examples/builtin_method.rs new file mode 100644 index 0000000..e8fcb28 --- /dev/null +++ b/examples/builtin_method.rs @@ -0,0 +1,4 @@ +fn main() { + println!("debug:{}", shadow_rs::is_debug()); // check if this is a debug build. e.g 'true/false' + println!("branch:{}", shadow_rs::branch()); // get current project branch. e.g 'master/develop' +} diff --git a/src/lib.rs b/src/lib.rs index 855a87e..68d39a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,9 @@ //! # Full Examples //! * Check out the [shadow_example](https://github.com/baoyachi/shadow-rs/tree/master/example_shadow) for a simple demonstration of how `shadow-rs` might be used to provide build-time information at run-time. //! +//! ## Built in method +//! * Check out the [examples](https://github.com/baoyachi/shadow-rs/tree/master/examples) for a simple demonstration of how `shadow-rs` might be used to provide build in method. +//! //! # Example //! //! ``` From b97516e98889808628558714e948f6caf94a82e9 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 10:57:44 +0800 Subject: [PATCH 14/15] update md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a2bd39d..5871c06 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ You can use this tool to check in production exactly where a binary came from an * Check out the [shadow_example](https://github.com/baoyachi/shadow-rs/tree/master/example_shadow) for a simple demonstration of how `shadow-rs` might be used to provide build-time information at run-time. +* built in method:[examples](https://github.com/baoyachi/shadow-rs/tree/master/examples). # Setup Guide From 683d2b72d096a57dbee50bc1dbf10b2679a5f9a4 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Thu, 14 Jan 2021 11:07:09 +0800 Subject: [PATCH 15/15] update ci --- .github/workflows/check.yml | 16 ++++++++-------- example_shadow/Cargo.toml | 6 ++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index b3f989a..3a23757 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -36,6 +36,14 @@ jobs: - name: Run tests run: cargo test --all --verbose -- --exact -Z unstable-options --format=json --show-output + # build on nightly + - uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + - name: Build on nightly + run: cargo build --release + # examples - name: Check examples format run: cargo fmt --all -- --check @@ -48,11 +56,3 @@ jobs: - name: Run examples run: cargo run --release && target/release/example_shadow -V working-directory: ./example_shadow - - # build on nightly - - uses: actions-rs/toolchain@v1 - with: - toolchain: nightly - override: true - - name: Build on nightly - run: cargo build --release \ No newline at end of file diff --git a/example_shadow/Cargo.toml b/example_shadow/Cargo.toml index a0e9855..e9f55cc 100644 --- a/example_shadow/Cargo.toml +++ b/example_shadow/Cargo.toml @@ -9,9 +9,7 @@ build = "build.rs" [dependencies] clap = "2.33.3" -#shadow-rs = "0.5" -shadow-rs = {path="../"} +shadow-rs = "0.5" [build-dependencies] -#shadow-rs = "0.5" -shadow-rs = {path="../"} \ No newline at end of file +shadow-rs = "0.5" \ No newline at end of file