Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add adapt clap_version() #46

Merged
merged 5 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ jobs:

# Run examples with debug
- name: Run examples with debug
run: cargo run --example builtin_method
run: cargo run --example builtin_fn

# Run examples with debug
- name: Run examples with release
run: cargo run --example builtin_method
run: cargo run --example builtin_fn

# example_shadow cargo fmt
- name: Check examples format
Expand Down
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.5.14"
version = "0.5.15"
authors = ["baoyachi <liaoymxsdl@gmail.com>"]
edition = "2018"
description = "A build script write by Rust"
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ You can use this tool to check in production exactly where a binary came from an
* Check out the [example_shadow_hook](https://github.com/baoyachi/shadow-rs/tree/master/example_shadow_hook) for a
simple demonstration of how `shadow-rs` might be used to provide build-time information at run-time,and add custom
hook.
* built in method:[examples](https://github.com/baoyachi/shadow-rs/tree/master/examples).
* built in function:[examples](https://github.com/baoyachi/shadow-rs/tree/master/examples).

# Setup Guide

Expand Down Expand Up @@ -78,7 +78,7 @@ build-time information.**
```rust
fn main() {

//shadow-rs built in method
//shadow-rs built in function
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!("{}", shadow_rs::tag()); // get current project git head tag. e.g.'v1.5.3'
Expand Down Expand Up @@ -115,25 +115,26 @@ fn main() {
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
## Support const,function in table

#### shadow-rs built in method.
#### shadow-rs built in function.

* how to use 👉 : [examples](https://github.com/baoyachi/shadow-rs/tree/master/examples)

| method | desc |
| function | desc |
| ------ | ------ |
| is_debug() | check if this is a debug build.e.g.'true/false' |
| branch() | get current project branch.e.g.'master/develop' |
| tag() | get current project tag.e.g.'v1.3.5' |

#### shadow-rs support build const.
#### shadow-rs support build const,function.

* how to use 👉 : [shadow_example](https://github.com/baoyachi/shadow-rs/tree/master/example_shadow)

| const | example |
| const/fn | example |
| ------ | ------ |
| version() | support mini version information.It's use easy. |
| clap_version() | support mini version information for clap.It's use easy. |
| BRANCH | master/develop |
| TAG | v1.0.0 |
| SHORT_COMMIT | 8405e28e |
Expand Down
6 changes: 4 additions & 2 deletions example_shadow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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="../" }
5 changes: 3 additions & 2 deletions example_shadow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ shadow!(build);

fn main() {
App::new("example_shadow")
.version(build::version().as_str())
.version(build::clap_version().as_str())
.get_matches(); //USAGE: ./example_shadow -V

// shadow-rs built in method
// shadow-rs built in function
println!("is_debug:{}", shadow_rs::is_debug());
println!("branch:{}", shadow_rs::branch());
println!("tag:{}", shadow_rs::tag());

println!("clap_version:{}", build::clap_version());
println!("version:{}", build::version());
println!("pkg_version:{}", build::PKG_VERSION);
println!("pkg_version_major:{}", build::PKG_VERSION_MAJOR);
Expand Down
2 changes: 1 addition & 1 deletion examples/builtin_method.rs → examples/builtin_fn.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// cargo run --example builtin_method
// cargo run --example builtin_fn
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'
Expand Down
101 changes: 101 additions & 0 deletions src/build_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
macro_rules! concat_fn {
($fn_name:ident,$fn_desc:expr,$fn_body:expr) => {
pub fn $fn_name() -> String {
format!("{}{}", $fn_desc, $fn_body)
}
};
}

const VERSION_BRANCH_FN: &str = r##"#[allow(dead_code)]
pub fn version() -> String {
format!(r#"
pkg_version:{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
)
}"##;

const VERSION_TAG_FN: &str = r##"#[allow(dead_code)]
pub fn version() -> String {
format!(r#"
pkg_version:{}
tag:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
)
}"##;

const CLAP_VERSION_BRANCH_FN: &str = r##"#[allow(dead_code)]
pub fn clap_version() -> String {
format!(r#"{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
)
}"##;

const CLAP_VERSION_TAG_FN: &str = r##"#[allow(dead_code)]
pub fn clap_version() -> String {
format!(r#"{}
tag:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
)
}"##;

const VERSION_FN_DESC: &str = r#"
/// The common version function. It's so easy to use this function.
///
/// # Examples
///
/// ```ignore
/// fn main(){
/// println!("{}",shadow::version()); //print version() function
/// }
/// ```
"#;

const CLAP_VERSION_FN_DESC: &str = r#"
/// The common version function. It's so easy to use this function with clap verion().
///
/// # Examples
///
///```ignore
/// App::new("example_shadow")
/// .version(shadow::clap_version().as_str())
/// .get_matches()
///```
"#;

concat_fn!(version_branch_fn, VERSION_FN_DESC, VERSION_BRANCH_FN);

concat_fn!(version_tag_fn, VERSION_FN_DESC, VERSION_TAG_FN);

concat_fn!(
clap_version_branch_fn,
CLAP_VERSION_FN_DESC,
CLAP_VERSION_BRANCH_FN
);
concat_fn!(
clap_version_tag_fn,
CLAP_VERSION_FN_DESC,
CLAP_VERSION_TAG_FN
);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_version_fn() {
assert!(version_branch_fn().contains(VERSION_FN_DESC));
assert!(version_tag_fn().contains(VERSION_TAG_FN));
assert!(clap_version_branch_fn().contains(CLAP_VERSION_BRANCH_FN));
assert!(clap_version_tag_fn().contains(CLAP_VERSION_FN_DESC));
}
}
10 changes: 7 additions & 3 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ impl Git {
self.update_val(COMMIT_AUTHOR, v.to_string());
}
}
println!("{:?}", path);
Ok(())
}

