-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
88 lines (70 loc) · 2.11 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use regex::Regex;
use std::fs::{self, File};
use std::io::{self, Write};
use std::process::Command;
use std::str::FromStr;
#[derive(Debug)]
enum Profile {
Debug,
Release,
Test,
}
impl FromStr for Profile {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let p = match s {
"debug" => Profile::Debug,
"release" => Profile::Release,
"test" => Profile::Test,
_ => return Err(format!("unable to convert profile: \"{s}\"")),
};
Ok(p)
}
}
fn get_profile() -> Profile {
let out_dir = std::env::var("OUT_DIR").unwrap();
let re = Regex::new(r#"target/(release|test|debug)"#).unwrap();
let captures = re.captures(&out_dir);
let profile_str = captures
.unwrap()
.get(1)
.map(|a| a.as_str().to_string())
.ok_or("cannot extract profile")
.unwrap();
Profile::from_str(&profile_str).unwrap()
}
fn last_commit() -> String {
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.unwrap()
.stdout;
String::from_utf8(output).unwrap()
}
fn main() -> io::Result<()> {
let profile = get_profile();
let commit = last_commit();
std::env::set_var("TPAWS_COMMIT_ID", commit);
if matches!(profile, Profile::Release) {
println!("cargo:warning=Building formula");
generate_formula(
"tpaws",
"https://github.com/rawnly/tpaws",
"CLI to manage TargetProcess and AWS CodeCommit",
)?;
}
Ok(())
}
fn generate_formula(bin_name: &str, repo: &str, description: &str) -> io::Result<()> {
let string_template = fs::read_to_string("./formula_template.rb")?;
let formula = string_template
.replace("{{bin}}", bin_name)
.replace("{{description}}", description)
.replace("{{homepage}}", repo)
.replace("{{repo}}", repo)
// .replace("{{shasum}}", &shasum)
.replace("{{version}}", env!("CARGO_PKG_VERSION"));
let mut file = File::create(format!("{bin_name}.rb"))?;
write!(file, "{formula}")?;
Ok(())
}