Skip to content

Commit cae7ead

Browse files
committed
Fetch Godot version more robustly (panic on non-zero exit code)
1 parent 818508e commit cae7ead

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

godot-bindings/src/godot_exe.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,26 @@ fn update_version_file(version: &str) {
100100
*/
101101

102102
pub(crate) fn read_godot_version(godot_bin: &Path) -> GodotVersion {
103-
let output = Command::new(godot_bin)
104-
.arg("--version")
105-
.output()
106-
.unwrap_or_else(|_| {
107-
panic!(
108-
"failed to invoke Godot executable '{}'",
109-
godot_bin.display()
110-
)
111-
});
103+
let mut cmd = Command::new(godot_bin);
104+
cmd.arg("--version");
112105

113-
let output = String::from_utf8(output.stdout).expect("convert Godot version to UTF-8");
114-
println!("Godot version: {output}");
106+
let output = execute(cmd, "read Godot version");
107+
let stdout = String::from_utf8(output.stdout).expect("convert Godot version to UTF-8");
115108

116-
match parse_godot_version(&output) {
109+
match parse_godot_version(&stdout) {
117110
Ok(parsed) => {
118111
assert_eq!(
119112
parsed.major,
120113
4,
121114
"Only Godot versions >= 4.0 are supported; found version {}.",
122-
output.trim()
115+
stdout.trim()
123116
);
124117

125118
parsed
126119
}
127120
Err(e) => {
128121
// Don't treat this as fatal error
129-
panic!("failed to parse Godot version '{output}': {e}")
122+
panic!("failed to parse Godot version '{stdout}': {e}")
130123
}
131124
}
132125
}
@@ -275,10 +268,14 @@ pub(crate) fn locate_godot_binary() -> PathBuf {
275268
}
276269
}
277270

278-
fn execute(mut cmd: Command, error_message: &str) -> Output {
271+
fn execute(cmd: Command, error_message: &str) -> Output {
272+
try_execute(cmd, error_message).unwrap_or_else(|e| panic!("{}", e))
273+
}
274+
275+
fn try_execute(mut cmd: Command, error_message: &str) -> Result<Output, String> {
279276
let output = cmd
280277
.output()
281-
.unwrap_or_else(|_| panic!("failed to execute command: {error_message}"));
278+
.map_err(|_| format!("failed to invoke command ({error_message})\n\t{cmd:?}"))?;
282279

283280
if output.status.success() {
284281
println!(
@@ -290,12 +287,14 @@ fn execute(mut cmd: Command, error_message: &str) -> Output {
290287
String::from_utf8(output.stderr.clone()).unwrap()
291288
);
292289
println!("[status] {}", output.status);
293-
output
290+
Ok(output)
294291
} else {
295292
println!("[stdout] {}", String::from_utf8(output.stdout).unwrap());
296293
println!("[stderr] {}", String::from_utf8(output.stderr).unwrap());
297294
println!("[status] {}", output.status);
298-
panic!("command returned error: {error_message}");
295+
Err(format!(
296+
"command returned error ({error_message})\n\t{cmd:?}"
297+
))
299298
}
300299
}
301300

0 commit comments

Comments
 (0)