-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
81 lines (73 loc) · 2.72 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
// This file is part of jinx. Copyright © 2024 jinx contributors.
// jinx is licensed under the GNU AGPL v3.0 or any later version. See LICENSE file for full text.
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs, io};
fn main() -> io::Result<()> {
let out_dir: PathBuf = env::var("OUT_DIR").expect("bad out dir?").into();
let constants_path = out_dir.join("constants.rs");
create_constants(constants_path.as_path())?;
println!(
"cargo:rustc-env=CONSTANTS_PATH={}",
constants_path.to_str().unwrap()
);
Ok(())
}
/// generate rust source to send constants into the actual build
fn create_constants<P: AsRef<Path>>(path: P) -> io::Result<()> {
let git_commit_hash = git_commit_hash();
let clap_version = clap_version(&git_commit_hash);
let discord_bot_version = discord_bot_version(&git_commit_hash);
let user_agent = user_agent();
let file = fs::File::create(path)?;
let mut writer = BufWriter::new(file);
writer.write_fmt(format_args!(
"pub const GIT_COMMIT_HASH: &str = \"{git_commit_hash}\";\n"
))?;
writer.write_fmt(format_args!(
"pub const CLAP_VERSION: &str = \"{clap_version}\";\n"
))?;
writer.write_fmt(format_args!(
"pub const DISCORD_BOT_VERSION: &str = \"{discord_bot_version}\";\n"
))?;
writer.write_fmt(format_args!(
"pub const USER_AGENT: &str = \"{user_agent}\";\n"
))?;
writer.flush()
}
/// override version string displayed by clap
fn clap_version(git_commit_hash: &str) -> String {
format!("{} commit {}\\nCopyright 2024 jinx contributors\\nLicense: GNU AGPL v3.0 or any later version\\nWritten by: {}",
env!("CARGO_PKG_VERSION"),
git_commit_hash,
env!("CARGO_PKG_AUTHORS"),
)
}
/// String shown when via the Discord bot's `/version` command
fn discord_bot_version(git_commit_hash: &str) -> String {
format!("{} {} commit {}\\nCopyright 2024 jinx contributors\\nLicense: GNU AGPL v3.0 or any later version\\n{}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
git_commit_hash,
env!("CARGO_PKG_REPOSITORY"),
)
}
/// Read git commit hash
fn git_commit_hash() -> String {
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.expect("failed to get git commit hash");
let untrimmed_git_commit_hash =
String::from_utf8(output.stdout).expect("failed to read git commit hash as UTF-8");
untrimmed_git_commit_hash.trim().to_string()
}
fn user_agent() -> String {
format!(
"{}/{} {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_REPOSITORY")
)
}