Skip to content

Commit

Permalink
Merge pull request #13 from baoyachi/issue_6
Browse files Browse the repository at this point in the history
support SHORT_COMMIT #6
  • Loading branch information
baoyachi authored Aug 21, 2020
2 parents 73e4999 + 89db6ca commit 731bcd2
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 119 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ jobs:
cargo fmt
cargo fix
cargo fix
cargo clippy
cargo build
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ shadow-rs = "0.3"
## step 2
in your project add file `build.rs`,then add with below config
```rust
fn main() -> shadow_rs::err::SdResult<()> {
fn main() -> shadow_rs::SdResult<()> {
let src_path = std::env::var("CARGO_MANIFEST_DIR")?;
let out_path = std::env::var("OUT_DIR")?;
shadow_rs::Shadow::build(src_path, out_path)?;
Expand All @@ -68,6 +68,7 @@ then you can use const that's shadow build it.
fn main() {
println!("{}",shadow::BRANCH); //master
println!("{}",shadow::COMMIT_HASH);//8405e28e64080a09525a6cf1b07c22fcaf71a5c5
println!("{}",shadow::SHORT_COMMIT);//8405e28e
println!("{}",shadow::COMMIT_DATE);//2020-08-16T06:22:24+00:00
println!("{}",shadow::COMMIT_AUTHOR);//baoyachi
println!("{}",shadow::COMMIT_EMAIL);//xxx@gmail.com
Expand Down
4 changes: 2 additions & 2 deletions example_shadow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
shadow-rs = "0.3"
shadow-rs = { version = "0.3", path = "../" }

[build-dependencies]
shadow-rs = "0.3"
shadow-rs = { version = "0.3", path = "../" }
2 changes: 1 addition & 1 deletion example_shadow/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() -> shadow_rs::err::SdResult<()> {
fn main() -> shadow_rs::SdResult<()> {
let src_path = std::env::var("CARGO_MANIFEST_DIR")?;
let out_path = std::env::var("OUT_DIR")?;
shadow_rs::Shadow::build(src_path, out_path)?;
Expand Down
32 changes: 16 additions & 16 deletions example_shadow/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
pub mod shadow{
pub mod shadow {
include!(concat!(env!("OUT_DIR"), "/shadow.rs"));
}

fn main() {
println!("{}",shadow::BRANCH);
println!("{}",shadow::COMMIT_HASH);
println!("{}",shadow::COMMIT_DATE);
println!("{}",shadow::COMMIT_AUTHOR);
println!("{}",shadow::COMMIT_EMAIL);
println!("branch:{}", shadow::BRANCH);
println!("commit_id:{}", shadow::COMMIT_HASH);
println!("short_commit:{}",shadow::SHORT_COMMIT);
println!("commit_date:{}", shadow::COMMIT_DATE);
println!("commit_author:{}", shadow::COMMIT_AUTHOR);
println!("commit_email:{}", shadow::COMMIT_EMAIL);

println!("{}",shadow::BUILD_OS);
println!("{}",shadow::RUST_VERSION);
println!("{}",shadow::RUST_CHANNEL);
println!("{}",shadow::CARGO_VERSION);
println!("{}",shadow::CARGO_LOCK);
println!("build_os:{}", shadow::BUILD_OS);
println!("rust_version:{}", shadow::RUST_VERSION);
println!("rust_channel:{}", shadow::RUST_CHANNEL);
println!("cargo_version:{}", shadow::CARGO_VERSION);
println!("cargo_lock:{}", shadow::CARGO_LOCK);

println!("{}",shadow::PROJECT_NAME);
println!("{}",shadow::BUILD_TIME);
println!("{}",shadow::BUILD_RUST_CHANNEL);

}
println!("project_name:{}", shadow::PROJECT_NAME);
println!("build_time:{}", shadow::BUILD_TIME);
println!("build_rust_channel:{}", shadow::BUILD_RUST_CHANNEL);
}
2 changes: 1 addition & 1 deletion src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn build_channel() -> BuildRustChannel {
if cfg!(debug_assertions) {
return BuildRustChannel::Debug;
}
return BuildRustChannel::Release;
BuildRustChannel::Release
}

impl ToString for BuildRustChannel {
Expand Down
126 changes: 62 additions & 64 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,12 @@ const CARGO_LOCK: ShadowConst = "CARGO_LOCK";
// const CARGO_TREE: &str = "CARGO_TREE";

impl SystemEnv {
pub fn new() -> HashMap<ShadowConst, RefCell<ConstVal>> {
let mut env = SystemEnv::default();
env.map.insert(
BUILD_OS,
RefCell::new(ConstVal {
desc: "display build system os".to_string(),
v: format!("{}-{}", env::consts::OS, env::consts::ARCH),
t: ConstType::Str,
}),
);

env.map.insert(
RUST_CHANNEL,
ConstVal::new("display build system rust channel"),
);
env.map.insert(
RUST_VERSION,
ConstVal::new("display build system rust version"),
);
env.map.insert(
CARGO_VERSION,
ConstVal::new("display build system cargo version"),
);
env.map.insert(
CARGO_LOCK,
ConstVal::new("display build project dependence cargo lock detail"),
);

if let Err(e) = env.init() {
println!("{}", e.to_string());
}
env.map
}

fn init(&mut self) -> SdResult<()> {
let update_val = |c: ShadowConst, v: String| {
if let Some(c) = self.map.get(c) {
let mut val = c.borrow_mut();
val.t = ConstType::Str;
val.v = v.to_string();
val.v = v;
}
};
if let Ok(out) = Command::new("rustup").arg("default").output() {
Expand All @@ -88,6 +54,40 @@ impl SystemEnv {
}
}

pub fn new_system_env() -> HashMap<ShadowConst, RefCell<ConstVal>> {
let mut env = SystemEnv::default();
env.map.insert(
BUILD_OS,
RefCell::new(ConstVal {
desc: "display build system os".to_string(),
v: format!("{}-{}", env::consts::OS, env::consts::ARCH),
t: ConstType::Str,
}),
);

env.map.insert(
RUST_CHANNEL,
ConstVal::new("display build system rust channel"),
);
env.map.insert(
RUST_VERSION,
ConstVal::new("display build system rust version"),
);
env.map.insert(
CARGO_VERSION,
ConstVal::new("display build system cargo version"),
);
env.map.insert(
CARGO_LOCK,
ConstVal::new("display build project dependence cargo lock detail"),
);

if let Err(e) = env.init() {
println!("{}", e.to_string());
}
env.map
}

#[derive(Default, Debug)]
pub struct Project {
map: HashMap<ShadowConst, RefCell<ConstVal>>,
Expand All @@ -97,35 +97,33 @@ const PROJECT_NAME: ShadowConst = "PROJECT_NAME";
const BUILD_TIME: ShadowConst = "BUILD_TIME";
const BUILD_RUST_CHANNEL: ShadowConst = "BUILD_RUST_CHANNEL";

impl Project {
pub fn new() -> HashMap<ShadowConst, RefCell<ConstVal>> {
let mut project = Project::default();
project.map.insert(
BUILD_TIME,
RefCell::new(ConstVal {
desc: "display project build time".to_string(),
v: Local::now().format("%Y-%m-%d %H:%M:%S").to_string(),
t: ConstType::Str,
}),
);
project.map.insert(
BUILD_RUST_CHANNEL,
RefCell::new(ConstVal {
desc: "display project build by rust channel [debug or release]".to_string(),
v: build_channel().to_string(),
t: ConstType::Str,
}),
);
project
.map
.insert(PROJECT_NAME, ConstVal::new("display project name"));
pub fn new_project() -> HashMap<ShadowConst, RefCell<ConstVal>> {
let mut project = Project::default();
project.map.insert(
BUILD_TIME,
RefCell::new(ConstVal {
desc: "display project build time".to_string(),
v: Local::now().format("%Y-%m-%d %H:%M:%S").to_string(),
t: ConstType::Str,
}),
);
project.map.insert(
BUILD_RUST_CHANNEL,
RefCell::new(ConstVal {
desc: "display project build by rust channel [debug or release]".to_string(),
v: build_channel().to_string(),
t: ConstType::Str,
}),
);
project
.map
.insert(PROJECT_NAME, ConstVal::new("display project name"));

if let (Some(v), Some(c)) = (option_env!("CARGO_PKG_NAME"), project.map.get(PROJECT_NAME)) {
let mut val = c.borrow_mut();
val.t = ConstType::Str;
val.v = v.to_string();
}

project.map
if let (Some(v), Some(c)) = (option_env!("CARGO_PKG_NAME"), project.map.get(PROJECT_NAME)) {
let mut val = c.borrow_mut();
val.t = ConstType::Str;
val.v = v.to_string();
}

project.map
}
83 changes: 53 additions & 30 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::cell::RefCell;
use std::collections::HashMap;

const BRANCH: ShadowConst = "BRANCH";
const SHORT_COMMIT: ShadowConst = "SHORT_COMMIT";
const COMMIT_HASH: ShadowConst = "COMMIT_HASH";
const COMMIT_DATE: ShadowConst = "COMMIT_DATE";
const COMMIT_AUTHOR: ShadowConst = "COMMIT_AUTHOR";
Expand All @@ -19,34 +20,6 @@ pub struct Git {
}

impl Git {
pub(crate) fn new(
path: &std::path::Path,
ci: CIType,
) -> HashMap<ShadowConst, RefCell<ConstVal>> {
let mut git = Git {
map: Default::default(),
ci_type: ci,
};
git.map
.insert(BRANCH, ConstVal::new("display current branch"));
git.map
.insert(COMMIT_HASH, ConstVal::new("display current commit_id"));
git.map.insert(
COMMIT_AUTHOR,
ConstVal::new("display current commit author"),
);
git.map
.insert(COMMIT_EMAIL, ConstVal::new("display current commit email"));
git.map
.insert(COMMIT_DATE, ConstVal::new("display current commit date"));

if let Err(e) = git.init(path) {
println!("{}", e.to_string());
}

git.map
}

fn init(&mut self, path: &std::path::Path) -> SdResult<()> {
let repo = git2::Repository::discover(path)?;
let reference = repo.head()?;
Expand All @@ -55,14 +28,21 @@ impl Git {
if let Some(c) = self.map.get(c) {
let mut val = c.borrow_mut();
val.t = ConstType::Str;
val.v = v.to_string();
val.v = v;
}
};

update_val(BRANCH, self.get_branch(&reference));

if let Some(v) = reference.target() {
update_val(COMMIT_HASH, v.to_string());
let commit = v.to_string();
update_val(COMMIT_HASH, commit.clone());
let mut short_commit = commit.as_str();

if commit.len() > 8 {
short_commit = &short_commit[0..8];
}
update_val(SHORT_COMMIT, short_commit.to_string());
}

let commit = reference.peel_to_commit()?;
Expand Down Expand Up @@ -106,3 +86,46 @@ impl Git {
branch.to_string()
}
}

pub fn new_git(path: &std::path::Path, ci: CIType) -> HashMap<ShadowConst, RefCell<ConstVal>> {
let mut git = Git {
map: Default::default(),
ci_type: ci,
};
git.map
.insert(BRANCH, ConstVal::new("display current branch"));
git.map
.insert(COMMIT_HASH, ConstVal::new("display current commit_id"));

git.map.insert(
SHORT_COMMIT,
ConstVal::new("display current short commit_id"),
);

git.map.insert(
COMMIT_AUTHOR,
ConstVal::new("display current commit author"),
);
git.map
.insert(COMMIT_EMAIL, ConstVal::new("display current commit email"));
git.map
.insert(COMMIT_DATE, ConstVal::new("display current commit date"));

if let Err(e) = git.init(path) {
println!("{}", e.to_string());
}

git.map
}

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

#[test]
fn test_git() {
let map = new_git(Path::new("./"), CIType::Github);
println!("map:{:?}", map);
}
}
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ impl Shadow {

let out = {
let path = Path::new(out_path.as_str());
if !out_path.ends_with("/") {
if !out_path.ends_with('/') {
path.join(format!("{}/{}", out_path, SHADOW_RS))
} else {
path.join(SHADOW_RS)
}
};

let mut map = Git::new(&src_path, ci_type);
for (k, v) in Project::new() {
let mut map = new_git(&src_path, ci_type);
for (k, v) in new_project() {
map.insert(k, v);
}
for (k, v) in SystemEnv::new() {
for (k, v) in new_system_env() {
map.insert(k, v);
}

Expand Down

0 comments on commit 731bcd2

Please sign in to comment.