From d0e8b8145c94a74ade49b76bd4815ac86a8e47f9 Mon Sep 17 00:00:00 2001 From: Ivan Pashchenko Date: Thu, 21 Mar 2024 18:47:03 +0100 Subject: [PATCH] IJPL-442: use anyhow in build script reason: it's already big enough for that + cef download is coming next GitOrigin-RevId: 1c88d373902d8b78d717e845aa1cd9070f8d75aa --- native/XPlatLauncher/Cargo.toml | 5 ++- native/XPlatLauncher/build.rs | 66 ++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/native/XPlatLauncher/Cargo.toml b/native/XPlatLauncher/Cargo.toml index 1cd0a9b3c8f6..85a827f0b144 100644 --- a/native/XPlatLauncher/Cargo.toml +++ b/native/XPlatLauncher/Cargo.toml @@ -44,9 +44,12 @@ native-dialog = "0.6.3" cargo-deny = "0.14.2" # checks licenses / vulns cargo-about = "0.5.7" # generates license report +[build-dependencies] +anyhow = { version = "1.0.81", features = ["std", "backtrace"] } + [target.'cfg(target_os = "windows")'.build-dependencies] -winresource = "0.1.17" windows = { version = "0.51.1", features = [ "Win32_System_SystemInformation", "Win32_Foundation" ] } +winresource = "0.1.17" [dev-dependencies] tempfile = "3.8.0" \ No newline at end of file diff --git a/native/XPlatLauncher/build.rs b/native/XPlatLauncher/build.rs index f988b045f9ea..4d0732368816 100644 --- a/native/XPlatLauncher/build.rs +++ b/native/XPlatLauncher/build.rs @@ -1,6 +1,7 @@ // technically we shouldn't use #cfg in build.rs due to cross-compilation, // but the only we do is windows x64 -> arm64, so it's fine for our purposes +use anyhow::{bail, Context, Result}; use std::fs; use std::fs::File; use std::io::{BufRead, BufReader, BufWriter, Write}; @@ -15,86 +16,91 @@ use { fn main() { println!("cargo:rerun-if-changed=build.rs"); main_os_specific() + .expect("Failed to execute buildscript"); } #[cfg(target_os = "windows")] -fn main_os_specific() { +fn main_os_specific() -> Result<()> { println!("cargo:rustc-link-lib=legacy_stdio_definitions"); let needs_metadata = std::env::var("XPLAT_LAUNCHER_EMBED_RESOURCES_AND_MANIFEST") .unwrap_or("0".to_string()); if needs_metadata == "1" { - embed_metadata() + embed_metadata()?; } + + Ok(()) } #[cfg(target_os = "windows")] -fn embed_metadata() { - let cargo_root_env_var = std::env::var("CARGO_MANIFEST_DIR").unwrap(); - +fn embed_metadata() -> Result<()> { + let cargo_root_env_var = std::env::var("CARGO_MANIFEST_DIR")?; let cargo_root = PathBuf::from(cargo_root_env_var); let manifest_file = cargo_root.join("./resources/windows/WinLauncher.manifest"); - assert!(manifest_file.is_file()); + assert_exists_and_file(&manifest_file)?; - let rc_file_template = PathBuf::from("./resources/windows/WinLauncher.rc"); - assert!(rc_file_template.is_file()); + let rc_template_file = PathBuf::from("./resources/windows/WinLauncher.rc"); + assert_exists_and_file(&rc_template_file)?; - let rc_file = process_rc_template(&rc_file_template); + let rc_file = process_rc_template(&rc_template_file)?; let mut res = WindowsResource::new(); - res.set_resource_file(rc_file.to_str().unwrap()); - res.set_manifest_file(manifest_file.to_str().unwrap()); - res.compile().expect("Failed to embed resource table and/or application manifest"); + res.set_resource_file(rc_file.to_str().context("Failed to get &str from rc file path")?); + res.set_manifest_file(manifest_file.to_str().context("Failed to get &str from manifest file path")?); + res.compile().context("Failed to embed resource table and/or application manifest") +} + +fn assert_exists_and_file(path: &Path) -> Result<()> { + if !path.exists() { + bail!("File '{path:?}' does not exist") + } + if !path.is_file() { + bail!("'{path:?}' is not a file") + } + + Ok(()) } #[cfg(target_os = "windows")] -fn process_rc_template(template: &Path) -> PathBuf { - let file = File::open(template) - .expect("Failed to open .rc template"); +fn process_rc_template(template: &Path) -> Result { + let file = File::open(template)?; let current_year = get_current_year(); - let package_name = std::env::var("CARGO_PKG_NAME") - .expect("CARGO_PKG_NAME must be set in build context"); + let package_name = std::env::var("CARGO_PKG_NAME")?; let mut processed_lines = Vec::with_capacity(60); for line in BufReader::new(file).lines() { - let line = line.expect("Failed to read from .rc template"); - let processed_line = line + let processed_line = line? .replace("@YEAR@", ¤t_year) .replace("@FILE_NAME@", &package_name); processed_lines.push(processed_line) } - let out_dir = std::env::var("OUT_DIR") - .expect("OUT_DIR must be set in build context"); + let out_dir = std::env::var("OUT_DIR")?; let out_dir = PathBuf::from(out_dir); let out_file_path = out_dir.join("xplat-launcher.rc"); - let out_file = File::create(&out_file_path) - .expect("Failed to create output .rc file"); + let out_file = File::create(&out_file_path)?; let mut buf_writer = BufWriter::new(out_file); for line in processed_lines { - writeln!(buf_writer, "{}", line) - .expect("Failed to write to output .rc file"); + writeln!(buf_writer, "{}", line)?; } - buf_writer.flush() - .expect("Failed to flush write buffer to .rc file"); + buf_writer.flush()?; let rc_dependencies = vec!["resource.h", "WinLauncher.ico"]; for dep in rc_dependencies { let src = PathBuf::from(format!("./resources/windows/{dep}")); let dest = out_dir.join(dep); - fs::copy(&src, &dest) - .expect("Failed to copy .rc dependency"); + fs::copy(&src, &dest)?; } - out_file_path + Ok(out_file_path) } #[cfg(target_os = "windows")]