Expand Down Expand Up @@ -199,7 +198,7 @@ pub mod git2_mod {
///
/// When current repository exists git folder.
///
/// It's use default feature.This method try use [git2] crates get current branch.
/// It's use default feature.This function try use [git2] crates get current branch.
/// If not use git2 feature,then try use [Command] to get.
pub fn branch() -> String {
#[cfg(feature = "git2")]
Expand Down Expand Up @@ -254,7 +253,12 @@ mod tests {
fn test_git() {
let map = Shadow::get_env();
let map = new_git(Path::new("./"), CIType::Github, &map);
println!("map:{:?}", map);
for (k, v) in map {
assert!(!v.desc.is_empty());
if !k.eq(TAG) {
assert!(!v.v.is_empty());
}
}
}

#[test]
Expand Down
56 changes: 19 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
//! * Check out the [example_shadow](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 [example_shadow_hook](https://github.com/baoyachi/shadow-rs/tree/master/example_shadow_hook) for a simple demonstration of how `shadow-rs` might be used to provide build-time information at run-time,and add custom hook.
//!
//! ## 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.
//! ## Built in function
//! * 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 function.
//!
//! # Example
//!
Expand Down Expand Up @@ -101,7 +101,7 @@
//! fn main(){
//! println!("{}",shadow_rs::is_debug());//get build mode. cargo build --release return false.normally return true.
//!
//! println!("{}",build::version()); //print version() method
//! println!("{}",build::version()); //print version() function
//! println!("{}",build::BRANCH); //master
//! println!("{}",build::SHORT_COMMIT);//8405e28e
//! println!("{}",build::COMMIT_HASH);//8405e28e64080a09525a6cf1b07c22fcaf71a5c5
Expand Down Expand Up @@ -130,6 +130,7 @@
//!

mod build;
mod build_fn;
mod channel;
mod ci;
mod env;
Expand All @@ -148,6 +149,9 @@ use std::fs::File;
use std::io::Write;
use std::path::Path;

use crate::build_fn::{
clap_version_branch_fn, clap_version_tag_fn, version_branch_fn, version_tag_fn,
};
pub use channel::BuildRustChannel;
use chrono::Local;
pub use err::{SdResult, ShadowError};
Expand All @@ -172,7 +176,7 @@ macro_rules! shadow {

/// It's shadow-rs Initialization entry.
///
/// In build.rs `main()` method call for this method.
/// In build.rs `main()` function call for this function.
///
/// # Examples
///
Expand All @@ -188,7 +192,7 @@ pub fn new() -> SdResult<()> {

/// It's shadow-rs Initialization entry with add custom hook.
///
/// In build.rs `main()` method call for this method.
/// In build.rs `main()` function call for this function.
///
/// # Examples
///
Expand Down Expand Up @@ -306,7 +310,7 @@ impl Shadow {

shadow.gen_const()?;

//write version method
//write version function
shadow.write_version()?;

Ok(shadow)
Expand Down Expand Up @@ -353,42 +357,18 @@ impl Shadow {
}

fn write_version(&mut self) -> SdResult<()> {
let desc: &str = "/// The common version method. It's so easy to use this method";

const VERSION_BRANCH_FN: &str = r##"#[allow(dead_code)]
pub fn version() -> String {
format!(r#"
pkg_version:{}
branch:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, BRANCH, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
)
}"##;

const VERSION_TAG_FN: &str = r##"#[allow(dead_code)]
pub fn version() -> String {
format!(r#"
pkg_version:{}
tag:{}
commit_hash:{}
build_time:{}
build_env:{},{}"#,PKG_VERSION, TAG, SHORT_COMMIT, BUILD_TIME, RUST_VERSION, RUST_CHANNEL
)
}"##;

let version_fn = match self.map.get(TAG) {
None => VERSION_BRANCH_FN,
let (ver_fn, clap_ver_fn) = match self.map.get(TAG) {
None => (version_branch_fn(), clap_version_branch_fn()),
Some(tag) => {
if !tag.v.is_empty() {
VERSION_TAG_FN
(version_tag_fn(), clap_version_tag_fn())
} else {
VERSION_BRANCH_FN
(version_branch_fn(), clap_version_branch_fn())
}
}
};
writeln!(&self.f, "{}", desc)?;
writeln!(&self.f, "{}\n", version_fn)?;
writeln!(&self.f, "{}\n", ver_fn)?;
writeln!(&self.f, "{}\n", clap_ver_fn)?;
Ok(())
}
}
Expand All @@ -402,7 +382,9 @@ mod tests {
fn test_build() -> SdResult<()> {
Shadow::build_inner("./".into(), "./".into())?;
let shadow = fs::read_to_string("./shadow.rs")?;
println!("{}", shadow);
assert!(shadow.contains(&version_branch_fn()));
assert!(shadow.contains(&clap_version_branch_fn()));
// println!("{}", shadow);
Ok(())
}

Expand Down