-
Notifications
You must be signed in to change notification settings - Fork 27
/
build.rs
58 lines (51 loc) · 1.85 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
use std::fs;
fn main() {
let current_version = match std::env::var("CARGO_PKG_VERSION") {
Ok(v) => v,
Err(_) => return,
};
let last_version = match fs::read_to_string("version") {
Ok(v) => v.trim().to_string(),
Err(_) => return,
};
if current_version.eq(&last_version) {
return;
}
println!("cargo:warning= current version: {current_version} last version: {last_version}");
// lib.rs
if replace_lib(¤t_version, &last_version).is_err() {
return;
}
// constant.rs
if replace_constant(¤t_version, &last_version).is_err() {
return;
}
// Cargo.toml
let _ = replace_cargo(¤t_version, &last_version);
}
fn replace_constant(current_version: &str, last_version: &str) -> Result<(), ()> {
let current_str = format!("SSH-2.0-SSH_RS-{current_version}");
let last_str = format!("SSH-2.0-SSH_RS-{last_version}");
replace_file("src/constant.rs", current_str, last_str)
}
fn replace_lib(current_version: &str, last_version: &str) -> Result<(), ()> {
let current_str = format!("ssh-rs = \"{current_version}\"");
let last_str = format!("ssh-rs = \"{last_version}\"");
replace_file("src/lib.rs", current_str, last_str)
}
fn replace_cargo(current_version: &str, last_version: &str) -> Result<(), ()> {
let current_str = format!("version = \"{current_version}\"");
let last_str = format!("version = \"{last_version}\"");
replace_file("Cargo.toml", current_str, last_str)
}
fn replace_file(file_path: &str, current_str: String, last_str: String) -> Result<(), ()> {
let buf_str = match fs::read_to_string(file_path) {
Ok(v) => v,
Err(_) => return Err(()),
};
let new_buf_str = buf_str.replace(¤t_str, &last_str);
if fs::write(file_path, new_buf_str).is_err() {
return Err(());
}
Ok(())
}