Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/baoyachi/shadow-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed Dec 21, 2020
2 parents 1de5f4b + 7f4b027 commit 7d2a383
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 81 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.4.1"
version = "0.5.0"
authors = ["baoyachi <[email protected]>"]
edition = "2018"
description = "A build script write by Rust"
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ In your `cargo.toml` `packgae` with package add with below config
build = "build.rs"

[dependencies]
shadow-rs = "0.4"
shadow-rs = "0.5"

[build-dependencies]
shadow-rs = "0.4"
shadow-rs = "0.5"
```

## step 2
In your project add file `build.rs`,then add with below config
```rust
fn main() -> shadow_rs::SdResult<()> {
shadow_rs::Shadow::new()
shadow_rs::new()
}
```

Expand All @@ -71,6 +71,7 @@ The `build` mod just we use `shadow!(build)` generated.

```rust
fn main() {
println!("{}",build::version());//print version() method
println!("{}",build::BRANCH); //master
println!("{}",build::SHORT_COMMIT);//8405e28e
println!("{}",build::COMMIT_HASH);//8405e28e64080a09525a6cf1b07c22fcaf71a5c5
Expand All @@ -97,6 +98,7 @@ And you can also use const with [clap](https://github.com/baoyachi/shadow-rs/blo
## Support const table
| const | example |
| ------ | ------ |
| version() | master/develop |
| BRANCH | master/develop |
| SHORT_COMMIT | 8405e28e |
| COMMIT_HASH | 8405e28e64080a09525a6cf1b07c22fcaf71a5c5 |
Expand Down
2 changes: 1 addition & 1 deletion example_shadow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ clap = "2.33.3"
shadow-rs = "0.5"

[build-dependencies]
shadow-rs = "0.5"
shadow-rs = "0.5"
2 changes: 1 addition & 1 deletion example_shadow/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() -> shadow_rs::SdResult<()> {
shadow_rs::Shadow::new()
shadow_rs::new()
}
18 changes: 1 addition & 17 deletions example_shadow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,9 @@ extern crate shadow_rs;

shadow!(build);

pub fn version() -> String {
format!(
r#"
branch:{}
commit-hash:{}
build_time:{}
build_env:{},{}
"#,
build::BRANCH,
build::SHORT_COMMIT,
build::BUILD_TIME,
build::RUST_VERSION,
build::RUST_CHANNEL,
)
}

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

println!("branch:{}", build::BRANCH);
Expand Down
7 changes: 3 additions & 4 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::cell::RefCell;
use std::collections::HashMap;

pub type ShadowConst = &'static str;
Expand All @@ -15,12 +14,12 @@ pub struct ConstVal {
}

impl ConstVal {
pub fn new<S: Into<String>>(desc: S) -> RefCell<ConstVal> {
RefCell::new(ConstVal {
pub fn new<S: Into<String>>(desc: S) -> ConstVal {
ConstVal {
desc: desc.into(),
v: "".to_string(),
t: ConstType::OptStr,
})
}
}
}

Expand Down
52 changes: 34 additions & 18 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@ use chrono::Local;
use std::env;
use std::process::Command;

use std::cell::RefCell;
use std::collections::HashMap;

#[derive(Default, Debug)]
pub struct SystemEnv {
map: HashMap<ShadowConst, RefCell<ConstVal>>,
map: HashMap<ShadowConst, ConstVal>,
}

const BUILD_OS: ShadowConst = "BUILD_OS";
const RUST_VERSION: ShadowConst = "RUST_VERSION";
const RUST_CHANNEL: ShadowConst = "RUST_CHANNEL";
const CARGO_VERSION: ShadowConst = "CARGO_VERSION";
const CARGO_TREE: ShadowConst = "CARGO_TREE";
// const CARGO_METADATA: ShadowConst = "CARGO_METADATA";
const PKG_VERSION: ShadowConst = "PKG_VERSION";

impl SystemEnv {
fn init(&mut self, std_env: &HashMap<String, String>) -> SdResult<()> {
let update_val = |c: ShadowConst, v: String| {
if let Some(c) = self.map.get(c) {
let mut val = c.borrow_mut();
let mut update_val = |c: ShadowConst, v: String| {
if let Some(mut val) = self.map.get_mut(c) {
val.t = ConstType::Str;
val.v = v;
}
Expand Down Expand Up @@ -59,6 +58,18 @@ impl SystemEnv {
}
}

if let Ok(_out) = Command::new("cargo")
.args(&["metadata", "--format-version", "1"])
.output()
{
//TODO completed

// update_val(
// CARGO_METADATA,
// String::from_utf8(out.stdout)?.trim().to_string(),
// );
}

if let Some(v) = std_env.get("CARGO_PKG_VERSION") {
update_val(PKG_VERSION, v.to_string());
}
Expand All @@ -84,17 +95,15 @@ fn filter_private_registry(lines: Vec<&str>) -> String {
tree
}

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

env.map.insert(
Expand All @@ -115,6 +124,11 @@ pub fn new_system_env(
ConstVal::new("display build cargo dependencies.It's used by rust version 1.44.0"),
);

// env.map.insert(
// CARGO_METADATA,
// ConstVal::new("display build cargo dependencies by metadata.It's use by exec command `cargo metadata`"),
// );

env.map.insert(
PKG_VERSION,
ConstVal::new("display build current project version"),
Expand All @@ -128,37 +142,39 @@ pub fn new_system_env(

#[derive(Default, Debug)]
pub struct Project {
map: HashMap<ShadowConst, RefCell<ConstVal>>,
map: HashMap<ShadowConst, ConstVal>,
}

const PROJECT_NAME: ShadowConst = "PROJECT_NAME";
const BUILD_TIME: ShadowConst = "BUILD_TIME";
const BUILD_RUST_CHANNEL: ShadowConst = "BUILD_RUST_CHANNEL";

pub fn new_project(std_env: &HashMap<String, String>) -> HashMap<ShadowConst, RefCell<ConstVal>> {
pub fn new_project(std_env: &HashMap<String, String>) -> HashMap<ShadowConst, ConstVal> {
let mut project = Project::default();
project.map.insert(
BUILD_TIME,
RefCell::new(ConstVal {
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 {
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)) = (std_env.get("CARGO_PKG_NAME"), project.map.get(PROJECT_NAME)) {
let mut val = c.borrow_mut();
if let (Some(v), Some(mut val)) = (
std_env.get("CARGO_PKG_NAME"),
project.map.get_mut(PROJECT_NAME),
) {
val.t = ConstType::Str;
val.v = v.to_string();
}
Expand Down
36 changes: 19 additions & 17 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::ci::CIType;
use crate::err::*;
use chrono::{DateTime, NaiveDateTime, Utc};
use git2::Reference;
use std::cell::RefCell;
use std::collections::HashMap;

const BRANCH: ShadowConst = "BRANCH";
Expand All @@ -15,50 +14,53 @@ const COMMIT_EMAIL: ShadowConst = "COMMIT_EMAIL";

#[derive(Default, Debug)]
pub struct Git {
map: HashMap<ShadowConst, RefCell<ConstVal>>,
map: HashMap<ShadowConst, ConstVal>,
ci_type: CIType,
}

impl Git {
fn update_val(&mut self, c: ShadowConst, v: String) {
if let Some(val) = self.map.get_mut(c) {
*val = ConstVal {
desc: val.desc.clone(),
v,
t: ConstType::Str,
}
}
}

fn init(&mut self, path: &std::path::Path, std_env: &HashMap<String, String>) -> SdResult<()> {
let repo = git2::Repository::discover(path)?;
let reference = repo.head()?;

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;
}
};

update_val(BRANCH, self.get_branch(&reference, std_env));
let branch = self.get_branch(&reference, &std_env);
self.update_val(BRANCH, branch);

if let Some(v) = reference.target() {
let commit = v.to_string();
update_val(COMMIT_HASH, commit.clone());
self.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());
self.update_val(SHORT_COMMIT, short_commit.to_string());
}

let commit = reference.peel_to_commit()?;

let time_stamp = commit.time().seconds().to_string().parse::<i64>()?;
let dt = NaiveDateTime::from_timestamp(time_stamp, 0);
let date_time = DateTime::<Utc>::from_utc(dt, Utc);
update_val(COMMIT_DATE, date_time.to_rfc3339());
self.update_val(COMMIT_DATE, date_time.to_rfc3339());

let author = commit.author();
if let Some(v) = author.email() {
update_val(COMMIT_EMAIL, v.to_string());
self.update_val(COMMIT_EMAIL, v.to_string());
}

if let Some(v) = author.name() {
update_val(COMMIT_AUTHOR, v.to_string());
self.update_val(COMMIT_AUTHOR, v.to_string());
}

Ok(())
Expand Down Expand Up @@ -91,7 +93,7 @@ pub fn new_git(
path: &std::path::Path,
ci: CIType,
std_env: &HashMap<String, String>,
) -> HashMap<ShadowConst, RefCell<ConstVal>> {
) -> HashMap<ShadowConst, ConstVal> {
let mut git = Git {
map: Default::default(),
ci_type: ci,
Expand Down
Loading

0 comments on commit 7d2a383

Please sign in to comment.