|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | + |
1 | 5 | fn main() { |
2 | | - tauri_build::build() |
| 6 | + import_js_scripts_to_rust(); |
| 7 | + |
| 8 | + // Run the Tauri build process |
| 9 | + tauri_build::build(); |
| 10 | +} |
| 11 | + |
| 12 | +/// Imports JavaScript file contents to Rust string constants |
| 13 | +/// |
| 14 | +/// This function handles the entire process of: |
| 15 | +/// 1. Locating the JavaScript files |
| 16 | +/// 2. Reading their contents |
| 17 | +/// 3. Generating Rust code with the JS content as string constants |
| 18 | +/// 4. Writing the generated code to the output file |
| 19 | +/// |
| 20 | +/// # Returns |
| 21 | +/// |
| 22 | +/// The path to the generated Rust file |
| 23 | +fn import_js_scripts_to_rust() -> PathBuf { |
| 24 | + // Get the output directory for generated files |
| 25 | + let out_dir = env::var("OUT_DIR").unwrap(); |
| 26 | + let dest_path = Path::new(&out_dir).join("js_scripts.rs"); |
| 27 | + |
| 28 | + // Get the manifest directory (where Cargo.toml is located) |
| 29 | + let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); |
| 30 | + let js_dir = Path::new(&manifest_dir) |
| 31 | + .join("src") |
| 32 | + .join("scripts") |
| 33 | + .join("js"); |
| 34 | + |
| 35 | + // Define the scripts to compile |
| 36 | + let scripts = [ |
| 37 | + ("FULLSCREEN_SCRIPT", "fullscreen_script.js"), |
| 38 | + ("PIP_SCRIPT", "pip_script.js"), |
| 39 | + ("PIP_OVERLAY_SCRIPT", "pip_overlay_script.js"), |
| 40 | + ("ZOOM_SCRIPT", "zoom_script.js"), |
| 41 | + ]; |
| 42 | + |
| 43 | + // Generate Rust code for each script |
| 44 | + let mut rust_code = String::new(); |
| 45 | + |
| 46 | + for (const_name, js_filename) in scripts.iter() { |
| 47 | + // Create the full path to the JS file |
| 48 | + let js_path: PathBuf = js_dir.join(js_filename); |
| 49 | + |
| 50 | + // Read the JS file content |
| 51 | + let js_content = fs::read_to_string(&js_path) |
| 52 | + .expect(&format!("Failed to read JS file: {}", js_path.display())); |
| 53 | + |
| 54 | + // Add the constant definition to the Rust code |
| 55 | + rust_code.push_str(&format!( |
| 56 | + "pub const {}: &str = r#\"\n{}\n\"#;\n\n", |
| 57 | + const_name, js_content |
| 58 | + )); |
| 59 | + |
| 60 | + // Tell Cargo to rerun the build script if this JS file changes |
| 61 | + println!("cargo:rerun-if-changed={}", js_path.display()); |
| 62 | + } |
| 63 | + |
| 64 | + // Write the generated Rust code to the output file |
| 65 | + fs::write(&dest_path, rust_code).expect("Failed to write generated Rust code"); |
| 66 | + |
| 67 | + // Return the path to the generated file |
| 68 | + dest_path |
3 | 69 | } |
0 commit comments