-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
155 lines (134 loc) · 4.35 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at
// https://mozilla.org/MPL/2.0/.
//
// Author: https://github.com/Prof-Bloodstone/
// Commit: "Some more work on tests, parsing json etc" https://github.com/Prof-Bloodstone/botstone/commit/7c8b6672919c67256a3735869159ef052b8faeb2
// I license all of my modifications and additions to this file also under the Mozilla Public License, v. 2.0,
// Obtain a copy at https://mozilla.org/MPL/2.0/
#[cfg(not(feature = "deterministic"))]
#[path = "src/version_data.rs"]
mod version_data;
use std::{
env::{self},
path::{Path, PathBuf},
};
#[cfg(not(feature = "deterministic"))]
use std::process::Command;
fn main() -> std::io::Result<()> {
println!("Version");
let out_dir = env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir).join("embed");
let _ = std::fs::create_dir(&out_dir);
let version_file_dest_path = out_dir.join("version.json");
copy_resource_reactions(&out_dir);
println!("?");
let _ = std::fs::remove_file(&version_file_dest_path);
#[cfg(not(feature = "deterministic"))]
{
use chrono::Utc;
use std::{
env::consts::{ARCH, OS},
fs::File,
io::Write,
};
use version_data::VersionData;
let mut file = File::create(version_file_dest_path)?;
let is_git_info_available = git_cmd()
.arg("status")
.status()
.map(|s| s.success())
.unwrap_or(false);
let data = VersionData {
build: env::var("PROFILE").unwrap(),
name: env!("CARGO_PKG_NAME").to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
branch: if is_git_info_available {
get_branch_name()
} else {
None
}
.unwrap_or_else(|| "NONE".to_string()),
commit: if is_git_info_available {
get_commit_hash()
} else {
None
}
.unwrap_or_else(|| "NONE".to_string()),
clean_worktree: is_git_info_available && is_working_tree_clean(),
os: OS.to_string(),
arch: ARCH.to_string(),
timestamp: Utc::now().format("%Y-%m-%d %H:%M").to_string(),
};
let version_path = env::var("OUT_DIR").unwrap();
println!("{}", version_path);
let serialized_data = serde_json::to_string::<VersionData>(&data).unwrap();
println!("Data: {:?}", data);
let _ = file.write(serialized_data.as_bytes()).unwrap();
file.flush()?;
}
println!("cargo:rustc-env=RESOURCE_PATH={}", out_dir.display());
Ok(())
}
fn copy_resource_reactions(out_dir: &PathBuf) {
let cargo = env::var("CARGO_MANIFEST_DIR").expect("fuc");
let _ = std::fs::copy(
Path::new(&cargo).join("res/reactions/skull.png"),
out_dir.join("skull.png"),
);
for i in 1..7 {
let _ = std::fs::copy(
Path::new(&cargo).join(format!("res/reactions/{}.png", i)),
out_dir.join(format!("{}.png", i)),
);
}
}
#[cfg(not(feature = "deterministic"))]
fn git_cmd() -> Command {
let mut cmd = Command::new("git".to_string());
cmd.current_dir(env!("CARGO_MANIFEST_DIR"));
cmd
}
#[cfg(not(feature = "deterministic"))]
fn get_commit_hash() -> Option<String> {
let output = git_cmd()
.arg("log")
.arg("-1")
.arg("--pretty=format:%h")
.output()
.unwrap();
if output.status.success() {
Some(String::from_utf8_lossy(&output.stdout).to_string())
} else {
None
}
}
#[cfg(not(feature = "deterministic"))]
fn get_branch_name() -> Option<String> {
let output = git_cmd()
.arg("rev-parse")
.arg("--abbrev-ref")
.arg("HEAD")
.output()
.unwrap();
if output.status.success() {
Some(
String::from_utf8_lossy(&output.stdout)
.trim_end()
.to_string(),
)
} else {
None
}
}
#[cfg(not(feature = "deterministic"))]
fn is_working_tree_clean() -> bool {
let status = git_cmd()
.arg("diff")
.arg("--quiet")
.arg("--exit-code")
.arg("HEAD")
.status()
.unwrap();
status.success()
}