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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
use std::env;
use std::path::PathBuf;
use std::process::Command;
const COMPILE_INFO_RS: &str = "./src/data/compile_info.rs";
const COMPILE_INFO_RS_TEMPLATE: &str = "./templates/compile_info.rs";
const SETUP_JV_CLI_ISS: &str = "./setup/windows/setup_jv_cli.iss";
const SETUP_JV_CLI_ISS_TEMPLATE: &str = "./templates/setup_jv_cli.iss";
fn main() {
println!("cargo:rerun-if-env-changed=FORCE_BUILD");
let repo_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
// Only generate installer script on Windows
if cfg!(target_os = "windows") {
if let Err(e) = generate_installer_script(&repo_root) {
eprintln!("Failed to generate installer script: {}", e);
std::process::exit(1);
}
}
if let Err(e) = generate_compile_info(&repo_root) {
eprintln!("Failed to generate compile info: {}", e);
std::process::exit(1);
}
}
/// Generate Inno Setup installer script (Windows only)
fn generate_installer_script(repo_root: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
let template_path = repo_root.join(SETUP_JV_CLI_ISS_TEMPLATE);
let output_path = repo_root.join(SETUP_JV_CLI_ISS);
let template = std::fs::read_to_string(&template_path)?;
let author = get_author()?;
let version = get_version();
let site = get_site()?;
let generated = template
.replace("<<<AUTHOR>>>", &author)
.replace("<<<VERSION>>>", &version)
.replace("<<<SITE>>>", &site);
std::fs::write(output_path, generated)?;
Ok(())
}
fn get_author() -> Result<String, Box<dyn std::error::Error>> {
let cargo_toml_path = std::path::Path::new("Cargo.toml");
let cargo_toml_content = std::fs::read_to_string(cargo_toml_path)?;
let cargo_toml: toml::Value = toml::from_str(&cargo_toml_content)?;
if let Some(package) = cargo_toml.get("package") {
if let Some(authors) = package.get("authors") {
if let Some(authors_array) = authors.as_array() {
if let Some(first_author) = authors_array.get(0) {
if let Some(author_str) = first_author.as_str() {
return Ok(author_str.to_string());
}
}
}
}
}
Err("Author not found in Cargo.toml".into())
}
fn get_site() -> Result<String, Box<dyn std::error::Error>> {
let cargo_toml_path = std::path::Path::new("Cargo.toml");
let cargo_toml_content = std::fs::read_to_string(cargo_toml_path)?;
let cargo_toml: toml::Value = toml::from_str(&cargo_toml_content)?;
if let Some(package) = cargo_toml.get("package") {
if let Some(homepage) = package.get("homepage") {
if let Some(site_str) = homepage.as_str() {
return Ok(site_str.to_string());
}
}
}
Err("Homepage not found in Cargo.toml".into())
}
/// Generate compile info
fn generate_compile_info(repo_root: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
// Read the template code
let template_code = std::fs::read_to_string(repo_root.join(COMPILE_INFO_RS_TEMPLATE))?;
let date = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
let target = env::var("TARGET").unwrap_or_else(|_| "unknown".to_string());
let platform = get_platform(&target);
let toolchain = get_toolchain();
let version = get_version();
let branch = get_git_branch().unwrap_or_else(|_| "unknown".to_string());
let commit = get_git_commit().unwrap_or_else(|_| "unknown".to_string());
let generated_code = template_code
.replace("{date}", &date)
.replace("{target}", &target)
.replace("{platform}", &platform)
.replace("{toolchain}", &toolchain)
.replace("{version}", &version)
.replace("{branch}", &branch)
.replace("{commit}", &commit);
// Write the generated code
let compile_info_path = repo_root.join(COMPILE_INFO_RS);
std::fs::write(compile_info_path, generated_code)?;
Ok(())
}
fn get_platform(target: &str) -> String {
if target.contains("windows") {
"Windows".to_string()
} else if target.contains("linux") {
"Linux".to_string()
} else if target.contains("darwin") || target.contains("macos") {
"macOS".to_string()
} else if target.contains("android") {
"Android".to_string()
} else if target.contains("ios") {
"iOS".to_string()
} else {
"Unknown".to_string()
}
}
fn get_toolchain() -> String {
let rustc_version = std::process::Command::new("rustc")
.arg("--version")
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
.unwrap_or_else(|| "unknown".to_string())
.trim()
.to_string();
let channel = if rustc_version.contains("nightly") {
"nightly"
} else if rustc_version.contains("beta") {
"beta"
} else {
"stable"
};
format!("{} ({})", rustc_version, channel)
}
fn get_version() -> String {
let cargo_toml_path = std::path::Path::new("Cargo.toml");
let cargo_toml_content = match std::fs::read_to_string(cargo_toml_path) {
Ok(content) => content,
Err(_) => return "unknown".to_string(),
};
let cargo_toml: toml::Value = match toml::from_str(&cargo_toml_content) {
Ok(value) => value,
Err(_) => return "unknown".to_string(),
};
if let Some(workspace) = cargo_toml.get("workspace") {
if let Some(package) = workspace.get("package") {
if let Some(version) = package.get("version") {
if let Some(version_str) = version.as_str() {
return version_str.to_string();
}
}
}
}
"unknown".to_string()
}
/// Get current git branch
fn get_git_branch() -> Result<String, Box<dyn std::error::Error>> {
let output = Command::new("git")
.args(["branch", "--show-current"])
.output()?;
if output.status.success() {
let branch = String::from_utf8(output.stdout)?.trim().to_string();
if branch.is_empty() {
// Try to get HEAD reference if no branch (detached HEAD)
let output = Command::new("git")
.args(["rev-parse", "--abbrev-ref", "HEAD"])
.output()?;
if output.status.success() {
let head_ref = String::from_utf8(output.stdout)?.trim().to_string();
return Ok(head_ref);
}
} else {
return Ok(branch);
}
}
Err("Failed to get git branch".into())
}
/// Get current git commit hash
fn get_git_commit() -> Result<String, Box<dyn std::error::Error>> {
let output = Command::new("git").args(["rev-parse", "HEAD"]).output()?;
if output.status.success() {
let commit = String::from_utf8(output.stdout)?.trim().to_string();
return Ok(commit);
}
Err("Failed to get git commit".into())
}
|