Skip to content

Commit

Permalink
Merge pull request #10 from baoyachi/issue_8
Browse files Browse the repository at this point in the history
add gitlab ,github ci env ,get current commit branch
  • Loading branch information
baoyachi authored Aug 17, 2020
2 parents 20e4be9 + e75ef85 commit 85768c0
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 8 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: build

on:
push:
branches: [main, master]
branches: [master,issue_*]
pull_request:
branches: [main, master]
branches: [master,issue_*]

jobs:
lint:
Expand All @@ -20,6 +20,7 @@ jobs:
override: true
- name: Lint
run: |
cargo run
cargo fmt
cargo fix
cargo fix
Expand Down
26 changes: 26 additions & 0 deletions src/ci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[derive(Debug)]
pub enum CIType {
Github,
Gitlab,
Jenkins,
Travis,
None,
}

impl Default for CIType {
fn default() -> Self {
Self::None
}
}

impl ToString for CIType {
fn to_string(&self) -> String {
match self {
CIType::Github => "github".into(),
CIType::Gitlab => "gitlab".into(),
CIType::Jenkins => "jenkins".into(),
CIType::Travis => "travis".into(),
_ => "none".into(),
}
}
}
39 changes: 34 additions & 5 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::build::{ConstType, ConstVal, ShadowConst};
use crate::ci::CIType;
use crate::err::*;
use chrono::{DateTime, NaiveDateTime, Utc};
use git2::Reference;
use std::cell::RefCell;
use std::collections::HashMap;

Expand All @@ -13,11 +15,18 @@ const COMMIT_EMAIL: ShadowConst = "COMMIT_EMAIL";
#[derive(Default, Debug)]
pub struct Git {
map: HashMap<ShadowConst, RefCell<ConstVal>>,
ci_type: CIType,
}

impl Git {
pub(crate) fn new(path: &std::path::Path) -> HashMap<ShadowConst, RefCell<ConstVal>> {
let mut git = Git::default();
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
Expand Down Expand Up @@ -50,9 +59,7 @@ impl Git {
}
};

if let Some(v) = reference.shorthand() {
update_val(BRANCH, v.to_string());
}
update_val(BRANCH, self.get_branch(&reference));

if let Some(v) = reference.target() {
update_val(COMMIT_HASH, v.to_string());
Expand All @@ -76,4 +83,26 @@ impl Git {

Ok(())
}

fn get_branch(&self, reference: &Reference<'_>) -> String {
let mut branch = "";
if let Some(v) = reference.shorthand() {
branch = v;
}
match self.ci_type {
CIType::Gitlab => {
if let Some(v) = option_env!("CI_COMMIT_REF_NAME") {
branch = v;
}
}
CIType::Github => {
if let Some(v) = option_env!("CI_COMMIT_REF_NAME") {
branch = v;
}
}
_ => {}
}

branch.to_string()
}
}
24 changes: 23 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod build;
pub mod channel;
mod ci;
mod env;
pub mod err;
mod git;
Expand All @@ -9,6 +10,7 @@ use env::*;
use err::*;
use git::*;

use crate::ci::CIType;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::File;
Expand All @@ -26,6 +28,25 @@ pub struct Shadow {
}

impl Shadow {
//try get current ci env
fn try_ci() -> CIType {
if let Some(c) = option_env!("GITLAB_CI") {
if c == "true" {
return CIType::Gitlab;
}
}

if let Some(c) = option_env!("GITHUB_ACTIONS") {
if c == "true" {
return CIType::Github;
}
}

//TODO completed [travis,jenkins] env

CIType::None
}

/// generated rust const by exec:`cargo build`
///
///```rust
Expand All @@ -45,6 +66,7 @@ impl Shadow {
///
/// ```
pub fn build(src_path: String, out_path: String) -> SdResult<()> {
let ci_type = Self::try_ci();
let src_path = Path::new(src_path.as_str());

let out = {
Expand All @@ -56,7 +78,7 @@ impl Shadow {
}
};

let mut map = Git::new(&src_path);
let mut map = Git::new(&src_path, ci_type);
for (k, v) in Project::new() {
map.insert(k, v);
}
Expand Down
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extern crate shadow_rs;

use shadow_rs::Shadow;
use std::fs;

fn main() -> shadow_rs::err::SdResult<()> {
let src_path = std::env::var("CARGO_MANIFEST_DIR")?;

Shadow::build(src_path, "./".to_string())?;

for (k, v) in std::env::vars_os() {
println!("{:?},{:?}", k, v);
}
println!("{}", fs::read_to_string("./shadow.rs")?);

Ok(())
}

0 comments on commit 85768c0

Please sign in to comment.