-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathbuild.rs
executable file
·117 lines (97 loc) · 3.27 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
// Copyright 2023-2025, shadow3, shadow3aaa
//
// This file is part of fas-rs.
//
// fas-rs is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// fas-rs is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with fas-rs. If not, see <https://www.gnu.org/licenses/>.
use std::{fs, io::Write};
use anyhow::Result;
use serde::{Deserialize, Serialize};
#[derive(Deserialize)]
struct Package {
pub authors: Vec<String>,
pub name: String,
pub version: String,
pub description: String,
}
#[derive(Deserialize)]
struct CargoConfig {
pub package: Package,
}
#[allow(non_snake_case)]
#[derive(Serialize)]
struct UpdateJson {
versionCode: usize,
version: String,
zipUrl: String,
changelog: String,
}
fn main() -> Result<()> {
println!("cargo:rerun-if-changed=Cargo.lock");
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=update");
let toml = fs::read_to_string("Cargo.toml")?;
let data: CargoConfig = toml::from_str(&toml)?;
gen_module_prop(&data)?;
update_json(&data)?;
Ok(())
}
fn gen_module_prop(data: &CargoConfig) -> Result<()> {
let package = &data.package;
let id = package.name.replace('-', "_");
let version_code: usize = package.version.replace('.', "").trim().parse()?;
let authors = &package.authors;
let mut author = String::new();
for a in authors {
author += &format!("{a} ");
}
let author = author.trim();
let mut file = fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open("module/module.prop")?;
writeln!(file, "id={id}")?;
writeln!(file, "name={}", package.name)?;
writeln!(file, "version=v{}", package.version)?;
writeln!(file, "versionCode={version_code}")?;
writeln!(file, "author={author}")?;
writeln!(file, "description={}", package.description)?;
Ok(())
}
fn update_json(data: &CargoConfig) -> Result<()> {
let version = &data.package.version;
let version_code: usize = version.replace('.', "").trim().parse()?;
let version = format!("v{version}");
let zip_url =
format!("https://github.com/shadow3aaa/fas-rs/releases/download/{version}/fas-rs.zip");
let cn = UpdateJson {
versionCode: version_code,
version: version.clone(),
zipUrl: zip_url.clone(),
changelog: "https://github.com/shadow3aaa/fas-rs/raw/master/update/zh-CN/changelog.md"
.into(),
};
let en = UpdateJson {
versionCode: version_code,
version,
zipUrl: zip_url,
changelog: "https://github.com/shadow3aaa/fas-rs/raw/master/update/en-US/changelog.md"
.into(),
};
let cn = serde_json::to_string_pretty(&cn)?;
let en = serde_json::to_string_pretty(&en)?;
fs::write("update/update.json", cn)?;
fs::write("update/update_en.json", en)?;
Ok(())
}